Refactor unit tests to use ExpectedException
Instead of catching the expected exception and calling fail() when the exception is not raised, use the ExpectedException.expect() rule to confirm that the expected exception is raised. In cases where a specific exception message is expected, instead of catching the exception and then asserting on the value returned from its getMessage(), use the ExpectedException.expectMessage() rule. Change-Id: I15a61bbed33e11f77c5d0fac02374630f540a68e
This commit is contained in:
@@ -18,11 +18,15 @@ import static com.google.gwtexpui.safehtml.client.LinkFindReplace.hasValidScheme
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
public class LinkFindReplaceTest {
|
public class LinkFindReplaceTest {
|
||||||
|
@Rule
|
||||||
|
public ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNoEscaping() {
|
public void testNoEscaping() {
|
||||||
String find = "find";
|
String find = "find";
|
||||||
@@ -55,21 +59,15 @@ public class LinkFindReplaceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvalidSchemeInReplace() {
|
public void testInvalidSchemeInReplace() {
|
||||||
try {
|
exception.expect(IllegalArgumentException.class);
|
||||||
new LinkFindReplace("find", "javascript:alert(1)").replace("find");
|
new LinkFindReplace("find", "javascript:alert(1)").replace("find");
|
||||||
fail("Expected IllegalStateException");
|
|
||||||
} catch (IllegalArgumentException expected) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvalidSchemeWithBackreference() {
|
public void testInvalidSchemeWithBackreference() {
|
||||||
try {
|
exception.expect(IllegalArgumentException.class);
|
||||||
new LinkFindReplace(".*(script:[^;]*)", "java$1")
|
new LinkFindReplace(".*(script:[^;]*)", "java$1")
|
||||||
.replace("Look at this script: alert(1);");
|
.replace("Look at this script: alert(1);");
|
||||||
fail("Expected IllegalStateException");
|
|
||||||
} catch (IllegalArgumentException expected) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
@@ -20,11 +20,15 @@ import static org.junit.Assert.assertNotNull;
|
|||||||
import static org.junit.Assert.assertNotSame;
|
import static org.junit.Assert.assertNotSame;
|
||||||
import static org.junit.Assert.assertSame;
|
import static org.junit.Assert.assertSame;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
public class SafeHtmlBuilderTest {
|
public class SafeHtmlBuilderTest {
|
||||||
|
@Rule
|
||||||
|
public ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmpty() {
|
public void testEmpty() {
|
||||||
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
final SafeHtmlBuilder b = new SafeHtmlBuilder();
|
||||||
@@ -258,34 +262,25 @@ public class SafeHtmlBuilderTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testRejectJavaScript_AnchorHref() {
|
public void testRejectJavaScript_AnchorHref() {
|
||||||
final String href = "javascript:window.close();";
|
final String href = "javascript:window.close();";
|
||||||
try {
|
exception.expect(RuntimeException.class);
|
||||||
new SafeHtmlBuilder().openAnchor().setAttribute("href", href);
|
exception.expectMessage("javascript unsafe in href: " + href);
|
||||||
fail("accepted javascript in a href");
|
new SafeHtmlBuilder().openAnchor().setAttribute("href", href);
|
||||||
} catch (RuntimeException e) {
|
|
||||||
assertEquals("javascript unsafe in href: " + href, e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRejectJavaScript_ImgSrc() {
|
public void testRejectJavaScript_ImgSrc() {
|
||||||
final String href = "javascript:window.close();";
|
final String href = "javascript:window.close();";
|
||||||
try {
|
exception.expect(RuntimeException.class);
|
||||||
new SafeHtmlBuilder().openElement("img").setAttribute("src", href);
|
exception.expectMessage("javascript unsafe in href: " + href);
|
||||||
fail("accepted javascript in img src");
|
new SafeHtmlBuilder().openElement("img").setAttribute("src", href);
|
||||||
} catch (RuntimeException e) {
|
|
||||||
assertEquals("javascript unsafe in href: " + href, e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRejectJavaScript_FormAction() {
|
public void testRejectJavaScript_FormAction() {
|
||||||
final String href = "javascript:window.close();";
|
final String href = "javascript:window.close();";
|
||||||
try {
|
exception.expect(RuntimeException.class);
|
||||||
new SafeHtmlBuilder().openElement("form").setAttribute("action", href);
|
exception.expectMessage("javascript unsafe in href: " + href);
|
||||||
fail("accepted javascript in form action");
|
new SafeHtmlBuilder().openElement("form").setAttribute("action", href);
|
||||||
} catch (RuntimeException e) {
|
|
||||||
assertEquals("javascript unsafe in href: " + href, e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String escape(final char c) {
|
private static String escape(final char c) {
|
||||||
|
|||||||
@@ -14,12 +14,10 @@
|
|||||||
|
|
||||||
package com.google.gerrit.rules;
|
package com.google.gerrit.rules;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
|
||||||
import static com.google.gerrit.common.data.Permission.LABEL;
|
import static com.google.gerrit.common.data.Permission.LABEL;
|
||||||
import static com.google.gerrit.server.project.Util.allow;
|
import static com.google.gerrit.server.project.Util.allow;
|
||||||
import static com.google.gerrit.server.project.Util.category;
|
import static com.google.gerrit.server.project.Util.category;
|
||||||
import static com.google.gerrit.server.project.Util.value;
|
import static com.google.gerrit.server.project.Util.value;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import com.google.gerrit.common.TimeUtil;
|
import com.google.gerrit.common.TimeUtil;
|
||||||
import com.google.gerrit.common.data.LabelType;
|
import com.google.gerrit.common.data.LabelType;
|
||||||
@@ -42,7 +40,9 @@ import com.googlecode.prolog_cafe.lang.SymbolTerm;
|
|||||||
|
|
||||||
import org.eclipse.jgit.lib.Config;
|
import org.eclipse.jgit.lib.Config;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
import java.io.PushbackReader;
|
import java.io.PushbackReader;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
@@ -62,6 +62,9 @@ public class GerritCommonTest extends PrologTestCase {
|
|||||||
private ProjectConfig local;
|
private ProjectConfig local;
|
||||||
private Util util;
|
private Util util;
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
util = new Util();
|
util = new Util();
|
||||||
@@ -125,12 +128,9 @@ public class GerritCommonTest extends PrologTestCase {
|
|||||||
throw new CompileException("Cannot consult " + nameTerm);
|
throw new CompileException("Cannot consult " + nameTerm);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
exception.expect(ReductionLimitException.class);
|
||||||
env.once(Prolog.BUILTIN, "call", new StructureTerm(":",
|
exception.expectMessage("exceeded reduction limit of 1300");
|
||||||
SymbolTerm.create("user"), SymbolTerm.create("loopy")));
|
env.once(Prolog.BUILTIN, "call", new StructureTerm(":",
|
||||||
fail("long running loop did not abort with ReductionLimitException");
|
SymbolTerm.create("user"), SymbolTerm.create("loopy")));
|
||||||
} catch (ReductionLimitException e) {
|
|
||||||
assertThat(e.getMessage()).isEqualTo("exceeded reduction limit of 1300");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,11 +19,12 @@ import static org.junit.Assert.assertFalse;
|
|||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import com.google.gerrit.server.util.HostPlatform;
|
import com.google.gerrit.server.util.HostPlatform;
|
||||||
|
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@@ -32,6 +33,9 @@ import java.nio.file.Path;
|
|||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
public class SitePathsTest {
|
public class SitePathsTest {
|
||||||
|
@Rule
|
||||||
|
public ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCreate_NotExisting() throws IOException {
|
public void testCreate_NotExisting() throws IOException {
|
||||||
final Path root = random();
|
final Path root = random();
|
||||||
@@ -77,12 +81,8 @@ public class SitePathsTest {
|
|||||||
final Path root = random();
|
final Path root = random();
|
||||||
try {
|
try {
|
||||||
Files.createFile(root);
|
Files.createFile(root);
|
||||||
try {
|
exception.expect(NotDirectoryException.class);
|
||||||
new SitePaths(root);
|
new SitePaths(root);
|
||||||
fail("Did not throw exception");
|
|
||||||
} catch (NotDirectoryException e) {
|
|
||||||
// Expected.
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
Files.delete(root);
|
Files.delete(root);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,6 @@
|
|||||||
|
|
||||||
package com.google.gerrit.server.notedb;
|
package com.google.gerrit.server.notedb;
|
||||||
|
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import com.google.gerrit.common.TimeUtil;
|
import com.google.gerrit.common.TimeUtil;
|
||||||
|
|
||||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||||
@@ -29,12 +27,17 @@ import org.eclipse.jgit.revwalk.RevCommit;
|
|||||||
import org.eclipse.jgit.revwalk.RevWalk;
|
import org.eclipse.jgit.revwalk.RevWalk;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
public class ChangeNotesParserTest extends AbstractChangeNotesTest {
|
public class ChangeNotesParserTest extends AbstractChangeNotesTest {
|
||||||
private TestRepository<InMemoryRepository> testRepo;
|
private TestRepository<InMemoryRepository> testRepo;
|
||||||
private RevWalk walk;
|
private RevWalk walk;
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUpTestRepo() throws Exception {
|
public void setUpTestRepo() throws Exception {
|
||||||
testRepo = new TestRepository<>(repo);
|
testRepo = new TestRepository<>(repo);
|
||||||
@@ -202,10 +205,8 @@ public class ChangeNotesParserTest extends AbstractChangeNotesTest {
|
|||||||
|
|
||||||
private void assertParseFails(RevCommit commit) throws Exception {
|
private void assertParseFails(RevCommit commit) throws Exception {
|
||||||
try (ChangeNotesParser parser = newParser(commit)) {
|
try (ChangeNotesParser parser = newParser(commit)) {
|
||||||
|
exception.expect(ConfigInvalidException.class);
|
||||||
parser.parseAll();
|
parser.parseAll();
|
||||||
fail("Expected parse to fail:\n" + commit.getFullMessage());
|
|
||||||
} catch (ConfigInvalidException e) {
|
|
||||||
// Expected.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import static java.nio.charset.StandardCharsets.UTF_8;
|
|||||||
import static java.util.concurrent.TimeUnit.HOURS;
|
import static java.util.concurrent.TimeUnit.HOURS;
|
||||||
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
import static java.util.concurrent.TimeUnit.MILLISECONDS;
|
||||||
import static java.util.concurrent.TimeUnit.MINUTES;
|
import static java.util.concurrent.TimeUnit.MINUTES;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
import com.google.common.base.MoreObjects;
|
import com.google.common.base.MoreObjects;
|
||||||
@@ -74,7 +73,9 @@ import org.joda.time.DateTimeUtils.MillisProvider;
|
|||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -94,6 +95,9 @@ public abstract class AbstractQueryChangesTest {
|
|||||||
return updateConfig(NotesMigration.allEnabledConfig());
|
return updateConfig(NotesMigration.allEnabledConfig());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
private static Config updateConfig(Config cfg) {
|
private static Config updateConfig(Config cfg) {
|
||||||
cfg.setInt("index", null, "maxPages", 10);
|
cfg.setInt("index", null, "maxPages", 10);
|
||||||
return cfg;
|
return cfg;
|
||||||
@@ -1100,12 +1104,8 @@ public abstract class AbstractQueryChangesTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void assertBadQuery(QueryRequest query) throws Exception {
|
protected void assertBadQuery(QueryRequest query) throws Exception {
|
||||||
try {
|
exception.expect(BadRequestException.class);
|
||||||
query.get();
|
query.get();
|
||||||
fail("expected BadRequestException for query: " + query);
|
|
||||||
} catch (BadRequestException e) {
|
|
||||||
// Expected.
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected TestRepository<Repo> createProject(String name) throws Exception {
|
protected TestRepository<Repo> createProject(String name) throws Exception {
|
||||||
|
|||||||
@@ -23,9 +23,10 @@ import static java.net.InetSocketAddress.createUnresolved;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
|
|
||||||
import java.net.Inet4Address;
|
import java.net.Inet4Address;
|
||||||
import java.net.Inet6Address;
|
import java.net.Inet6Address;
|
||||||
@@ -34,6 +35,9 @@ import java.net.InetSocketAddress;
|
|||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
public class SocketUtilTest {
|
public class SocketUtilTest {
|
||||||
|
@Rule
|
||||||
|
public ExpectedException exception = ExpectedException.none();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIsIPv6() throws UnknownHostException {
|
public void testIsIPv6() throws UnknownHostException {
|
||||||
final InetAddress ipv6 = getByName("1:2:3:4:5:6:7:8");
|
final InetAddress ipv6 = getByName("1:2:3:4:5:6:7:8");
|
||||||
@@ -92,20 +96,20 @@ public class SocketUtilTest {
|
|||||||
parse("[foo.bar.example.com]:1234", 80));
|
parse("[foo.bar.example.com]:1234", 80));
|
||||||
assertEquals(createUnresolved("foo.bar.example.com", 80), //
|
assertEquals(createUnresolved("foo.bar.example.com", 80), //
|
||||||
parse("[foo.bar.example.com]", 80));
|
parse("[foo.bar.example.com]", 80));
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
@Test
|
||||||
parse("[:3", 80);
|
public void testParseInvalidIPv6() {
|
||||||
fail("did not throw exception");
|
exception.expect(IllegalArgumentException.class);
|
||||||
} catch (IllegalArgumentException e) {
|
exception.expectMessage("invalid IPv6: [:3");
|
||||||
assertEquals("invalid IPv6: [:3", e.getMessage());
|
parse("[:3", 80);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
@Test
|
||||||
parse("localhost:A", 80);
|
public void testParseInvalidPort() {
|
||||||
fail("did not throw exception");
|
exception.expect(IllegalArgumentException.class);
|
||||||
} catch (IllegalArgumentException e) {
|
exception.expectMessage("invalid port: localhost:A");
|
||||||
assertEquals("invalid port: localhost:A", e.getMessage());
|
parse("localhost:A", 80);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user