Skip to content

Commit 4879291

Browse files
author
Kate Ivanova
committed
Refactor TaggedPdfFormTest to decrease code duplication
DEVSIX-4613
1 parent 34da5c6 commit 4879291

File tree

13 files changed

+68
-85
lines changed

13 files changed

+68
-85
lines changed

src/test/java/com/itextpdf/html2pdf/element/TaggedPdfFormTest.java

Lines changed: 43 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,23 @@ This file is part of the iText (R) project.
4242
*/
4343
package com.itextpdf.html2pdf.element;
4444

45-
46-
import com.itextpdf.forms.PdfAcroForm;
47-
import com.itextpdf.html2pdf.ConverterProperties;
48-
import com.itextpdf.html2pdf.HtmlConverter;
45+
import com.itextpdf.html2pdf.ExtendedHtmlConversionITextTest;
4946
import com.itextpdf.kernel.PdfException;
50-
import com.itextpdf.kernel.pdf.PdfDocument;
51-
import com.itextpdf.kernel.pdf.PdfReader;
52-
import com.itextpdf.kernel.pdf.PdfWriter;
53-
import com.itextpdf.kernel.utils.CompareTool;
54-
import com.itextpdf.test.ExtendedITextTest;
5547
import com.itextpdf.test.annotations.type.IntegrationTest;
56-
import org.junit.Assert;
48+
49+
import javax.xml.parsers.ParserConfigurationException;
5750
import org.junit.BeforeClass;
5851
import org.junit.Ignore;
5952
import org.junit.Rule;
6053
import org.junit.Test;
6154
import org.junit.experimental.categories.Category;
6255
import org.junit.rules.ExpectedException;
6356

64-
import java.io.FileInputStream;
6557
import java.io.IOException;
58+
import org.xml.sax.SAXException;
6659

6760
@Category(IntegrationTest.class)
68-
public class TaggedPdfFormTest extends ExtendedITextTest {
69-
61+
public class TaggedPdfFormTest extends ExtendedHtmlConversionITextTest {
7062

7163
public static final String sourceFolder = "./src/test/resources/com/itextpdf/html2pdf/element/TaggedPdfFormTest/";
7264
public static final String destinationFolder = "./target/test/com/itextpdf/html2pdf/element/TaggedPdfFormTest/";
@@ -80,107 +72,86 @@ public static void beforeClass() {
8072
public ExpectedException junitExpectedException = ExpectedException.none();
8173

8274
@Test
83-
public void simpleTextFieldTagged() throws IOException, InterruptedException {
84-
runTest("simpleTextFieldTagged");
75+
public void simpleTextFieldTagged()
76+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
77+
convertToPdfAcroformFlattenAndCompare("simpleTextField", sourceFolder, destinationFolder, true);
8578
}
8679

8780
@Test
88-
public void simpleTextareaTagged() throws IOException, InterruptedException {
89-
runTest("simpleTextareaTagged");
81+
public void simpleTextareaTagged()
82+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
83+
convertToPdfAcroformFlattenAndCompare("simpleTextarea", sourceFolder, destinationFolder, true);
9084
}
9185

9286
@Test
93-
public void simpleButtonTagged() throws IOException, InterruptedException {
94-
runTest("simpleButtonTagged");
87+
public void simpleButtonTagged()
88+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
89+
convertToPdfAcroformFlattenAndCompare("simpleButton", sourceFolder, destinationFolder, true);
9590
}
9691

9792
@Test
98-
public void simpleLabelTagged() throws IOException, InterruptedException {
99-
runTest("simpleLabelTagged");
93+
public void simpleLabelTagged()
94+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
95+
convertToPdfAcroformFlattenAndCompare("simpleLabel", sourceFolder, destinationFolder, true);
10096
}
10197

10298
@Test
103-
public void simpleCheckboxTagged() throws IOException, InterruptedException {
104-
runTest("simpleCheckboxTagged");
99+
public void simpleCheckboxTagged()
100+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
101+
convertToPdfAcroformFlattenAndCompare("simpleCheckbox", sourceFolder, destinationFolder, true);
105102
}
106103

107104
@Test
108105
@Ignore("DEVSIX-1901")
109-
public void simpleSelectTagged() throws IOException, InterruptedException {
110-
runTest("simpleSelectTagged");
106+
public void simpleSelectTagged()
107+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
108+
convertToPdfAcroformFlattenAndCompare("simpleSelect", sourceFolder, destinationFolder, true);
111109
}
112110

113111
@Test
114112
@Ignore("DEVSIX-1901")
115-
public void listBoxSelectTagged() throws IOException, InterruptedException {
116-
runTest("listBoxSelectTagged");
113+
public void listBoxSelectTagged()
114+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
115+
convertToPdfAcroformFlattenAndCompare("listBoxSelect", sourceFolder, destinationFolder, true);
117116
}
118117

119118
@Test
120119
@Ignore("DEVSIX-1901")
121-
public void listBoxOptGroupSelectTagged() throws IOException, InterruptedException {
122-
runTest("listBoxOptGroupSelectTagged");
120+
public void listBoxOptGroupSelectTagged()
121+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
122+
convertToPdfAcroformFlattenAndCompare("listBoxOptGroupSelect", sourceFolder,
123+
destinationFolder, true);
123124
}
124125

125126
@Test
126127
@Ignore("DEVSIX-1901")
127-
public void simpleRadioFormTagged() throws IOException, InterruptedException {
128-
runTest("simpleRadioFormTagged");
128+
public void simpleRadioFormTagged()
129+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
130+
convertToPdfAcroformFlattenAndCompare("simpleRadioForm", sourceFolder, destinationFolder, true);
129131
}
130132

131133
@Test
132134
@Ignore("DEVSIX-980. DefaultHtmlProcessor ERROR No worker found for tag datalist")
133-
public void dataListFormTagged() throws IOException, InterruptedException {
134-
runTest("dataListFormTagged");
135+
public void dataListFormTagged()
136+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
137+
convertToPdfAcroformFlattenAndCompare("dataListForm", sourceFolder, destinationFolder, true);
135138
}
136139

137140
@Test
138-
public void fieldSetFormTagged() throws IOException, InterruptedException {
139-
runTest("fieldSetFormTagged");
141+
public void fieldSetFormTagged()
142+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
143+
convertToPdfAcroformFlattenAndCompare("fieldSetForm", sourceFolder, destinationFolder, true);
140144
}
141145

142146
@Test
143147
// TODO DEVSIX-4601
144148
// exception is thrown on "convert tagged PDF with acroform" stage
145-
public void inputFormPrematureFlush() throws IOException, InterruptedException {
149+
public void inputFormPrematureFlush()
150+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
146151
junitExpectedException.expect(PdfException.class);
147152
junitExpectedException.expectMessage(PdfException.TagStructureFlushingFailedItMightBeCorrupted);
148-
runTest("inputFormPrematureFlush");
149-
}
150153

151-
private void runTest(String name) throws IOException, InterruptedException {
152-
String htmlPath = sourceFolder + name + ".html";
153-
String outTaggedPdfPath = destinationFolder + name + ".pdf";
154-
String outTaggedPdfPathAcro = destinationFolder + name + "_acro.pdf";
155-
String outTaggedPdfPathFlatted = destinationFolder + name + "_acro_flatten.pdf";
156-
String cmpPdfPath = sourceFolder + "cmp_" + name + ".pdf";
157-
String cmpPdfPathAcro = sourceFolder + "cmp_" + name + "_acro.pdf";
158-
String cmpPdfPathAcroFlatten = sourceFolder + "cmp_" + name + "_acro_flatten.pdf";
159-
String diff1 = "diff1_" + name;
160-
String diff2 = "diff2_" + name;
161-
String diff3 = "diff3_" + name;
162-
163-
//convert tagged PDF without acroform (from html with form elements)
164-
PdfWriter taggedWriter = new PdfWriter(outTaggedPdfPath);
165-
PdfDocument pdfTagged = new PdfDocument(taggedWriter);
166-
pdfTagged.setTagged();
167-
HtmlConverter.convertToPdf(new FileInputStream(htmlPath), pdfTagged);
168-
169-
//convert tagged PDF with acroform
170-
PdfWriter taggedWriterAcro = new PdfWriter(outTaggedPdfPathAcro);
171-
PdfDocument pdfTaggedAcro = new PdfDocument(taggedWriterAcro);
172-
pdfTaggedAcro.setTagged();
173-
ConverterProperties converterPropertiesAcro = new ConverterProperties();
174-
converterPropertiesAcro.setCreateAcroForm(true);
175-
HtmlConverter.convertToPdf(new FileInputStream(htmlPath), pdfTaggedAcro, converterPropertiesAcro);
176-
177-
//flatted created tagged PDF with acroform
178-
PdfDocument document = new PdfDocument(new PdfReader(outTaggedPdfPathAcro), new PdfWriter(outTaggedPdfPathFlatted));
179-
PdfAcroForm.getAcroForm(document, false).flattenFields();
180-
document.close();
181-
182-
Assert.assertNull(new CompareTool().compareByContent(outTaggedPdfPath, cmpPdfPath, destinationFolder, diff1));
183-
Assert.assertNull(new CompareTool().compareByContent(outTaggedPdfPathAcro, cmpPdfPathAcro, destinationFolder, diff2));
184-
Assert.assertNull(new CompareTool().compareByContent(outTaggedPdfPathFlatted, cmpPdfPathAcroFlatten, destinationFolder, diff3));
154+
convertToPdfAcroformFlattenAndCompare("inputFormPrematureFlush",
155+
sourceFolder, destinationFolder, true);
185156
}
186157
}

src/test/java/com/itextpdf/html2pdf/element/TagsInsideButtonTest.java

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ This file is part of the iText (R) project.
5555
import javax.xml.parsers.ParserConfigurationException;
5656
import java.io.IOException;
5757

58-
5958
@Category(IntegrationTest.class)
6059
public class TagsInsideButtonTest extends ExtendedHtmlConversionITextTest {
6160

@@ -71,32 +70,45 @@ public static void beforeClass() {
7170
}
7271

7372
@Test
74-
public void buttonWithImageInside() throws IOException, InterruptedException, ParserConfigurationException, SAXException {
75-
convertToPdfAcroformFlattenAndCompare("buttonWithImageInside", sourceFolder, destinationFolder, false);
73+
public void buttonWithImageInside()
74+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
75+
convertToPdfAcroformFlattenAndCompare("buttonWithImageInside",
76+
sourceFolder, destinationFolder, false);
7677
}
7778

7879
@Test
79-
public void buttonWithImageInsideTagged() throws IOException, InterruptedException, ParserConfigurationException, SAXException {
80-
convertToPdfAcroformFlattenAndCompare("buttonWithImageInside", sourceFolder, destinationFolder, true);
80+
public void buttonWithImageInsideTagged()
81+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
82+
convertToPdfAcroformFlattenAndCompare("buttonWithImageInside",
83+
sourceFolder, destinationFolder, true);
8184
}
8285

8386
@Test
84-
public void buttonWithPInside() throws IOException, InterruptedException, ParserConfigurationException, SAXException {
85-
convertToPdfAcroformFlattenAndCompare("buttonWithPInside", sourceFolder, destinationFolder, false);
87+
public void buttonWithPInside()
88+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
89+
convertToPdfAcroformFlattenAndCompare("buttonWithPInside",
90+
sourceFolder, destinationFolder, false);
8691
}
8792

8893
@Test
89-
public void imageNotFinishedTagged() throws IOException, InterruptedException, ParserConfigurationException, SAXException {
90-
convertToPdfAcroformFlattenAndCompare("imageNotFinishedTagged", sourceFolder, destinationFolder, true);
94+
public void imageNotFinishedTagged()
95+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
96+
convertToPdfAcroformFlattenAndCompare("imageNotFinishedTagged", sourceFolder,
97+
destinationFolder, true);
9198
}
9299

93100
@Test
94-
public void buttonWithPInsideTagged() throws IOException, InterruptedException, ParserConfigurationException, SAXException {
95-
convertToPdfAcroformFlattenAndCompare("buttonWithPInside", sourceFolder, destinationFolder, true);
101+
public void buttonWithPInsideTagged()
102+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
103+
convertToPdfAcroformFlattenAndCompare("buttonWithPInside", sourceFolder,
104+
destinationFolder, true);
96105
}
106+
97107
@Test
98-
public void buttonInsideMoreThanTwoAreas() throws IOException, InterruptedException, ParserConfigurationException, SAXException {
108+
public void buttonInsideMoreThanTwoAreas()
109+
throws IOException, InterruptedException, ParserConfigurationException, SAXException {
99110
junitExpectedException.expect(PdfException.class);
100-
convertToPdfAcroformFlattenAndCompare("buttonInsideMoreThanTwoAreas", sourceFolder, destinationFolder, true);
111+
convertToPdfAcroformFlattenAndCompare("buttonInsideMoreThanTwoAreas", sourceFolder,
112+
destinationFolder, true);
101113
}
102114
}

0 commit comments

Comments
 (0)