@Test public void testJavaScript() throws IOException, ImporterHandlerException { String script = "metadata.addString('test', 'success');" + "text = content.replace(/Alice/g, 'Roger');" + "/*return*/ text;"; ScriptTransformer t = new ScriptTransformer(); t.setScript(script); File htmlFile = TestUtil.getAliceHtmlFile(); FileInputStream is = new FileInputStream(htmlFile); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImporterMetadata metadata = new ImporterMetadata(); metadata.setString(ImporterMetadata.DOC_CONTENT_TYPE, "text/html"); t.transformDocument(htmlFile.getAbsolutePath(), is, out, metadata, false); is.close(); String successField = metadata.getString("test"); Assert.assertEquals("success", successField); String content = new String(out.toString()); Assert.assertEquals(0, StringUtils.countMatches(content, "Alice")); Assert.assertEquals(34, StringUtils.countMatches(content, "Roger")); }
@Test public void testTransformTextDocument() throws IOException, ImporterHandlerException { StripAfterTransformer t = new StripAfterTransformer(); t.setStripAfterRegex("<p>"); t.setCaseSensitive(false); t.setInclusive(true); File htmlFile = TestUtil.getAliceHtmlFile(); FileInputStream is = new FileInputStream(htmlFile); ByteArrayOutputStream os = new ByteArrayOutputStream(); ImporterMetadata metadata = new ImporterMetadata(); metadata.setString(ImporterMetadata.DOC_CONTENT_TYPE, "text/html"); t.transformDocument(htmlFile.getAbsolutePath(), is, os, metadata, false); System.out.println(os.toString()); Assert.assertEquals( "Length of doc content after transformation is incorrect.", 552, os.toString().length()); is.close(); os.close(); }