Example usage for org.apache.commons.io IOUtils toCharArray

List of usage examples for org.apache.commons.io IOUtils toCharArray

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils toCharArray.

Prototype

public static char[] toCharArray(Reader input) throws IOException 

Source Link

Document

Get the contents of a Reader as a character array.

Usage

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.html.HtmlFilterReaderBaseTest.java

@Test
public void testTagNameLetterCase() throws Exception {
    String inputXml = "<Root/>";
    StringReader inputReader = new StringReader(inputXml);

    HtmlFilterReaderBase filterReader = new NoopXmlFilterReader(inputReader);
    String outputXml = new String(IOUtils.toCharArray(filterReader));
    assertThat(the(outputXml), hasXPath("/Root"));
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.html.HtmlFilterReaderBaseTest.java

@Test
public void testXmlWithHtmlTagNames() throws Exception {
    String inputXml = "<root><br><table name=\"table1\"></table><table name=\"table2\"></table></br></root>";
    StringReader inputReader = new StringReader(inputXml);

    HtmlFilterReaderBase filterReader = new NoopXmlFilterReader(inputReader);
    String outputXml = new String(IOUtils.toCharArray(filterReader));
    assertThat(the(outputXml), hasXPath("/root/br/table[1]/@name", equalTo("table1")));
    assertThat(the(outputXml), hasXPath("/root/br/table[2]/@name", equalTo("table2")));
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.javascript.JavaScriptFilterReaderTest.java

@Test
public void testSimple() throws IOException {
    String inputJs = "function load_page() {}\n";
    StringReader inputReader = new StringReader(inputJs);
    UrlRewriteFilterContentDescriptor config = new UrlRewriteFilterContentDescriptorImpl();
    JavaScriptFilterReader filterReader = new NoopJsFilterReader(inputReader, config);
    String outputJs = new String(IOUtils.toCharArray(filterReader));
    assertThat(outputJs, is(inputJs));/*from  www. j a v a 2 s  . co m*/
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.javascript.JavaScriptFilterReaderTest.java

@Test
public void testSimpleMultipleLines() throws IOException {
    String inputJs = "var url = '/webhdfs/v1' + abs_path + '?op=GET_BLOCK_LOCATIONS';\n"
            + "$.ajax({\"url\": url, \"crossDomain\": true}).done(function(data) {}).error(network_error_handler(url));\n";
    StringReader inputReader = new StringReader(inputJs);
    UrlRewriteFilterContentDescriptor config = new UrlRewriteFilterContentDescriptorImpl();
    config.addApply("/webhdfs/v1", "test-rule");
    JavaScriptFilterReader filterReader = new NoopJsFilterReader(inputReader, config);
    String outputJs = new String(IOUtils.toCharArray(filterReader));
    assertThat(outputJs, is(inputJs));/*  www .j  av  a  2  s .  c om*/
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.javascript.JavaScriptFilterReaderTest.java

@Test
public void testMatchedJsContent() throws IOException {
    Map<String, Map<String, String>> rules = new HashMap<>();
    Map<String, String> map = new HashMap<>();
    map.put("(https?://[^/':,]+:[\\d]+)?/cluster/app", "https://knoxhost:8443/cluster/app");
    map.put("/webhdfs/v1", "https://knoxhost:8443/webhdfs/v1");
    rules.put("test-rule", map);
    String inputJs = "var url = '/webhdfs/v1' + abs_path + '?op=GET_BLOCK_LOCATIONS';\n"
            + "$.ajax({\"url\": url, \"crossDomain\": true}).done(function(data) {\n"
            + "  var url = http://testhost:8088/cluster/app/application_1436831599487_0001;\n"
            + "}).error(network_error_handler(url));\n";
    StringReader inputReader = new StringReader(inputJs);
    UrlRewriteFilterContentDescriptor config = new UrlRewriteFilterContentDescriptorImpl();
    config.addApply("(https?://[^/':,]+:[\\d]+)?/cluster/app", "test-rule");
    config.addApply("/webhdfs/v1", "test-rule");
    JavaScriptFilterReader filterReader = new MatchRuleJsFilterReader(inputReader, rules, config);
    String outputJs = new String(IOUtils.toCharArray(filterReader));
    String expectedOutputJs = "var url = 'https://knoxhost:8443/webhdfs/v1' + abs_path + '?op=GET_BLOCK_LOCATIONS';\n"
            + "$.ajax({\"url\": url, \"crossDomain\": true}).done(function(data) {\n"
            + "  var url = https://knoxhost:8443/cluster/app/application_1436831599487_0001;\n"
            + "}).error(network_error_handler(url));\n";
    assertThat(outputJs, is(expectedOutputJs));
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.json.JsonFilterReaderTest.java

@Test
public void testSimple() throws IOException {
    String inputJson = "{ \"test-name\" : \"test-value\" }";
    StringReader inputReader = new StringReader(inputJson);
    JsonFilterReader filterReader = new TestJsonFilterReader(inputReader, null);
    String outputJson = new String(IOUtils.toCharArray(filterReader));
    //System.out.println( "JSON=" + outputJson );

    JsonAssert.with(outputJson).assertThat("name<test-name>", is("value:null<test-value>"));
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.json.JsonFilterReaderTest.java

@Test
public void testRootArray() throws Exception {
    String inputJson = "[\"test-value-1\",\"test-value-2\",\"test-value-3\"]";
    StringReader inputReader = new StringReader(inputJson);
    JsonFilterReader filterReader = new TestJsonFilterReader(inputReader, null);
    String outputJson = new String(IOUtils.toCharArray(filterReader));
    //System.out.println( "JSON=" + outputJson );
    JsonAssert.with(outputJson).assertThat("$.[0]", is("value:null<test-value-1>"));
    JsonAssert.with(outputJson).assertThat("$.[1]", is("value:null<test-value-2>"));
    JsonAssert.with(outputJson).assertThat("$.[2]", is("value:null<test-value-3>"));

    inputJson = "[777,42]";
    inputReader = new StringReader(inputJson);
    filterReader = new TestJsonFilterReader(inputReader, null);
    outputJson = new String(IOUtils.toCharArray(filterReader));
    //System.out.println( "JSON=" + outputJson );
    JsonAssert.with(outputJson).assertThat("$.[0]", is(777));
    JsonAssert.with(outputJson).assertThat("$.[1]", is(42));
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.json.JsonFilterReaderTest.java

@Test
public void testEmptyObject() throws IOException {
    String inputJson = "{}";
    StringReader inputReader = new StringReader(inputJson);
    JsonFilterReader filterReader = new TestJsonFilterReader(inputReader, null);
    String outputJson = new String(IOUtils.toCharArray(filterReader));

    assertThat(outputJson, is("{}"));
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.json.JsonFilterReaderTest.java

@Test
public void testEmptyArray() throws IOException {
    String inputJson = "[]";
    StringReader inputReader = new StringReader(inputJson);
    JsonFilterReader filterReader = new TestJsonFilterReader(inputReader, null);
    String outputJson = new String(IOUtils.toCharArray(filterReader));

    assertThat(outputJson, is("[]"));
}

From source file:org.apache.hadoop.gateway.filter.rewrite.impl.UrlRewriteResponseTest.java

private void testStreamResponseGzip(String content, UrlRewriteResponse rewriteResponse, boolean isGzip)
        throws IOException {
    File targetDir = new File(System.getProperty("user.dir"), "target");
    File inputFile = new File(targetDir, "input.test");
    File outputFile = new File(targetDir, "output.test");
    OutputStream outStream = null, output = null;
    InputStream inStream = null, input = null;
    try {//from  w ww.  j a  va 2s .co m
        outStream = isGzip ? new GZIPOutputStream(new FileOutputStream(inputFile))
                : new FileOutputStream(inputFile);
        outStream.write(content.getBytes());
        outStream.close();

        input = new FileInputStream(inputFile);
        output = new FileOutputStream(outputFile);
        rewriteResponse.streamResponse(input, output);

        inStream = isGzip ? new GZIPInputStream(new FileInputStream(outputFile))
                : new FileInputStream(outputFile);
        assertThat(String.valueOf(IOUtils.toCharArray(inStream)), is(content));
    } finally {
        if (inStream != null) {
            inStream.close();
        }
        if (input != null) {
            input.close();
        }
        inputFile.delete();
        outputFile.delete();
    }
}