diff --git a/antlr4-maven-plugin/src/test/java/org/antlr/mojo/antlr4/Antlr4MojoTest.java b/antlr4-maven-plugin/src/test/java/org/antlr/mojo/antlr4/Antlr4MojoTest.java index d7dac181d1..db20660f5c 100644 --- a/antlr4-maven-plugin/src/test/java/org/antlr/mojo/antlr4/Antlr4MojoTest.java +++ b/antlr4-maven-plugin/src/test/java/org/antlr/mojo/antlr4/Antlr4MojoTest.java @@ -16,21 +16,20 @@ import org.codehaus.plexus.util.xml.Xpp3Dom; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.function.ThrowingRunnable; import java.io.Closeable; import java.io.File; import java.io.IOException; import java.util.Arrays; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; import static org.junit.Assume.assumeTrue; public class Antlr4MojoTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Rule public final TestResources resources = new TestResources(); @@ -347,9 +346,9 @@ public void processWhenDependencyRemoved() throws Exception { File baseGrammar = new File(antlrDir, "imports/HelloBase.g4"); - MavenProject project = maven.readMavenProject(baseDir); - MavenSession session = maven.newMavenSession(project); - MojoExecution exec = maven.newMojoExecution("antlr4"); + final MavenProject project = maven.readMavenProject(baseDir); + final MavenSession session = maven.newMavenSession(project); + final MojoExecution exec = maven.newMojoExecution("antlr4"); maven.executeMojo(session, project, exec); @@ -358,10 +357,14 @@ public void processWhenDependencyRemoved() throws Exception { // if the base grammar no longer exists, processing must be performed assertTrue(baseGrammar.delete()); - thrown.expect(MojoExecutionException.class); - thrown.expectMessage("ANTLR 4 caught 1 build errors."); + Throwable t = assertThrows(MojoExecutionException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + maven.executeMojo(session, project, exec); + } + }); - maven.executeMojo(session, project, exec); + assertEquals("ANTLR 4 caught 1 build errors.", t.getMessage()); } finally { temp.close(); } diff --git a/runtime/Java/test/org/antlr/v4/runtime/TestCodePointCharStream.java b/runtime/Java/test/org/antlr/v4/runtime/TestCodePointCharStream.java index 57345d3b86..1d0d05ac0d 100644 --- a/runtime/Java/test/org/antlr/v4/runtime/TestCodePointCharStream.java +++ b/runtime/Java/test/org/antlr/v4/runtime/TestCodePointCharStream.java @@ -6,17 +6,14 @@ package org.antlr.v4.runtime; import org.antlr.v4.runtime.misc.Interval; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.function.ThrowingRunnable; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; public class TestCodePointCharStream { - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void emptyBytesHasSize0() { CodePointCharStream s = CharStreams.fromString(""); @@ -34,10 +31,15 @@ public void emptyBytesLookAheadReturnsEOF() { @Test public void consumingEmptyStreamShouldThrow() { - CodePointCharStream s = CharStreams.fromString(""); - thrown.expect(IllegalStateException.class); - thrown.expectMessage("cannot consume EOF"); - s.consume(); + final CodePointCharStream s = CharStreams.fromString(""); + Throwable t = assertThrows(IllegalStateException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + s.consume(); + } + }); + + assertEquals("cannot consume EOF", t.getMessage()); } @Test @@ -56,11 +58,17 @@ public void consumingSingleLatinCodePointShouldMoveIndex() { @Test public void consumingPastSingleLatinCodePointShouldThrow() { - CodePointCharStream s = CharStreams.fromString("X"); - s.consume(); - thrown.expect(IllegalStateException.class); - thrown.expectMessage("cannot consume EOF"); + final CodePointCharStream s = CharStreams.fromString("X"); s.consume(); + + Throwable t = assertThrows(IllegalStateException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + s.consume(); + } + }); + + assertEquals("cannot consume EOF", t.getMessage()); } @Test @@ -104,11 +112,17 @@ public void consumingSingleCJKCodePointShouldMoveIndex() { @Test public void consumingPastSingleCJKCodePointShouldThrow() { - CodePointCharStream s = CharStreams.fromString("\u611B"); - s.consume(); - thrown.expect(IllegalStateException.class); - thrown.expectMessage("cannot consume EOF"); + final CodePointCharStream s = CharStreams.fromString("\u611B"); s.consume(); + + Throwable t = assertThrows(IllegalStateException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + s.consume(); + } + }); + + assertEquals("cannot consume EOF", t.getMessage()); } @Test @@ -144,14 +158,20 @@ public void consumingSingleEmojiCodePointShouldMoveIndex() { @Test public void consumingPastEndOfEmojiCodePointWithShouldThrow() { - CodePointCharStream s = CharStreams.fromString( + final CodePointCharStream s = CharStreams.fromString( new StringBuilder().appendCodePoint(0x1F4A9).toString()); assertEquals(0, s.index()); s.consume(); assertEquals(1, s.index()); - thrown.expect(IllegalStateException.class); - thrown.expectMessage("cannot consume EOF"); - s.consume(); + + Throwable t = assertThrows(IllegalStateException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + s.consume(); + } + }); + + assertEquals("cannot consume EOF", t.getMessage()); } @Test diff --git a/tool/test/org/antlr/v4/test/tool/TestCharStreams.java b/tool/test/org/antlr/v4/test/tool/TestCharStreams.java index 15a9ff7e94..1603ab02d2 100644 --- a/tool/test/org/antlr/v4/test/tool/TestCharStreams.java +++ b/tool/test/org/antlr/v4/test/tool/TestCharStreams.java @@ -10,7 +10,7 @@ import org.antlr.v4.runtime.misc.Utils; import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.function.ThrowingRunnable; import org.junit.rules.TemporaryFolder; import java.io.File; @@ -25,14 +25,12 @@ import java.nio.charset.CodingErrorAction; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; public class TestCharStreams { @Rule public TemporaryFolder folder = new TemporaryFolder(); - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void fromBMPStringHasExpectedSize() { CharStream s = CharStreams.fromString("hello"); @@ -164,10 +162,14 @@ public void fromInvalidUTF8BytesThrowsInReportMode() throws Exception { File p = folder.newFile(); byte[] toWrite = new byte[] { (byte)0xCA, (byte)0xFE }; Utils.writeFile(p, toWrite); - ReadableByteChannel c = Channels.newChannel(new FileInputStream(p)); + final ReadableByteChannel c = Channels.newChannel(new FileInputStream(p)); try { - thrown.expect(CharacterCodingException.class); - CharStreams.fromChannel(c, 4096, CodingErrorAction.REPORT, "foo"); + assertThrows(CharacterCodingException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + CharStreams.fromChannel(c, 4096, CodingErrorAction.REPORT, "foo"); + } + }); } finally { c.close(); diff --git a/tool/test/org/antlr/v4/test/tool/TestIntegerList.java b/tool/test/org/antlr/v4/test/tool/TestIntegerList.java index 3a37cdf831..837e59fff3 100644 --- a/tool/test/org/antlr/v4/test/tool/TestIntegerList.java +++ b/tool/test/org/antlr/v4/test/tool/TestIntegerList.java @@ -8,16 +8,13 @@ import org.antlr.v4.runtime.misc.IntegerList; import org.antlr.v4.runtime.misc.Utils; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.function.ThrowingRunnable; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertThrows; public class TestIntegerList { - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void emptyListToEmptyCharArray() { IntegerList l = new IntegerList(); @@ -26,10 +23,15 @@ public void emptyListToEmptyCharArray() { @Test public void negativeIntegerToCharArrayThrows() { - IntegerList l = new IntegerList(); + final IntegerList l = new IntegerList(); l.add(-42); - thrown.expect(IllegalArgumentException.class); - Utils.toCharArray(l); + + assertThrows(IllegalArgumentException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + Utils.toCharArray(l); + } + }); } @Test @@ -44,10 +46,15 @@ public void surrogateRangeIntegerToCharArray() { @Test public void tooLargeIntegerToCharArrayThrows() { - IntegerList l = new IntegerList(); + final IntegerList l = new IntegerList(); l.add(0x110000); - thrown.expect(IllegalArgumentException.class); - Utils.toCharArray(l); + + assertThrows(IllegalArgumentException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + Utils.toCharArray(l); + } + }); } @Test diff --git a/tool/test/org/antlr/v4/test/tool/TestPerformance.java b/tool/test/org/antlr/v4/test/tool/TestPerformance.java index 693f3cc87f..31e9d85af2 100644 --- a/tool/test/org/antlr/v4/test/tool/TestPerformance.java +++ b/tool/test/org/antlr/v4/test/tool/TestPerformance.java @@ -90,7 +90,7 @@ import java.util.logging.Logger; import static org.hamcrest.CoreMatchers.instanceOf; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertTrue; public class TestPerformance extends BaseTest { diff --git a/tool/test/org/antlr/v4/test/tool/TestUnicodeData.java b/tool/test/org/antlr/v4/test/tool/TestUnicodeData.java index 0dd6b935a2..acfc7c8896 100644 --- a/tool/test/org/antlr/v4/test/tool/TestUnicodeData.java +++ b/tool/test/org/antlr/v4/test/tool/TestUnicodeData.java @@ -7,17 +7,15 @@ package org.antlr.v4.test.tool; import org.antlr.v4.unicode.UnicodeData; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; +import org.junit.function.ThrowingRunnable; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; public class TestUnicodeData { - @Rule - public ExpectedException thrown = ExpectedException.none(); - @Test public void testUnicodeGeneralCategoriesLatin() { assertTrue(UnicodeData.getPropertyCodePoints("Lu").contains('X')); @@ -208,8 +206,13 @@ public void testPropertyDashSameAsUnderscore() { @Test public void modifyingUnicodeDataShouldThrow() { - thrown.expect(IllegalStateException.class); - thrown.expectMessage("can't alter readonly IntervalSet"); - UnicodeData.getPropertyCodePoints("L").add(0x12345); + Throwable t = assertThrows(IllegalStateException.class, new ThrowingRunnable() { + @Override + public void run() throws Throwable { + UnicodeData.getPropertyCodePoints("L").add(0x12345); + } + }); + + assertEquals("can't alter readonly IntervalSet", t.getMessage()); } }