Skip to content

Commit

Permalink
Add Path tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Dec 7, 2023
1 parent 2ce7b46 commit 6f03013
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/test/java/org/jsoup/helper/DataUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

import static org.jsoup.integration.ParseTest.getFile;
import static org.jsoup.integration.ParseTest.getPath;
import static org.junit.jupiter.api.Assertions.*;

public class DataUtilTest {
Expand Down Expand Up @@ -207,13 +209,21 @@ public void supportsXmlCharsetDeclaration() throws IOException {


@Test
public void lLoadsGzipFile() throws IOException {
public void loadsGzipFile() throws IOException {
File in = getFile("/htmltests/gzip.html.gz");
Document doc = Jsoup.parse(in, null);
assertEquals("Gzip test", doc.title());
assertEquals("This is a gzipped HTML file.", doc.selectFirst("p").text());
}

@Test
public void loadsGzipPath() throws IOException {
Path in = getPath("/htmltests/gzip.html.gz");
Document doc = Jsoup.parse(in, null);
assertEquals("Gzip test", doc.title());
assertEquals("This is a gzipped HTML file.", doc.selectFirst("p").text());
}

@Test
public void loadsZGzipFile() throws IOException {
// compressed on win, with z suffix
Expand All @@ -223,6 +233,15 @@ public void loadsZGzipFile() throws IOException {
assertEquals("This is a gzipped HTML file.", doc.selectFirst("p").text());
}

@Test
public void loadsZGzipPath() throws IOException {
// compressed on win, with z suffix
Path in = getPath("/htmltests/gzip.html.z");
Document doc = Jsoup.parse(in, null);
assertEquals("Gzip test", doc.title());
assertEquals("This is a gzipped HTML file.", doc.selectFirst("p").text());
}

@Test
public void handlesFakeGzipFile() throws IOException {
File in = getFile("/htmltests/fake-gzip.html.gz");
Expand All @@ -231,6 +250,14 @@ public void handlesFakeGzipFile() throws IOException {
assertEquals("And should still be readable.", doc.selectFirst("p").text());
}

@Test
public void handlesFakeGzipPath() throws IOException {
Path in = getPath("/htmltests/fake-gzip.html.gz");
Document doc = Jsoup.parse(in, null);
assertEquals("This is not gzipped", doc.title());
assertEquals("And should still be readable.", doc.selectFirst("p").text());
}

// an input stream to give a range of output sizes, that changes on each read
static class VaryingReadInputStream extends InputStream {
final InputStream in;
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/jsoup/integration/ParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.GZIPInputStream;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -133,6 +135,15 @@ public static File getFile(String resourceName) {
}
}

public static Path getPath(String resourceName) {
try {
URL resource = ParseTest.class.getResource(resourceName);
return resource != null ? Paths.get(resource.toURI()) : Paths.get("/404");
} catch (URISyntaxException e) {
throw new IllegalStateException(e);
}
}

public static InputStream inputStreamFrom(String s) {
return new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
}
Expand Down

0 comments on commit 6f03013

Please sign in to comment.