Chapter name and order change

This commit is contained in:
Bruce Eckel 2016-08-29 07:27:53 -06:00
parent 941213aae8
commit 33a529c417
30 changed files with 195 additions and 188 deletions

View File

@ -277,9 +277,9 @@ subprojects {
task run(dependsOn: createdTasks)
}
project(':verifying') {
project(':validating') {
jmh {
include = 'verifying.jmh.*'
include = 'validating.jmh.*'
}
}

View File

@ -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);
}

View File

@ -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.*;

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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<String> {

View File

@ -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.*;

View File

@ -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<DynamicTest> testVersions(String id,
Function<StringInverter, String> test) {
List<StringInverter> 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<DynamicTest> 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<DynamicTest> basicInversion2() {
return testVersions(
"Basic inversion (should fail)",
inverter -> isEqual(inverter.invert("X"), "X"));
}
@TestFactory
Stream<DynamicTest> 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<DynamicTest> 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<DynamicTest> lengthNoGreaterThan30() {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() > 30);
return testVersions(
"Length must be less than 31 (throws exception)",
inverter -> inverter.invert(str)
);
}
@TestFactory
Stream<DynamicTest> lengthLessThan31() {
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
assertTrue(str.length() < 31);
return testVersions(
"Length must be less than 31 (should succeed)",
inverter -> inverter.invert(str)
);
}
}

View File

@ -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.*;

View File

@ -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.

View File

@ -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; }
}

View File

@ -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++) {

View File

@ -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!");

View File

@ -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";

View File

@ -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.

View File

@ -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 {

View File

@ -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); }

View File

@ -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.*;

View File

@ -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.

View File

@ -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.

View File

@ -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.

View File

@ -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);

View File

@ -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);

View File

@ -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.

View File

@ -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.*;

View File

@ -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<DynamicTest>
testVersions(String id, Consumer<StringInverter> test) {
List<StringInverter> 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<DynamicTest> 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<DynamicTest> basicInversion_Fail() {
return testVersions(
"A Basic Inversion that Fails",
(version) -> isEqual(version.invert("X"), "X"));
}
@TestFactory
Stream<DynamicTest> 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<DynamicTest> allowedCharacters_Succeed() {
return testVersions(
"Allowed Characters (Succeeds)",
(version) -> {
version.invert("abcdefghijklmnopqrstuvwxyz ,.");
version.invert("ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.");
}
);
}
@TestFactory
Stream<DynamicTest> 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<DynamicTest> 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");
}
);
}
}