Skip to content

Commit a9c9d6a

Browse files
committed
Add some basic tests to increase coverage
1 parent cdb6ec0 commit a9c9d6a

File tree

7 files changed

+705
-5
lines changed

7 files changed

+705
-5
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2025 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
APRYSE GROUP. APRYSE GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.rups.io;
44+
45+
import java.io.IOException;
46+
import java.nio.charset.StandardCharsets;
47+
import javax.swing.JTextArea;
48+
import org.junit.jupiter.api.Assertions;
49+
import org.junit.jupiter.api.Tag;
50+
import org.junit.jupiter.api.Test;
51+
52+
@Tag("UnitTest")
53+
class TextAreaOutputStreamTest {
54+
@Test
55+
void write() throws IOException {
56+
final JTextArea textArea = new JTextArea("Initial Text");
57+
try (final TextAreaOutputStream stream = new TextAreaOutputStream(textArea)) {
58+
Assertions.assertEquals("", textArea.getText());
59+
stream.write('\n');
60+
Assertions.assertEquals("\n", textArea.getText());
61+
stream.write("тест".getBytes(StandardCharsets.UTF_8), 2, 4);
62+
Assertions.assertEquals("\nес", textArea.getText());
63+
stream.write("ТЕСТ".getBytes(StandardCharsets.UTF_8));
64+
Assertions.assertEquals("\nесТЕСТ", textArea.getText());
65+
}
66+
}
67+
}

src/test/java/com/itextpdf/rups/model/PdfSyntaxUtilsTest.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,36 +45,48 @@ This file is part of the iText (R) project.
4545
import com.itextpdf.kernel.pdf.PdfArray;
4646
import com.itextpdf.kernel.pdf.PdfBoolean;
4747
import com.itextpdf.kernel.pdf.PdfDictionary;
48+
import com.itextpdf.kernel.pdf.PdfDocument;
49+
import com.itextpdf.kernel.pdf.PdfIndirectReference;
4850
import com.itextpdf.kernel.pdf.PdfName;
4951
import com.itextpdf.kernel.pdf.PdfNumber;
5052
import com.itextpdf.kernel.pdf.PdfString;
53+
import com.itextpdf.kernel.pdf.PdfWriter;
5154
import com.itextpdf.test.ExtendedITextTest;
5255

56+
import java.io.ByteArrayOutputStream;
5357
import java.util.Arrays;
5458

5559
import org.junit.jupiter.api.Assertions;
5660
import org.junit.jupiter.api.Tag;
5761
import org.junit.jupiter.api.Test;
5862

5963
@Tag("UnitTest")
60-
public class PdfSyntaxUtilsTest extends ExtendedITextTest {
64+
class PdfSyntaxUtilsTest extends ExtendedITextTest {
6165

6266
@Test
63-
public void testSimpleString() {
67+
void testSimpleString() {
6468
Assertions.assertEquals("(hello world)", PdfSyntaxUtils.getSyntaxString(new PdfString("hello world")));
6569
}
6670

6771
@Test
68-
public void testSimpleDictionary() {
72+
void testSimpleDictionary() {
6973
PdfDictionary pdfDictionary = new PdfDictionary();
7074
pdfDictionary.put(PdfName.ON, PdfBoolean.TRUE);
7175
Assertions.assertEquals("<< /ON true >>", PdfSyntaxUtils.getSyntaxString(pdfDictionary));
7276
}
7377

7478
@Test
75-
public void testSimpleArray() {
79+
void testSimpleArray() {
7680
PdfArray array = new PdfArray(Arrays.asList(new PdfNumber(1), new PdfString("hello")));
7781
Assertions.assertEquals("[ 1 (hello) ]", PdfSyntaxUtils.getSyntaxString(array));
7882
}
7983

84+
@Test
85+
void testIndirectRef() {
86+
final PdfDocument doc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
87+
final PdfIndirectReference ref = new PdfString("test")
88+
.makeIndirect(doc)
89+
.getIndirectReference();
90+
Assertions.assertEquals(ref.getObjNumber() + " 0 R", PdfSyntaxUtils.getSyntaxString(ref));
91+
}
8092
}

src/test/java/com/itextpdf/rups/model/TreeNodeFactoryTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,20 @@ void expandNode_Asn1SVCertWithInvalidFormFieldType() {
559559
doSVCertTest(PdfName.Tx, null, null, new PdfString(new byte[] {0x30, 0x00}), null);
560560
}
561561

562+
@Test
563+
void addNewIndirectObject() {
564+
final PdfDocument doc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
565+
final TreeNodeFactory factory = new TreeNodeFactory(createIndirectObjectFactory(doc));
566+
final PdfString object = new PdfString("TEST");
567+
factory.addNewIndirectObject(object);
568+
final PdfIndirectReference ref = object.getIndirectReference();
569+
Assertions.assertNotNull(ref);
570+
Assertions.assertSame(doc, ref.getDocument());
571+
final PdfObjectTreeNode node = factory.getNode(ref.getObjNumber());
572+
Assertions.assertNotNull(node);
573+
Assertions.assertSame(object, node.getPdfObject());
574+
}
575+
562576
private static void doSigTest(PdfName ft, PdfName type, PdfObject contents, String expectedLeafNodeName) {
563577
final PdfDocument doc = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
564578
final PdfDictionary sigDict = createSigDict(doc, type, contents);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2025 Apryse Group NV
4+
Authors: Apryse Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
APRYSE GROUP. APRYSE GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
package com.itextpdf.rups.view;
44+
45+
import org.junit.jupiter.api.Assertions;
46+
import org.junit.jupiter.api.Tag;
47+
import org.junit.jupiter.api.Test;
48+
49+
@Tag("UnitTest")
50+
final class PreferencesWindowTest {
51+
@Test
52+
void constructor_Smoke() {
53+
// Most basic test, that the window does not blow up on construction
54+
Assertions.assertDoesNotThrow(PreferencesWindow::new);
55+
}
56+
}

src/test/java/com/itextpdf/rups/view/itext/FormTreeTest.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ This file is part of the iText (R) project.
4747
import com.itextpdf.kernel.pdf.PdfObject;
4848
import com.itextpdf.kernel.pdf.PdfReader;
4949
import com.itextpdf.rups.controller.PdfReaderController;
50+
import com.itextpdf.rups.mock.NoopProgressDialog;
51+
import com.itextpdf.rups.model.IRupsEventListener;
5052
import com.itextpdf.rups.model.IndirectObjectFactory;
53+
import com.itextpdf.rups.model.ObjectLoader;
54+
import com.itextpdf.rups.model.PdfFile;
5155
import com.itextpdf.rups.model.TreeNodeFactory;
5256
import com.itextpdf.rups.view.itext.treenodes.PdfObjectTreeNode;
5357
import com.itextpdf.rups.view.itext.treenodes.XfaTreeNode;
@@ -60,7 +64,7 @@ This file is part of the iText (R) project.
6064
import java.io.File;
6165

6266
@Tag("UnitTest")
63-
class FormTreeTest extends ExtendedITextTest {
67+
final class FormTreeTest extends ExtendedITextTest {
6468
private static final String SOURCE_DIR = "./src/test/resources/com/itextpdf/rups/view/itext/";
6569

6670
@Test
@@ -91,4 +95,27 @@ void testLoadXfa() {
9195
FormTree.loadXfa(factory, xfaTreeNode, xfaObjTreeNode);
9296
});
9397
}
98+
99+
@Test
100+
void testHandleOpenDocument() {
101+
Assertions.assertDoesNotThrow(() -> {
102+
// Preload everything
103+
final PdfFile pdfFile = PdfFile.open(
104+
new File(SOURCE_DIR + "cmp_purchase_order_filled.pdf")
105+
);
106+
107+
// Using a noop listener here to prevent threading issues
108+
final ObjectLoader loader = new ObjectLoader(
109+
new IRupsEventListener() {
110+
}, pdfFile, "Test loader", new NoopProgressDialog()
111+
);
112+
loader.execute();
113+
loader.get();
114+
115+
// There is a FormTree inside the controller, which will also
116+
// handle the event
117+
final PdfReaderController controller = new PdfReaderController(null, null);
118+
controller.handleOpenDocument(loader);
119+
});
120+
}
94121
}

0 commit comments

Comments
 (0)