Skip to content

Commit 7be0674

Browse files
author
Eugene Bochilo
committed
Move XML parsers creation to util methods and fix several tests
DEVSIX-6388
1 parent e3c011f commit 7be0674

File tree

22 files changed

+126
-77
lines changed

22 files changed

+126
-77
lines changed

commons/src/test/java/com/itextpdf/commons/utils/MessageFormatUtilTest.java

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,42 @@ This file is part of the iText (R) project.
2424

2525
import com.itextpdf.test.ExtendedITextTest;
2626

27-
import java.awt.SystemTray;
28-
import java.io.Console;
29-
import java.text.MessageFormat;
30-
import java.util.ArrayList;
3127
import java.util.Arrays;
32-
import java.util.List;
33-
import java.util.Locale;
34-
import java.util.Stack;
28+
29+
import com.itextpdf.test.annotations.type.UnitTest;
3530
import org.junit.Assert;
3631
import org.junit.Test;
32+
import org.junit.experimental.categories.Category;
3733
import org.junit.runner.RunWith;
3834
import org.junit.runners.Parameterized;
39-
import org.junit.runners.Parameterized.Parameters;
4035

4136
@RunWith(Parameterized.class)
37+
@Category(UnitTest.class)
4238
public class MessageFormatUtilTest extends ExtendedITextTest {
4339

4440
private String expectedResult;
4541
private String pattern;
4642
private Object[] arguments;
4743

4844
public MessageFormatUtilTest(Object expectedResult, Object pattern, Object arguments, Object name) {
49-
this.expectedResult = (String)expectedResult;
50-
this.pattern = (String)pattern;
45+
this.expectedResult = (String) expectedResult;
46+
this.pattern = (String) pattern;
5147
this.arguments = (Object[]) arguments;
5248
}
5349

5450
@Parameterized.Parameters(name = "{index}: {3} format: {1}; {0}")
5551
public static Iterable<Object[]> dataSource() {
5652
return Arrays.asList(new Object[][]{
57-
{"Plain message with params 1 test", "Plain message with params {0} {1}", new Object[] {1, "test"},"test with simple params"},
58-
{"Message with 'single quotes'", "Message with 'single quotes'", new Object[0],"test with single quotes"},
59-
{"Message with ''doubled single quotes''", "Message with ''doubled single quotes''", new Object[0],"test with doubled single quotes"},
60-
{"Message with {curly braces} and a parameter {I'm between curly braces too}", "Message with {{curly braces}} and a parameter {{{0}}}", new Object[]{"I'm between curly braces too"},"Test with curly braces"},
61-
{"'{value}'", "'{{{0}}}'", new Object[]{"value"},"Mix om multiple brackets and quotes 1"},
62-
{"'value'", "'{0}'", new Object[]{"value"},"Mix of brackets and quotes"},
63-
{"{'0'}", "{{'0'}}", new Object[0],"Mix of multiple brackets and quotes 2"},
64-
{"single opening brace {0 test" , "single opening brace {{0 test", new Object[0],"Test single opening brace"},
65-
{"single closing brace 0} test", "single closing brace 0}} test", new Object[0],"Test single closing brace"},
66-
{"single opening + closing brace { test }", "single opening + closing brace {{ {0} }}", new Object[]{"test"},"Test single opening and closing brace"},
53+
{"Plain message with params 1 test", "Plain message with params {0} {1}", new Object[]{1, "test"}, "test with simple params"},
54+
{"Message with 'single quotes'", "Message with 'single quotes'", new Object[0], "test with single quotes"},
55+
{"Message with ''doubled single quotes''", "Message with ''doubled single quotes''", new Object[0], "test with doubled single quotes"},
56+
{"Message with {curly braces} and a parameter {I'm between curly braces too}", "Message with {{curly braces}} and a parameter {{{0}}}", new Object[]{"I'm between curly braces too"}, "Test with curly braces"},
57+
{"'{value}'", "'{{{0}}}'", new Object[]{"value"}, "Mix om multiple brackets and quotes 1"},
58+
{"'value'", "'{0}'", new Object[]{"value"}, "Mix of brackets and quotes"},
59+
{"{'0'}", "{{'0'}}", new Object[0], "Mix of multiple brackets and quotes 2"},
60+
{"single opening brace {0 test", "single opening brace {{0 test", new Object[0], "Test single opening brace"},
61+
{"single closing brace 0} test", "single closing brace 0}} test", new Object[0], "Test single closing brace"},
62+
{"single opening + closing brace { test }", "single opening + closing brace {{ {0} }}", new Object[]{"test"}, "Test single opening and closing brace"},
6763
});
6864
}
6965

forms/src/test/java/com/itextpdf/forms/xfa/SecurityTestXmlParserFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.forms.xfa;
4444

45+
import com.itextpdf.io.util.XmlUtil;
4546
import com.itextpdf.kernel.exceptions.PdfException;
4647
import com.itextpdf.kernel.utils.DefaultSafeXmlParserFactory;
4748
import com.itextpdf.test.ExceptionTestUtil;
4849

4950
import javax.xml.parsers.DocumentBuilder;
50-
import javax.xml.parsers.DocumentBuilderFactory;
5151
import javax.xml.parsers.ParserConfigurationException;
52+
5253
import org.xml.sax.EntityResolver;
5354
import org.xml.sax.InputSource;
5455

@@ -58,7 +59,7 @@ public class SecurityTestXmlParserFactory extends DefaultSafeXmlParserFactory {
5859
public DocumentBuilder createDocumentBuilderInstance(boolean namespaceAware, boolean ignoringComments) {
5960
DocumentBuilder db;
6061
try {
61-
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
62+
db = XmlUtil.getDocumentBuilderFactory().newDocumentBuilder();
6263
} catch (ParserConfigurationException e) {
6364
throw new PdfException(e.getMessage(), e);
6465
}

forms/src/test/java/com/itextpdf/forms/xfa/XfaSecurityTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class XfaSecurityTest extends ExtendedITextTest {
7979

8080
@Before
8181
public void resetXmlParserFactoryToDefault() {
82-
XmlProcessorCreator.setXmlParserFactory(new DefaultSafeXmlParserFactory());
82+
XmlProcessorCreator.setXmlParserFactory(null);
8383
}
8484

8585
@Test

forms/src/test/java/com/itextpdf/forms/xfdf/SecurityTestXmlParserFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.forms.xfdf;
4444

45+
import com.itextpdf.io.util.XmlUtil;
4546
import com.itextpdf.kernel.exceptions.PdfException;
4647
import com.itextpdf.kernel.utils.DefaultSafeXmlParserFactory;
4748

4849
import javax.xml.parsers.DocumentBuilder;
49-
import javax.xml.parsers.DocumentBuilderFactory;
5050
import javax.xml.parsers.ParserConfigurationException;
51+
5152
import org.xml.sax.EntityResolver;
5253
import org.xml.sax.InputSource;
5354

@@ -56,7 +57,7 @@ class SecurityTestXmlParserFactory extends DefaultSafeXmlParserFactory {
5657
public DocumentBuilder createDocumentBuilderInstance(boolean namespaceAware, boolean ignoringComments) {
5758
DocumentBuilder db;
5859
try {
59-
db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
60+
db = XmlUtil.getDocumentBuilderFactory().newDocumentBuilder();
6061
} catch (ParserConfigurationException e) {
6162
throw new PdfException(e.getMessage(), e);
6263
}

forms/src/test/java/com/itextpdf/forms/xfdf/XfdfSecurityTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public class XfdfSecurityTest extends ExtendedITextTest {
7777
@Test
7878
public void xxeVulnerabilityXfdfTest()
7979
throws IOException {
80-
XmlProcessorCreator.setXmlParserFactory(new DefaultSafeXmlParserFactory());
80+
XmlProcessorCreator.setXmlParserFactory(null);
8181
try (InputStream inputStream = new ByteArrayInputStream(XFDF_WITH_XXE.getBytes(StandardCharsets.UTF_8))) {
8282
Exception e = Assert.assertThrows(PdfException.class,
8383
() -> XfdfFileUtils.createXfdfDocumentFromStream(inputStream)

io/src/main/java/com/itextpdf/io/util/XmlUtil.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ This file is part of the iText (R) project.
4747

4848
import javax.xml.parsers.DocumentBuilderFactory;
4949
import javax.xml.parsers.ParserConfigurationException;
50+
import javax.xml.parsers.SAXParserFactory;
5051

5152
/**
5253
* This file is a helper class for internal usage only.
@@ -57,6 +58,24 @@ public final class XmlUtil {
5758
private XmlUtil() {
5859
}
5960

61+
/**
62+
* Creates default document builder factory.
63+
*
64+
* @return document builder factory implementation
65+
*/
66+
public static DocumentBuilderFactory getDocumentBuilderFactory() {
67+
return DocumentBuilderFactory.newInstance();
68+
}
69+
70+
/**
71+
* Creates default SAX parser factory.
72+
*
73+
* @return SAX parser factory implementation
74+
*/
75+
public static SAXParserFactory createSAXParserFactory() {
76+
return SAXParserFactory.newInstance();
77+
}
78+
6079
/**
6180
* This method creates a new empty Xml document.
6281
*

io/src/test/java/com/itextpdf/io/source/OutputStreamTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ public void resetExceptionTest() throws IOException {
354354

355355
@Test
356356
public void localHighPrecisionOverridesGlobalTest() throws IOException {
357+
358+
boolean highPrecision = OutputStream.getHighPrecision();
359+
357360
//the data is random
358361
double numberToWrite = 2.000002d;
359362
try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
@@ -364,6 +367,8 @@ public void localHighPrecisionOverridesGlobalTest() throws IOException {
364367
stream.writeDouble(numberToWrite);
365368
stream.flush();
366369
Assert.assertEquals("2", new String(bytes.toByteArray(), StandardCharsets.UTF_8));
370+
} finally {
371+
OutputStream.setHighPrecision(highPrecision);
367372
}
368373
}
369374
}

io/src/test/java/com/itextpdf/io/util/XmlUtilTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,16 @@ This file is part of the iText (R) project.
4444

4545
import com.itextpdf.test.ExtendedITextTest;
4646
import com.itextpdf.test.annotations.type.UnitTest;
47+
import com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl;
48+
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
4749
import org.junit.Assert;
4850
import org.junit.Test;
4951
import org.junit.experimental.categories.Category;
5052
import org.w3c.dom.Document;
5153

54+
import javax.xml.parsers.DocumentBuilderFactory;
55+
import javax.xml.parsers.SAXParserFactory;
56+
5257
@Category(UnitTest.class)
5358
public class XmlUtilTest extends ExtendedITextTest {
5459

@@ -57,4 +62,18 @@ public void initNewXmlDocumentTest() throws Exception {
5762
Document doc = XmlUtil.initNewXmlDocument();
5863
Assert.assertNotNull(doc);
5964
}
65+
66+
@Test
67+
public void getDocumentBuilderFactoryTest() {
68+
DocumentBuilderFactory factory = XmlUtil.getDocumentBuilderFactory();
69+
70+
Assert.assertEquals(DocumentBuilderFactoryImpl.class, factory.getClass());
71+
}
72+
73+
@Test
74+
public void createSAXParserFactoryTest() {
75+
SAXParserFactory factory = XmlUtil.createSAXParserFactory();
76+
77+
Assert.assertEquals(SAXParserFactoryImpl.class, factory.getClass());
78+
}
6079
}

kernel/src/main/java/com/itextpdf/kernel/utils/DefaultSafeXmlParserFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ This file is part of the iText (R) project.
4343
package com.itextpdf.kernel.utils;
4444

4545
import com.itextpdf.commons.utils.MessageFormatUtil;
46+
import com.itextpdf.io.util.XmlUtil;
4647
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
4748
import com.itextpdf.kernel.logs.KernelLogMessageConstant;
4849
import com.itextpdf.kernel.exceptions.PdfException;
@@ -52,6 +53,7 @@ This file is part of the iText (R) project.
5253
import javax.xml.parsers.ParserConfigurationException;
5354
import javax.xml.parsers.SAXParser;
5455
import javax.xml.parsers.SAXParserFactory;
56+
5557
import org.slf4j.Logger;
5658
import org.slf4j.LoggerFactory;
5759
import org.xml.sax.EntityResolver;
@@ -112,7 +114,7 @@ public DefaultSafeXmlParserFactory() {
112114

113115
@Override
114116
public DocumentBuilder createDocumentBuilderInstance(boolean namespaceAware, boolean ignoringComments) {
115-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
117+
DocumentBuilderFactory factory = XmlUtil.getDocumentBuilderFactory();
116118
configureSafeDocumentBuilderFactory(factory);
117119
factory.setNamespaceAware(namespaceAware);
118120
factory.setIgnoringComments(ignoringComments);
@@ -128,7 +130,7 @@ public DocumentBuilder createDocumentBuilderInstance(boolean namespaceAware, boo
128130

129131
@Override
130132
public XMLReader createXMLReaderInstance(boolean namespaceAware, boolean validating) {
131-
SAXParserFactory factory = SAXParserFactory.newInstance();
133+
SAXParserFactory factory = XmlUtil.createSAXParserFactory();
132134
factory.setNamespaceAware(namespaceAware);
133135
factory.setValidating(validating);
134136
configureSafeSAXParserFactory(factory);

kernel/src/test/java/com/itextpdf/kernel/pdf/PdfObjectReleaseTest.java

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ This file is part of the iText (R) project.
4141
@Category(IntegrationTest.class)
4242
public class PdfObjectReleaseTest extends ExtendedITextTest {
4343

44-
public static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/pdf/PdfObjectReleaseTest/";
45-
public static final String destinationFolder = "./target/test/com/itextpdf/kernel/pdf/PdfObjectReleaseTest/";
44+
public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/kernel/pdf/PdfObjectReleaseTest/";
45+
public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/kernel/pdf/PdfObjectReleaseTest/";
4646

4747
@BeforeClass
4848
public static void beforeClass() {
49-
createOrClearDestinationFolder(destinationFolder);
49+
createOrClearDestinationFolder(DESTINATION_FOLDER);
5050
}
5151

5252
@Test
@@ -76,72 +76,66 @@ public void releaseObjectsInSimpleDocTest() throws IOException, InterruptedExcep
7676
@Test
7777
@LogMessages(messages = @LogMessage(messageTemplate = IoLogMessageConstant.FORBID_RELEASE_IS_SET))
7878
public void releaseCatalogTest() throws IOException, InterruptedException {
79-
String srcFile = sourceFolder + "releaseObjectsInSimpleDoc.pdf";
80-
String release = destinationFolder + "outReleaseObjectsInSimpleDoc.pdf";
79+
String srcFile = SOURCE_FOLDER + "releaseObjectsInSimpleDoc.pdf";
80+
String release = DESTINATION_FOLDER + "outReleaseObjectsInSimpleDoc.pdf";
8181

8282
try (PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(release))) {
8383
doc.getCatalog().getPdfObject().release();
8484
}
8585

86-
Assert.assertNull(new CompareTool().compareByContent(release, srcFile, destinationFolder));
86+
Assert.assertNull(new CompareTool().compareByContent(release, srcFile, DESTINATION_FOLDER));
8787
}
8888

8989
@Test
9090
@LogMessages(messages = @LogMessage(messageTemplate = IoLogMessageConstant.FORBID_RELEASE_IS_SET))
9191
public void releasePagesTest() throws IOException, InterruptedException {
92-
String srcFile = sourceFolder + "releaseObjectsInSimpleDoc.pdf";
93-
String release = destinationFolder + "outReleaseObjectsInSimpleDoc.pdf";
92+
String srcFile = SOURCE_FOLDER + "releaseObjectsInSimpleDoc.pdf";
93+
String release = DESTINATION_FOLDER + "outReleaseObjectsInSimpleDoc.pdf";
9494

9595
try (PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(release))) {
9696
doc.getCatalog().getPdfObject().getAsDictionary(PdfName.Pages).release();
9797
}
9898

99-
Assert.assertNull(new CompareTool().compareByContent(release, srcFile, destinationFolder));
99+
Assert.assertNull(new CompareTool().compareByContent(release, srcFile, DESTINATION_FOLDER));
100100
}
101101

102102
@Test
103103
@LogMessages(messages = @LogMessage(messageTemplate = IoLogMessageConstant.FORBID_RELEASE_IS_SET))
104104
public void releaseStructTreeRootTest() throws IOException, InterruptedException {
105-
String srcFile = sourceFolder + "releaseObjectsInDocWithStructTreeRoot.pdf";
106-
String release = destinationFolder + "outReleaseObjectsInDocWithStructTreeRoot.pdf";
105+
String srcFile = SOURCE_FOLDER + "releaseObjectsInDocWithStructTreeRoot.pdf";
106+
String release = DESTINATION_FOLDER + "outReleaseObjectsInDocWithStructTreeRoot.pdf";
107107

108108
try (PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(release))) {
109109
doc.getStructTreeRoot().getPdfObject().release();
110110
}
111111

112-
Assert.assertNull(new CompareTool().compareByContent(release, srcFile, destinationFolder));
112+
Assert.assertNull(new CompareTool().compareByContent(release, srcFile, DESTINATION_FOLDER));
113113
}
114114

115115
@Test
116-
public void noForbidReleaseObjectsModifyingTest() throws IOException, InterruptedException {
117-
String srcFile = sourceFolder + "noForbidReleaseObjectsModifying.pdf";
118-
String stampReleased = sourceFolder + "noForbidReleaseObjectsModified.pdf";
116+
@LogMessages(messages = @LogMessage(messageTemplate = IoLogMessageConstant.FORBID_RELEASE_IS_SET))
117+
public void releaseModifiedObjectTest() throws IOException, InterruptedException {
118+
String srcFile = SOURCE_FOLDER + "releaseModifiedObject.pdf";
119+
String cmpFile = SOURCE_FOLDER + "cmp_releaseModifiedObject.pdf";
120+
String outFile = DESTINATION_FOLDER + "releaseModifiedObject.pdf";
119121

120-
try (PdfDocument doc = new PdfDocument(
121-
new PdfReader(srcFile),
122-
new PdfWriter(destinationFolder + "noForbidReleaseObjectsModifying.pdf"),
123-
new StampingProperties().useAppendMode())) {
122+
try (PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outFile))) {
124123

125124
PdfAnnotation annots = doc.getPage(1).getAnnotations().get(0);
126125

127126
annots.setRectangle(new PdfArray(new Rectangle(100, 100, 80, 50)));
128-
annots.getRectangle().release();
129-
}
130-
131-
try (PdfDocument openPrev = new PdfDocument(new PdfReader(stampReleased))) {
132-
Assert.assertTrue(new Rectangle(100, 100, 80, 50).equalsWithEpsilon(
133-
openPrev.getPage(1).getAnnotations().get(0).getRectangle().toRectangle()));
127+
annots.getPdfObject().release();
134128
}
135129

136-
Assert.assertNotNull(new CompareTool().compareByContent(srcFile, stampReleased, destinationFolder));
130+
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFile, DESTINATION_FOLDER));
137131
}
138132

139133
@Test
140134
public void addingReleasedObjectToDocumentTest() throws IOException {
141-
String srcFile = sourceFolder + "releaseObjectsInSimpleDoc.pdf";
135+
String srcFile = SOURCE_FOLDER + "releaseObjectsInSimpleDoc.pdf";
142136

143137
PdfDocument doc = new PdfDocument(new PdfReader(srcFile),
144-
new PdfWriter(sourceFolder + "addingReleasedObjectToDocument.pdf"));
138+
new PdfWriter(SOURCE_FOLDER + "addingReleasedObjectToDocument.pdf"));
145139
try {
146140
PdfObject releasedObj = doc.getPdfObject(1);
147141
releasedObj.release();
@@ -157,9 +151,9 @@ public void addingReleasedObjectToDocumentTest() throws IOException {
157151
}
158152

159153
private void singlePdfObjectReleaseTest(String inputFilename, String outStampingFilename, String outStampingReleaseFilename) throws IOException, InterruptedException {
160-
String srcFile = sourceFolder + inputFilename;
161-
String outPureStamping = destinationFolder + outStampingFilename;
162-
String outStampingRelease = destinationFolder + outStampingReleaseFilename;
154+
String srcFile = SOURCE_FOLDER + inputFilename;
155+
String outPureStamping = DESTINATION_FOLDER + outStampingFilename;
156+
String outStampingRelease = DESTINATION_FOLDER + outStampingReleaseFilename;
163157

164158
PdfDocument doc = new PdfDocument(new PdfReader(srcFile), new PdfWriter(outPureStamping));
165159
// We open/close document to make sure that the results of release logic and simple overwriting coincide.
@@ -176,6 +170,6 @@ private void singlePdfObjectReleaseTest(String inputFilename, String outStamping
176170

177171
stamperRelease.close();
178172

179-
Assert.assertNull(new CompareTool().compareByContent(outStampingRelease, outPureStamping, destinationFolder));
173+
Assert.assertNull(new CompareTool().compareByContent(outStampingRelease, outPureStamping, DESTINATION_FOLDER));
180174
}
181175
}

0 commit comments

Comments
 (0)