下面列出了org.apache.commons.io.IOUtils#toCharArray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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 );
}
@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 ) );
}
@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" ) );
}
@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" ) ) );
}
@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 ) );
}
@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 ) );
}
@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" ) ) );
}
@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" ) ) );
}
@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( "{}" ) );
}
@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" ) ) );
}
@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 ) );
}
@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" ) );
}
@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" ) );
}
@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 ) );
}
@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);
}
@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" ) );
}
@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" ) ) );
}
@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" ) ) );
}
@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" ) ) );
}
@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" ) ) );
}