|
| 1 | +package com.itextpdf.kernel.actions; |
| 2 | + |
| 3 | +import com.itextpdf.io.source.ByteArrayOutputStream; |
| 4 | +import com.itextpdf.kernel.pdf.*; |
| 5 | +import com.itextpdf.test.ExtendedITextTest; |
| 6 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 7 | +import org.junit.Assert; |
| 8 | +import org.junit.BeforeClass; |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.experimental.categories.Category; |
| 11 | + |
| 12 | +import java.io.ByteArrayInputStream; |
| 13 | +import java.io.IOException; |
| 14 | + |
| 15 | +@Category(IntegrationTest.class) |
| 16 | +public class ProducerBuilderIntegrationTest extends ExtendedITextTest { |
| 17 | + |
| 18 | + private static String ITEXT_PRODUCER; |
| 19 | + |
| 20 | + private static final String MODIFIED_USING = "; modified using "; |
| 21 | + |
| 22 | + @BeforeClass |
| 23 | + public static void beforeClass() throws IOException { |
| 24 | + byte[] docBytes; |
| 25 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 26 | + try (PdfDocument doc = new PdfDocument(new PdfWriter(outputStream))) { |
| 27 | + doc.addNewPage(); |
| 28 | + } |
| 29 | + docBytes = outputStream.toByteArray(); |
| 30 | + } |
| 31 | + |
| 32 | + try (PdfDocument docReopen = new PdfDocument(new PdfReader(new ByteArrayInputStream(docBytes)))) { |
| 33 | + ITEXT_PRODUCER = docReopen.getDocumentInfo().getProducer(); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void modifiedByItextTest() throws IOException { |
| 39 | + byte[] docBytes; |
| 40 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 41 | + try (PdfDocument doc = new PdfDocument(new PdfWriter(outputStream))) { |
| 42 | + doc.getDocumentInfo().setProducer("someProducer"); |
| 43 | + } |
| 44 | + docBytes = outputStream.toByteArray(); |
| 45 | + } |
| 46 | + |
| 47 | + try (PdfDocument docReopen = new PdfDocument(new PdfReader(new ByteArrayInputStream(docBytes)))) { |
| 48 | + Assert.assertEquals("someProducer" + MODIFIED_USING + ITEXT_PRODUCER |
| 49 | + , docReopen.getDocumentInfo().getProducer()); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void modifiedSecondTimeModifiedByItextTest() throws IOException { |
| 55 | + byte[] docBytes; |
| 56 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 57 | + try (PdfDocument doc = new PdfDocument(new PdfWriter(outputStream))) { |
| 58 | + doc.getDocumentInfo().setProducer("someProducer; modified using anotherProducer"); |
| 59 | + } |
| 60 | + docBytes = outputStream.toByteArray(); |
| 61 | + } |
| 62 | + |
| 63 | + try (PdfDocument docReopen = new PdfDocument(new PdfReader(new ByteArrayInputStream(docBytes)))) { |
| 64 | + Assert.assertEquals("someProducer; modified using anotherProducer" + MODIFIED_USING + ITEXT_PRODUCER |
| 65 | + , docReopen.getDocumentInfo().getProducer()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void createdByItextModifiedByItextTest() throws IOException { |
| 71 | + byte[] docBytes; |
| 72 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 73 | + try (PdfDocument doc = new PdfDocument(new PdfWriter(outputStream))) { |
| 74 | + doc.getDocumentInfo().setProducer(ITEXT_PRODUCER); |
| 75 | + } |
| 76 | + docBytes = outputStream.toByteArray(); |
| 77 | + } |
| 78 | + |
| 79 | + try (PdfDocument docReopen = new PdfDocument(new PdfReader(new ByteArrayInputStream(docBytes)))) { |
| 80 | + Assert.assertEquals(ITEXT_PRODUCER, docReopen.getDocumentInfo().getProducer()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void modifiedByItextSecondTimeModifiedByItextTest() throws IOException { |
| 86 | + byte[] docBytes; |
| 87 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 88 | + try (PdfDocument doc = new PdfDocument(new PdfWriter(outputStream))) { |
| 89 | + doc.getDocumentInfo().setProducer("someProducer" + MODIFIED_USING +ITEXT_PRODUCER); |
| 90 | + } |
| 91 | + docBytes = outputStream.toByteArray(); |
| 92 | + } |
| 93 | + |
| 94 | + try (PdfDocument docReopen = new PdfDocument(new PdfReader(new ByteArrayInputStream(docBytes)))) { |
| 95 | + Assert.assertEquals("someProducer" + MODIFIED_USING + ITEXT_PRODUCER |
| 96 | + , docReopen.getDocumentInfo().getProducer()); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void modifiedByItextSecondTimeModifiedThirdTimeModifiedByItextTest() throws IOException { |
| 102 | + byte[] docBytes; |
| 103 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 104 | + try (PdfDocument doc = new PdfDocument(new PdfWriter(outputStream))) { |
| 105 | + doc.getDocumentInfo().setProducer("someProducer" + MODIFIED_USING + ITEXT_PRODUCER + MODIFIED_USING |
| 106 | + + "thirdProducer"); |
| 107 | + } |
| 108 | + docBytes = outputStream.toByteArray(); |
| 109 | + } |
| 110 | + |
| 111 | + try (PdfDocument docReopen = new PdfDocument(new PdfReader(new ByteArrayInputStream(docBytes)))) { |
| 112 | + Assert.assertEquals("someProducer" + MODIFIED_USING + ITEXT_PRODUCER + MODIFIED_USING |
| 113 | + + "thirdProducer" + MODIFIED_USING + ITEXT_PRODUCER, docReopen.getDocumentInfo().getProducer()); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments