Skip to content

Commit e9acf07

Browse files
Alexander PliushchouiText-CI
authored andcommitted
Improve producer line creation
DEVSIX-8421 Autoported commit. Original commit hash: [67d714469]
1 parent 2f5332f commit e9acf07

File tree

5 files changed

+149
-5
lines changed

5 files changed

+149
-5
lines changed

itext.tests/itext.commons.tests/itext/commons/actions/producer/ProducerBuilderTest.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,39 @@ public virtual void UnknownPlaceHoldersTest() {
164164
NUnit.Framework.Assert.AreEqual("||", newProducerLine);
165165
}
166166

167+
[NUnit.Framework.Test]
168+
public virtual void ModifiedUsingEqualsCurrentProducerTest() {
169+
IList<ConfirmedEventWrapper> events = GetEvents("some Author", 1, 2, 3);
170+
String newProducerLine = ProducerBuilder.ModifyProducer(events, "Old producer; modified using some Author"
171+
);
172+
NUnit.Framework.Assert.AreEqual("Old producer; modified using some Author", newProducerLine);
173+
}
174+
175+
[NUnit.Framework.Test]
176+
public virtual void PrevModifiedUsingEqualsCurrentProducerTest() {
177+
IList<ConfirmedEventWrapper> events = GetEvents("some Author", 1, 2, 3);
178+
String newProducerLine = ProducerBuilder.ModifyProducer(events, "Old producer; modified using some Author; modified using another tool"
179+
);
180+
NUnit.Framework.Assert.AreEqual("Old producer; modified using some Author; modified using another tool; "
181+
+ "modified using some Author", newProducerLine);
182+
}
183+
184+
[NUnit.Framework.Test]
185+
public virtual void SeveralModifiedUsingEqualsCurrentProducerTest() {
186+
IList<ConfirmedEventWrapper> events = GetEvents("some Author", 1, 2, 3);
187+
String newProducerLine = ProducerBuilder.ModifyProducer(events, "Old producer; modified using some Author; modified using some Author"
188+
);
189+
NUnit.Framework.Assert.AreEqual("Old producer; modified using some Author; modified using some Author", newProducerLine
190+
);
191+
}
192+
193+
[NUnit.Framework.Test]
194+
public virtual void OldProducerEqualsCurrentProducerTest() {
195+
IList<ConfirmedEventWrapper> events = GetEvents("some Author", 1, 2, 3);
196+
String newProducerLine = ProducerBuilder.ModifyProducer(events, "some Author");
197+
NUnit.Framework.Assert.AreEqual("some Author", newProducerLine);
198+
}
199+
167200
private IList<ConfirmedEventWrapper> GetEvents(String initialProducerLine, params int[] indexes) {
168201
IList<ConfirmedEventWrapper> events = new List<ConfirmedEventWrapper>();
169202
for (int ind = 0; ind < indexes.Length; ind++) {
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.IO;
3+
using iText.IO.Source;
4+
using iText.Kernel.Pdf;
5+
using iText.Test;
6+
7+
namespace iText.Kernel.Actions {
8+
[NUnit.Framework.Category("IntegrationTest")]
9+
public class ProducerBuilderIntegrationTest : ExtendedITextTest {
10+
private static String ITEXT_PRODUCER;
11+
12+
private const String MODIFIED_USING = "; modified using ";
13+
14+
[NUnit.Framework.OneTimeSetUp]
15+
public static void BeforeClass() {
16+
byte[] docBytes;
17+
using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
18+
using (PdfDocument doc = new PdfDocument(new PdfWriter(outputStream))) {
19+
doc.AddNewPage();
20+
}
21+
docBytes = outputStream.ToArray();
22+
}
23+
using (PdfDocument docReopen = new PdfDocument(new PdfReader(new MemoryStream(docBytes)))) {
24+
ITEXT_PRODUCER = docReopen.GetDocumentInfo().GetProducer();
25+
}
26+
}
27+
28+
[NUnit.Framework.Test]
29+
public virtual void ModifiedByItextTest() {
30+
byte[] docBytes;
31+
using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
32+
using (PdfDocument doc = new PdfDocument(new PdfWriter(outputStream))) {
33+
doc.GetDocumentInfo().SetProducer("someProducer");
34+
}
35+
docBytes = outputStream.ToArray();
36+
}
37+
using (PdfDocument docReopen = new PdfDocument(new PdfReader(new MemoryStream(docBytes)))) {
38+
NUnit.Framework.Assert.AreEqual("someProducer" + MODIFIED_USING + ITEXT_PRODUCER, docReopen.GetDocumentInfo
39+
().GetProducer());
40+
}
41+
}
42+
43+
[NUnit.Framework.Test]
44+
public virtual void ModifiedSecondTimeModifiedByItextTest() {
45+
byte[] docBytes;
46+
using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
47+
using (PdfDocument doc = new PdfDocument(new PdfWriter(outputStream))) {
48+
doc.GetDocumentInfo().SetProducer("someProducer; modified using anotherProducer");
49+
}
50+
docBytes = outputStream.ToArray();
51+
}
52+
using (PdfDocument docReopen = new PdfDocument(new PdfReader(new MemoryStream(docBytes)))) {
53+
NUnit.Framework.Assert.AreEqual("someProducer; modified using anotherProducer" + MODIFIED_USING + ITEXT_PRODUCER
54+
, docReopen.GetDocumentInfo().GetProducer());
55+
}
56+
}
57+
58+
[NUnit.Framework.Test]
59+
public virtual void CreatedByItextModifiedByItextTest() {
60+
byte[] docBytes;
61+
using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
62+
using (PdfDocument doc = new PdfDocument(new PdfWriter(outputStream))) {
63+
doc.GetDocumentInfo().SetProducer(ITEXT_PRODUCER);
64+
}
65+
docBytes = outputStream.ToArray();
66+
}
67+
using (PdfDocument docReopen = new PdfDocument(new PdfReader(new MemoryStream(docBytes)))) {
68+
NUnit.Framework.Assert.AreEqual(ITEXT_PRODUCER, docReopen.GetDocumentInfo().GetProducer());
69+
}
70+
}
71+
72+
[NUnit.Framework.Test]
73+
public virtual void ModifiedByItextSecondTimeModifiedByItextTest() {
74+
byte[] docBytes;
75+
using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
76+
using (PdfDocument doc = new PdfDocument(new PdfWriter(outputStream))) {
77+
doc.GetDocumentInfo().SetProducer("someProducer" + MODIFIED_USING + ITEXT_PRODUCER);
78+
}
79+
docBytes = outputStream.ToArray();
80+
}
81+
using (PdfDocument docReopen = new PdfDocument(new PdfReader(new MemoryStream(docBytes)))) {
82+
NUnit.Framework.Assert.AreEqual("someProducer" + MODIFIED_USING + ITEXT_PRODUCER, docReopen.GetDocumentInfo
83+
().GetProducer());
84+
}
85+
}
86+
87+
[NUnit.Framework.Test]
88+
public virtual void ModifiedByItextSecondTimeModifiedThirdTimeModifiedByItextTest() {
89+
byte[] docBytes;
90+
using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
91+
using (PdfDocument doc = new PdfDocument(new PdfWriter(outputStream))) {
92+
doc.GetDocumentInfo().SetProducer("someProducer" + MODIFIED_USING + ITEXT_PRODUCER + MODIFIED_USING + "thirdProducer"
93+
);
94+
}
95+
docBytes = outputStream.ToArray();
96+
}
97+
using (PdfDocument docReopen = new PdfDocument(new PdfReader(new MemoryStream(docBytes)))) {
98+
NUnit.Framework.Assert.AreEqual("someProducer" + MODIFIED_USING + ITEXT_PRODUCER + MODIFIED_USING + "thirdProducer"
99+
+ MODIFIED_USING + ITEXT_PRODUCER, docReopen.GetDocumentInfo().GetProducer());
100+
}
101+
}
102+
}
103+
}

itext.tests/itext.kernel.tests/itext/kernel/actions/events/FlushPdfDocumentEventTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public virtual void FlushEventAfterEachEventTest() {
139139
using (PdfDocument pdf_1 = new PdfDocument(new PdfReader(new MemoryStream(baos.ToArray())))) {
140140
String producerLine = pdf_1.GetDocumentInfo().GetProducer();
141141
String modifiedByItext = "modified using iText\u00ae Core";
142-
NUnit.Framework.Assert.AreNotEqual(producerLine.IndexOf(modifiedByItext, StringComparison.Ordinal), producerLine
142+
NUnit.Framework.Assert.AreEqual(producerLine.IndexOf(modifiedByItext, StringComparison.Ordinal), producerLine
143143
.LastIndexOf(modifiedByItext));
144144
}
145145
}

itext/itext.commons/itext/commons/actions/producer/ProducerBuilder.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,15 @@ private ProducerBuilder() {
8787
/// <see cref="iText.Commons.Actions.Confirmations.ConfirmedEventWrapper"/>
8888
/// or not.
8989
/// Format of the new producer line will be defined by the first event in the list.
90-
/// Placeholder will be replaced and merged all together
90+
/// Placeholder will be replaced and merged all together.
9191
/// </remarks>
9292
/// <param name="events">list of events registered for the document</param>
9393
/// <param name="oldProducer">
9494
/// old producer line. If <c>null</c> or empty, will be replaced
9595
/// with a new one. Otherwise new line will be attached with
9696
/// <c>modified using</c> prefix. If old producer line already contains
97-
/// <c>modified using</c> substring, it will be overriden with a new one
97+
/// <c>modified using itext</c> substring with the current version of itext at the end,
98+
/// no changes will be made
9899
/// </param>
99100
/// <returns>modified producer line</returns>
100101
public static String ModifyProducer<_T0>(IList<_T0> events, String oldProducer)
@@ -116,7 +117,14 @@ public static String ModifyProducer<_T0>(IList<_T0> events, String oldProducer)
116117
return newProducer;
117118
}
118119
else {
119-
return oldProducer + MODIFIED_USING + newProducer;
120+
//if the last time document was modified or created with the itext of the same version,
121+
//then no changes occur.
122+
if (oldProducer.Equals(newProducer) || oldProducer.EndsWith(MODIFIED_USING + newProducer)) {
123+
return oldProducer;
124+
}
125+
else {
126+
return oldProducer + MODIFIED_USING + newProducer;
127+
}
120128
}
121129
}
122130

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cecbc27166eb8d323051127a3436131ba4993639
1+
67d7144691e9403ba8591eace64d019eb05c88a0

0 commit comments

Comments
 (0)