Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 33a529c

Browse files
committed
Chapter name and order change
1 parent 941213a commit 33a529c

30 files changed

+195
-188
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,9 @@ subprojects {
277277
task run(dependsOn: createdTasks)
278278
}
279279

280-
project(':verifying') {
280+
project(':validating') {
281281
jmh {
282-
include = 'verifying.jmh.*'
282+
include = 'validating.jmh.*'
283283
}
284284
}
285285

network/SimpleClient.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
public class SimpleClient {
1212
public static void
1313
main(String[] args) throws IOException {
14-
// Passing null to getByName() produces the
15-
// special "Local Loopback" IP address, for
16-
// testing on one machine w/o a network:
14+
// Produce the "Local Loopback" IP address
15+
// for testing on one machine w/o a network:
1716
InetAddress addr =
1817
InetAddress.getLoopbackAddress();
1918
System.out.println("addr = " + addr);
@@ -35,7 +34,7 @@ public class SimpleClient {
3534
new OutputStreamWriter(
3635
socket.getOutputStream())),true);
3736
for(int i = 0; i < 10; i ++) {
38-
out.println("howdy " + i);
37+
out.println("hello " + i);
3938
String str = in.readLine();
4039
System.out.println(str);
4140
}

network/SimpleServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
// Just echoes whatever the client sends
5+
// Echoes what the client sends
66
// {ValidateByHand}
77
import java.io.*;
88
import java.net.*;
@@ -30,7 +30,7 @@ public class SimpleServer {
3030
new PrintWriter(
3131
new BufferedWriter(
3232
new OutputStreamWriter(
33-
socket.getOutputStream())),true);
33+
socket.getOutputStream())), true);
3434
while (true) {
3535
String str = in.readLine();
3636
if(str.equals("END")) break;

verifying/Assert1.java renamed to validating/Assert1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// verifying/Assert1.java
1+
// validating/Assert1.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.

verifying/Assert2.java renamed to validating/Assert2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// verifying/Assert2.java
1+
// validating/Assert2.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.

verifying/BadMicroBenchmark.java renamed to validating/BadMicroBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// verifying/BadMicroBenchmark.java
1+
// validating/BadMicroBenchmark.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.

verifying/BadMicroBenchmark2.java renamed to validating/BadMicroBenchmark2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// verifying/BadMicroBenchmark2.java
1+
// validating/BadMicroBenchmark2.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.

verifying/BadMicroBenchmark3.java renamed to validating/BadMicroBenchmark3.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// verifying/BadMicroBenchmark3.java
1+
// validating/BadMicroBenchmark3.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.

verifying/CountedList.java renamed to validating/CountedList.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// verifying/CountedList.java
1+
// validating/CountedList.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
55
// A List that keeps track of how many
66
// of itself are created.
7-
package verifying;
7+
package validating;
88
import java.util.*;
99

1010
public class CountedList extends ArrayList<String> {

verifying/CountedListTest.java renamed to validating/CountedListTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// verifying/CountedListTest.java
1+
// validating/CountedListTest.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
55
// Simple use of JUnit to test CountedList.
6-
package verifying;
6+
package validating;
77
import java.util.*;
88
import org.junit.jupiter.api.*;
99
import static org.junit.jupiter.api.Assertions.*;
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// validating/DynamicStringInverterTests.java
2+
// (c)2016 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5+
package validating;
6+
import java.util.*;
7+
import java.util.function.*;
8+
import java.util.stream.*;
9+
import org.junit.jupiter.api.*;
10+
import static org.junit.jupiter.api.Assertions.*;
11+
import static org.junit.jupiter.api.DynamicTest.*;
12+
13+
class DynamicStringInverterTests {
14+
// Combine operations to prevent code duplication:
15+
Stream<DynamicTest> testVersions(String id,
16+
Function<StringInverter, String> test) {
17+
List<StringInverter> versions = Arrays.asList(
18+
new Inverter1(), new Inverter2(),
19+
new Inverter3(), new Inverter4());
20+
return DynamicTest.stream(
21+
versions.iterator(),
22+
inverter -> inverter.getClass().getSimpleName(),
23+
inverter -> {
24+
System.out.println(
25+
inverter.getClass().getSimpleName() + ": " + id
26+
);
27+
try {
28+
if(test.apply(inverter) != "fail")
29+
System.out.println("Success");
30+
} catch(Exception | Error e) {
31+
System.out.println("Exception: " + e.getMessage());
32+
}
33+
}
34+
);
35+
}
36+
String isEqual(String lval, String rval) {
37+
if(lval.equals(rval))
38+
return "success";
39+
System.out.println("FAIL: " + lval + " != " + rval);
40+
return "fail";
41+
}
42+
@BeforeAll
43+
static void startMsg() {
44+
System.out.println(
45+
">>> Starting DynamicStringInverterTests <<<");
46+
}
47+
@AfterAll
48+
static void endMsg() {
49+
System.out.println(
50+
">>> Finished DynamicStringInverterTests <<<");
51+
}
52+
@TestFactory
53+
Stream<DynamicTest> basicInversion1() {
54+
String in = "Exit, Pursued by a Bear.";
55+
String out = "eXIT, pURSUED BY A bEAR.";
56+
return testVersions(
57+
"Basic inversion (should succeed)",
58+
inverter -> isEqual(inverter.invert(in), out)
59+
);
60+
}
61+
@TestFactory
62+
Stream<DynamicTest> basicInversion2() {
63+
return testVersions(
64+
"Basic inversion (should fail)",
65+
inverter -> isEqual(inverter.invert("X"), "X"));
66+
}
67+
@TestFactory
68+
Stream<DynamicTest> disallowedCharacters() {
69+
String disallowed = ";-_()*&^%$#@!~`0123456789";
70+
return testVersions(
71+
"Disallowed characters",
72+
inverter -> {
73+
String result = disallowed.chars()
74+
.mapToObj(c -> {
75+
String cc = Character.toString((char)c);
76+
try {
77+
inverter.invert(cc);
78+
return "";
79+
} catch(RuntimeException e) {
80+
return cc;
81+
}
82+
}).collect(Collectors.joining(""));
83+
if(result.length() == 0)
84+
return "success";
85+
System.out.println("Bad characters: " + result);
86+
return "fail";
87+
}
88+
);
89+
}
90+
@TestFactory
91+
Stream<DynamicTest> allowedCharacters() {
92+
String lowcase = "abcdefghijklmnopqrstuvwxyz ,.";
93+
String upcase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ ,.";
94+
return testVersions(
95+
"Allowed characters (should succeed)",
96+
inverter -> {
97+
assertEquals(inverter.invert(lowcase), upcase);
98+
assertEquals(inverter.invert(upcase), lowcase);
99+
return "success";
100+
}
101+
);
102+
}
103+
@TestFactory
104+
Stream<DynamicTest> lengthNoGreaterThan30() {
105+
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
106+
assertTrue(str.length() > 30);
107+
return testVersions(
108+
"Length must be less than 31 (throws exception)",
109+
inverter -> inverter.invert(str)
110+
);
111+
}
112+
@TestFactory
113+
Stream<DynamicTest> lengthLessThan31() {
114+
String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
115+
assertTrue(str.length() < 31);
116+
return testVersions(
117+
"Length must be less than 31 (should succeed)",
118+
inverter -> inverter.invert(str)
119+
);
120+
}
121+
}

verifying/FirstJUnit5Tests.java renamed to validating/FirstJUnit5Tests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// verifying/FirstJUnit5Tests.java
1+
// validating/FirstJUnit5Tests.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
package verifying;
5+
package validating;
66
import org.junit.jupiter.api.*;
77
import static org.junit.jupiter.api.Assertions.*;
88

verifying/GuavaPreconditions.java renamed to validating/GuavaPreconditions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// verifying/GuavaPreconditions.java
1+
// validating/GuavaPreconditions.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// verifying/StringInverter1.java
1+
// validating/Inverter1.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
package verifying;
5+
package validating;
66

7-
public class StringInverter1 implements StringInverter {
8-
public String invert(String str) { return ""; }
7+
public class Inverter1 implements StringInverter {
8+
public String invert(String str) { return str; }
99
}

verifying/StringInverter2.java renamed to validating/Inverter2.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// verifying/StringInverter2.java
1+
// validating/Inverter2.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
package verifying;
5+
package validating;
66
import static java.lang.Character.*;
77

8-
public class StringInverter2 implements StringInverter {
8+
public class Inverter2 implements StringInverter {
99
public String invert(String str) {
1010
String result = "";
1111
for(int i = 0; i < str.length(); i++) {

verifying/StringInverter3.java renamed to validating/Inverter3.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// verifying/StringInverter3.java
1+
// validating/Inverter3.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
package verifying;
5+
package validating;
66
import static java.lang.Character.*;
77

8-
public class StringInverter3 implements StringInverter {
8+
public class Inverter3 implements StringInverter {
99
public String invert(String str) {
1010
if(str.length() > 30)
1111
throw new RuntimeException("argument too long!");

verifying/StringInverter4.java renamed to validating/Inverter4.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// verifying/StringInverter4.java
1+
// validating/Inverter4.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
package verifying;
5+
package validating;
66
import static java.lang.Character.*;
77

8-
public class StringInverter4 implements StringInverter {
8+
public class Inverter4 implements StringInverter {
99
static final String allowed =
1010
"abcdefghijklmnopqrstuvwxyz ,." +
1111
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

verifying/LoaderAssertions.java renamed to validating/LoaderAssertions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// verifying/LoaderAssertions.java
1+
// validating/LoaderAssertions.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.

verifying/Queue.java renamed to validating/Queue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// verifying/Queue.java
1+
// validating/Queue.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
55
// Demonstration of Design by Contract (DbC)
6-
package verifying;
6+
package validating;
77
import java.util.*;
88

99
public class Queue {

verifying/QueueException.java renamed to validating/QueueException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// verifying/QueueException.java
1+
// validating/QueueException.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
package verifying;
5+
package validating;
66

77
public class QueueException extends RuntimeException {
88
public QueueException(String why) { super(why); }

verifying/QueueTest.java renamed to validating/QueueTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// verifying/QueueTest.java
1+
// validating/QueueTest.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
package verifying;
6-
import verifying.Queue;
5+
package validating;
6+
//import validating.Queue;
77
import org.junit.jupiter.api.*;
88
import static org.junit.jupiter.api.Assertions.*;
99

verifying/SLF4JLevels.java renamed to validating/SLF4JLevels.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// verifying/SLF4JLevels.java
1+
// validating/SLF4JLevels.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.

verifying/SLF4JLogging.java renamed to validating/SLF4JLogging.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// verifying/SLF4JLogging.java
1+
// validating/SLF4JLogging.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.

verifying/SimpleDebugging.java renamed to validating/SimpleDebugging.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// verifying/SimpleDebugging.java
1+
// validating/SimpleDebugging.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.

verifying/StringInverter.java renamed to validating/StringInverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// verifying/StringInverter.java
1+
// validating/StringInverter.java
22
// (c)2016 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
5-
package verifying;
5+
package validating;
66

77
interface StringInverter {
88
public String invert(String str);

0 commit comments

Comments
 (0)