Skip to content

Commit 8eeaf82

Browse files
Add tests to reset outlineHandler and cover resolveAgainstBaseUri. Refactor Html2PdfTest
DEVSIX-6137
1 parent 80d5a3b commit 8eeaf82

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+140
-5588
lines changed

src/test/java/com/itextpdf/html2pdf/Html2PdfTest.java

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

45-
import com.itextpdf.html2pdf.attach.IHtmlProcessor;
46-
import com.itextpdf.html2pdf.attach.impl.DefaultHtmlProcessor;
47-
import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
4845
import com.itextpdf.html2pdf.logs.Html2PdfLogMessageConstant;
49-
import com.itextpdf.io.logs.IoLogMessageConstant;
50-
import com.itextpdf.io.util.UrlUtil;
51-
import com.itextpdf.kernel.pdf.PdfDocument;
52-
import com.itextpdf.kernel.pdf.PdfWriter;
53-
import com.itextpdf.kernel.utils.CompareTool;
54-
import com.itextpdf.layout.Document;
55-
import com.itextpdf.layout.font.FontProvider;
56-
import com.itextpdf.styledxmlparser.IXmlParser;
5746
import com.itextpdf.styledxmlparser.logs.StyledXmlParserLogMessageConstant;
58-
import com.itextpdf.styledxmlparser.css.media.MediaDeviceDescription;
59-
import com.itextpdf.styledxmlparser.css.media.MediaType;
60-
import com.itextpdf.styledxmlparser.node.IDocumentNode;
61-
import com.itextpdf.styledxmlparser.node.impl.jsoup.JsoupHtmlParser;
62-
import com.itextpdf.test.ExtendedITextTest;
6347
import com.itextpdf.test.annotations.LogMessage;
6448
import com.itextpdf.test.annotations.LogMessages;
6549
import com.itextpdf.test.annotations.type.IntegrationTest;
6650

67-
import java.io.FileInputStream;
68-
import org.junit.Assert;
51+
import java.io.IOException;
6952
import org.junit.BeforeClass;
7053
import org.junit.Test;
7154
import org.junit.experimental.categories.Category;
7255

73-
import java.io.File;
74-
import java.io.IOException;
75-
7656
@Category(IntegrationTest.class)
77-
public class Html2PdfTest extends ExtendedITextTest {
57+
public class Html2PdfTest extends ExtendedHtmlConversionITextTest {
7858

79-
public static final String sourceFolder = "./src/test/resources/com/itextpdf/html2pdf/Html2PdfTest/";
80-
public static final String destinationFolder = "./target/test/com/itextpdf/html2pdf/Html2PdfTest/";
59+
public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/html2pdf/Html2PdfTest/";
60+
public static final String DESTINATION_FOLDER = "./target/test/com/itextpdf/html2pdf/Html2PdfTest/";
8161

8262
@BeforeClass
8363
public static void beforeClass() {
84-
createDestinationFolder(destinationFolder);
64+
createOrClearDestinationFolder(DESTINATION_FOLDER);
8565
}
8666

8767
@Test
8868
public void helloWorldParagraphTest() throws IOException, InterruptedException {
89-
convertAndCompare(sourceFolder + "hello_paragraph.html", destinationFolder + "hello_paragraph.pdf",
90-
sourceFolder + "cmp_hello_paragraph.pdf", destinationFolder, "diff01_");
69+
convertToPdfAndCompare("hello_paragraph", SOURCE_FOLDER, DESTINATION_FOLDER);
9170
}
9271

9372
@Test
9473
public void helloParagraphTableTest() throws IOException, InterruptedException {
95-
convertAndCompare(sourceFolder + "hello_paragraph_table.html", destinationFolder + "hello_paragraph_table.pdf",
96-
sourceFolder + "cmp_hello_paragraph_table.pdf", destinationFolder, "diff02_");
74+
convertToPdfAndCompare("hello_paragraph_table", SOURCE_FOLDER, DESTINATION_FOLDER);
9775
}
9876

9977
@Test
10078
public void helloMalformedDocumentTest() throws IOException, InterruptedException {
101-
convertAndCompare(sourceFolder + "hello_malformed.html", destinationFolder + "hello_malformed.pdf",
102-
sourceFolder + "cmp_hello_malformed.pdf", destinationFolder, "diff03_");
79+
convertToPdfAndCompare("hello_malformed", SOURCE_FOLDER, DESTINATION_FOLDER);
10380
}
10481

10582
@Test
10683
public void helloParagraphJunkSpacesDocumentTest() throws IOException, InterruptedException {
107-
convertAndCompare(sourceFolder + "hello_paragraph_junk_spaces.html", destinationFolder + "hello_paragraph_junk_spaces.pdf",
108-
sourceFolder + "cmp_hello_paragraph_junk_spaces.pdf", destinationFolder, "diff03_");
84+
convertToPdfAndCompare("hello_paragraph_junk_spaces", SOURCE_FOLDER, DESTINATION_FOLDER);
10985
}
11086

11187
@Test
11288
public void helloParagraphNestedInTableDocumentTest() throws IOException, InterruptedException {
113-
convertAndCompare(sourceFolder + "hello_paragraph_nested_in_table.html", destinationFolder + "hello_paragraph_nested_in_table.pdf",
114-
sourceFolder + "cmp_hello_paragraph_nested_in_table.pdf", destinationFolder, "diff03_");
89+
convertToPdfAndCompare("hello_paragraph_nested_in_table", SOURCE_FOLDER, DESTINATION_FOLDER);
11590
}
11691

11792
@Test
11893
public void helloParagraphWithSpansDocumentTest() throws IOException, InterruptedException {
119-
convertAndCompare(sourceFolder + "hello_paragraph_with_span.html", destinationFolder + "hello_paragraph_with_span.pdf",
120-
sourceFolder + "cmp_hello_paragraph_with_span.pdf", destinationFolder, "diff03_");
94+
convertToPdfAndCompare("hello_paragraph_with_span", SOURCE_FOLDER, DESTINATION_FOLDER);
12195
}
12296

12397
@Test
12498
public void helloDivDocumentTest() throws IOException, InterruptedException {
125-
convertAndCompare(sourceFolder + "hello_div.html", destinationFolder + "hello_div.pdf",
126-
sourceFolder + "cmp_hello_div.pdf", destinationFolder, "diff03_");
99+
convertToPdfAndCompare("hello_div", SOURCE_FOLDER, DESTINATION_FOLDER);
127100
}
128101

129102
@Test
130103
public void aBlockInPTagTest() throws IOException, InterruptedException {
131-
convertAndCompare(sourceFolder + "aBlockInPTag.html", destinationFolder + "aBlockInPTag.pdf",
132-
sourceFolder + "cmp_aBlockInPTag.pdf", destinationFolder, "diff03_");
104+
convertToPdfAndCompare("aBlockInPTag", SOURCE_FOLDER, DESTINATION_FOLDER);
133105
}
134106

135107
@Test
136108
public void base64svgTest() throws IOException, InterruptedException {
137-
HtmlConverter.convertToPdf(new File(sourceFolder + "objectTag_base64svg.html"), new File(destinationFolder + "objectTag_base64svg.pdf"));
138-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "objectTag_base64svg.pdf", sourceFolder + "cmp_objectTag_base64svg.pdf", destinationFolder, "diff01_"));
109+
convertToPdfAndCompare("objectTag_base64svg", SOURCE_FOLDER, DESTINATION_FOLDER);
139110
}
140111

141112
@Test
142-
@LogMessages(messages = {@LogMessage(messageTemplate = StyledXmlParserLogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI, count = 1),
113+
@LogMessages(messages = {
114+
@LogMessage(messageTemplate =
115+
StyledXmlParserLogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI, count = 1),
143116
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.WORKER_UNABLE_TO_PROCESS_OTHER_WORKER, count = 1)})
144117
public void htmlObjectIncorrectBase64Test() throws IOException, InterruptedException {
145-
HtmlConverter.convertToPdf(new File(sourceFolder + "objectTag_incorrectBase64svg.html"), new File(destinationFolder + "objectTag_incorrectBase64svg.pdf"));
146-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "objectTag_incorrectBase64svg.pdf", sourceFolder + "cmp_objectTag_incorrectBase64svg.pdf", destinationFolder, "diff01_"));
147-
118+
convertToPdfAndCompare("objectTag_incorrectBase64svg", SOURCE_FOLDER, DESTINATION_FOLDER);
148119
}
149120

150121
@Test
151-
@LogMessages(messages = {@LogMessage(messageTemplate = Html2PdfLogMessageConstant.WORKER_UNABLE_TO_PROCESS_IT_S_TEXT_CONTENT, count = 1),
122+
//TODO: update after DEVSIX-1346
123+
@LogMessages(messages = {
124+
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.WORKER_UNABLE_TO_PROCESS_IT_S_TEXT_CONTENT,
125+
count = 1),
152126
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.WORKER_UNABLE_TO_PROCESS_OTHER_WORKER, count = 2),
153127
})
154128
public void htmlObjectAltTextTest() throws IOException, InterruptedException {
155-
//update after DEVSIX-1346
156-
HtmlConverter.convertToPdf(new File(sourceFolder + "objectTag_altText.html"), new File(destinationFolder + "objectTag_altText.pdf"));
157-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "objectTag_altText.pdf", sourceFolder + "cmp_objectTag_altText.pdf", destinationFolder, "diff01_"));
158-
129+
convertToPdfAndCompare("objectTag_altText", SOURCE_FOLDER, DESTINATION_FOLDER);
159130
}
160131

161132
@Test
162-
@LogMessages(messages = {@LogMessage(messageTemplate = Html2PdfLogMessageConstant.WORKER_UNABLE_TO_PROCESS_OTHER_WORKER, count = 1),})
133+
@LogMessages(messages = {
134+
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.WORKER_UNABLE_TO_PROCESS_OTHER_WORKER,
135+
count = 1),})
163136
public void htmlObjectNestedObjectTest() throws IOException, InterruptedException {
164-
HtmlConverter.convertToPdf(new File(sourceFolder + "objectTag_nestedTag.html"), new File(destinationFolder + "objectTag_nestedTag.pdf"));
165-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "objectTag_nestedTag.pdf", sourceFolder + "cmp_objectTag_nestedTag.pdf", destinationFolder, "diff01_"));
137+
convertToPdfAndCompare("objectTag_nestedTag", SOURCE_FOLDER, DESTINATION_FOLDER);
166138
}
167139

168140
@Test
169-
@LogMessages(ignore = true, messages = {
170-
@LogMessage(messageTemplate = StyledXmlParserLogMessageConstant.RULE_IS_NOT_SUPPORTED),
171-
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.CSS_PROPERTY_IN_PERCENTS_NOT_SUPPORTED),
172-
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.PADDING_VALUE_IN_PERCENT_NOT_SUPPORTED),
173-
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.MARGIN_VALUE_IN_PERCENT_NOT_SUPPORTED),
174-
@LogMessage(messageTemplate = Html2PdfLogMessageConstant.ERROR_PARSING_CSS_SELECTOR),
175-
@LogMessage(messageTemplate = IoLogMessageConstant.WIDOWS_CONSTRAINT_VIOLATED),
176-
})
177-
public void batchConversionTest() throws IOException, InterruptedException {
178-
ConverterProperties properties = new ConverterProperties().setBaseUri(sourceFolder)
179-
.setMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT));
180-
FontProvider fontProvider = new DefaultFontProvider(true, false, false);
181-
fontProvider.addDirectory(sourceFolder + "fonts/");
182-
properties.setFontProvider(fontProvider);
183-
IHtmlProcessor processor = new DefaultHtmlProcessor(properties);
184-
185-
IXmlParser parser = new JsoupHtmlParser();
186-
String outPdfPath = destinationFolder + "smashing1.pdf";
187-
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outPdfPath));
188-
IDocumentNode doc = parser.parse(new FileInputStream(sourceFolder + "smashing01.html"),
189-
properties.getCharset());
190-
Document document = processor.processDocument(doc, pdfDocument);
191-
document.close();
192-
193-
Assert.assertNull(new CompareTool().compareByContent(outPdfPath,
194-
sourceFolder + "cmp_smashing1.pdf", destinationFolder, "diff01_"));
195-
}
196-
197-
@Test
198-
public void htmlImgBase64SVGTest() throws IOException, InterruptedException {
199-
HtmlConverter.convertToPdf(new File(sourceFolder + "imgTag_base64svg.html"), new File(destinationFolder + "imgTag_base64svg.pdf"));
200-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "imgTag_base64svg.pdf", sourceFolder + "cmp_imgTag_base64svg.pdf", destinationFolder, "diff01_"));
201-
}
202-
203-
private void convertAndCompare(String srcFilename, String outFilename, String cmpFilename, String outFolder, String diff) throws IOException, InterruptedException {
204-
HtmlConverter.convertToPdf(new File(srcFilename), new File(outFilename));
205-
System.out.println("html: " + UrlUtil.getNormalizedFileUriString(srcFilename) + "\n");
206-
Assert.assertNull(new CompareTool().compareByContent(outFilename, cmpFilename, outFolder, diff));
141+
public void htmlImgBase64SVGTest() throws IOException, InterruptedException {
142+
convertToPdfAndCompare("imgTag_base64svg", SOURCE_FOLDER, DESTINATION_FOLDER);
207143
}
208144
}

src/test/java/com/itextpdf/html2pdf/attach/impl/OutlineHandlerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,18 @@ public void defaultOutlineHandlerWithHTagHavingIdTest() throws IOException, Inte
125125
Assert.assertNull(new CompareTool().compareByContent(outFile, cmpFile, DESTINATION_FOLDER, "diff_defaultOutlineHandlerWithHTagHavingIdTest"));
126126
}
127127

128+
@Test
129+
public void resetOutlineHandlerTest() throws IOException, InterruptedException {
130+
OutlineHandler outlineHandler = OutlineHandler.createStandardHandler();
131+
132+
for (int i = 1; i <= 3; i++) {
133+
String srcHtml = SOURCE_FOLDER + "outlines0" + i + ".html";
134+
String outPdf = DESTINATION_FOLDER + "outlines0" + i + ".pdf";
135+
String cmpPdf = SOURCE_FOLDER + "cmp_outlines0" + i + ".pdf";
136+
137+
HtmlConverter.convertToPdf(new File(srcHtml), new File(outPdf),
138+
new ConverterProperties().setOutlineHandler(outlineHandler));
139+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_" + i));
140+
}
141+
}
128142
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
package com.itextpdf.html2pdf.resolver;
24+
25+
import com.itextpdf.html2pdf.ConverterProperties;
26+
import com.itextpdf.html2pdf.HtmlConverter;
27+
import com.itextpdf.kernel.utils.CompareTool;
28+
import com.itextpdf.styledxmlparser.logs.StyledXmlParserLogMessageConstant;
29+
import com.itextpdf.test.ExtendedITextTest;
30+
import com.itextpdf.test.annotations.LogMessage;
31+
import com.itextpdf.test.annotations.LogMessages;
32+
import com.itextpdf.test.annotations.type.IntegrationTest;
33+
34+
import java.io.File;
35+
import java.io.IOException;
36+
import org.junit.Assert;
37+
import org.junit.BeforeClass;
38+
import org.junit.Test;
39+
import org.junit.experimental.categories.Category;
40+
41+
@Category(IntegrationTest.class)
42+
public class UriResolverTest extends ExtendedITextTest {
43+
44+
private static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/html2pdf/resolver/UriResolverTest/";
45+
private static final String DESTINATION_FOLDER =
46+
"./target/test/resources/com/itextpdf/html2pdf/resolver/UriResolverTest/";
47+
48+
@BeforeClass
49+
public static void before() throws IOException {
50+
createOrClearDestinationFolder(DESTINATION_FOLDER);
51+
}
52+
53+
@Test
54+
@LogMessages(messages = {@LogMessage(messageTemplate = StyledXmlParserLogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI)})
55+
public void illegalColonCharTest() throws IOException, InterruptedException {
56+
String srcHtml = SOURCE_FOLDER + "illegalColonChar.html";
57+
String outPdf = DESTINATION_FOLDER + "illegalColonChar.pdf";
58+
String cmpPdf = SOURCE_FOLDER + "cmp_illegalColonChar.pdf";
59+
60+
ConverterProperties properties = new ConverterProperties().setBaseUri(SOURCE_FOLDER);
61+
HtmlConverter.convertToPdf(new File(srcHtml), new File(outPdf), properties);
62+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, DESTINATION_FOLDER, "diff_"));
63+
}
64+
}
Binary file not shown.

src/test/resources/com/itextpdf/html2pdf/Html2PdfTest/fonts/LICENSE_OFL.txt

Lines changed: 0 additions & 92 deletions
This file was deleted.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)