org.apache.commons.io.IOUtils#toCharArray ( )源码实例Demo

下面列出了org.apache.commons.io.IOUtils#toCharArray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: database   文件: BulletParserTest.java
public void testParser() throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, FileNotFoundException, IOException {
	char[] text = IOUtils.toCharArray( this.getClass().getResourceAsStream( "test.html" ), "UTF-8" );

	final Callback mockCallback = (Callback)Proxy.getProxyClass( Callback.class.getClassLoader(), Callback.class ).getConstructor( InvocationHandler.class )
			.newInstance( new Object[] { new InvocationHandler() {
				int call = 0;

				String[] methods = { "configure", "startDocument", "endDocument" };

				public Object invoke( final Object proxy, final Method method, final Object[] args ) throws Throwable {
					if ( call < methods.length )
						assertEquals( method.getName(), methods[ call++ ] );
					return Boolean.TRUE;
				}
			} } );

	new BulletParser().setCallback( mockCallback ).parse( text, 0, text.length );
}
 
源代码2 项目: knox   文件: 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 ) );
}
 
源代码3 项目: knox   文件: HtmlFilterReaderBaseTest.java
@Test
public void testSimple() throws IOException, ParserConfigurationException {
  String inputXml = "<root/>";
  StringReader inputReader = new StringReader( inputXml );
  HtmlFilterReaderBase filterReader = new NoopXmlFilterReader( inputReader );
  String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
  assertThat( the( outputHtml ), hasXPath( "/root" ) );
}
 
源代码4 项目: knox   文件: HtmlFilterReaderBaseTest.java
@Test
public void testMappedText() throws IOException, ParserConfigurationException {
  Map<String,String> map = new HashMap<>();
  map.put( "input-text", "output-text" );
  String inputXml = "<root>input-text</root>";
  StringReader inputReader = new StringReader( inputXml );
  HtmlFilterReaderBase filterReader = new MapXmlFilterReader( inputReader, map );
  String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
  assertThat( the( outputHtml ), hasXPath( "/root/text()", equalTo( "output-text" ) ) );
}
 
源代码5 项目: knox   文件: JsonFilterReaderTest.java
@Test
public void testNumber() throws IOException {
  int num = ThreadLocalRandom.current().nextInt();
  String inputJson = String.valueOf(num);
  StringReader inputReader = new StringReader( inputJson );
  JsonFilterReader filterReader = new TestJsonFilterReader( inputReader, null );
  String outputJson = new String( IOUtils.toCharArray( filterReader ) );
  JsonAssert.with( outputJson ).assertThat( "$", is( num ) );
}
 
源代码6 项目: knox   文件: HtmlFilterReaderBaseTest.java
@Test
public void testMatchedJavaScriptText() throws IOException, ParserConfigurationException {
  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" );
  rules.put( "test-rule", map );
  String inputXml =
      "<root>\n" +
      "  <script type=\"text/javascript\">\n" +
      "    var appsTableData=[\n" +
      "      [\"<a href='/cluster/app/application_1436831599487_0008'>application_1436831599487_0008</a>\",\"hdfs\",\"Spark Pi\",\"SPARK\",\"<a href='http://testhost:8088/cluster/app/application_1436831599487_0008'>History</a>\"],\n" +
      "      [\"<a href='/cluster/app/application_1436831599487_0006'>application_1436831599487_0006</a>\",\"hdfs\",\"Spark Pi\",\"SPARK\",\"<a href='http://testhost:8088/cluster/app/application_1436831599487_0006'>History</a>\"],\n" +
      "      [\"<a href='/cluster/app/application_1436831599487_0007'>application_1436831599487_0007</a>\",\"hdfs\",\"Spark Pi\",\"SPARK\",\"<a href='http://testhost:8088/cluster/app/application_1436831599487_0007'>History</a>\"]\n" +
      "    ]\n" +
      "  </script>\n" +
      "</root>\n";
  StringReader inputReader = new StringReader( inputXml );
  UrlRewriteFilterContentDescriptor config = new UrlRewriteFilterContentDescriptorImpl();
  config.addApply( "(https?://[^/':,]+:[\\d]+)?/cluster/app", "test-rule" );
  HtmlFilterReaderBase filterReader = new MatchRuleXmlFilterReader( inputReader, rules, config );
  String outputXml = new String( IOUtils.toCharArray( filterReader ) );
  String expectedOutput =
      "<root>\n" +
      "  <script type=\"text/javascript\">\n" +
      "    var appsTableData=[\n" +
      "      [\"<a href='https://knoxhost:8443/cluster/app/application_1436831599487_0008'>application_1436831599487_0008</a>\",\"hdfs\",\"Spark Pi\",\"SPARK\",\"<a href='https://knoxhost:8443/cluster/app/application_1436831599487_0008'>History</a>\"],\n" +
      "      [\"<a href='https://knoxhost:8443/cluster/app/application_1436831599487_0006'>application_1436831599487_0006</a>\",\"hdfs\",\"Spark Pi\",\"SPARK\",\"<a href='https://knoxhost:8443/cluster/app/application_1436831599487_0006'>History</a>\"],\n" +
      "      [\"<a href='https://knoxhost:8443/cluster/app/application_1436831599487_0007'>application_1436831599487_0007</a>\",\"hdfs\",\"Spark Pi\",\"SPARK\",\"<a href='https://knoxhost:8443/cluster/app/application_1436831599487_0007'>History</a>\"]\n" +
      "    ]\n" +
      "  </script>\n" +
      "</root>\n";
  assertThat( outputXml, is( expectedOutput ) );
}
 
源代码7 项目: knox   文件: 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" ) ) );
}
 
源代码8 项目: knox   文件: HtmlFilterReaderBaseTest.java
@Test
public void testSimpleAttribute() throws IOException, ParserConfigurationException {
  String inputXml = "<root name='value'/>";
  StringReader inputReader = new StringReader( inputXml );
  HtmlFilterReaderBase filterReader = new NoopXmlFilterReader( inputReader );
  String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
  assertThat( the( outputHtml ), hasXPath( "/root/@name", equalTo( "value" ) ) );
}
 
源代码9 项目: knox   文件: 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( "{}" ) );
}
 
源代码10 项目: knox   文件: HtmlFilterReaderBaseTest.java
@Test
public void testSimpleTextNode() throws IOException, ParserConfigurationException {
  String inputXml = "<root>text</root>";
  StringReader inputReader = new StringReader( inputXml );
  HtmlFilterReaderBase filterReader = new NoopXmlFilterReader( inputReader );
  String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
  assertThat( the( outputHtml ), hasXPath( "/root/text()", equalTo( "text" ) ) );
}
 
源代码11 项目: knox   文件: 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 ) );
}
 
源代码12 项目: knox   文件: HtmlFilterReaderBaseTest.java
@Test
public void testSimpleNested() throws IOException, ParserConfigurationException {
  String inputXml = "<root><child1><child11/><child12/></child1><child2><child21/><child22/></child2></root>";
  StringReader inputReader = new StringReader( inputXml );
  HtmlFilterReaderBase filterReader = new NoopXmlFilterReader( inputReader );
  String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
  assertThat( the( outputHtml ), hasXPath( "/root" ) );
  assertThat( the( outputHtml ), hasXPath( "/root/child1" ) );
  assertThat( the( outputHtml ), hasXPath( "/root/child1/child11" ) );
  assertThat( the( outputHtml ), hasXPath( "/root/child1/child12" ) );
  assertThat( the( outputHtml ), hasXPath( "/root/child2" ) );
  assertThat( the( outputHtml ), hasXPath( "/root/child2/child21" ) );
  assertThat( the( outputHtml ), hasXPath( "/root/child2/child22" ) );
}
 
源代码13 项目: knox   文件: XmlFilterReaderTest.java
@Test
public void testSimpleStreaming() throws IOException, ParserConfigurationException, XMLStreamException {
  UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
  UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter( "filter-1" );
  UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent( "text/xml" );

  String inputXml = "<root/>";
  StringReader inputReader = new StringReader( inputXml );
  XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, contentConfig );
  String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
  assertThat( the( outputHtml ), hasXPath( "/root" ) );
}
 
源代码14 项目: knox   文件: HtmlFilterReaderBaseTest.java
@Test
public void testSimpleWithNamespace() throws IOException, ParserConfigurationException {
  String inputXml = "<ns:root xmlns:ns='http://hortonworks.com/xml/ns'></ns:root>";
  StringReader inputReader = new StringReader( inputXml );
  HtmlFilterReaderBase filterReader = new NoopXmlFilterReader( inputReader );
  String outputHtml = new String( IOUtils.toCharArray( filterReader ) );

  SimpleNamespaceContext ns = new SimpleNamespaceContext();
  ns.bind( "ns", "http://hortonworks.com/xml/ns" );
  assertThat( the( outputHtml ), hasXPath( "/ns:root", ns ) );
}
 
源代码15 项目: knox   文件: HtmlFilterReaderBaseTest.java
@Test
public void testSimpleBooleanAttribute() throws IOException, ParserConfigurationException {
  String inputXml = "<root name/>";
  StringReader inputReader = new StringReader(inputXml);
  HtmlFilterReaderBase filterReader = new NoopXmlFilterReader(inputReader);
  String outputHtml = new String(IOUtils.toCharArray(filterReader));
  assertEquals(inputXml, outputHtml);
}
 
源代码16 项目: knox   文件: 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" ) );
}
 
源代码17 项目: knox   文件: XmlFilterReaderTest.java
@Test
public void testSimpleTextNode() throws IOException, ParserConfigurationException, XMLStreamException {
  String inputXml = "<root>text</root>";
  StringReader inputReader = new StringReader( inputXml );
  XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, null );
  String outputXml = new String( IOUtils.toCharArray( filterReader ) );
  assertThat( the( outputXml ), hasXPath( "/root/text()", equalTo( "text" ) ) );
}
 
源代码18 项目: knox   文件: XmlFilterReaderTest.java
@Test
public void testSimpleAttribute() throws IOException, ParserConfigurationException, XMLStreamException {
  String inputXml = "<root name='value'/>";
  StringReader inputReader = new StringReader( inputXml );
  XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, null );
  String outputXml = new String( IOUtils.toCharArray( filterReader ) );
  assertThat( the( outputXml ), hasXPath( "/root/@name", equalTo( "value" ) ) );
}
 
源代码19 项目: knox   文件: XmlFilterReaderTest.java
@Test
public void testSimpleTextNodeBuffered() throws IOException, ParserConfigurationException, XMLStreamException {
  UrlRewriteRulesDescriptor rulesConfig = UrlRewriteRulesDescriptorFactory.create();
  UrlRewriteFilterDescriptor filterConfig = rulesConfig.addFilter( "filter-1" );
  UrlRewriteFilterContentDescriptor contentConfig = filterConfig.addContent( "text/xml" );
  UrlRewriteFilterBufferDescriptor scopeConfig = contentConfig.addBuffer( "/root" );
  assertNotNull(scopeConfig);

  String inputXml = "<root>text</root>";
  StringReader inputReader = new StringReader( inputXml );
  XmlFilterReader filterReader = new NoopXmlFilterReader( inputReader, contentConfig );
  String outputHtml = new String( IOUtils.toCharArray( filterReader ) );
  assertThat( the( outputHtml ), hasXPath( "/root/text()", equalTo( "text" ) ) );
}
 
源代码20 项目: knox   文件: HtmlFilterReaderBaseTest.java
@Test
public void testCombined() throws IOException, ParserConfigurationException {
  Map<String,String> map = new HashMap<>();
  map.put( "attr1-input", "attr1-output" );
  map.put( "attr2-input", "attr2-output" );
  map.put( "attr3-input", "attr3-output" );
  map.put( "attr4-input", "attr4-output" );
  map.put( "attr5-input", "attr5-output" );
  map.put( "attr6-input", "attr6-output" );
  map.put( "attr7-input", "attr7-output" );
  map.put( "root-input1", "root-output1" );
  map.put( "root-input2", "root-output2" );
  map.put( "root-input3", "root-output3" );
  map.put( "child1-input", "child1-output" );
  map.put( "child2-input", "child2-output" );

  String inputXml =
        "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
          "<!-- Comment -->\n" +
          "<ns1:root xmlns:ns1='http://hortonworks.com/xml/ns1' attr1='attr1-input' ns1:attr2='attr2-input'>\n" +
          "  root-input1\n" +
          "  <child1 attr3='attr3-input' ns1:attr4='attr4-input'>\n" +
          "    child1-input\n" +
          "  </child1>\n" +
          "  root-input2\n" +
          "  <ns2:child2 xmlns:ns2='http://hortonworks.com/xml/ns2' attr5='attr5-input' ns1:attr6='attr6-input' ns2:attr7='attr7-input'>\n" +
          "    child2-input\n" +
          "  </ns2:child2>\n" +
          "  root-input3\n" +
          "</ns1:root>";

  StringReader inputReader = new StringReader( inputXml );
  HtmlFilterReaderBase filterReader = new MapXmlFilterReader( inputReader, map );
  String outputXml = new String( IOUtils.toCharArray( filterReader ) );

  SimpleNamespaceContext ns = new SimpleNamespaceContext();
  ns.bind( "n1", "http://hortonworks.com/xml/ns1" );
  ns.bind( "n2", "http://hortonworks.com/xml/ns2" );

  assertThat( the( outputXml ), hasXPath( "/n1:root", ns ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/@attr1", ns, equalTo( "attr1-output" ) ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/@n1:attr2", ns, equalTo( "attr2-output" ) ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/text()[1]", ns, equalTo( "root-output1" ) ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/text()[2]", ns, equalTo( "root-output2" ) ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/text()[3]", ns, equalTo( "root-output3" ) ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/child1", ns ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/child1/@attr3", ns, equalTo( "attr3-output" ) ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/child1/@n1:attr4", ns, equalTo( "attr4-output" ) ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/child1/text()", ns, equalTo( "child1-output" ) ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/n2:child2", ns ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/n2:child2/@attr5", ns, equalTo( "attr5-output" ) ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/n2:child2/@n1:attr6", ns, equalTo( "attr6-output" ) ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/n2:child2/@n2:attr7", ns, equalTo( "attr7-output" ) ) );
  assertThat( the( outputXml ), hasXPath( "/n1:root/n2:child2/text()", ns, equalTo( "child2-output" ) ) );
}