From e98b99edcd523e6690a9b5b963fd52b2f310eb9b Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Mon, 15 Jul 2019 15:25:17 -0700 Subject: [PATCH 1/5] Properly acquire JUnit 4, drop json-simple --- buildSrc/src/main/kotlin/PlatformConfig.kt | 5 +++++ worldedit-core/build.gradle.kts | 1 - .../sk89q/worldedit/util/paste/EngineHubPaste.java | 13 ++++++++----- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/buildSrc/src/main/kotlin/PlatformConfig.kt b/buildSrc/src/main/kotlin/PlatformConfig.kt index c57dbdd20..e4ab9629a 100644 --- a/buildSrc/src/main/kotlin/PlatformConfig.kt +++ b/buildSrc/src/main/kotlin/PlatformConfig.kt @@ -9,6 +9,7 @@ import org.gradle.api.tasks.javadoc.Javadoc import org.gradle.external.javadoc.CoreJavadocOptions import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.configure +import org.gradle.kotlin.dsl.dependencies import org.gradle.kotlin.dsl.get import org.gradle.kotlin.dsl.getByName import org.gradle.kotlin.dsl.named @@ -38,6 +39,10 @@ fun Project.applyPlatformAndCoreConfiguration() { toolVersion = "7.6.1" } + dependencies { + "testImplementation"("junit:junit:4.12") + } + // Java 8 turns on doclint which we fail tasks.withType().configureEach { (options as CoreJavadocOptions).addStringOption("Xdoclint:none", "-quiet") diff --git a/worldedit-core/build.gradle.kts b/worldedit-core/build.gradle.kts index 6a9266247..274cf2e06 100644 --- a/worldedit-core/build.gradle.kts +++ b/worldedit-core/build.gradle.kts @@ -20,7 +20,6 @@ dependencies { "compile"("com.google.guava:guava:21.0") "compile"("com.google.code.findbugs:jsr305:1.3.9") "compile"("com.google.code.gson:gson:2.8.0") - "compile"("com.googlecode.json-simple:json-simple:1.1.1") "compile"("org.slf4j:slf4j-api:1.7.26") "compileOnly"(project(":worldedit-libs:core:ap")) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/paste/EngineHubPaste.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/paste/EngineHubPaste.java index ac7484846..ab8ebe029 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/paste/EngineHubPaste.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/paste/EngineHubPaste.java @@ -19,8 +19,9 @@ package com.sk89q.worldedit.util.paste; +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import com.sk89q.worldedit.util.net.HttpRequest; -import org.json.simple.JSONValue; import java.io.IOException; import java.net.URL; @@ -33,6 +34,8 @@ public class EngineHubPaste implements Paster { private static final Pattern URL_PATTERN = Pattern.compile("https?://.+$"); + private static final Gson GSON = new Gson(); + @Override public Callable paste(String content) { return new PasteTask(content); @@ -59,10 +62,10 @@ public class EngineHubPaste implements Paster { .returnContent() .asString("UTF-8").trim(); - Object object = JSONValue.parse(result); - if (object instanceof Map) { - @SuppressWarnings("unchecked") - String urlString = String.valueOf(((Map) object).get("url")); + Map object = GSON.fromJson(result, new TypeToken>() { + }.getType()); + if (object != null) { + String urlString = String.valueOf(object.get("url")); Matcher m = URL_PATTERN.matcher(urlString); if (m.matches()) { From 429d022752af0951bb53990907c2130a901cfab1 Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Mon, 15 Jul 2019 16:07:11 -0700 Subject: [PATCH 2/5] Move Core to JUnit 5 --- buildSrc/src/main/kotlin/PlatformConfig.kt | 8 ++++- buildSrc/src/main/kotlin/Versions.kt | 1 + worldedit-bukkit/build.gradle.kts | 4 ++- .../util/commands/CommandContextTest.java | 25 +++++++------ .../transform/BlockTransformExtentTest.java | 18 +++++----- .../command/CommandArgParserTest.java | 4 +-- .../internal/expression/ExpressionTest.java | 35 +++++++++---------- .../sk89q/worldedit/util/LocationTest.java | 8 ++--- .../worldedit/util/eventbus/EventBusTest.java | 4 +-- 9 files changed, 59 insertions(+), 48 deletions(-) diff --git a/buildSrc/src/main/kotlin/PlatformConfig.kt b/buildSrc/src/main/kotlin/PlatformConfig.kt index e4ab9629a..c5deea857 100644 --- a/buildSrc/src/main/kotlin/PlatformConfig.kt +++ b/buildSrc/src/main/kotlin/PlatformConfig.kt @@ -6,6 +6,7 @@ import org.gradle.api.plugins.JavaPluginConvention import org.gradle.api.plugins.quality.CheckstyleExtension import org.gradle.api.tasks.bundling.Jar import org.gradle.api.tasks.javadoc.Javadoc +import org.gradle.api.tasks.testing.Test import org.gradle.external.javadoc.CoreJavadocOptions import org.gradle.kotlin.dsl.apply import org.gradle.kotlin.dsl.configure @@ -39,8 +40,13 @@ fun Project.applyPlatformAndCoreConfiguration() { toolVersion = "7.6.1" } + tasks.withType().configureEach { + useJUnitPlatform() + } + dependencies { - "testImplementation"("junit:junit:4.12") + "testImplementation"("org.junit.jupiter:junit-jupiter-api:${Versions.JUNIT}") + "testRuntime"("org.junit.jupiter:junit-jupiter-engine:${Versions.JUNIT}") } // Java 8 turns on doclint which we fail diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 0f79cbfd1..4679c2d40 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -3,4 +3,5 @@ object Versions { const val TEXT_EXTRAS = "3.0.2" const val PISTON = "0.4.2" const val AUTO_VALUE = "1.6.5" + const val JUNIT = "5.5.0" } diff --git a/worldedit-bukkit/build.gradle.kts b/worldedit-bukkit/build.gradle.kts index ee1b94c27..1343ff2be 100644 --- a/worldedit-bukkit/build.gradle.kts +++ b/worldedit-bukkit/build.gradle.kts @@ -22,7 +22,9 @@ configurations.all { dependencies { "api"(project(":worldedit-core")) "api"(project(":worldedit-libs:bukkit")) - "api"("org.bukkit:bukkit:1.13.2-R0.1-SNAPSHOT") + "api"("org.bukkit:bukkit:1.13.2-R0.1-SNAPSHOT") { + exclude("junit", "junit") + } "compileOnly"("com.destroystokyo.paper:paper-api:1.13.2-R0.1-SNAPSHOT") "implementation"("io.papermc:paperlib:1.0.2") "compileOnly"("com.sk89q:dummypermscompat:1.10") diff --git a/worldedit-core/src/test/java/com/sk89q/minecraft/util/commands/CommandContextTest.java b/worldedit-core/src/test/java/com/sk89q/minecraft/util/commands/CommandContextTest.java index 555c83889..19bd552ec 100644 --- a/worldedit-core/src/test/java/com/sk89q/minecraft/util/commands/CommandContextTest.java +++ b/worldedit-core/src/test/java/com/sk89q/minecraft/util/commands/CommandContextTest.java @@ -19,19 +19,20 @@ package com.sk89q.minecraft.util.commands; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Arrays; import java.util.HashSet; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class CommandContextTest { @@ -39,7 +40,7 @@ public class CommandContextTest { private static final String firstCmdString = "herpderp -opw testers \"mani world\" 'another thing' because something"; CommandContext firstCommand; - @Before + @BeforeEach public void setUpTest() { try { firstCommand = new CommandContext(firstCmdString, new HashSet<>(Arrays.asList('o', 'w'))); @@ -49,10 +50,12 @@ public class CommandContextTest { } } - @Test(expected = CommandException.class) - public void testInvalidFlags() throws CommandException { + @Test + public void testInvalidFlags() { final String failingCommand = "herpderp -opw testers"; - new CommandContext(failingCommand, new HashSet<>(Arrays.asList('o', 'w'))); + assertThrows(CommandException.class, () -> { + new CommandContext(failingCommand, new HashSet<>(Arrays.asList('o', 'w'))); + }); } @Test diff --git a/worldedit-core/src/test/java/com/sk89q/worldedit/extent/transform/BlockTransformExtentTest.java b/worldedit-core/src/test/java/com/sk89q/worldedit/extent/transform/BlockTransformExtentTest.java index 633e359f8..101509aa4 100644 --- a/worldedit-core/src/test/java/com/sk89q/worldedit/extent/transform/BlockTransformExtentTest.java +++ b/worldedit-core/src/test/java/com/sk89q/worldedit/extent/transform/BlockTransformExtentTest.java @@ -19,33 +19,33 @@ package com.sk89q.worldedit.extent.transform; -import static org.junit.Assert.assertEquals; - import com.sk89q.worldedit.math.transform.AffineTransform; import com.sk89q.worldedit.math.transform.Transform; import com.sk89q.worldedit.world.block.BlockState; import com.sk89q.worldedit.world.block.BlockType; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; import java.util.HashSet; import java.util.Set; -@Ignore("A platform is currently required to get properties, preventing this test.") +import static org.junit.jupiter.api.Assertions.assertEquals; + +@Disabled("A platform is currently required to get properties, preventing this test.") public class BlockTransformExtentTest { private static final Transform ROTATE_90 = new AffineTransform().rotateY(-90); private static final Transform ROTATE_NEG_90 = new AffineTransform().rotateY(90); private final Set ignored = new HashSet<>(); - @Before - public void setUp() throws Exception { + @BeforeEach + public void setUp() { BlockType.REGISTRY.register("worldedit:test", new BlockType("worldedit:test")); } @Test - public void testTransform() throws Exception { + public void testTransform() { for (BlockType type : BlockType.REGISTRY.values()) { if (ignored.contains(type)) { continue; diff --git a/worldedit-core/src/test/java/com/sk89q/worldedit/internal/command/CommandArgParserTest.java b/worldedit-core/src/test/java/com/sk89q/worldedit/internal/command/CommandArgParserTest.java index 7c75de4a6..c696bdabb 100644 --- a/worldedit-core/src/test/java/com/sk89q/worldedit/internal/command/CommandArgParserTest.java +++ b/worldedit-core/src/test/java/com/sk89q/worldedit/internal/command/CommandArgParserTest.java @@ -21,10 +21,10 @@ package com.sk89q.worldedit.internal.command; import com.google.common.collect.ImmutableList; import com.sk89q.worldedit.internal.util.Substring; -import org.junit.Test; +import org.junit.jupiter.api.Test; import static com.sk89q.worldedit.internal.command.CommandArgParser.spaceSplit; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; public class CommandArgParserTest { @Test diff --git a/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java b/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java index 28ad67b37..4fe511925 100644 --- a/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java +++ b/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java @@ -19,16 +19,6 @@ package com.sk89q.worldedit.internal.expression; -import static java.lang.Math.atan2; -import static java.lang.Math.sin; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; - import com.sk89q.worldedit.LocalConfiguration; import com.sk89q.worldedit.WorldEdit; import com.sk89q.worldedit.extension.platform.Platform; @@ -36,9 +26,18 @@ import com.sk89q.worldedit.internal.expression.lexer.LexerException; import com.sk89q.worldedit.internal.expression.parser.ParserException; import com.sk89q.worldedit.internal.expression.runtime.EvaluationException; import com.sk89q.worldedit.internal.expression.runtime.ExpressionEnvironment; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import static java.lang.Math.atan2; +import static java.lang.Math.sin; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; public class ExpressionTest { - @Before + @BeforeEach public void setup() { Platform mockPlat = Mockito.mock(Platform.class); Mockito.when(mockPlat.getConfiguration()).thenReturn(new LocalConfiguration() { @@ -51,7 +50,7 @@ public class ExpressionTest { @Test public void testEvaluate() throws ExpressionException { - // check + // check assertEquals(1 - 2 + 3, simpleEval("1 - 2 + 3"), 0); // check unary ops @@ -74,7 +73,7 @@ public class ExpressionTest { compile("#"); fail("Error expected"); } catch (LexerException e) { - assertEquals("Error position", 0, e.getPosition()); + assertEquals(0, e.getPosition(), "Error position"); } // test parser errors @@ -82,13 +81,13 @@ public class ExpressionTest { compile("x"); fail("Error expected"); } catch (ParserException e) { - assertEquals("Error position", 0, e.getPosition()); + assertEquals(0, e.getPosition(), "Error position"); } try { compile("x()"); fail("Error expected"); } catch (ParserException e) { - assertEquals("Error position", 0, e.getPosition()); + assertEquals(0, e.getPosition(), "Error position"); } try { compile("("); @@ -104,19 +103,19 @@ public class ExpressionTest { compile("atan2(1)"); fail("Error expected"); } catch (ParserException e) { - assertEquals("Error position", 0, e.getPosition()); + assertEquals(0, e.getPosition(), "Error position"); } try { compile("atan2(1, 2, 3)"); fail("Error expected"); } catch (ParserException e) { - assertEquals("Error position", 0, e.getPosition()); + assertEquals(0, e.getPosition(), "Error position"); } try { compile("rotate(1, 2, 3)"); fail("Error expected"); } catch (ParserException e) { - assertEquals("Error position", 0, e.getPosition()); + assertEquals(0, e.getPosition(), "Error position"); } } diff --git a/worldedit-core/src/test/java/com/sk89q/worldedit/util/LocationTest.java b/worldedit-core/src/test/java/com/sk89q/worldedit/util/LocationTest.java index 50dfa649e..2c990ab74 100644 --- a/worldedit-core/src/test/java/com/sk89q/worldedit/util/LocationTest.java +++ b/worldedit-core/src/test/java/com/sk89q/worldedit/util/LocationTest.java @@ -19,12 +19,12 @@ package com.sk89q.worldedit.util; -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.mock; - import com.sk89q.worldedit.math.Vector3; import com.sk89q.worldedit.world.World; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; /** * Tests {@link Location}. diff --git a/worldedit-core/src/test/java/com/sk89q/worldedit/util/eventbus/EventBusTest.java b/worldedit-core/src/test/java/com/sk89q/worldedit/util/eventbus/EventBusTest.java index 6289301dc..82fdb11c0 100644 --- a/worldedit-core/src/test/java/com/sk89q/worldedit/util/eventbus/EventBusTest.java +++ b/worldedit-core/src/test/java/com/sk89q/worldedit/util/eventbus/EventBusTest.java @@ -19,14 +19,14 @@ package com.sk89q.worldedit.util.eventbus; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.util.ArrayList; import java.util.List; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; public class EventBusTest { From 3b157b67c3fb56d26ad800fe3da38386c88db276 Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Mon, 15 Jul 2019 16:10:50 -0700 Subject: [PATCH 3/5] Move Bukkit to JUnit 5 --- .../sk89q/wepif/DinnerPermsResolverTest.java | 30 +++++++++---------- .../worldedit/bukkit/BukkitWorldTest.java | 7 +++-- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/worldedit-bukkit/src/test/java/com/sk89q/wepif/DinnerPermsResolverTest.java b/worldedit-bukkit/src/test/java/com/sk89q/wepif/DinnerPermsResolverTest.java index e207e7b8d..37deab330 100644 --- a/worldedit-bukkit/src/test/java/com/sk89q/wepif/DinnerPermsResolverTest.java +++ b/worldedit-bukkit/src/test/java/com/sk89q/wepif/DinnerPermsResolverTest.java @@ -19,26 +19,26 @@ package com.sk89q.wepif; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - import org.bukkit.Server; import org.bukkit.plugin.PluginManager; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class DinnerPermsResolverTest { private DinnerPermsResolver resolver; - @Before + @BeforeEach public void setUp() { Server server = mock(Server.class); when(server.getPluginManager()).thenReturn(mock(PluginManager.class)); resolver = new DinnerPermsResolver(server); } - + @Test public void testBasicResolving() { final TestOfflinePermissible permissible = new TestOfflinePermissible(); @@ -49,7 +49,7 @@ public class DinnerPermsResolverTest { assertFalse(resolver.hasPermission(permissible, "completely.unrelated")); permissible.clearPermissions(); } - + @Test public void testBasicWildcardResolution() { final TestOfflinePermissible permissible = new TestOfflinePermissible(); @@ -59,7 +59,7 @@ public class DinnerPermsResolverTest { assertTrue(resolver.hasPermission(permissible, "commandbook.spawnmob.spider.skeleton")); permissible.clearPermissions(); } - + @Test public void testNegatingNodes() { final TestOfflinePermissible permissible = new TestOfflinePermissible(); @@ -67,16 +67,16 @@ public class DinnerPermsResolverTest { permissible.setPermission("commandbook.cuteasianboys", false); permissible.setPermission("commandbook.warp.*", false); permissible.setPermission("commandbook.warp.create", true); - + assertTrue(resolver.hasPermission(permissible, "commandbook.motd")); assertFalse(resolver.hasPermission(permissible, "commandbook.cuteasianboys")); assertFalse(resolver.hasPermission(permissible, "commandbook.warp.remove")); assertTrue(resolver.hasPermission(permissible, "commandbook.warp.create")); - + permissible.clearPermissions(); } - - + + @Test public void testInGroup() { final TestOfflinePermissible permissible = new TestOfflinePermissible(); diff --git a/worldedit-bukkit/src/test/java/com/sk89q/worldedit/bukkit/BukkitWorldTest.java b/worldedit-bukkit/src/test/java/com/sk89q/worldedit/bukkit/BukkitWorldTest.java index e6776cf8c..8851fb926 100644 --- a/worldedit-bukkit/src/test/java/com/sk89q/worldedit/bukkit/BukkitWorldTest.java +++ b/worldedit-bukkit/src/test/java/com/sk89q/worldedit/bukkit/BukkitWorldTest.java @@ -20,15 +20,16 @@ package com.sk89q.worldedit.bukkit; import com.sk89q.worldedit.util.TreeGenerator; -import org.junit.Assert; -import org.junit.Test; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; public class BukkitWorldTest { @Test public void testTreeTypeMapping() { for (TreeGenerator.TreeType type : TreeGenerator.TreeType.values()) { - Assert.assertNotNull("No mapping for: " + type, BukkitWorld.toBukkitTreeType(type)); + assertNotNull(BukkitWorld.toBukkitTreeType(type), "No mapping for: " + type); } } From 5c5c822f4b8fc1ba771a85ed868d49632241a861 Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Mon, 15 Jul 2019 16:19:28 -0700 Subject: [PATCH 4/5] Replace try-fail-catch-assert with assertThrows --- .../internal/expression/ExpressionTest.java | 105 ++++++++---------- 1 file changed, 47 insertions(+), 58 deletions(-) diff --git a/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java b/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java index 4fe511925..5cbb6e7b2 100644 --- a/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java +++ b/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java @@ -32,9 +32,10 @@ import org.mockito.Mockito; import static java.lang.Math.atan2; import static java.lang.Math.sin; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; public class ExpressionTest { @BeforeEach @@ -67,56 +68,46 @@ public class ExpressionTest { } @Test - public void testErrors() throws ExpressionException { - // test lexer errors - try { - compile("#"); - fail("Error expected"); - } catch (LexerException e) { - assertEquals(0, e.getPosition(), "Error position"); - } - - // test parser errors - try { - compile("x"); - fail("Error expected"); - } catch (ParserException e) { - assertEquals(0, e.getPosition(), "Error position"); - } - try { - compile("x()"); - fail("Error expected"); - } catch (ParserException e) { - assertEquals(0, e.getPosition(), "Error position"); - } - try { - compile("("); - fail("Error expected"); - } catch (ParserException ignored) {} - try { - compile("x("); - fail("Error expected"); - } catch (ParserException ignored) {} - - // test overloader errors - try { - compile("atan2(1)"); - fail("Error expected"); - } catch (ParserException e) { - assertEquals(0, e.getPosition(), "Error position"); - } - try { - compile("atan2(1, 2, 3)"); - fail("Error expected"); - } catch (ParserException e) { - assertEquals(0, e.getPosition(), "Error position"); - } - try { - compile("rotate(1, 2, 3)"); - fail("Error expected"); - } catch (ParserException e) { - assertEquals(0, e.getPosition(), "Error position"); - } + public void testErrors() { + assertAll( + // test lexer errors + () -> { + LexerException e = assertThrows(LexerException.class, + () -> compile("#")); + assertEquals(0, e.getPosition(), "Error position"); + }, + // test parser errors + () -> { + ParserException e = assertThrows(ParserException.class, + () -> compile("x")); + assertEquals(0, e.getPosition(), "Error position"); + }, + () -> { + ParserException e = assertThrows(ParserException.class, + () -> compile("x()")); + assertEquals(0, e.getPosition(), "Error position"); + }, + () -> assertThrows(ParserException.class, + () -> compile("(")), + () -> assertThrows(ParserException.class, + () -> compile("x(")), + // test overloader errors + () -> { + ParserException e = assertThrows(ParserException.class, + () -> compile("atan2(1)")); + assertEquals(0, e.getPosition(), "Error position"); + }, + () -> { + ParserException e = assertThrows(ParserException.class, + () -> compile("atan2(1, 2, 3)")); + assertEquals(0, e.getPosition(), "Error position"); + }, + () -> { + ParserException e = assertThrows(ParserException.class, + () -> compile("rotate(1, 2, 3)")); + assertEquals(0, e.getPosition(), "Error position"); + } + ); } @Test @@ -180,13 +171,11 @@ public class ExpressionTest { } @Test - public void testTimeout() throws Exception { - try { - simpleEval("for(i=0;i<256;i++){for(j=0;j<256;j++){for(k=0;k<256;k++){for(l=0;l<256;l++){ln(pi)}}}}"); - fail("Loop was not stopped."); - } catch (EvaluationException e) { - assertTrue(e.getMessage().contains("Calculations exceeded time limit")); - } + public void testTimeout() { + EvaluationException e = assertThrows(EvaluationException.class, + () -> simpleEval("for(i=0;i<256;i++){for(j=0;j<256;j++){for(k=0;k<256;k++){for(l=0;l<256;l++){ln(pi)}}}}"), + "Loop was not stopped."); + assertTrue(e.getMessage().contains("Calculations exceeded time limit")); } private double simpleEval(String expressionString) throws ExpressionException { From c1f4eecd774c5fd13f69a8364e473d948986dd9b Mon Sep 17 00:00:00 2001 From: Kenzie Togami Date: Mon, 15 Jul 2019 17:18:10 -0700 Subject: [PATCH 5/5] Narrow timeout test exception type --- .../sk89q/worldedit/internal/expression/ExpressionTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java b/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java index 5cbb6e7b2..d26d955d0 100644 --- a/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java +++ b/worldedit-core/src/test/java/com/sk89q/worldedit/internal/expression/ExpressionTest.java @@ -26,6 +26,7 @@ import com.sk89q.worldedit.internal.expression.lexer.LexerException; import com.sk89q.worldedit.internal.expression.parser.ParserException; import com.sk89q.worldedit.internal.expression.runtime.EvaluationException; import com.sk89q.worldedit.internal.expression.runtime.ExpressionEnvironment; +import com.sk89q.worldedit.internal.expression.runtime.ExpressionTimeoutException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -172,7 +173,7 @@ public class ExpressionTest { @Test public void testTimeout() { - EvaluationException e = assertThrows(EvaluationException.class, + ExpressionTimeoutException e = assertThrows(ExpressionTimeoutException.class, () -> simpleEval("for(i=0;i<256;i++){for(j=0;j<256;j++){for(k=0;k<256;k++){for(l=0;l<256;l++){ln(pi)}}}}"), "Loop was not stopped."); assertTrue(e.getMessage().contains("Calculations exceeded time limit"));