Skip to content

Commit

Permalink
upgrade from JUnit 3/4 to JUnit 5
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Dec 29, 2023
1 parent 1dd13bd commit 0864937
Show file tree
Hide file tree
Showing 26 changed files with 263 additions and 178 deletions.
1 change: 1 addition & 0 deletions flying-saucer-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-parent</artifactId>
<version>9.3.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>flying-saucer-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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++) {
Expand All @@ -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");

Expand All @@ -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");

Expand All @@ -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')");
}
}
Original file line number Diff line number Diff line change
@@ -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&param2=false inside.",
new String[] {"Sentence ", "with ", "url ", "https://github.", "com", "/flyingsaucerproject", "/flyingsaucer?",
"test=true&param2=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://"});
}

Expand All @@ -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.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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 <a href="https://code.google.com/archive/p/flying-saucer/issues/262">...</a>
* <p>
* 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");
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
7 changes: 1 addition & 6 deletions flying-saucer-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<groupId>org.xhtmlrenderer</groupId>
<artifactId>flying-saucer-parent</artifactId>
<version>9.3.2-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>flying-saucer-examples</artifactId>
Expand Down Expand Up @@ -39,12 +40,6 @@
<artifactId>flying-saucer-pdf</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.codeborne</groupId>
<artifactId>pdf-test</artifactId>
<version>${pdftest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Loading

0 comments on commit 0864937

Please sign in to comment.