diff --git a/verify_output.py b/_verify_output.py similarity index 100% rename from verify_output.py rename to _verify_output.py diff --git a/build.gradle b/build.gradle index 699c25f2..671310d1 100644 --- a/build.gradle +++ b/build.gradle @@ -126,9 +126,6 @@ class Tags { } ext { - junit4Version = '4.12' - junitVintageVersion = '4.12.0-M2' - junitPlatformVersion = '1.0.0-M2' junitJupiterVersion = '5.0.0-M2' } @@ -148,26 +145,16 @@ subprojects { } dependencies { - // compile 'junit:junit:4.12' + // Logging: compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.+' compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.+' // You can also use the JDK's built-in logging as the back end: // compile group: 'org.slf4j', name: 'slf4j-jdk14', version: '1.7.5' - // JUnit Jupiter API and TestEngine implementation -/* testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}") - testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}") - - // If you also want to support JUnit 3 and JUnit 4 tests - testCompile("junit:junit:${junit4Version}") - testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}")*/ - compile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2" - testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2" - testRuntime "org.junit.jupiter:junit-jupiter-engine:5.0.0-M2" -/* testCompile "junit:junit:4.12" - testRuntime "org.junit.vintage:junit-vintage-engine:4.12.0-M2" -*//* compile "org.junit.jupiter:junit-jupiter-api:5.0.0-M2" - compile "org.junit.vintage:junit-vintage-engine:4.12.0-M2"*/ + // JUnit testing: + compile "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}" + testCompile "org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}" + testRuntime "org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}" } junitPlatform { @@ -191,11 +178,8 @@ subprojects { main { java { srcDir projectDir - // exclude "*Test.java" - exclude "*Tests.java" - exclude "*JUnit.java" - exclude "StringInverter*.java" - // exclude "Queue.java" + // Remove this when it's working: + exclude "DynamicStringInverterTests.java" } resources { srcDir projectDir @@ -234,7 +218,7 @@ subprojects { // Exclude java sources that will not compile if (tags.compileTimeError) { - println ">>> Excluding " + file.name + // println ">>> Excluding " + file.name sourceSets.main.java.excludes.add(file.name) } else { JavaExec javaTask = null @@ -362,9 +346,6 @@ configure(subprojects - project(':onjava')) { compile group: 'com.google.guava', name: 'guava', version: '19.0' compile "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}" compile 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2' - - //compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2' - //compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2' } } diff --git a/verifying/CountedListTest.java b/verifying/CountedListTest.java index 8ed45d52..76d20cc1 100644 --- a/verifying/CountedListTest.java +++ b/verifying/CountedListTest.java @@ -5,7 +5,6 @@ // Simple use of JUnit to test CountedList. package verifying; import java.util.*; -//import verifying.CountedList; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; @@ -43,8 +42,8 @@ public class CountedListTest { // as it isn't annotated with @Test, it will not // be automatically executed by JUnit. private - void compare(ArrayList lst, String[] strs) { - String[] array = (String[])lst.toArray(); + void compare(List lst, String[] strs) { + String[] array = lst.toArray(new String[0]); assertTrue(array.length == strs.length, "Arrays not the same length"); for(int i = 0; i < array.length; i++) @@ -72,11 +71,6 @@ public class CountedListTest { compare(list, new String[] { "0", "1", "2", "An", "African", "Swallow" }); } -/* public static void main(String[] args) { - // Invoke JUnit on the class: - org.junit.runner.JUnitCore.runClasses( - SimpleJUnit.class); - } */ } /* Output: CountedList #0 diff --git a/verifying/DynamicStringInverterTests.java b/verifying/DynamicStringInverterTests.java new file mode 100644 index 00000000..4ac6a36f --- /dev/null +++ b/verifying/DynamicStringInverterTests.java @@ -0,0 +1,80 @@ +// verifying/DynamicStringInverterTests.java +// (c)2016 MindView LLC: see Copyright.txt +// We make no guarantees that this code is fit for any purpose. +// Visit http://mindviewinc.com/Books/OnJava/ for more book information. +package verifying; +import java.util.*; +import java.util.function.*; +import java.util.stream.*; +import org.junit.jupiter.api.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.DynamicTest.*; + +class DynamicStringInverterTests { + Stream + multiple_version_test(Consumer test) { + List versions = Arrays.asList( + new StringInverter1(), new StringInverter2(), + new StringInverter3(), new StringInverter4()); + return DynamicTest.stream(versions.iterator(), + (version) -> version.getClass().getSimpleName(), + test + ); + } + + @TestFactory + Stream basicInversion_Succeed() { + return multiple_version_test( (version) -> { + String in = "Exit, Pursued by a Bear."; + String out = "eXIT, pURSUED BY A bEAR."; + assertEquals(version.invert(in), out); + }); + } + + @TestFactory + Stream basicInversion_Fail() { + return multiple_version_test( (version) -> { + expectThrows(RuntimeException.class, () -> { + assertEquals(version.invert("X"), "X"); + }); + }); + } + + @TestFactory + Stream allowedCharacters_Fail() { + return multiple_version_test( (version) -> { + expectThrows(RuntimeException.class, () -> { + version.invert(";-_()*&^%$#@!~`"); + version.invert("0123456789"); + }); + }); + } + + @TestFactory + Stream allowedCharacters_Succeed() { + return multiple_version_test( (version) -> { + version.invert("abcdefghijklmnopqrstuvwxyz ,."); + version.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,."); + }); + } + + @TestFactory + Stream lengthLessThan26_Fail() { + return multiple_version_test( (version) -> { + String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; + assertTrue(str.length() > 25); + expectThrows(RuntimeException.class, () -> { + version.invert(str); + }); + }); + } + + @TestFactory + Stream lengthLessThan26_Succeed() { + return multiple_version_test( (version) -> { + String str = "xxxxxxxxxxxxxxxxxxxxxxxxx"; + assertTrue(str.length() < 26); + version.invert(str); + }); + } +} diff --git a/verifying/FirstJUnit5Tests.java b/verifying/FirstJUnit5Tests.java index c4ced5d9..486661c8 100644 --- a/verifying/FirstJUnit5Tests.java +++ b/verifying/FirstJUnit5Tests.java @@ -7,8 +7,9 @@ import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; class FirstJUnit5Tests { - @Test - void myFirstTest() { - assertEquals(2, 1 + 1); - } + @Test + void myFirstTest() { + System.out.println("FirstJUnit5Tests"); + assertEquals(2, 1 + 1); + } } diff --git a/verifying/StringInverter.java b/verifying/StringInverter.java index f3096286..2ca5189d 100644 --- a/verifying/StringInverter.java +++ b/verifying/StringInverter.java @@ -2,6 +2,7 @@ // (c)2016 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://mindviewinc.com/Books/OnJava/ for more book information. +package verifying; interface StringInverter { public String invert(String str); diff --git a/verifying/StringInverter1.java b/verifying/StringInverter1.java index b89f0d45..91b0567c 100644 --- a/verifying/StringInverter1.java +++ b/verifying/StringInverter1.java @@ -2,7 +2,7 @@ // (c)2016 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://mindviewinc.com/Books/OnJava/ for more book information. -// {java StringInverterTest StringInverter1} +package verifying; public class StringInverter1 implements StringInverter { public String invert(String str) { return ""; } diff --git a/verifying/StringInverter2.java b/verifying/StringInverter2.java index 2799f31c..80a27b06 100644 --- a/verifying/StringInverter2.java +++ b/verifying/StringInverter2.java @@ -2,7 +2,7 @@ // (c)2016 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://mindviewinc.com/Books/OnJava/ for more book information. -// {java StringInverterTest StringInverter2} +package verifying; import static java.lang.Character.*; public class StringInverter2 implements StringInverter { diff --git a/verifying/StringInverter3.java b/verifying/StringInverter3.java index d907e84e..d445a87f 100644 --- a/verifying/StringInverter3.java +++ b/verifying/StringInverter3.java @@ -2,7 +2,7 @@ // (c)2016 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://mindviewinc.com/Books/OnJava/ for more book information. -// {java StringInverterTest StringInverter3} +package verifying; import static java.lang.Character.*; public class StringInverter3 implements StringInverter { diff --git a/verifying/StringInverter4.java b/verifying/StringInverter4.java index 21616f2c..9012eaad 100644 --- a/verifying/StringInverter4.java +++ b/verifying/StringInverter4.java @@ -2,7 +2,7 @@ // (c)2016 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://mindviewinc.com/Books/OnJava/ for more book information. -// {java StringInverterTest StringInverter4} +package verifying; import static java.lang.Character.*; public class StringInverter4 implements StringInverter { diff --git a/verifying/StringInverterTest.java b/verifying/StringInverterTest.java deleted file mode 100644 index 7910cb9f..00000000 --- a/verifying/StringInverterTest.java +++ /dev/null @@ -1,69 +0,0 @@ -// verifying/StringInverterTest.java -// (c)2016 MindView LLC: see Copyright.txt -// We make no guarantees that this code is fit for any purpose. -// Visit http://mindviewinc.com/Books/OnJava/ for more book information. -// {ValidateByHand} // Don't run by itself -import java.util.*; -import org.junit.jupiter.api.*; -import static org.junit.jupiter.api.Assertions.*; -// import org.junit.platform.runner.JUnitPlatform; - -public class StringInverterTest { - static StringInverter inverter; - @Test - public final void basicInversion_Succeed() { - String in = "Exit, Pursued by a Bear."; - String out = "eXIT, pURSUED BY A bEAR."; - assertEquals(inverter.invert(in), out); - } - @Test - public final void basicInversion_Fail() { - expectThrows(RuntimeException.class, () -> { - assertEquals(inverter.invert("X"), "X"); - }); - } - @Test - public final void allowedCharacters_Fail() { - expectThrows(RuntimeException.class, () -> { - inverter.invert(";-_()*&^%$#@!~`"); - inverter.invert("0123456789"); - }); - } - @Test - public final void allowedCharacters_Succeed() { - inverter.invert("abcdefghijklmnopqrstuvwxyz ,."); - inverter.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,."); - } - @Test - public final void lengthLessThan26_Fail() { - String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; - assertTrue(str.length() > 25); - expectThrows(RuntimeException.class, () -> { - inverter.invert(str); - }); - } - @Test - public final void lengthLessThan26_Succeed() { - String str = "xxxxxxxxxxxxxxxxxxxxxxxxx"; - assertTrue(str.length() < 26); - inverter.invert(str); - } - /* - public static void main(String[] args) throws Exception{ - assertEquals(args.length, 1); - inverter = (StringInverter) - Class.forName(args[0]).newInstance(); - Result result = org.junit.runner.JUnitCore.runClasses( - StringInverterTest.class); - List failures = result.getFailures(); - System.out.printf("%s has %d FAILURES:\n", - args[0], failures.size()); - int count = 1; - for(Failure f : failures) { - System.out.printf("Failure %d:\n", count++); - System.out.println(f.getDescription()); - System.out.println(f.getMessage()); - } - } - */ -}