diff --git a/eraser-base/build.gradle b/eraser-base/build.gradle index bda6f86b130a008a80336a26635f152f0ed734ed..d7d479060974e959b4caeefa1e592897d922f028 100644 --- a/eraser-base/build.gradle +++ b/eraser-base/build.gradle @@ -20,7 +20,8 @@ dependencies { api group: 'org.fusesource.mqtt-client', name: 'mqtt-client', version: '1.16' implementation group: 'org.influxdb', name: 'influxdb-java', version: '2.20' testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: "${jupiter_version}" - testImplementation group: 'org.testcontainers', name: 'testcontainers', version: '1.15.0' +// testImplementation group: 'org.testcontainers', name: 'testcontainers', version: '1.15.0' + testImplementation group: 'org.testcontainers', name: 'junit-jupiter', version: '1.15.0' testImplementation group: 'org.testcontainers', name: 'influxdb', version: '1.15.0' testImplementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: "${log4j_version}" } diff --git a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/ControllingItemTest.java b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/ControllingItemTest.java index 81a71fb6a5e13e96e96d1a18ee71e6ac27237d42..b25642533f1bd893fffc0605f8080fddfcdbbf29 100644 --- a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/ControllingItemTest.java +++ b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/ControllingItemTest.java @@ -1,9 +1,8 @@ package de.tudresden.inf.st.eraser; +import de.tudresden.inf.st.eraser.jastadd.model.*; import de.tudresden.inf.st.eraser.util.TestUtils; import de.tudresden.inf.st.eraser.util.TestUtils.ModelAndItem; -import de.tudresden.inf.st.eraser.jastadd.model.*; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.util.function.Consumer; diff --git a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/ExpressionEvalTest.java b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/ExpressionEvalTest.java index c602b5f4d3761714b5ba23e4aadfece2a21bfd7a..7b14c73467a69294104a382618b0b752207a6dae 100644 --- a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/ExpressionEvalTest.java +++ b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/ExpressionEvalTest.java @@ -30,7 +30,7 @@ public class ExpressionEvalTest { } @Test - public void multExpression() throws IOException, Parser.Exception { + public void mulExpression() throws IOException, Parser.Exception { NumberExpression sut = ParserUtils.parseNumberExpression("(3 * 4)"); assertEquals(12.0, sut.eval()); } diff --git a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/InfluxTest.java b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/InfluxTest.java index acbd2f541d554161e1185fc0c1a9e2c271d868aa..8d9d5a4c50382b63b0e43324dad3968219903f63 100644 --- a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/InfluxTest.java +++ b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/InfluxTest.java @@ -3,10 +3,16 @@ package de.tudresden.inf.st.eraser; import de.tudresden.inf.st.eraser.jastadd.model.*; import de.tudresden.inf.st.eraser.util.TestUtils; import de.tudresden.inf.st.eraser.util.TestUtils.ModelAndItem; -import org.junit.jupiter.api.*; +import org.junit.ClassRule; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import org.testcontainers.containers.InfluxDBContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; import java.time.Instant; import java.util.ArrayList; @@ -23,27 +29,31 @@ import static org.junit.jupiter.api.Assumptions.assumeTrue; * * @author rschoene - Initial contribution */ -//@RunWith(Parameterized.class) -@Disabled("Parameterized test still to be ported to junit5") +@Testcontainers public class InfluxTest { private static final double DELTA = 0.001; - private List<DoubleStatePoint> points = new ArrayList<>(); + private final List<DoubleStatePoint> points = new ArrayList<>(); private static final double firstState = 2.0; private static final double secondState = 4.0; private static final double thirdState = 3.0; private ModelAndItem mai; -// @ClassRule - public static InfluxDBContainer influxDbContainer = new InfluxDBContainer() - .withDatabase(InfluxRoot.createDefault().getDbName()) - .withAdmin(InfluxRoot.createDefault().getUser()) - .withAdminPassword(InfluxRoot.createDefault().getPassword()); + @Container + private static final InfluxDBContainer<?> influxDbContainer = + new InfluxDBContainer<>( + DockerImageName + .parse("influxdb") + .withTag("1.8.3")) + .withDatabase(InfluxRoot.createDefault().getDbName()) + .withAdmin(InfluxRoot.createDefault().getUser()) + .withAdminPassword(InfluxRoot.createDefault().getPassword()); @ParameterizedTest @ValueSource(booleans = {true, false}) public void oneItem(boolean useStub) { + setNewModel(useStub); NumberItem item = mai.item; // set state once item.setState(firstState); @@ -67,6 +77,7 @@ public class InfluxTest { @ParameterizedTest @ValueSource(booleans = {true, false}) public void twoItems(boolean useStub) { + setNewModel(useStub); NumberItem item1 = mai.item; NumberItem item2 = TestUtils.addItemTo(mai.model, 1.0, true); // set state once for first item @@ -110,6 +121,7 @@ public class InfluxTest { @ParameterizedTest @ValueSource(booleans = {true, false}) public void justAdapter(boolean useStub) { + setNewModel(useStub); InfluxAdapter influxAdapter = getInfluxRoot().influxAdapter(); assertTrue(influxAdapter.isConnected(), "Adapter not connected"); influxAdapter.deleteDatabase(); @@ -189,8 +201,8 @@ public class InfluxTest { } } - @BeforeEach - public void setNewModel(boolean useStub) { + private void setNewModel(boolean useStub) { + // BeforeEach does not work with parameterized tests :( mai = createModel(useStub); getInfluxRoot().influxAdapter().disableAsyncQuery(); } @@ -201,18 +213,4 @@ public class InfluxTest { getInfluxRoot().influxAdapter().close(); } } - -// @Parameterized.Parameter -// public String name; -// -// @Parameterized.Parameter(1) -// public boolean useStub; - -// @Parameterized.Parameters(name = "{0}") - public static Iterable<Object[]> getTests() { - return Arrays.asList(new Object[][] { - {"Impl", false}, - {"Stub", true} - }); - } } diff --git a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/ItemTests.java b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/ItemTests.java index f56a3bc6a2065a657c1d384a44a7c59a9db4b285..b7074f22763d928b56a8dc180aa08d5e3464d036 100644 --- a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/ItemTests.java +++ b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/ItemTests.java @@ -3,7 +3,6 @@ package de.tudresden.inf.st.eraser; import de.tudresden.inf.st.eraser.jastadd.model.*; import de.tudresden.inf.st.eraser.util.TestUtils; import de.tudresden.inf.st.eraser.util.TestUtils.ModelAndItem; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import java.time.Instant; diff --git a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/MarshallingTests.java b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/MarshallingTests.java index 2c795bdb37a03f2be183203bc9e21eb63c115945..60bfd0a759a03ce485bc8aa158b2ac8770340d47 100644 --- a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/MarshallingTests.java +++ b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/MarshallingTests.java @@ -1,57 +1,42 @@ package de.tudresden.inf.st.eraser; -import de.tudresden.inf.st.eraser.jastadd_test.core.*; +import de.tudresden.inf.st.eraser.jastadd_test.core.TestConfiguration; +import de.tudresden.inf.st.eraser.jastadd_test.core.TestProperties; +import de.tudresden.inf.st.eraser.jastadd_test.core.TestRunner; +import de.tudresden.inf.st.eraser.jastadd_test.core.Util; import de.tudresden.inf.st.eraser.parser.EraserParserHelper; import de.tudresden.inf.st.eraser.util.ParserUtils; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.List; /** * Testing round-trip parsing-printing-comparing. * * @author rschoene - Initial contribution */ -//@RunWith(ParallelParameterized.class) -@RunWith(Parameterized.class) public class MarshallingTests { - private static final TestProperties properties = new TestProperties(); - static { - properties.put("jastadd3", "false"); - properties.put("options", "indent=tab"); - properties.setTestRoot("src/test/resources/tests"); -// properties.exclude(Tests.FAILING); -// properties.exclude(Tests.UNSTABLE); - } - - private final TestConfiguration unitTest; - - @BeforeClass - public static void setupParser() { - EraserParserHelper.setCheckUnusedElements(false); - ParserUtils.setVerboseLoading(false); - } - - /** - * Construct a new JastAdd test - * @param unitTest The test to run. - */ - public MarshallingTests(TestConfiguration unitTest) { - this.unitTest = unitTest; - } - /** * Run the JastAdd test + * @param unitTest The test to run. */ - @Test - public void runTest() throws Exception { - TestRunner.runTest(unitTest, properties); + @ParameterizedTest + @MethodSource("getTests") + public void runTest(TestConfiguration unitTest) throws Exception { + EraserParserHelper.setCheckUnusedElements(false); + ParserUtils.setVerboseLoading(false); + TestRunner.runTest(unitTest); } - @Parameterized.Parameters(name = "{0}") - public static Iterable<Object[]> getTests() { + public static List<TestConfiguration> getTests() { + TestProperties properties = new TestProperties(); + properties.put("jastadd3", "false"); + properties.put("options", "indent=tab"); + properties.setTestRoot("src/test/resources/tests"); +// properties.exclude(Tests.FAILING); +// properties.exclude(Tests.UNSTABLE); return Util.getTests(properties); } diff --git a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/MqttTests.java b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/MqttTests.java index c58d70e71379deecad67710468412b05b52e558f..cca050f08b6065e208ccd9c6732c03eae251724b 100644 --- a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/MqttTests.java +++ b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/MqttTests.java @@ -5,13 +5,12 @@ import de.tudresden.inf.st.eraser.util.MqttReceiver; import de.tudresden.inf.st.eraser.util.TestUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Assumptions; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; import java.io.IOException; import java.util.ArrayList; @@ -21,7 +20,6 @@ import java.util.concurrent.TimeUnit; import java.util.function.Supplier; import static org.assertj.core.api.Assertions.assertThat; -import static org.hamcrest.core.IsCollectionContaining.hasItem; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; @@ -31,8 +29,7 @@ import static org.junit.jupiter.api.Assumptions.assumeTrue; * * @author rschoene - Initial contribution */ -//@RunWith(Parameterized.class) -@Disabled("Parameterized test still to be ported to junit5") +@Testcontainers public class MqttTests { private static final String outgoingPrefix = "out"; @@ -43,11 +40,11 @@ public class MqttTests { private static final double secondState = 2.0; private static final double thirdState = 3.0; - private List<String> messages = new ArrayList<>(); - private static Logger logger = LogManager.getLogger(MqttTests.class); + private final List<String> messages = new ArrayList<>(); + private static final Logger logger = LogManager.getLogger(MqttTests.class); -// @ClassRule - public static GenericContainer mqttBroker = new GenericContainer<>("eclipse-mosquitto:1.5") + @Container + public static GenericContainer<?> mqttBroker = new GenericContainer<>("eclipse-mosquitto:1.5") .withExposedPorts(1883); @ParameterizedTest @@ -154,6 +151,7 @@ public class MqttTests { private void createMqttReceiver(boolean useStub, String... expectedTopics) throws IOException { if (useStub) { // do not need receiver, as messages are directly written by MqttSenderStub and callback + messages.clear(); return; } MqttReceiver receiver = new MqttReceiver(); @@ -219,6 +217,8 @@ public class MqttTests { } long targetEndTime = System.nanoTime() + TimeUnit.SECONDS.toNanos(seconds); while (System.nanoTime() < targetEndTime) { + // this is indeed busy waiting in favour of new dependencies handling it + //noinspection BusyWait Thread.sleep(100); if (expected == actualProvider.get()) { break; diff --git a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/NeuralNetworkTest.java b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/NeuralNetworkTest.java index 517cdd3fac64a612d7946d2d6f7a6a6a0f12c004..185f80749afc7061bf37030b556edd02a957d23a 100644 --- a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/NeuralNetworkTest.java +++ b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/NeuralNetworkTest.java @@ -3,7 +3,6 @@ package de.tudresden.inf.st.eraser; import de.tudresden.inf.st.eraser.jastadd.model.*; import de.tudresden.inf.st.eraser.util.TestUtils; import de.tudresden.inf.st.eraser.util.TestUtils.ModelAndItem; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/OpenHabImporterTest.java b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/OpenHabImporterTest.java index 926e92c6a35190ae8da71953a209aaf6a20ceb93..c6ebe6ce0e35e17fc157bd25ba5c9d2289635c71 100644 --- a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/OpenHabImporterTest.java +++ b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/OpenHabImporterTest.java @@ -1,20 +1,16 @@ package de.tudresden.inf.st.eraser; import de.tudresden.inf.st.eraser.jastadd.model.SmartHomeEntityModel; -import de.tudresden.inf.st.eraser.jastadd.model.Root; import de.tudresden.inf.st.eraser.jastadd_test.core.*; import de.tudresden.inf.st.eraser.openhab2.OpenHab2Importer; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; -import java.io.File; -import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; -import java.util.Properties; +import java.util.List; import static org.junit.Assert.fail; @@ -23,13 +19,12 @@ import static org.junit.Assert.fail; * * @author rschoene - Initial contribution */ -@RunWith(ParallelParameterized.class) public class OpenHabImporterTest { private static final String HOST = "localhost"; static class OpenHab2ImporterFromFile extends OpenHab2Importer { - private String directory; + private final String directory; OpenHab2ImporterFromFile(String directory) { this.directory = directory; @@ -63,7 +58,7 @@ public class OpenHabImporterTest { } static class OpenHabImportRunner extends TestRunner { - public static void runTest(TestConfiguration config, Properties jastaddProperties) { + public static void runTest(TestConfiguration config) { Result expected = config.getExpected(); @@ -93,34 +88,22 @@ public class OpenHabImporterTest { } } - private static final TestProperties properties = new TestProperties(); - static { - properties.put("jastadd3", "false"); - properties.setTestRoot("src/test/resources/openhabtest"); -// properties.exclude(Tests.FAILING); -// properties.exclude(Tests.UNSTABLE); - } - - private final TestConfiguration unitTest; - - /** - * Construct a new OpenHabImporterTest - * @param unitTest The test to run. - */ - public OpenHabImporterTest(TestConfiguration unitTest) { - this.unitTest = unitTest; - } - /** * Run the OpenHabImporterTest + * @param unitTest The test to run. */ - @Test - public void runTest() { - OpenHabImportRunner.runTest(unitTest, properties); + @ParameterizedTest + @MethodSource("getTests") + public void runTest(TestConfiguration unitTest) { + OpenHabImportRunner.runTest(unitTest); } - @Parameterized.Parameters(name = "{0}") - public static Iterable<Object[]> getTests() { + public static List<TestConfiguration> getTests() { + TestProperties properties = new TestProperties(); + properties.put("jastadd3", "false"); + properties.setTestRoot("src/test/resources/openhabtest"); +// properties.exclude(Tests.FAILING); +// properties.exclude(Tests.UNSTABLE); return Util.getTests(properties); } } diff --git a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/RulesTest.java b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/RulesTest.java index 28058d1149406248267307edacc043b46e765717..1c5289208cee3c5f4ca1f07f55d0cbc5d9d52ac3 100644 --- a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/RulesTest.java +++ b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/RulesTest.java @@ -27,7 +27,7 @@ public class RulesTest { private static final double DELTA = 0.01d; - class CountingAction extends NoopAction { + static class CountingAction extends NoopAction { final Map<Item, AtomicInteger> counters = new HashMap<>(); CountingAction() { @@ -334,7 +334,7 @@ public class RulesTest { assertEquals(5, item2.asItemWithDoubleState().getState(), DELTA, "Change of item state should set the state of the affected item"); } - class ValuedStateProvider implements NewStateProvider { + static class ValuedStateProvider implements NewStateProvider { int value; @Override public String get() { diff --git a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/jastadd_test/core/ParallelParameterized.java b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/jastadd_test/core/ParallelParameterized.java deleted file mode 100644 index 2d335313a0115e22b18968af59ebfdc9a07407ff..0000000000000000000000000000000000000000 --- a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/jastadd_test/core/ParallelParameterized.java +++ /dev/null @@ -1,78 +0,0 @@ -/* Copyright (c) 2005-2015, The JastAdd Team - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its - * contributors may be used to endorse or promote products derived from this - * software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -package de.tudresden.inf.st.eraser.jastadd_test.core; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; - -import org.junit.runners.Parameterized; -import org.junit.runners.model.RunnerScheduler; - -/** - * Runs parameterized tests in parallel - */ -public class ParallelParameterized extends Parameterized { - - private static final int NUM_THREADS = 16; - - /** - * Constructor - * @param klass - * @throws Throwable - */ - public ParallelParameterized(Class<?> klass) throws Throwable { - super(klass); - setScheduler(new ThreadPoolScheduler()); - } - - private static class ThreadPoolScheduler implements RunnerScheduler { - private final ExecutorService executor; - - public ThreadPoolScheduler() { - executor = Executors.newFixedThreadPool(NUM_THREADS); - } - - @Override - public void finished() { - executor.shutdown(); - try { - executor.awaitTermination(10, TimeUnit.MINUTES); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - - @Override - public void schedule(Runnable test) { - executor.submit(test); - } - } -} diff --git a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/jastadd_test/core/TestRunner.java b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/jastadd_test/core/TestRunner.java index 1b4ba7616039ba915162919f5717a593a44972f6..c7ade8111b0f140bd3a1413f9783bef41839eba8 100644 --- a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/jastadd_test/core/TestRunner.java +++ b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/jastadd_test/core/TestRunner.java @@ -30,18 +30,16 @@ package de.tudresden.inf.st.eraser.jastadd_test.core; import beaver.Parser; -import de.tudresden.inf.st.eraser.jastadd.model.SmartHomeEntityModel; import de.tudresden.inf.st.eraser.jastadd.model.Root; import de.tudresden.inf.st.eraser.util.ParserUtils; import java.io.File; import java.io.IOException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.util.Properties; -import static org.junit.Assert.fail; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; /** * Utility methods for running JastAdd unit tests. @@ -50,7 +48,7 @@ import static org.junit.Assert.assertEquals; public class TestRunner { private static int TEST_TIMEOUT = 5000; - private static String SYS_LINE_SEP = System.getProperty("line.separator"); + private static final String SYS_LINE_SEP = System.getProperty("line.separator"); static { // Set up test timeout. @@ -60,9 +58,8 @@ public class TestRunner { /** * Run test with given JastAdd configuration. * @param config test case specific configuration - * @param jastaddProperties global test configuration */ - public static void runTest(TestConfiguration config, Properties jastaddProperties) + public static void runTest(TestConfiguration config) throws Exception { Result expected = config.expected; @@ -129,9 +126,10 @@ public class TestRunner { try { File expected = expectedJastAddErrorOutput(testDir); File actual = new File(tmpDir, "jastadd.err"); - assertEquals("Error output files differ", + assertEquals( readFileToString(expected), - readFileToString(actual)); + readFileToString(actual), + "Error output files differ"); } catch (IOException e) { fail("IOException occurred while comparing JastAdd error output: " + e.getMessage()); } @@ -167,7 +165,7 @@ public class TestRunner { fail("Missing file: " + expectedFileLocation); } String expected = readFileToString(expectedOutput); - assertEquals("Output differs!", expected, actual); + assertEquals(expected, actual, "Output differs!"); } catch (IOException e) { fail("IOException occurred while comparing output: " + e.getMessage()); } @@ -189,7 +187,7 @@ public class TestRunner { if (!file.isFile()) { return ""; } - String content = new String(Files.readAllBytes(file.toPath()), Charset.forName("UTF-8")); + String content = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8); return normalizeText(content); } @@ -197,7 +195,7 @@ public class TestRunner { * Normalize line endings and replace back-slashes with slashes. * This is used to avoid insignificant platform differences from * altering test results. - * @param text + * @param text text to normalize * @return normalized text string */ private static String normalizeText(String text) { diff --git a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/jastadd_test/core/Util.java b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/jastadd_test/core/Util.java index 117b8d75546692f311b44cb59c76d0b70efcc7a1..019e9da2b3c94da40e8c7b5b52c4bc16c755de3b 100644 --- a/eraser-base/src/test/java/de/tudresden/inf/st/eraser/jastadd_test/core/Util.java +++ b/eraser-base/src/test/java/de/tudresden/inf/st/eraser/jastadd_test/core/Util.java @@ -47,7 +47,7 @@ public class Util { * @param tests * @param excludes */ - private static void addChildTestDirs(File testRoot, Collection<Object[]> tests, + private static void addChildTestDirs(File testRoot, List<TestConfiguration> tests, Collection<String> excludes) { if (testRoot == null) { return; @@ -63,7 +63,7 @@ public class Util { } private static void addTestDir(String testRoot, File dir, - Collection<Object[]> tests, Collection<String> excludes) { + List<TestConfiguration> tests, Collection<String> excludes) { if (dir.isDirectory()) { String path = dir.getPath().replace(File.separatorChar, '/'); @@ -89,23 +89,23 @@ public class Util { /** * Add separate test for each option set. */ - private static void addUnitTests(String testRoot, Collection<Object[]> tests, String path, File dir) { + private static void addUnitTests(String testRoot, List<TestConfiguration> tests, String path, File dir) { Properties testProperties = Util.getTestProperties(dir); String optionsProperty = testProperties.getProperty("options", ""); String extraOptions = testProperties.getProperty("extraoptions", ""); - String options[] = optionsProperty.split("\\|", -1); + String[] options = optionsProperty.split("\\|", -1); int index = 1; for (String option : options) { TestOptions u = new TestOptions((option + " " + extraOptions).trim(), options.length > 1, index); TestConfiguration config = new TestConfiguration(testRoot, path, u); - tests.add(new Object[] { config }); + tests.add(config); index++; } } private static void addByPattern(File root, String pattern, - Collection<Object[]> tests, Collection<String> excludes) { + List<TestConfiguration> tests, Collection<String> excludes) { if (pattern.isEmpty()) { addTestDir(root.getPath(), root, tests, excludes); } else { @@ -116,7 +116,7 @@ public class Util { rest = ""; } else { part = pattern.substring(0, index); - rest = pattern.substring(index+1, pattern.length()); + rest = pattern.substring(index + 1); } if (part.indexOf('*') == -1) { addByPattern(new File(root, part), rest, tests, excludes); @@ -168,8 +168,8 @@ public class Util { * @param properties * @return A collection of String arrays containing the test directories */ - public static Iterable<Object[]> getTests(TestProperties properties) { - List<Object[]> tests = new LinkedList<>(); + public static List<TestConfiguration> getTests(TestProperties properties) { + List<TestConfiguration> tests = new ArrayList<>(); Collection<String> includes = properties.includes(); Collection<String> excludes = properties.excludes(); @@ -182,12 +182,12 @@ public class Util { } } - for (Object[] test : tests) { - ((TestConfiguration) test[0]).addOptions(properties.getProperty("extraoptions", "")); + for (TestConfiguration test : tests) { + test.addOptions(properties.getProperty("extraoptions", "")); } // Sort the tests lexicographically. - tests.sort(Comparator.comparing(a -> a[0].toString())); + tests.sort(Comparator.comparing(TestConfiguration::toString)); return tests; } @@ -201,7 +201,6 @@ public class Util { FileInputStream in = new FileInputStream(propertiesFile); properties.load(in); in.close(); - } catch (FileNotFoundException e) { } catch (IOException e) { e.printStackTrace(); } @@ -231,7 +230,6 @@ public class Util { break; } } - } catch (FileNotFoundException e) { } catch (IOException e) { e.printStackTrace(); }