Skip to content

Commit 67d7144

Browse files
author
Alexander Pliushchou
committed
Improve producer line creation
DEVSIX-8421
1 parent cecbc27 commit 67d7144

File tree

4 files changed

+164
-4
lines changed

4 files changed

+164
-4
lines changed

commons/src/main/java/com/itextpdf/commons/actions/producer/ProducerBuilder.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,14 @@ private ProducerBuilder() { }
8585
* Modifies an old producer line according to events registered for the document.
8686
* Events can be either wrapped with {@link ConfirmedEventWrapper} or not.
8787
* Format of the new producer line will be defined by the first event in the list.
88-
* Placeholder will be replaced and merged all together
88+
* Placeholder will be replaced and merged all together.
8989
*
9090
* @param events list of events registered for the document
9191
* @param oldProducer old producer line. If <code>null</code> or empty, will be replaced
9292
* with a new one. Otherwise new line will be attached with
9393
* <code>modified using</code> prefix. If old producer line already contains
94-
* <code>modified using</code> substring, it will be overriden with a new one
94+
* <code>modified using itext</code> substring with the current version of itext at the end,
95+
* no changes will be made
9596
* @return modified producer line
9697
*/
9798
public static String modifyProducer(List<? extends AbstractProductProcessITextEvent> events, String oldProducer) {
@@ -111,7 +112,14 @@ public static String modifyProducer(List<? extends AbstractProductProcessITextEv
111112
if (oldProducer == null || oldProducer.isEmpty()) {
112113
return newProducer;
113114
} else {
114-
return oldProducer + MODIFIED_USING + newProducer;
115+
//if the last time document was modified or created with the itext of the same version,
116+
//then no changes occur.
117+
if (oldProducer.equals(newProducer)
118+
|| oldProducer.endsWith(MODIFIED_USING + newProducer)) {
119+
return oldProducer;
120+
} else {
121+
return oldProducer + MODIFIED_USING + newProducer;
122+
}
115123
}
116124
}
117125

commons/src/test/java/com/itextpdf/commons/actions/producer/ProducerBuilderTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,42 @@ public void unknownPlaceHoldersTest() {
185185
Assert.assertEquals("||", newProducerLine);
186186
}
187187

188+
@Test
189+
public void modifiedUsingEqualsCurrentProducerTest() {
190+
List<ConfirmedEventWrapper> events = getEvents("some Author", 1, 2, 3);
191+
String newProducerLine = ProducerBuilder.modifyProducer(events
192+
, "Old producer; modified using some Author");
193+
194+
Assert.assertEquals("Old producer; modified using some Author", newProducerLine);
195+
}
196+
197+
@Test
198+
public void prevModifiedUsingEqualsCurrentProducerTest() {
199+
List<ConfirmedEventWrapper> events = getEvents("some Author", 1, 2, 3);
200+
String newProducerLine = ProducerBuilder.modifyProducer(events
201+
, "Old producer; modified using some Author; modified using another tool");
202+
203+
Assert.assertEquals("Old producer; modified using some Author; modified using another tool; " +
204+
"modified using some Author", newProducerLine);
205+
}
206+
207+
@Test
208+
public void severalModifiedUsingEqualsCurrentProducerTest() {
209+
List<ConfirmedEventWrapper> events = getEvents("some Author", 1, 2, 3);
210+
String newProducerLine = ProducerBuilder.modifyProducer(events
211+
, "Old producer; modified using some Author; modified using some Author");
212+
213+
Assert.assertEquals("Old producer; modified using some Author; modified using some Author", newProducerLine);
214+
}
215+
216+
@Test
217+
public void oldProducerEqualsCurrentProducerTest() {
218+
List<ConfirmedEventWrapper> events = getEvents("some Author", 1, 2, 3);
219+
String newProducerLine = ProducerBuilder.modifyProducer(events, "some Author");
220+
221+
Assert.assertEquals("some Author", newProducerLine);
222+
}
223+
188224
private List<ConfirmedEventWrapper> getEvents(String initialProducerLine, int ... indexes) {
189225
List<ConfirmedEventWrapper> events = new ArrayList<>();
190226

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
}

kernel/src/test/java/com/itextpdf/kernel/actions/events/FlushPdfDocumentEventTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public void flushEventAfterEachEventTest() throws IOException {
162162
try (PdfDocument pdf = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())))) {
163163
String producerLine = pdf.getDocumentInfo().getProducer();
164164
String modifiedByItext = "modified using iText\u00ae Core";
165-
Assert.assertNotEquals(producerLine.indexOf(modifiedByItext), producerLine.lastIndexOf(modifiedByItext));
165+
Assert.assertEquals(producerLine.indexOf(modifiedByItext), producerLine.lastIndexOf(modifiedByItext));
166166
}
167167
}
168168

0 commit comments

Comments
 (0)