From 33a529c41765d356cd704b788acf0bbc9c7f6413 Mon Sep 17 00:00:00 2001 From: Bruce Eckel Date: Mon, 29 Aug 2016 07:27:53 -0600 Subject: [PATCH] Chapter name and order change --- build.gradle | 4 +- network/SimpleClient.java | 7 +- network/SimpleServer.java | 4 +- {verifying => validating}/Assert1.java | 2 +- {verifying => validating}/Assert2.java | 2 +- .../BadMicroBenchmark.java | 2 +- .../BadMicroBenchmark2.java | 2 +- .../BadMicroBenchmark3.java | 2 +- {verifying => validating}/CountedList.java | 4 +- .../CountedListTest.java | 4 +- validating/DynamicStringInverterTests.java | 121 +++++++++++++++++ .../FirstJUnit5Tests.java | 4 +- .../GuavaPreconditions.java | 2 +- .../Inverter1.java | 8 +- .../Inverter2.java | 6 +- .../Inverter3.java | 6 +- .../Inverter4.java | 6 +- .../LoaderAssertions.java | 2 +- {verifying => validating}/Queue.java | 4 +- {verifying => validating}/QueueException.java | 4 +- {verifying => validating}/QueueTest.java | 6 +- {verifying => validating}/SLF4JLevels.java | 2 +- {verifying => validating}/SLF4JLogging.java | 2 +- .../SimpleDebugging.java | 2 +- {verifying => validating}/StringInverter.java | 4 +- .../StringInverterTests.java | 39 ++++-- {verifying => validating}/Time.java | 2 +- .../jmh/ParallelSetAll.java | 4 +- {verifying => validating}/logback.xml | 0 verifying/DynamicStringInverterTests.java | 126 ------------------ 30 files changed, 195 insertions(+), 188 deletions(-) rename {verifying => validating}/Assert1.java (94%) rename {verifying => validating}/Assert2.java (95%) rename {verifying => validating}/BadMicroBenchmark.java (93%) rename {verifying => validating}/BadMicroBenchmark2.java (94%) rename {verifying => validating}/BadMicroBenchmark3.java (96%) rename {verifying => validating}/CountedList.java (90%) rename {verifying => validating}/CountedListTest.java (97%) create mode 100644 validating/DynamicStringInverterTests.java rename {verifying => validating}/FirstJUnit5Tests.java (87%) rename {verifying => validating}/GuavaPreconditions.java (90%) rename verifying/StringInverter1.java => validating/Inverter1.java (53%) rename verifying/StringInverter2.java => validating/Inverter2.java (81%) rename verifying/StringInverter3.java => validating/Inverter3.java (83%) rename verifying/StringInverter4.java => validating/Inverter4.java (87%) rename {verifying => validating}/LoaderAssertions.java (95%) rename {verifying => validating}/Queue.java (98%) rename {verifying => validating}/QueueException.java (84%) rename {verifying => validating}/QueueTest.java (97%) rename {verifying => validating}/SLF4JLevels.java (96%) rename {verifying => validating}/SLF4JLogging.java (93%) rename {verifying => validating}/SimpleDebugging.java (96%) rename {verifying => validating}/StringInverter.java (82%) rename {verifying => validating}/StringInverterTests.java (58%) rename {verifying => validating}/Time.java (95%) rename {verifying => validating}/jmh/ParallelSetAll.java (89%) rename {verifying => validating}/logback.xml (100%) delete mode 100644 verifying/DynamicStringInverterTests.java diff --git a/build.gradle b/build.gradle index 7a421ec6..162354dc 100644 --- a/build.gradle +++ b/build.gradle @@ -277,9 +277,9 @@ subprojects { task run(dependsOn: createdTasks) } -project(':verifying') { +project(':validating') { jmh { - include = 'verifying.jmh.*' + include = 'validating.jmh.*' } } diff --git a/network/SimpleClient.java b/network/SimpleClient.java index ae6abd15..4fc4eed4 100644 --- a/network/SimpleClient.java +++ b/network/SimpleClient.java @@ -11,9 +11,8 @@ import java.io.*; public class SimpleClient { public static void main(String[] args) throws IOException { - // Passing null to getByName() produces the - // special "Local Loopback" IP address, for - // testing on one machine w/o a network: + // Produce the "Local Loopback" IP address + // for testing on one machine w/o a network: InetAddress addr = InetAddress.getLoopbackAddress(); System.out.println("addr = " + addr); @@ -35,7 +34,7 @@ public class SimpleClient { new OutputStreamWriter( socket.getOutputStream())),true); for(int i = 0; i < 10; i ++) { - out.println("howdy " + i); + out.println("hello " + i); String str = in.readLine(); System.out.println(str); } diff --git a/network/SimpleServer.java b/network/SimpleServer.java index 9295fd49..b4ce86f3 100644 --- a/network/SimpleServer.java +++ b/network/SimpleServer.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. -// Just echoes whatever the client sends +// Echoes what the client sends // {ValidateByHand} import java.io.*; import java.net.*; @@ -30,7 +30,7 @@ public class SimpleServer { new PrintWriter( new BufferedWriter( new OutputStreamWriter( - socket.getOutputStream())),true); + socket.getOutputStream())), true); while (true) { String str = in.readLine(); if(str.equals("END")) break; diff --git a/verifying/Assert1.java b/validating/Assert1.java similarity index 94% rename from verifying/Assert1.java rename to validating/Assert1.java index 3ccef65e..ea6d8277 100644 --- a/verifying/Assert1.java +++ b/validating/Assert1.java @@ -1,4 +1,4 @@ -// verifying/Assert1.java +// validating/Assert1.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. diff --git a/verifying/Assert2.java b/validating/Assert2.java similarity index 95% rename from verifying/Assert2.java rename to validating/Assert2.java index e8475ea2..64e72e59 100644 --- a/verifying/Assert2.java +++ b/validating/Assert2.java @@ -1,4 +1,4 @@ -// verifying/Assert2.java +// validating/Assert2.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. diff --git a/verifying/BadMicroBenchmark.java b/validating/BadMicroBenchmark.java similarity index 93% rename from verifying/BadMicroBenchmark.java rename to validating/BadMicroBenchmark.java index be79f3da..2948e417 100644 --- a/verifying/BadMicroBenchmark.java +++ b/validating/BadMicroBenchmark.java @@ -1,4 +1,4 @@ -// verifying/BadMicroBenchmark.java +// validating/BadMicroBenchmark.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. diff --git a/verifying/BadMicroBenchmark2.java b/validating/BadMicroBenchmark2.java similarity index 94% rename from verifying/BadMicroBenchmark2.java rename to validating/BadMicroBenchmark2.java index 5b3121c4..e171f086 100644 --- a/verifying/BadMicroBenchmark2.java +++ b/validating/BadMicroBenchmark2.java @@ -1,4 +1,4 @@ -// verifying/BadMicroBenchmark2.java +// validating/BadMicroBenchmark2.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. diff --git a/verifying/BadMicroBenchmark3.java b/validating/BadMicroBenchmark3.java similarity index 96% rename from verifying/BadMicroBenchmark3.java rename to validating/BadMicroBenchmark3.java index cba94d84..43e47c86 100644 --- a/verifying/BadMicroBenchmark3.java +++ b/validating/BadMicroBenchmark3.java @@ -1,4 +1,4 @@ -// verifying/BadMicroBenchmark3.java +// validating/BadMicroBenchmark3.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. diff --git a/verifying/CountedList.java b/validating/CountedList.java similarity index 90% rename from verifying/CountedList.java rename to validating/CountedList.java index 23b47f67..7cf17cc1 100644 --- a/verifying/CountedList.java +++ b/validating/CountedList.java @@ -1,10 +1,10 @@ -// verifying/CountedList.java +// validating/CountedList.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. // A List that keeps track of how many // of itself are created. -package verifying; +package validating; import java.util.*; public class CountedList extends ArrayList { diff --git a/verifying/CountedListTest.java b/validating/CountedListTest.java similarity index 97% rename from verifying/CountedListTest.java rename to validating/CountedListTest.java index 76d20cc1..f20014bb 100644 --- a/verifying/CountedListTest.java +++ b/validating/CountedListTest.java @@ -1,9 +1,9 @@ -// verifying/CountedListTest.java +// validating/CountedListTest.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. // Simple use of JUnit to test CountedList. -package verifying; +package validating; import java.util.*; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; diff --git a/validating/DynamicStringInverterTests.java b/validating/DynamicStringInverterTests.java new file mode 100644 index 00000000..f095cf51 --- /dev/null +++ b/validating/DynamicStringInverterTests.java @@ -0,0 +1,121 @@ +// validating/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 validating; +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 { + // Combine operations to prevent code duplication: + Stream testVersions(String id, + Function test) { + List versions = Arrays.asList( + new Inverter1(), new Inverter2(), + new Inverter3(), new Inverter4()); + return DynamicTest.stream( + versions.iterator(), + inverter -> inverter.getClass().getSimpleName(), + inverter -> { + System.out.println( + inverter.getClass().getSimpleName() + ": " + id + ); + try { + if(test.apply(inverter) != "fail") + System.out.println("Success"); + } catch(Exception | Error e) { + System.out.println("Exception: " + e.getMessage()); + } + } + ); + } + String isEqual(String lval, String rval) { + if(lval.equals(rval)) + return "success"; + System.out.println("FAIL: " + lval + " != " + rval); + return "fail"; + } + @BeforeAll + static void startMsg() { + System.out.println( + ">>> Starting DynamicStringInverterTests <<<"); + } + @AfterAll + static void endMsg() { + System.out.println( + ">>> Finished DynamicStringInverterTests <<<"); + } + @TestFactory + Stream basicInversion1() { + String in = "Exit, Pursued by a Bear."; + String out = "eXIT, pURSUED BY A bEAR."; + return testVersions( + "Basic inversion (should succeed)", + inverter -> isEqual(inverter.invert(in), out) + ); + } + @TestFactory + Stream basicInversion2() { + return testVersions( + "Basic inversion (should fail)", + inverter -> isEqual(inverter.invert("X"), "X")); + } + @TestFactory + Stream disallowedCharacters() { + String disallowed = ";-_()*&^%$#@!~`0123456789"; + return testVersions( + "Disallowed characters", + inverter -> { + String result = disallowed.chars() + .mapToObj(c -> { + String cc = Character.toString((char)c); + try { + inverter.invert(cc); + return ""; + } catch(RuntimeException e) { + return cc; + } + }).collect(Collectors.joining("")); + if(result.length() == 0) + return "success"; + System.out.println("Bad characters: " + result); + return "fail"; + } + ); + } + @TestFactory + Stream allowedCharacters() { + String lowcase = "abcdefghijklmnopqrstuvwxyz ,."; + String upcase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ,."; + return testVersions( + "Allowed characters (should succeed)", + inverter -> { + assertEquals(inverter.invert(lowcase), upcase); + assertEquals(inverter.invert(upcase), lowcase); + return "success"; + } + ); + } + @TestFactory + Stream lengthNoGreaterThan30() { + String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; + assertTrue(str.length() > 30); + return testVersions( + "Length must be less than 31 (throws exception)", + inverter -> inverter.invert(str) + ); + } + @TestFactory + Stream lengthLessThan31() { + String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; + assertTrue(str.length() < 31); + return testVersions( + "Length must be less than 31 (should succeed)", + inverter -> inverter.invert(str) + ); + } +} diff --git a/verifying/FirstJUnit5Tests.java b/validating/FirstJUnit5Tests.java similarity index 87% rename from verifying/FirstJUnit5Tests.java rename to validating/FirstJUnit5Tests.java index 486661c8..c3f71dfd 100644 --- a/verifying/FirstJUnit5Tests.java +++ b/validating/FirstJUnit5Tests.java @@ -1,8 +1,8 @@ -// verifying/FirstJUnit5Tests.java +// validating/FirstJUnit5Tests.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; +package validating; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; diff --git a/verifying/GuavaPreconditions.java b/validating/GuavaPreconditions.java similarity index 90% rename from verifying/GuavaPreconditions.java rename to validating/GuavaPreconditions.java index 5dabcd21..2b39b1be 100644 --- a/verifying/GuavaPreconditions.java +++ b/validating/GuavaPreconditions.java @@ -1,4 +1,4 @@ -// verifying/GuavaPreconditions.java +// validating/GuavaPreconditions.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. diff --git a/verifying/StringInverter1.java b/validating/Inverter1.java similarity index 53% rename from verifying/StringInverter1.java rename to validating/Inverter1.java index b1d03ecb..0fd6286c 100644 --- a/verifying/StringInverter1.java +++ b/validating/Inverter1.java @@ -1,9 +1,9 @@ -// verifying/StringInverter1.java +// validating/Inverter1.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; +package validating; -public class StringInverter1 implements StringInverter { - public String invert(String str) { return ""; } +public class Inverter1 implements StringInverter { + public String invert(String str) { return str; } } diff --git a/verifying/StringInverter2.java b/validating/Inverter2.java similarity index 81% rename from verifying/StringInverter2.java rename to validating/Inverter2.java index 6f1d515d..6c966e66 100644 --- a/verifying/StringInverter2.java +++ b/validating/Inverter2.java @@ -1,11 +1,11 @@ -// verifying/StringInverter2.java +// validating/Inverter2.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; +package validating; import static java.lang.Character.*; -public class StringInverter2 implements StringInverter { +public class Inverter2 implements StringInverter { public String invert(String str) { String result = ""; for(int i = 0; i < str.length(); i++) { diff --git a/verifying/StringInverter3.java b/validating/Inverter3.java similarity index 83% rename from verifying/StringInverter3.java rename to validating/Inverter3.java index 7d577482..208fff51 100644 --- a/verifying/StringInverter3.java +++ b/validating/Inverter3.java @@ -1,11 +1,11 @@ -// verifying/StringInverter3.java +// validating/Inverter3.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; +package validating; import static java.lang.Character.*; -public class StringInverter3 implements StringInverter { +public class Inverter3 implements StringInverter { public String invert(String str) { if(str.length() > 30) throw new RuntimeException("argument too long!"); diff --git a/verifying/StringInverter4.java b/validating/Inverter4.java similarity index 87% rename from verifying/StringInverter4.java rename to validating/Inverter4.java index 2d6d6cbf..ce52b0bb 100644 --- a/verifying/StringInverter4.java +++ b/validating/Inverter4.java @@ -1,11 +1,11 @@ -// verifying/StringInverter4.java +// validating/Inverter4.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; +package validating; import static java.lang.Character.*; -public class StringInverter4 implements StringInverter { +public class Inverter4 implements StringInverter { static final String allowed = "abcdefghijklmnopqrstuvwxyz ,." + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; diff --git a/verifying/LoaderAssertions.java b/validating/LoaderAssertions.java similarity index 95% rename from verifying/LoaderAssertions.java rename to validating/LoaderAssertions.java index e9414d94..8fe0b377 100644 --- a/verifying/LoaderAssertions.java +++ b/validating/LoaderAssertions.java @@ -1,4 +1,4 @@ -// verifying/LoaderAssertions.java +// validating/LoaderAssertions.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. diff --git a/verifying/Queue.java b/validating/Queue.java similarity index 98% rename from verifying/Queue.java rename to validating/Queue.java index a2ccd694..01b4db18 100644 --- a/verifying/Queue.java +++ b/validating/Queue.java @@ -1,9 +1,9 @@ -// verifying/Queue.java +// validating/Queue.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. // Demonstration of Design by Contract (DbC) -package verifying; +package validating; import java.util.*; public class Queue { diff --git a/verifying/QueueException.java b/validating/QueueException.java similarity index 84% rename from verifying/QueueException.java rename to validating/QueueException.java index 276be650..d381d7c8 100644 --- a/verifying/QueueException.java +++ b/validating/QueueException.java @@ -1,8 +1,8 @@ -// verifying/QueueException.java +// validating/QueueException.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; +package validating; public class QueueException extends RuntimeException { public QueueException(String why) { super(why); } diff --git a/verifying/QueueTest.java b/validating/QueueTest.java similarity index 97% rename from verifying/QueueTest.java rename to validating/QueueTest.java index ba937447..9c5b11cf 100644 --- a/verifying/QueueTest.java +++ b/validating/QueueTest.java @@ -1,9 +1,9 @@ -// verifying/QueueTest.java +// validating/QueueTest.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 verifying.Queue; +package validating; +//import validating.Queue; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; diff --git a/verifying/SLF4JLevels.java b/validating/SLF4JLevels.java similarity index 96% rename from verifying/SLF4JLevels.java rename to validating/SLF4JLevels.java index 8c17ca3b..e1b2e11d 100644 --- a/verifying/SLF4JLevels.java +++ b/validating/SLF4JLevels.java @@ -1,4 +1,4 @@ -// verifying/SLF4JLevels.java +// validating/SLF4JLevels.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. diff --git a/verifying/SLF4JLogging.java b/validating/SLF4JLogging.java similarity index 93% rename from verifying/SLF4JLogging.java rename to validating/SLF4JLogging.java index 423895d0..2d3b5a05 100644 --- a/verifying/SLF4JLogging.java +++ b/validating/SLF4JLogging.java @@ -1,4 +1,4 @@ -// verifying/SLF4JLogging.java +// validating/SLF4JLogging.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. diff --git a/verifying/SimpleDebugging.java b/validating/SimpleDebugging.java similarity index 96% rename from verifying/SimpleDebugging.java rename to validating/SimpleDebugging.java index f5568d8b..70d9437d 100644 --- a/verifying/SimpleDebugging.java +++ b/validating/SimpleDebugging.java @@ -1,4 +1,4 @@ -// verifying/SimpleDebugging.java +// validating/SimpleDebugging.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. diff --git a/verifying/StringInverter.java b/validating/StringInverter.java similarity index 82% rename from verifying/StringInverter.java rename to validating/StringInverter.java index 2ca5189d..dd237377 100644 --- a/verifying/StringInverter.java +++ b/validating/StringInverter.java @@ -1,8 +1,8 @@ -// verifying/StringInverter.java +// validating/StringInverter.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; +package validating; interface StringInverter { public String invert(String str); diff --git a/verifying/StringInverterTests.java b/validating/StringInverterTests.java similarity index 58% rename from verifying/StringInverterTests.java rename to validating/StringInverterTests.java index 7315cfe3..85565d0d 100644 --- a/verifying/StringInverterTests.java +++ b/validating/StringInverterTests.java @@ -1,42 +1,55 @@ -// verifying/StringInverterTests.java +// validating/StringInverterTests.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; +package validating; import java.util.*; +import java.util.stream.*; import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; public class StringInverterTests { - StringInverter inverter = new StringInverter4(); + StringInverter inverter = new Inverter4(); + @BeforeAll + static void startMsg() { + System.out.println(">>> StringInverterTests <<<"); + } @Test - void basicInversion_Succeed() { + void basicInversion1() { String in = "Exit, Pursued by a Bear."; String out = "eXIT, pURSUED BY A bEAR."; assertEquals(inverter.invert(in), out); } @Test - void basicInversion_Fail() { + void basicInversion2() { expectThrows(Error.class, () -> { assertEquals(inverter.invert("X"), "X"); }); } @Test - void allowedCharacters_Fail() { - expectThrows(RuntimeException.class, () -> { - inverter.invert(";-_()*&^%$#@!~`"); - inverter.invert("0123456789"); - }); + void disallowedCharacters() { + String disallowed = ";-_()*&^%$#@!~`0123456789"; + String result = disallowed.chars() + .mapToObj(c -> { + String cc = Character.toString((char)c); + try { + inverter.invert(cc); + return ""; + } catch(RuntimeException e) { + return cc; + } + }).collect(Collectors.joining("")); + assertEquals(result, disallowed); } @Test - void allowedCharacters_Succeed() { + void allowedCharacters() { String lowcase = "abcdefghijklmnopqrstuvwxyz ,."; String upcase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ,."; assertEquals(inverter.invert(lowcase), upcase); assertEquals(inverter.invert(upcase), lowcase); } @Test - void lengthLessThan31_Fail() { + void lengthNoGreaterThan30() { String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; assertTrue(str.length() > 30); expectThrows(RuntimeException.class, () -> { @@ -44,7 +57,7 @@ public class StringInverterTests { }); } @Test - void lengthLessThan31_Succeed() { + void lengthLessThan31() { String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; assertTrue(str.length() < 31); inverter.invert(str); diff --git a/verifying/Time.java b/validating/Time.java similarity index 95% rename from verifying/Time.java rename to validating/Time.java index a5332fc0..6b1a8893 100644 --- a/verifying/Time.java +++ b/validating/Time.java @@ -1,4 +1,4 @@ -// verifying/Time.java +// validating/Time.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. diff --git a/verifying/jmh/ParallelSetAll.java b/validating/jmh/ParallelSetAll.java similarity index 89% rename from verifying/jmh/ParallelSetAll.java rename to validating/jmh/ParallelSetAll.java index c91e3c28..8e7c76f2 100644 --- a/verifying/jmh/ParallelSetAll.java +++ b/validating/jmh/ParallelSetAll.java @@ -1,8 +1,8 @@ -// verifying/jmh/ParallelSetAll.java +// validating/jmh/ParallelSetAll.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.jmh; +package validating.jmh; import java.util.*; import org.openjdk.jmh.annotations.*; diff --git a/verifying/logback.xml b/validating/logback.xml similarity index 100% rename from verifying/logback.xml rename to validating/logback.xml diff --git a/verifying/DynamicStringInverterTests.java b/verifying/DynamicStringInverterTests.java deleted file mode 100644 index 462c440a..00000000 --- a/verifying/DynamicStringInverterTests.java +++ /dev/null @@ -1,126 +0,0 @@ -// 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 { - // Combine operations to prevent code duplication: - Stream - testVersions(String id, Consumer test) { - List versions = Arrays.asList( - new StringInverter1(), new StringInverter2(), - new StringInverter3(), new StringInverter4()); - return DynamicTest.stream(versions.iterator(), - (version) -> version.getClass().getSimpleName(), - (stringInverter) -> { - System.out.println( - stringInverter.getClass().getSimpleName() + - ": " + id - ); - try { // Capture failing tests - test.accept(stringInverter); - } catch(Error | Exception e) { - System.out.println(e.getMessage()); - } - } - ); - } - void isTrue(String description, boolean assertion) { - System.out.print(description + ": "); - System.out.println(assertion); - } - void isEqual(String lval, String rval) { - System.out.print(lval + " equals " + rval); - if(!lval.equals(rval)) - System.out.println(" FAIL"); - else - System.out.println(); - } - @BeforeAll - static void startMsg() { - System.out.println( - ">>> Starting DynamicStringInverterTests <<<"); - } - @AfterAll - static void endMsg() { - System.out.println( - ">>> Finished DynamicStringInverterTests <<<"); - } - @TestFactory - Stream basicInversion_Succeed() { - return testVersions( - "A Basic Inversion that Succeeds", - (version) -> { - String in = "Exit, Pursued by a Bear."; - String out = "eXIT, pURSUED BY A bEAR."; - isEqual(version.invert(in), out); - } - ); - } - @TestFactory - Stream basicInversion_Fail() { - return testVersions( - "A Basic Inversion that Fails", - (version) -> isEqual(version.invert("X"), "X")); - } - @TestFactory - Stream allowedCharacters_Fail() { - return testVersions( - "Disallowed characters (throws exception)", - (version) -> { - try { - version.invert(";-_()*&^%$#@!~`"); - version.invert("0123456789"); - System.out.println("Success"); - } catch(Exception e) { - System.out.println("FAIL: " + e.getMessage()); - } - } - ); - } - @TestFactory - Stream allowedCharacters_Succeed() { - return testVersions( - "Allowed Characters (Succeeds)", - (version) -> { - version.invert("abcdefghijklmnopqrstuvwxyz ,."); - version.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,."); - } - ); - } - @TestFactory - Stream lengthLessThan31_Fail() { - return testVersions( - "Length must be less than 31 (Throws Exception)", - (version) -> { - String str = "xxxxxxxxxxxxxxxxxxxxxxxxxx"; - assertTrue(str.length() > 30); - try { - version.invert(str); - System.out.println("Success"); - } catch(Exception e) { - System.out.println("FAIL: " + e.getMessage()); - } - } - ); - } - @TestFactory - Stream lengthLessThan31_Succeed() { - String str = "xxxxxxxxxxxxxxxxxxxxxxxxx"; - assertTrue(str.length() < 31); - return testVersions( - "Length must be less than 31 (Succeeds)", - (version) -> { - version.invert(str); - System.out.println("Success"); - } - ); - } -}