You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/04/19 10:53:30 UTC

[2/7] camel git commit: PDF component created.

http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfCreationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfCreationTest.java b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfCreationTest.java
new file mode 100644
index 0000000..7b65cbb
--- /dev/null
+++ b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfCreationTest.java
@@ -0,0 +1,124 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.pdf;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.Predicate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
+import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
+import org.apache.pdfbox.util.PDFTextStripper;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.instanceOf;
+
+public class PdfCreationTest extends CamelTestSupport {
+
+    @EndpointInject(uri = "mock:result")
+    protected MockEndpoint resultEndpoint;
+
+    @Override
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+    }
+
+    @Test
+    public void testPdfCreation() throws Exception {
+        final String expectedText = "expectedText";
+        template.sendBody("direct:start", expectedText);
+        resultEndpoint.setExpectedMessageCount(1);
+        resultEndpoint.expectedMessagesMatches(new Predicate() {
+            @Override
+            public boolean matches(Exchange exchange) {
+                Object body = exchange.getIn().getBody();
+                assertThat(body, instanceOf(ByteArrayOutputStream.class));
+                try {
+                    PDDocument doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) body).toByteArray()));
+                    PDFTextStripper pdfTextStripper = new PDFTextStripper();
+                    String text = pdfTextStripper.getText(doc);
+                    assertEquals(1, doc.getNumberOfPages());
+                    assertThat(text, containsString(expectedText));
+                } catch (IOException e) {
+                    throw new RuntimeException(e);
+                }
+                return true;
+            }
+        });
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    @Test
+    public void testPdfCreationWithEncryption() throws Exception {
+        final String ownerPass = "ownerPass";
+        final String userPass = "userPass";
+        final String expectedText = "expectedText";
+        AccessPermission accessPermission = new AccessPermission();
+        accessPermission.setCanPrint(false);
+        StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy(ownerPass, userPass, accessPermission);
+        protectionPolicy.setEncryptionKeyLength(128);
+        template.sendBodyAndHeader("direct:start",
+                expectedText,
+                PdfHeaderConstants.PROTECTION_POLICY_HEADER_NAME,
+                protectionPolicy);
+
+        resultEndpoint.setExpectedMessageCount(1);
+        resultEndpoint.expectedMessagesMatches(new Predicate() {
+            @Override
+            public boolean matches(Exchange exchange) {
+                Object body = exchange.getIn().getBody();
+                assertThat(body, instanceOf(ByteArrayOutputStream.class));
+                try {
+                    PDDocument doc = PDDocument.load(new ByteArrayInputStream(((ByteArrayOutputStream) body).toByteArray()));
+                    assertTrue("Expected encrypted document", doc.isEncrypted());
+                    doc.decrypt(userPass);
+                    assertFalse("Printing should not be permitted", doc.getCurrentAccessPermission().canPrint());
+                    PDFTextStripper pdfTextStripper = new PDFTextStripper();
+                    String text = pdfTextStripper.getText(doc);
+                    assertEquals(1, doc.getNumberOfPages());
+                    assertThat(text, containsString(expectedText));
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+                return true;
+            }
+        });
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .to("pdf:create")
+                        .to("mock:result");
+            }
+        };
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfTextExtractionTest.java
----------------------------------------------------------------------
diff --git a/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfTextExtractionTest.java b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfTextExtractionTest.java
new file mode 100644
index 0000000..db46882
--- /dev/null
+++ b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/PdfTextExtractionTest.java
@@ -0,0 +1,133 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.pdf;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.Predicate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDPage;
+import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
+import org.apache.pdfbox.pdmodel.encryption.StandardDecryptionMaterial;
+import org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicy;
+import org.apache.pdfbox.pdmodel.font.PDType1Font;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.instanceOf;
+
+public class PdfTextExtractionTest extends CamelTestSupport {
+
+    @EndpointInject(uri = "mock:result")
+    protected MockEndpoint resultEndpoint;
+
+    @Test
+    public void testExtractText() throws Exception {
+        final String expectedText = "Test string";
+        PDDocument document = new PDDocument();
+        PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
+        document.addPage(page);
+        PDPageContentStream contentStream = new PDPageContentStream(document, page);
+        contentStream.setFont(PDType1Font.HELVETICA, 12);
+        contentStream.beginText();
+        contentStream.moveTextPositionByAmount(20, 400);
+        contentStream.drawString(expectedText);
+        contentStream.endText();
+        contentStream.close();
+
+        template.sendBody("direct:start", document);
+
+        resultEndpoint.setExpectedMessageCount(1);
+        resultEndpoint.expectedMessagesMatches(new Predicate() {
+            @Override
+            public boolean matches(Exchange exchange) {
+                Object body = exchange.getIn().getBody();
+                assertThat(body, instanceOf(String.class));
+                assertThat((String) body, containsString(expectedText));
+                return true;
+            }
+        });
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    @Test
+    public void testExtractTextFromEncrypted() throws Exception {
+        final String ownerPass = "ownerPass";
+        final String userPass = "userPass";
+        AccessPermission accessPermission = new AccessPermission();
+        accessPermission.setCanExtractContent(false);
+        StandardProtectionPolicy protectionPolicy = new StandardProtectionPolicy(ownerPass, userPass, accessPermission);
+        protectionPolicy.setEncryptionKeyLength(128);
+        PDDocument document = new PDDocument();
+
+        final String expectedText = "Test string";
+        PDPage page = new PDPage(PDPage.PAGE_SIZE_A4);
+        document.addPage(page);
+        PDPageContentStream contentStream = new PDPageContentStream(document, page);
+        contentStream.setFont(PDType1Font.HELVETICA, 12);
+        contentStream.beginText();
+        contentStream.moveTextPositionByAmount(20, 400);
+        contentStream.drawString(expectedText);
+        contentStream.endText();
+        contentStream.close();
+
+        document.protect(protectionPolicy);
+
+        ByteArrayOutputStream output = new ByteArrayOutputStream();
+        document.save(output);
+
+        // Encryption happens after saving.
+        PDDocument encryptedDocument = PDDocument.load(new ByteArrayInputStream(output.toByteArray()));
+
+        template.sendBodyAndHeader("direct:start",
+                encryptedDocument,
+                PdfHeaderConstants.DECRYPTION_MATERIAL_HEADER_NAME,
+                new StandardDecryptionMaterial(userPass));
+
+        resultEndpoint.setExpectedMessageCount(1);
+        resultEndpoint.expectedMessagesMatches(new Predicate() {
+            @Override
+            public boolean matches(Exchange exchange) {
+                Object body = exchange.getIn().getBody();
+                assertThat(body, instanceOf(String.class));
+                assertThat((String) body, containsString(expectedText));
+                return true;
+            }
+        });
+        resultEndpoint.assertIsSatisfied();
+        document.isEncrypted();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .to("pdf:extractText")
+                        .to("mock:result");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/text/PatternSplitStrategyTest.java
----------------------------------------------------------------------
diff --git a/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/text/PatternSplitStrategyTest.java b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/text/PatternSplitStrategyTest.java
new file mode 100644
index 0000000..620dc7b
--- /dev/null
+++ b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/text/PatternSplitStrategyTest.java
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.pdf.text;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class PatternSplitStrategyTest {
+    private static final String PATTERN = "\n";
+    private PatternSplitStrategy patternSplitStrategy = new PatternSplitStrategy(PATTERN);
+
+    @Test
+    public void testSplit() throws Exception {
+        Collection<String> split = patternSplitStrategy.split("hello" + PATTERN + "world");
+        assertEquals(2, split.size());
+        assertEquals("world", new ArrayList<String>(split).get(1));
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/text/WordSplitStrategyTest.java
----------------------------------------------------------------------
diff --git a/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/text/WordSplitStrategyTest.java b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/text/WordSplitStrategyTest.java
new file mode 100644
index 0000000..b7805ab
--- /dev/null
+++ b/components/camel-pdf/src/test/java/org/apache/camel/component/pdf/text/WordSplitStrategyTest.java
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.pdf.text;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class WordSplitStrategyTest {
+
+    private WordSplitStrategy wordSplitStrategy = new WordSplitStrategy();
+
+    @Test
+    public void testSplit() throws Exception {
+        Collection<String> split = wordSplitStrategy.split("Hello World\n Foo Bar");
+        assertEquals(4, split.size());
+        assertEquals("Bar", new ArrayList<String>(split).get(3));
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/camel-pdf/src/test/resources/log4j.properties
----------------------------------------------------------------------
diff --git a/components/camel-pdf/src/test/resources/log4j.properties b/components/camel-pdf/src/test/resources/log4j.properties
new file mode 100644
index 0000000..690f423
--- /dev/null
+++ b/components/camel-pdf/src/test/resources/log4j.properties
@@ -0,0 +1,38 @@
+## ---------------------------------------------------------------------------
+## Licensed to the Apache Software Foundation (ASF) under one or more
+## contributor license agreements.  See the NOTICE file distributed with
+## this work for additional information regarding copyright ownership.
+## The ASF licenses this file to You under the Apache License, Version 2.0
+## (the "License"); you may not use this file except in compliance with
+## the License.  You may obtain a copy of the License at
+##
+## http://www.apache.org/licenses/LICENSE-2.0
+##
+## Unless required by applicable law or agreed to in writing, software
+## distributed under the License is distributed on an "AS IS" BASIS,
+## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+## See the License for the specific language governing permissions and
+## limitations under the License.
+## ---------------------------------------------------------------------------
+
+#
+# The logging properties used during tests
+#
+log4j.rootLogger=INFO, stdout
+
+# uncomment this to turn on debug of camel
+log4j.logger.org.apache.camel=DEBUG
+#log4j.logger.org.apache.camel.component.pgevent=TRACE
+
+# CONSOLE appender not used by default
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
+
+# File appender
+log4j.appender.file=org.apache.log4j.FileAppender
+log4j.appender.file.layout=org.apache.log4j.PatternLayout
+log4j.appender.file.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
+log4j.appender.file.file=target/camel-pgevent-test.log
+log4j.appender.file.append=true
+

http://git-wip-us.apache.org/repos/asf/camel/blob/4a5258c4/components/pom.xml
----------------------------------------------------------------------
diff --git a/components/pom.xml b/components/pom.xml
index 457db22..09ef943 100644
--- a/components/pom.xml
+++ b/components/pom.xml
@@ -169,6 +169,7 @@
     <module>camel-olingo2</module>
     <module>camel-openshift</module>
     <module>camel-optaplanner</module>
+    <module>camel-pdf</module>
     <module>camel-paho</module>
     <module>camel-paxlogging</module>
     <module>camel-pgevent</module>