Skip to content

Commit

Permalink
Test improvements; assertions updated and unused exception declarations
Browse files Browse the repository at this point in the history
removed.
  • Loading branch information
offa authored and jhy committed Mar 2, 2020
1 parent 140b48a commit 8ddcef3
Show file tree
Hide file tree
Showing 25 changed files with 174 additions and 179 deletions.
6 changes: 3 additions & 3 deletions src/test/java/org/jsoup/integration/ConnectIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class ConnectIT {
// Slow Rider tests. Ignored by default so tests don't take aaages
@Test
public void canInterruptBodyStringRead() throws IOException, InterruptedException {
public void canInterruptBodyStringRead() throws InterruptedException {
// todo - implement in interruptable channels, so it's immediate
final String[] body = new String[1];
Thread runner = new Thread(new Runnable() {
Expand Down Expand Up @@ -47,7 +47,7 @@ public void run() {
}

@Test
public void canInterruptDocumentRead() throws IOException, InterruptedException {
public void canInterruptDocumentRead() throws InterruptedException {
// todo - implement in interruptable channels, so it's immediate
final String[] body = new String[1];
Thread runner = new Thread(new Runnable() {
Expand All @@ -70,7 +70,7 @@ public void run() {
assertTrue(runner.isInterrupted());
runner.join();

assertTrue(body[0].length() == 0); // doesn't ready a failed doc
assertEquals(0, body[0].length()); // doesn't ready a failed doc
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/jsoup/integration/ConnectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public void handlesWrongContentLengthDuringBufferedRead() throws IOException {
assertEquals(HelloServlet.Url, doc.location());
}

@Test public void handlesEmptyRedirect() throws IOException {
@Test public void handlesEmptyRedirect() {
boolean threw = false;
try {
Connection.Response res = Jsoup.connect(RedirectServlet.Url)
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/jsoup/integration/ParseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void testHtml5Charset() throws IOException {
in = getFile("/htmltests/meta-charset-2.html"); //
doc = Jsoup.parse(in, null, "http://example.com"); // gb2312, no charset
assertEquals("UTF-8", doc.outputSettings().charset().displayName());
assertFalse("新".equals(doc.text()));
assertNotEquals("新", doc.text());

// confirm fallback to utf8
in = getFile("/htmltests/meta-charset-3.html");
Expand Down
19 changes: 8 additions & 11 deletions src/test/java/org/jsoup/integration/UrlConnectTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jsoup.integration;

import org.jsoup.Connection;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.UnsupportedMimeTypeException;
import org.jsoup.internal.StringUtil;
Expand All @@ -23,8 +22,7 @@
import java.net.URL;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

/**
Tests the URL connection. Not enabled by default, so tests don't require network connection.
Expand All @@ -47,7 +45,7 @@ public void fetchBaidu() throws IOException {
assert(res.hasCookie("BAIDUID"));
assertEquals("text/html;charset=gbk", res.contentType());
}

@Test
public void exceptOnUnknownContentType() {
String url = "http://direct.jsoup.org/rez/osi_logo.png"; // not text/* but image/png, should throw
Expand Down Expand Up @@ -227,7 +225,7 @@ public void handlesDodgyCharset() throws IOException {
String url = "http://direct.infohound.net/tools/bad-charset.pl";
Connection.Response res = Jsoup.connect(url).execute();
assertEquals("text/html; charset=UFT8", res.header("Content-Type")); // from the header
assertEquals(null, res.charset()); // tried to get from header, not supported, so returns null
assertNull(res.charset()); // tried to get from header, not supported, so returns null
Document doc = res.parse(); // would throw an error if charset unsupported
assertTrue(doc.text().contains("Hello!"));
assertEquals("UTF-8", res.charset()); // set from default on parse
Expand Down Expand Up @@ -276,8 +274,7 @@ public void testUnsafeFail() throws Exception {
*/
@Test(expected = IOException.class)
public void testSNIFail() throws Exception {
String url = WEBSITE_WITH_SNI;
Jsoup.connect(url).execute();
Jsoup.connect(WEBSITE_WITH_SNI).execute();
}

@Test
Expand Down Expand Up @@ -443,7 +440,7 @@ public void fetchViaHttpProxySetByArgument() throws IOException {
}

@Test
public void invalidProxyFails() throws IOException {
public void invalidProxyFails() {
boolean caught = false;
String url = "https://jsoup.org";
try {
Expand Down Expand Up @@ -486,23 +483,23 @@ public void canSpecifyResponseCharset() throws IOException {

// included in meta
Connection.Response res1 = Jsoup.connect(charsetUrl).execute();
assertEquals(null, res1.charset()); // not set in headers
assertNull(res1.charset()); // not set in headers
final Document doc1 = res1.parse();
assertEquals("windows-1252", doc1.charset().displayName()); // but determined at parse time
assertEquals("Cost is €100", doc1.select("p").text());
assertTrue(doc1.text().contains("€"));

// no meta, no override
Connection.Response res2 = Jsoup.connect(noCharsetUrl).execute();
assertEquals(null, res2.charset()); // not set in headers
assertNull(res2.charset()); // not set in headers
final Document doc2 = res2.parse();
assertEquals("UTF-8", doc2.charset().displayName()); // so defaults to utf-8
assertEquals("Cost is �100", doc2.select("p").text());
assertTrue(doc2.text().contains("�"));

// no meta, let's override
Connection.Response res3 = Jsoup.connect(noCharsetUrl).execute();
assertEquals(null, res3.charset()); // not set in headers
assertNull(res3.charset()); // not set in headers
res3.charset("windows-1252");
assertEquals("windows-1252", res3.charset()); // read back
final Document doc3 = res3.parse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Deflateservlet extends BaseServlet {
public static final String Url = TestServer.map(Deflateservlet.class);

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.setContentType(TextHtml);
res.setStatus(HttpServletResponse.SC_OK);
res.setHeader("Content-Encoding", "deflate");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
public class FileServlet extends BaseServlet {
public static final String Url = TestServer.map(FileServlet.class);
public static final String ContentTypeParam = "contentType";
public static final String LocationParam = "loc";
public static final String DefaultType = "text/html";

@Override
Expand Down Expand Up @@ -43,7 +42,7 @@ public static String urlTo(String path) {
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
doGet(req, res);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class HelloServlet extends BaseServlet {
public static final String Url = TestServer.map(HelloServlet.class);

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
res.setContentType(TextHtml);
res.setStatus(HttpServletResponse.SC_OK);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.jsoup.integration.TestServer;
import org.jsoup.parser.CharacterReaderTest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
Expand All @@ -15,7 +14,7 @@ public class InterruptedServlet extends BaseServlet {


@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
String magnitude = req.getParameter(Magnitude);
magnitude = magnitude == null ? "" : magnitude;
res.setContentType(TextHtml);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class RedirectServlet extends BaseServlet {
private static final int DefaultCode = HttpServletResponse.SC_MOVED_TEMPORARILY;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
protected void doGet(HttpServletRequest req, HttpServletResponse res) {
String location = req.getParameter(LocationParam);
if (location == null)
location = "";
Expand All @@ -36,7 +36,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOE
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
doGet(req, res);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOE
int maxTime = -1;
String maxTimeP = req.getParameter(MaxTimeParam);
if (maxTimeP != null) {
maxTime = Integer.valueOf(maxTimeP);
maxTime = Integer.parseInt(maxTimeP);
}

long startTime = System.currentTimeMillis();
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/org/jsoup/internal/StringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;

import static org.jsoup.internal.StringUtil.*;
import static org.junit.Assert.assertEquals;
Expand All @@ -14,8 +15,8 @@
public class StringUtilTest {

@Test public void join() {
assertEquals("", StringUtil.join(Arrays.asList(""), " "));
assertEquals("one", StringUtil.join(Arrays.asList("one"), " "));
assertEquals("", StringUtil.join(Collections.singletonList(""), " "));
assertEquals("one", StringUtil.join(Collections.singletonList("one"), " "));
assertEquals("one two three", StringUtil.join(Arrays.asList("one", "two", "three"), " "));
}

Expand Down Expand Up @@ -62,7 +63,7 @@ public class StringUtilTest {
assertTrue(StringUtil.isWhitespace('\r'));
assertTrue(StringUtil.isWhitespace('\f'));
assertTrue(StringUtil.isWhitespace(' '));

assertFalse(StringUtil.isWhitespace('\u00a0'));
assertFalse(StringUtil.isWhitespace('\u2000'));
assertFalse(StringUtil.isWhitespace('\u3000'));
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/org/jsoup/nodes/AttributeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class AttributeTest {
assertEquals("two", oldVal);
assertEquals("three", attr.getKey());
assertEquals("four", attr.getValue());
assertEquals(null, attr.parent);
assertNull(attr.parent);
}

@Test public void hasValue() {
Expand Down
Loading

0 comments on commit 8ddcef3

Please sign in to comment.