From 08649377dd42878c55fa11c500dace2949e2d9ef Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Fri, 29 Dec 2023 17:43:40 +0200 Subject: [PATCH] upgrade from JUnit 3/4 to JUnit 5 --- flying-saucer-core/pom.xml | 1 + .../xhtmlrenderer/css/parser/ParserTest.java | 37 +++++---- .../UrlAwareLineBreakIteratorTest.java | 77 ++++++++++--------- .../simple/Graphics2DRendererTest.java | 2 +- .../swing/NaiveUserAgentTest.java | 62 +++++++-------- .../xhtmlrenderer/util/ConfigurationTest.java | 2 +- flying-saucer-examples/pom.xml | 7 +- .../pdf/ConcurrentPdfGenerationTest.java | 12 +-- .../org/xhtmlrenderer/pdf/PDFRenderTest.java | 8 +- .../pdf/PDFRenderToMultiplePagesTest.java | 4 +- .../org/xhtmlrenderer/pdf/SimpleHtmlTest.java | 13 +++- flying-saucer-fop/pom.xml | 1 + .../xhtmlrenderer/fop/PDFHyphenationTest.java | 22 ++++-- .../fop/nbsp/NonBreakPointsEnhancerTest.java | 38 ++++----- .../nbsp/NonBreakPointsLoaderImplTest.java | 13 +--- .../fop/nbsp/NonBreakPointsTest.java | 14 ++-- flying-saucer-log4j/pom.xml | 1 + flying-saucer-pdf-itext5/pom.xml | 1 + flying-saucer-pdf/pom.xml | 1 + .../pdf/DocumentSplitterTest.java | 20 ++--- .../BorderRadiusNonRegressionTest.java | 22 +++--- .../pdf/bug/EndlessLoopTest.java | 20 ++++- .../pdf/bug/OutlineGenerationIssueTest.java | 9 +-- flying-saucer-swt-examples/pom.xml | 1 + flying-saucer-swt/pom.xml | 1 + pom.xml | 52 ++++++++++++- 26 files changed, 263 insertions(+), 178 deletions(-) diff --git a/flying-saucer-core/pom.xml b/flying-saucer-core/pom.xml index decb26c66..e540e08ad 100644 --- a/flying-saucer-core/pom.xml +++ b/flying-saucer-core/pom.xml @@ -8,6 +8,7 @@ org.xhtmlrenderer flying-saucer-parent 9.3.2-SNAPSHOT + ../pom.xml flying-saucer-core diff --git a/flying-saucer-core/src/test/java/org/xhtmlrenderer/css/parser/ParserTest.java b/flying-saucer-core/src/test/java/org/xhtmlrenderer/css/parser/ParserTest.java index e2d61d4c5..ba31a1eaf 100644 --- a/flying-saucer-core/src/test/java/org/xhtmlrenderer/css/parser/ParserTest.java +++ b/flying-saucer-core/src/test/java/org/xhtmlrenderer/css/parser/ParserTest.java @@ -19,8 +19,7 @@ */ package org.xhtmlrenderer.css.parser; -import junit.framework.TestCase; -import org.junit.Assert; +import org.junit.jupiter.api.Test; import org.xhtmlrenderer.css.newmatch.Selector; import org.xhtmlrenderer.css.sheet.PropertyDeclaration; import org.xhtmlrenderer.css.sheet.Ruleset; @@ -29,17 +28,20 @@ import java.io.IOException; import java.io.StringReader; -public class ParserTest extends TestCase { +import static org.assertj.core.api.Assertions.assertThat; + +public class ParserTest { private final String test = String.format("div { background-image: url('something') }%n"); private final CSSErrorHandler errorHandler = (uri, message) -> System.out.println(message); - public void test_cssParsingPerformance() throws IOException { + @Test + public void cssParsingPerformance() throws IOException { int count = 10_000; StringBuilder longTest = new StringBuilder(); for (int i = 0 ; i < count; i++) { longTest.append(test); } - Assert.assertEquals("Long enough input", test.length() * count, longTest.length()); + assertThat(longTest.length()).as("Long enough input").isEqualTo(test.length() * count); long total = 0; for (int i = 0; i < 40; i++) { @@ -50,7 +52,7 @@ public void test_cssParsingPerformance() throws IOException { // System.out.println("Took " + (end-start) + " ms"); total += (end-start); - assertEquals(count, stylesheet.getContents().size()); + assertThat(stylesheet.getContents()).hasSize(count); } System.out.println("Average " + (total/10) + " ms"); @@ -62,7 +64,7 @@ public void test_cssParsingPerformance() throws IOException { long end = System.currentTimeMillis(); // System.out.println("Took " + (end-start) + " ms"); total += (end-start); - assertEquals(count, stylesheet.getContents().size()); + assertThat(stylesheet.getContents()).hasSize(count); } System.out.println("Average " + (total/10) + " ms"); @@ -81,18 +83,19 @@ public void test_cssParsingPerformance() throws IOException { System.out.println("Average " + (total/10) + " ms"); } - public void test_parseCss() throws IOException { + @Test + public void parseCss() throws IOException { CSSParser p = new CSSParser(errorHandler); - + Stylesheet stylesheet = p.parseStylesheet(null, 0, new StringReader(test)); - assertEquals(1, stylesheet.getContents().size()); + assertThat(stylesheet.getContents()).hasSize(1); Ruleset ruleset = (Ruleset) stylesheet.getContents().get(0); - assertEquals(1, ruleset.getFSSelectors().size()); - assertEquals(Selector.class, ruleset.getFSSelectors().get(0).getClass()); - assertEquals(1, ruleset.getPropertyDeclarations().size()); - PropertyDeclaration propertyDeclaration = (PropertyDeclaration) ruleset.getPropertyDeclarations().get(0); - assertEquals("background-image", propertyDeclaration.getPropertyName()); - assertEquals("background-image", propertyDeclaration.getCSSName().toString()); - assertEquals("url('something')", propertyDeclaration.getValue().getCssText()); + org.assertj.core.api.Assertions.assertThat(ruleset.getFSSelectors()).hasSize(1); + assertThat(ruleset.getFSSelectors().get(0)).isInstanceOf(Selector.class); + org.assertj.core.api.Assertions.assertThat(ruleset.getPropertyDeclarations()).hasSize(1); + PropertyDeclaration propertyDeclaration = ruleset.getPropertyDeclarations().get(0); + assertThat(propertyDeclaration.getPropertyName()).isEqualTo("background-image"); + assertThat(propertyDeclaration.getCSSName()).hasToString("background-image"); + assertThat(propertyDeclaration.getValue().getCssText()).isEqualTo("url('something')"); } } diff --git a/flying-saucer-core/src/test/java/org/xhtmlrenderer/layout/breaker/UrlAwareLineBreakIteratorTest.java b/flying-saucer-core/src/test/java/org/xhtmlrenderer/layout/breaker/UrlAwareLineBreakIteratorTest.java index 73f0af251..311b05250 100644 --- a/flying-saucer-core/src/test/java/org/xhtmlrenderer/layout/breaker/UrlAwareLineBreakIteratorTest.java +++ b/flying-saucer-core/src/test/java/org/xhtmlrenderer/layout/breaker/UrlAwareLineBreakIteratorTest.java @@ -1,105 +1,108 @@ package org.xhtmlrenderer.layout.breaker; -import junit.framework.TestCase; +import org.junit.jupiter.api.Test; import java.text.BreakIterator; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; -public class UrlAwareLineBreakIteratorTest extends TestCase { +public class UrlAwareLineBreakIteratorTest { - public void testNext_BreakAtSpace() { + @Test + public void breakAtSpace() { assertBreaksCorrectly("Hello World! World foo", new String[] {"Hello ", "World! ", "World ", "foo"}); } - - public void testNext_BreakAtPunctuation() { + @Test + public void breakAtPunctuation() { assertBreaksCorrectly("The.quick,brown:fox;jumps!over?the(lazy)[dog]", new String[] {"The.", "quick,", "brown:", "fox;", "jumps!", "over?", "the", "(lazy)", "[dog]"}); } - - public void testNext_BreakAtHyphen() { + @Test + public void breakAtHyphen() { assertBreaksCorrectly("Pseudo-element", new String[] {"Pseudo-", "element"}); } - - public void testNext_BreakAtSlash() { + @Test + public void breakAtSlash() { assertBreaksCorrectly("Justice/Law", new String[] {"Justice", "/Law"}); } - - public void testNext_WordBeginsWithSlash() { + @Test + public void wordBeginsWithSlash() { assertBreaksCorrectly("Justice /Law", new String[] {"Justice ", "/Law"}); } - - public void testNext_WordEndsWithSlash() { + @Test + public void wordEndsWithSlash() { assertBreaksCorrectly("Justice/ Law", new String[] {"Justice/ ", "Law"}); } - - public void testNext_WordEndsWithSlashMultipleWhitespace() { + @Test + public void wordEndsWithSlashMultipleWhitespace() { assertBreaksCorrectly("Justice/ Law", new String[] {"Justice/ ", "Law"}); } - - public void testNext_SlashSeparatedSequence() { + @Test + public void slashSeparatedSequence() { assertBreaksCorrectly("/this/is/a/long/path/name/", new String[] {"/this", "/is", "/a", "/long", "/path", "/name/"}); } - - public void testNext_UrlInside() { + @Test + public void urlInside() { assertBreaksCorrectly("Sentence with url https://github.com/flyingsaucerproject/flyingsaucer?test=true¶m2=false inside.", new String[] {"Sentence ", "with ", "url ", "https://github.", "com", "/flyingsaucerproject", "/flyingsaucer?", "test=true¶m2=false ", "inside."}); } - - public void testNext_MultipleSlashesInWord() { + @Test + public void multipleSlashesInWord() { assertBreaksCorrectly("word/////word", new String[] {"word", "/////word"}); } - - public void testNext_MultipleSlashesBeforeWord() { + @Test + public void multipleSlashesBeforeWord() { assertBreaksCorrectly("hello /////world", new String[] {"hello ", "/////world"}); } - - public void testNext_MultipleSlashesAfterWord() { + @Test + public void multipleSlashesAfterWord() { assertBreaksCorrectly("hello world/////", new String[] {"hello ", "world/////"}); } - - public void testNext_MultipleSlashesAroundWord() { + @Test + public void multipleSlashesAroundWord() { assertBreaksCorrectly("hello /////world/////", new String[] {"hello ", "/////world/////"}); } - - public void testNext_WhitespaceAfterTrailingSlashes() { + @Test + public void whitespaceAfterTrailingSlashes() { assertBreaksCorrectly("hello world/// ", new String[] {"hello ", "world/// "}); } - - public void testNext_ShortUrl() { + @Test + public void shortUrl() { assertBreaksCorrectly("http://localhost", new String[] {"http://localhost"}); } - - public void testNext_IncompleteUrl() { - assertBreaksCorrectly("http://", + @Test + public void incompleteUrl() { + assertBreaksCorrectly("http://", new String[] {"http://"}); } @@ -113,13 +116,15 @@ private void assertBreaksCorrectly(String input, String[] segments) { while ((breakpoint = iterator.next()) != BreakIterator.DONE) { if (segmentIndex < segments.length) { String segment = segments[segmentIndex++]; - assertEquals("Segment #" + segmentIndex + " does not match.", segment, input.substring(lastBreakPoint, breakpoint)); + assertThat(input.substring(lastBreakPoint, breakpoint)) + .as("Segment #" + segmentIndex + " does not match.") + .isEqualTo(segment); lastBreakPoint = breakpoint; } else { fail("Too few segments."); } } - assertEquals("Last breakpoint is wrong.", input.length(), lastBreakPoint); + assertThat(lastBreakPoint).as("Last breakpoint is wrong.").isEqualTo(input.length()); if (segmentIndex != segments.length) { fail("Too many segments."); } diff --git a/flying-saucer-core/src/test/java/org/xhtmlrenderer/simple/Graphics2DRendererTest.java b/flying-saucer-core/src/test/java/org/xhtmlrenderer/simple/Graphics2DRendererTest.java index 33518f2fe..2a9072688 100644 --- a/flying-saucer-core/src/test/java/org/xhtmlrenderer/simple/Graphics2DRendererTest.java +++ b/flying-saucer-core/src/test/java/org/xhtmlrenderer/simple/Graphics2DRendererTest.java @@ -1,6 +1,6 @@ package org.xhtmlrenderer.simple; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/flying-saucer-core/src/test/java/org/xhtmlrenderer/swing/NaiveUserAgentTest.java b/flying-saucer-core/src/test/java/org/xhtmlrenderer/swing/NaiveUserAgentTest.java index 0a3ec3926..8a1d4f3c2 100644 --- a/flying-saucer-core/src/test/java/org/xhtmlrenderer/swing/NaiveUserAgentTest.java +++ b/flying-saucer-core/src/test/java/org/xhtmlrenderer/swing/NaiveUserAgentTest.java @@ -1,66 +1,66 @@ package org.xhtmlrenderer.swing; -import junit.framework.TestCase; +import org.junit.jupiter.api.Test; -public class NaiveUserAgentTest - extends TestCase -{ - private static String resolve(String baseUri, String uri) - { +import static org.assertj.core.api.Assertions.assertThat; + +public class NaiveUserAgentTest { + private static String resolve(String baseUri, String uri) { NaiveUserAgent userAgent=new NaiveUserAgent(); userAgent.setBaseURL(baseUri); return userAgent.resolveURI(uri); } - public void testBasicResolve() - { + @Test + public void basicResolve() { // absolute uris should be unchanged - assertEquals("http://www.example.com", resolve(null, "http://www.example.com")); - assertEquals("http://www.example.com", resolve("ftp://www.example.com/other","http://www.example.com")); + assertThat(resolve(null, "http://www.example.com")).isEqualTo("http://www.example.com"); + assertThat(resolve("ftp://www.example.com/other", "http://www.example.com")).isEqualTo("http://www.example.com"); // by default relative uris resolves as file - assertNotNull(resolve(null, "www.example.com")); - assertTrue(resolve(null, "www.example.com").startsWith("file:")); + assertThat(resolve(null, "www.example.com")) + .isNotNull() + .startsWith("file:"); // relative uris without slash - assertEquals("ftp://www.example.com/test", resolve("ftp://www.example.com/other","test")); + assertThat(resolve("ftp://www.example.com/other", "test")).isEqualTo("ftp://www.example.com/test"); // relative uris with slash - assertEquals("ftp://www.example.com/other/test", resolve("ftp://www.example.com/other/","test")); - assertEquals("ftp://www.example.com/test", resolve("ftp://www.example.com/other/","/test")); + assertThat(resolve("ftp://www.example.com/other/", "test")).isEqualTo("ftp://www.example.com/other/test"); + assertThat(resolve("ftp://www.example.com/other/", "/test")).isEqualTo("ftp://www.example.com/test"); } - public void testCustomProtocolResolve() - { + @Test + public void customProtocolResolve() { // absolute uris should be unchanged - assertEquals("custom://www.example.com", resolve(null, "custom://www.example.com")); - assertEquals("custom://www.example.com", resolve("ftp://www.example.com/other","custom://www.example.com")); + assertThat(resolve(null, "custom://www.example.com")).isEqualTo("custom://www.example.com"); + assertThat(resolve("ftp://www.example.com/other", "custom://www.example.com")).isEqualTo("custom://www.example.com"); // relative uris without slash - assertEquals("custom://www.example.com/test", resolve("custom://www.example.com/other","test")); + assertThat(resolve("custom://www.example.com/other", "test")).isEqualTo("custom://www.example.com/test"); // relative uris with slash - assertEquals("custom://www.example.com/other/test", resolve("custom://www.example.com/other/","test")); - assertEquals("custom://www.example.com/test", resolve("custom://www.example.com/other/","/test")); + assertThat(resolve("custom://www.example.com/other/", "test")).isEqualTo("custom://www.example.com/other/test"); + assertThat(resolve("custom://www.example.com/other/", "/test")).isEqualTo("custom://www.example.com/test"); } /** - * This reproduces https://code.google.com/archive/p/flying-saucer/issues/262 - * + * This reproduces ... + *

* Below test was green with 9.0.6 and turned red in 9.0.7 */ - public void testJarFileUriResolve() - { + @Test + public void jarFileUriResolve() { // absolute uris should be unchanged - assertEquals("jar:file:/path/jarfile.jar!/foo/index.xhtml", resolve(null, "jar:file:/path/jarfile.jar!/foo/index.xhtml")); - assertEquals("jar:file:/path/jarfile.jar!/foo/index.xhtml", resolve("ftp://www.example.com/other","jar:file:/path/jarfile.jar!/foo/index.xhtml")); + assertThat(resolve(null, "jar:file:/path/jarfile.jar!/foo/index.xhtml")).isEqualTo("jar:file:/path/jarfile.jar!/foo/index.xhtml"); + assertThat(resolve("ftp://www.example.com/other", "jar:file:/path/jarfile.jar!/foo/index.xhtml")).isEqualTo("jar:file:/path/jarfile.jar!/foo/index.xhtml"); // relative uris without slash - assertEquals("jar:file:/path/jarfile.jar!/foo/other.xhtml", resolve("jar:file:/path/jarfile.jar!/foo/index.xhtml","other.xhtml")); + assertThat(resolve("jar:file:/path/jarfile.jar!/foo/index.xhtml", "other.xhtml")).isEqualTo("jar:file:/path/jarfile.jar!/foo/other.xhtml"); // relative uris with slash - assertEquals("jar:file:/path/jarfile.jar!/foo/other.xhtml", resolve("jar:file:/path/jarfile.jar!/foo/","other.xhtml")); - assertEquals("jar:file:/path/jarfile.jar!/other.xhtml", resolve("jar:file:/path/jarfile.jar!/foo/","/other.xhtml")); + assertThat(resolve("jar:file:/path/jarfile.jar!/foo/", "other.xhtml")).isEqualTo("jar:file:/path/jarfile.jar!/foo/other.xhtml"); + assertThat(resolve("jar:file:/path/jarfile.jar!/foo/", "/other.xhtml")).isEqualTo("jar:file:/path/jarfile.jar!/other.xhtml"); } } diff --git a/flying-saucer-core/src/test/java/org/xhtmlrenderer/util/ConfigurationTest.java b/flying-saucer-core/src/test/java/org/xhtmlrenderer/util/ConfigurationTest.java index be914ac89..e7c3edc6c 100644 --- a/flying-saucer-core/src/test/java/org/xhtmlrenderer/util/ConfigurationTest.java +++ b/flying-saucer-core/src/test/java/org/xhtmlrenderer/util/ConfigurationTest.java @@ -1,6 +1,6 @@ package org.xhtmlrenderer.util; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; diff --git a/flying-saucer-examples/pom.xml b/flying-saucer-examples/pom.xml index a2bbf4cfe..acef03b4c 100644 --- a/flying-saucer-examples/pom.xml +++ b/flying-saucer-examples/pom.xml @@ -7,6 +7,7 @@ org.xhtmlrenderer flying-saucer-parent 9.3.2-SNAPSHOT + ../pom.xml flying-saucer-examples @@ -39,12 +40,6 @@ flying-saucer-pdf ${project.version} - - com.codeborne - pdf-test - ${pdftest.version} - test - diff --git a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/ConcurrentPdfGenerationTest.java b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/ConcurrentPdfGenerationTest.java index 55f3919a9..ef9317ab9 100644 --- a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/ConcurrentPdfGenerationTest.java +++ b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/ConcurrentPdfGenerationTest.java @@ -2,7 +2,7 @@ import com.codeborne.pdftest.PDF; import com.lowagie.text.DocumentException; -import junit.framework.TestCase; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; @@ -27,15 +27,17 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; -public class ConcurrentPdfGenerationTest extends TestCase { +public class ConcurrentPdfGenerationTest { private static final Logger log = LoggerFactory.getLogger(ConcurrentPdfGenerationTest.class); - - public void testSamplePdf() { + + @Test + public void samplePdf() { byte[] pdf = generatePdf("sample.html"); verifyPdf(pdf); } - public void testConcurrentPdfGeneration() throws InterruptedException { + @Test + public void concurrentPdfGeneration() throws InterruptedException { ScheduledExecutorService timer = newScheduledThreadPool(1); timer.scheduleWithFixedDelay(System::gc, 0, 15, MILLISECONDS); diff --git a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderTest.java b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderTest.java index fda1f282a..4e6f89f19 100644 --- a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderTest.java +++ b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderTest.java @@ -2,13 +2,14 @@ import com.codeborne.pdftest.PDF; import com.lowagie.text.DocumentException; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.xhtmlrenderer.resource.XMLResource; import org.xml.sax.InputSource; +import javax.annotation.ParametersAreNonnullByDefault; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -24,7 +25,7 @@ public class PDFRenderTest { private static final Logger log = LoggerFactory.getLogger(PDFRenderTest.class); @Test - public void testConvertSimpleHtmlToPdf() throws IOException, DocumentException { + public void convertSimpleHtmlToPdf() throws IOException, DocumentException { URL source = requireNonNull(Thread.currentThread().getContextClassLoader().getResource("hello.html")); File output = File.createTempFile("flying-saucer-" + getClass().getSimpleName(), ".hello.pdf"); PDF pdf = generatePDF(source, output); @@ -32,7 +33,7 @@ public void testConvertSimpleHtmlToPdf() throws IOException, DocumentException { } @Test - public void testConvertComplexHtmlToPdf() throws IOException, DocumentException { + public void convertComplexHtmlToPdf() throws IOException, DocumentException { URL source = requireNonNull(Thread.currentThread().getContextClassLoader().getResource("hamlet.xhtml")); File output = File.createTempFile("flying-saucer-" + getClass().getSimpleName(), ".hamlet.pdf"); PDF pdf = generatePDF(source, output); @@ -66,6 +67,7 @@ private static PDF generatePDF(URL source, File output) throws IOException, Docu return new PDF(output); } + @ParametersAreNonnullByDefault private static class ResourceLoaderUserAgent extends ITextUserAgent { private ResourceLoaderUserAgent(ITextOutputDevice outputDevice) { super(outputDevice); diff --git a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderToMultiplePagesTest.java b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderToMultiplePagesTest.java index 32b78c427..e64c85782 100644 --- a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderToMultiplePagesTest.java +++ b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/PDFRenderToMultiplePagesTest.java @@ -2,7 +2,7 @@ import com.codeborne.pdftest.PDF; import com.lowagie.text.DocumentException; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -20,7 +20,7 @@ public class PDFRenderToMultiplePagesTest { private static final Logger log = LoggerFactory.getLogger(PDFRenderToMultiplePagesTest.class); @Test - public void testGenerateSinglePdfFromMultipleInputDocuments() throws Exception { + public void generateSinglePdfFromMultipleInputDocuments() throws Exception { File output = File.createTempFile("flying-saucer-" + getClass().getSimpleName(), ".pdf"); String[] inputs = createSimpleFakeDocuments(); generatePDF(inputs, output); diff --git a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/SimpleHtmlTest.java b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/SimpleHtmlTest.java index 0175ab6b8..ec93f4981 100644 --- a/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/SimpleHtmlTest.java +++ b/flying-saucer-examples/src/test/java/org/xhtmlrenderer/pdf/SimpleHtmlTest.java @@ -1,8 +1,9 @@ package org.xhtmlrenderer.pdf; +import com.codeborne.pdftest.PDF; import com.lowagie.text.DocumentException; -import junit.framework.TestCase; import org.apache.pdfbox.io.IOUtils; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -12,10 +13,13 @@ import java.io.FileOutputStream; import java.io.IOException; -public class SimpleHtmlTest extends TestCase { +import static com.codeborne.pdftest.assertj.Assertions.assertThat; + +public class SimpleHtmlTest { private static final Logger log = LoggerFactory.getLogger(SimpleHtmlTest.class); - public void testSimplePdf() throws DocumentException, IOException { + @Test + public void simplePdf() throws DocumentException, IOException { ITextRenderer renderer = new ITextRenderer(); String htmlContent = "

My First Heading

My first paragraph.

"; @@ -32,5 +36,8 @@ public void testSimplePdf() throws DocumentException, IOException { IOUtils.copy(new ByteArrayInputStream(outputStream.toByteArray()), o); } log.info("Generated PDF: {}", file.getAbsolutePath()); + + PDF pdf = new PDF(file); + assertThat(pdf).containsText("My First Heading", "My first paragraph"); } } diff --git a/flying-saucer-fop/pom.xml b/flying-saucer-fop/pom.xml index fd9367365..8f1bdd20b 100644 --- a/flying-saucer-fop/pom.xml +++ b/flying-saucer-fop/pom.xml @@ -8,6 +8,7 @@ org.xhtmlrenderer flying-saucer-parent 9.3.2-SNAPSHOT + ../pom.xml flying-saucer-fop diff --git a/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/PDFHyphenationTest.java b/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/PDFHyphenationTest.java index bf9bde26d..f19682993 100644 --- a/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/PDFHyphenationTest.java +++ b/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/PDFHyphenationTest.java @@ -18,16 +18,18 @@ */ package org.xhtmlrenderer.fop; +import com.codeborne.pdftest.PDF; +import com.itextpdf.text.DocumentException; +import org.junit.jupiter.api.Test; +import org.xhtmlrenderer.pdf.ITextRenderer; +import org.xhtmlrenderer.pdf.ITextUserAgent; + import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; -import org.junit.Test; -import org.xhtmlrenderer.pdf.ITextRenderer; -import org.xhtmlrenderer.pdf.ITextUserAgent; - -import com.itextpdf.text.DocumentException; +import static com.codeborne.pdftest.assertj.Assertions.assertThat; /** * @author Lukas Zaruba, lukas.zaruba@gmail.com @@ -64,11 +66,17 @@ public class PDFHyphenationTest { ""; @Test - public void testGenerator() throws Exception { + public void generator() throws Exception { Path temp = Files.createTempFile("pdfTest", ".pdf"); OutputStream os = Files.newOutputStream(temp); generatePDF(XML, os); - System.out.println(temp); + System.out.println("Generated file: " + temp); + + PDF pdf = new PDF(temp.toFile()); + assertThat(pdf).containsText( + "Velice dlouhy text", + "pokud nebude perfektne nastaveno" + ); } private void generatePDF(String xml, OutputStream os) throws DocumentException, IOException { diff --git a/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsEnhancerTest.java b/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsEnhancerTest.java index 733fd255d..49bcbb15b 100644 --- a/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsEnhancerTest.java +++ b/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsEnhancerTest.java @@ -18,13 +18,13 @@ */ package org.xhtmlrenderer.fop.nbsp; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.Arrays; import static java.util.Collections.emptyList; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * @author Lukas Zaruba, lukas.zaruba@gmail.com @@ -32,40 +32,40 @@ public class NonBreakPointsEnhancerTest { @Test - public void testNullInput() { - assertNull(new NonBreakPointsEnhancer().enhance(null, "cs")); + public void nullInput() { + assertThat(new NonBreakPointsEnhancer().enhance(null, "cs")).isNull(); } @Test - public void testNullLang() { - assertEquals("some input", new NonBreakPointsEnhancer().enhance("some input", null)); + public void nullLang() { + assertThat(new NonBreakPointsEnhancer().enhance("some input", null)).isEqualTo("some input"); } @Test - public void testEmptyLang() { - assertEquals("some input", new NonBreakPointsEnhancer().enhance("some input", "")); + public void emptyLang() { + assertThat(new NonBreakPointsEnhancer().enhance("some input", "")).isEqualTo("some input"); } @Test - public void testEmptyInput() { - assertEquals("", new NonBreakPointsEnhancer().enhance("", "en")); + public void emptyInput() { + assertThat(new NonBreakPointsEnhancer().enhance("", "en")).isEqualTo(""); } @Test public void noDefinition() { NonBreakPointsLoader nullLoader = lang -> emptyList(); - assertEquals("some text with spaces", new NonBreakPointsEnhancer(nullLoader).enhance("some text with spaces", "cs")); + assertThat(new NonBreakPointsEnhancer(nullLoader).enhance("some text with spaces", "cs")).isEqualTo("some text with spaces"); } @Test - public void testLoaderConfiguration() { + public void loaderConfiguration() { final String[] c = new String[] {null}; NonBreakPointsLoader capturingLoader = lang -> { c[0] = lang; return emptyList(); }; new NonBreakPointsEnhancer(capturingLoader).enhance("some text with spaces", "cs"); - assertEquals("cs", c[0]); + assertThat(c[0]).isEqualTo("cs"); } @Test @@ -88,9 +88,11 @@ public void rules3() { testRulesInternal("prselo a potom jsme sli domu s kamarady a povidali si", "prselo a\u00A0potom jsme sli domu s\u00A0kamarady a\u00A0povidali si", "([\\s]+a)( )([^\\s]+)", "([\\s]+s)( )([^\\s]+)"); } - @Test (expected = IllegalArgumentException.class) + @Test public void invalidRuleGroups() { - testRulesInternal("a potom", "a\u00A0potom", "a( )[^\\s]{1,}"); + assertThatThrownBy(() -> testRulesInternal("a potom", "a\u00A0potom", "a( )[^\\s]{1,}")) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Expression must contain exactly 3 groups! a( )[^\\s]{1,}"); } @Test @@ -116,12 +118,12 @@ public void czechRules() { } private void assertCzech(String text, String expected) { - assertEquals(expected, new NonBreakPointsEnhancer().enhance(text, "cs")); + assertThat(new NonBreakPointsEnhancer().enhance(text, "cs")).isEqualTo(expected); } private void testRulesInternal(String text, String expected, final String ... rules) { NonBreakPointsLoader dummyLoader = lang -> Arrays.asList(rules); - assertEquals(expected, new NonBreakPointsEnhancer(dummyLoader).enhance(text, "en")); + assertThat(new NonBreakPointsEnhancer(dummyLoader).enhance(text, "en")).isEqualTo(expected); } } diff --git a/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImplTest.java b/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImplTest.java index 48ebbf55a..65ca10bab 100644 --- a/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImplTest.java +++ b/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImplTest.java @@ -18,13 +18,12 @@ */ package org.xhtmlrenderer.fop.nbsp; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; -import static org.junit.Assert.assertEquals; /** * @author Lukas Zaruba, lukas.zaruba@gmail.com @@ -48,15 +47,13 @@ public void emptyLang() { @Test public void loadExactMatch() { List lines = new NonBreakPointsLoaderImpl().loadNBSP("de"); - assertEquals(1, lines.size()); - assertEquals("deRuleščřžýá", lines.get(0)); // tests also UTF-8 chars + assertThat(lines).containsExactly("deRuleščřžýá"); // tests also UTF-8 chars } @Test public void loadNonExactMatch() { List lines = new NonBreakPointsLoaderImpl().loadNBSP("de_DE"); - assertEquals(1, lines.size()); - assertEquals("deRuleščřžýá", lines.get(0)); // tests also UTF-8 chars + assertThat(lines).containsExactly("deRuleščřžýá"); // tests also UTF-8 chars } @Test @@ -67,9 +64,7 @@ public void nonExisting() { @Test public void loadExactMatch2() { List lines = new NonBreakPointsLoaderImpl().loadNBSP("en_GB"); - assertEquals(2, lines.size()); - assertEquals("enGBRule1", lines.get(0)); - assertEquals("enGBRule2", lines.get(1)); + assertThat(lines).containsExactly("enGBRule1", "enGBRule2"); } } diff --git a/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsTest.java b/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsTest.java index 856ed4f79..c97206691 100644 --- a/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsTest.java +++ b/flying-saucer-fop/src/test/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsTest.java @@ -18,7 +18,7 @@ */ package org.xhtmlrenderer.fop.nbsp; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.xhtmlrenderer.layout.breaker.BreakPoint; import org.xhtmlrenderer.layout.breaker.UrlAwareLineBreakIterator; @@ -27,7 +27,7 @@ import java.util.Iterator; import java.util.TreeSet; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Lukas Zaruba, lukas.zaruba@gmail.com @@ -35,12 +35,12 @@ public class NonBreakPointsTest { @Test - public void testGeneral() { + public void general() { test("text s mezerami", 5, 7, 15); } @Test - public void testNBSP() { + public void nbsp() { test("text s\u00A0mezerami", 5, 15); } @@ -55,11 +55,11 @@ private void test(String text, int ... expected) { } private void assertBreakPoints(Collection calculated, int ... expected) { - if (calculated.size() != expected.length) throw new AssertionError("Expected " + expected.length + - " break points, got " + calculated.size() + ": " + calculated); + assertThat(calculated).hasSize(expected.length); + Iterator it = calculated.iterator(); for (int point : expected) { - assertEquals(point, it.next().getPosition()); + assertThat(it.next().getPosition()).isEqualTo(point); } } diff --git a/flying-saucer-log4j/pom.xml b/flying-saucer-log4j/pom.xml index c7f042e71..a3b48d310 100644 --- a/flying-saucer-log4j/pom.xml +++ b/flying-saucer-log4j/pom.xml @@ -8,6 +8,7 @@ org.xhtmlrenderer flying-saucer-parent 9.3.2-SNAPSHOT + ../pom.xml flying-saucer-log4j diff --git a/flying-saucer-pdf-itext5/pom.xml b/flying-saucer-pdf-itext5/pom.xml index 6b1df32c9..7fc9536ff 100644 --- a/flying-saucer-pdf-itext5/pom.xml +++ b/flying-saucer-pdf-itext5/pom.xml @@ -8,6 +8,7 @@ org.xhtmlrenderer flying-saucer-parent 9.3.2-SNAPSHOT + ../pom.xml flying-saucer-pdf-itext5 diff --git a/flying-saucer-pdf/pom.xml b/flying-saucer-pdf/pom.xml index 762c790bb..8592a525c 100644 --- a/flying-saucer-pdf/pom.xml +++ b/flying-saucer-pdf/pom.xml @@ -7,6 +7,7 @@ org.xhtmlrenderer flying-saucer-parent 9.3.2-SNAPSHOT + ../pom.xml flying-saucer-pdf diff --git a/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/DocumentSplitterTest.java b/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/DocumentSplitterTest.java index 4f1ca6f59..365bd7537 100644 --- a/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/DocumentSplitterTest.java +++ b/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/DocumentSplitterTest.java @@ -1,7 +1,7 @@ package org.xhtmlrenderer.pdf; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; @@ -26,8 +26,8 @@ public class DocumentSplitterTest { private XMLReader reader; private final DocumentSplitter splitter = new DocumentSplitter(); - @Before - public void setUp() throws ParserConfigurationException, SAXException { + @BeforeEach + public final void setUp() throws ParserConfigurationException, SAXException { factory.setNamespaceAware(true); factory.setValidating(false); reader = factory.newSAXParser().getXMLReader(); @@ -36,19 +36,19 @@ public void setUp() throws ParserConfigurationException, SAXException { } @Test - public void testSplitDocumentWithoutHead() throws Exception { + public void splitDocumentWithoutHead() throws Exception { reader.parse(new InputSource(new StringReader("

no head

"))); assertThat(splitter.getDocuments()).hasSize(0); } @Test - public void testSplitDocumentWithHead() throws Exception { + public void splitDocumentWithHead() throws Exception { reader.parse(new InputSource(new StringReader("" + "The head" + "

I have head

" + ""))); assertThat(splitter.getDocuments()).hasSize(1); - Document doc = (Document) splitter.getDocuments().get(0); + Document doc = splitter.getDocuments().get(0); assertThat(doc.getElementsByTagName("h1").getLength()).isEqualTo(1); assertThat(serialize(doc)) @@ -57,7 +57,7 @@ public void testSplitDocumentWithHead() throws Exception { } @Test - public void testSplitDocumentWithMultipleBodies() throws Exception { + public void splitDocumentWithMultipleBodies() throws Exception { reader.parse(new InputSource(new StringReader("" + "The head" + "

I have head

" + @@ -65,9 +65,9 @@ public void testSplitDocumentWithMultipleBodies() throws Exception { ""))); assertThat(splitter.getDocuments()).hasSize(2); - Document doc1 = (Document) splitter.getDocuments().get(0); + Document doc1 = splitter.getDocuments().get(0); assertThat(doc1.getElementsByTagName("h1").getLength()).isEqualTo(1); - Document doc2 = (Document) splitter.getDocuments().get(1); + Document doc2 = splitter.getDocuments().get(1); assertThat(doc2.getElementsByTagName("h2").getLength()).isEqualTo(1); assertThat(serialize(doc1)) diff --git a/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/borderradius/BorderRadiusNonRegressionTest.java b/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/borderradius/BorderRadiusNonRegressionTest.java index 438f09788..8422127f4 100644 --- a/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/borderradius/BorderRadiusNonRegressionTest.java +++ b/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/borderradius/BorderRadiusNonRegressionTest.java @@ -1,23 +1,25 @@ package org.xhtmlrenderer.pdf.borderradius; -import java.io.ByteArrayOutputStream; -import java.net.URL; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - +import com.codeborne.pdftest.PDF; +import org.junit.jupiter.api.Test; import org.w3c.dom.Document; import org.xhtmlrenderer.pdf.ITextRenderer; import org.xhtmlrenderer.resource.FSEntityResolver; -import junit.framework.TestCase; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.ByteArrayOutputStream; +import java.net.URL; + +import static com.codeborne.pdftest.assertj.Assertions.assertThat; -public class BorderRadiusNonRegressionTest extends TestCase { +public class BorderRadiusNonRegressionTest { /** * This used to throw a ClassCastException (before this fix). */ - public void testBorderRadiusWithBorderWidthZero() throws Exception { + @Test + public void borderRadiusWithBorderWidthZero() throws Exception { testNoException("borderRadiusWithBorderWidthZero.html"); } @@ -37,6 +39,8 @@ private void testNoException(String htmlPath) throws Exception { ByteArrayOutputStream bos = new ByteArrayOutputStream(); renderer.createPDF(bos); + + assertThat(new PDF(bos.toByteArray())).containsText("Some content"); } } diff --git a/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/bug/EndlessLoopTest.java b/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/bug/EndlessLoopTest.java index bbe9c457c..02eaf59c0 100644 --- a/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/bug/EndlessLoopTest.java +++ b/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/bug/EndlessLoopTest.java @@ -1,19 +1,33 @@ package org.xhtmlrenderer.pdf.bug; -import org.junit.Test; +import com.codeborne.pdftest.PDF; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.xhtmlrenderer.pdf.ITextRenderer; +import java.io.ByteArrayOutputStream; import java.io.File; import java.net.URL; +import static com.codeborne.pdftest.assertj.Assertions.assertThat; +import static java.util.concurrent.TimeUnit.SECONDS; + public class EndlessLoopTest { - @Test(timeout = 3000L) - public void testWordwrap() throws Exception { + @Test + @Timeout(value = 3, unit = SECONDS) + public void wordwrap() throws Exception { URL htmlUrl = getClass().getResource("EndlessLoopTest_wordwrap.html"); File htmlFile = new File(htmlUrl.toURI()); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(htmlFile); renderer.layout(); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + renderer.createPDF(bos); + assertThat(new PDF(bos.toByteArray())).containsText( + "floated", + "word wrapped" + ); } } diff --git a/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/bug/OutlineGenerationIssueTest.java b/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/bug/OutlineGenerationIssueTest.java index 8a8848746..86b60e305 100644 --- a/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/bug/OutlineGenerationIssueTest.java +++ b/flying-saucer-pdf/src/test/java/org/xhtmlrenderer/pdf/bug/OutlineGenerationIssueTest.java @@ -1,6 +1,6 @@ package org.xhtmlrenderer.pdf.bug; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.xhtmlrenderer.pdf.ITextOutputDevice; import org.xhtmlrenderer.pdf.ITextRenderer; @@ -32,14 +32,13 @@ public class OutlineGenerationIssueTest { "\n"; @Test - public void testOutline() throws Exception { + public void outline() throws Exception { writePDF("target/test-outline1.pdf", new ITextOutputDevice(26.666666F), html2, html1); writePDF("target/test-outline2.pdf", new ITextOutputDevice(26.666666F), html1, html2); } private void writePDF(String name, ITextOutputDevice outputDevice, String html1, String html2) throws Exception { - FileOutputStream stream = new FileOutputStream(name); - try { + try (FileOutputStream stream = new FileOutputStream(name)) { ITextRenderer renderer = new ITextRenderer(outputDevice.getDotsPerPoint(), 20, outputDevice); renderer.setDocumentFromString(html1); @@ -51,8 +50,6 @@ private void writePDF(String name, ITextOutputDevice outputDevice, String html1, renderer.writeNextDocument(); renderer.finishPDF(); - } finally { - stream.close(); } } } diff --git a/flying-saucer-swt-examples/pom.xml b/flying-saucer-swt-examples/pom.xml index 548699d38..aa737103e 100644 --- a/flying-saucer-swt-examples/pom.xml +++ b/flying-saucer-swt-examples/pom.xml @@ -7,6 +7,7 @@ org.xhtmlrenderer flying-saucer-parent 9.3.2-SNAPSHOT + ../pom.xml flying-saucer-swt-examples diff --git a/flying-saucer-swt/pom.xml b/flying-saucer-swt/pom.xml index c07f412de..aeb90f334 100644 --- a/flying-saucer-swt/pom.xml +++ b/flying-saucer-swt/pom.xml @@ -8,6 +8,7 @@ org.xhtmlrenderer flying-saucer-parent 9.3.2-SNAPSHOT + ../pom.xml flying-saucer-swt diff --git a/pom.xml b/pom.xml index 55b86abca..fc33e2daf 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,13 @@ openpdf ${openpdf.version} + + org.junit + junit-bom + ${junit.version} + pom + import + @@ -40,9 +47,8 @@ provided - junit - junit - ${junit.version} + org.junit.jupiter + junit-jupiter-api test @@ -51,6 +57,12 @@ ${assertj.version} test + + com.codeborne + pdf-test + ${pdftest.version} + test + org.slf4j slf4j-simple @@ -100,6 +112,38 @@ + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.3 + + + org/**/* + + + + + org.apache.maven.plugins + maven-enforcer-plugin + 3.4.1 + + + enforce + + + + + junit:junit + + + + + + enforce + + + + org.sonatype.plugins nexus-staging-maven-plugin @@ -207,7 +251,7 @@ UTF-8 1.3.35 - 4.13.2 + 5.10.1 2.1.7 2.0.10 3.24.2