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

Skip to content

Commit c497c38

Browse files
author
Bruce Eckel
committed
edits & improvements
1 parent ea67cac commit c497c38

11 files changed

+198
-62
lines changed

network/ChatterClient.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
55
// {ValidateByHand}
66
// Tests the ChatterServer by starting multiple
7-
// clients, each of which sends datagrams
7+
// clients, each of which sends datagrams.
88
import java.net.*;
99
import java.io.*;
1010
import onjava.*;
@@ -37,12 +37,11 @@ public ChatterClient(int identifier) {
3737
public void sendAndEcho(String msg) {
3838
try {
3939
// Make and send a datagram:
40-
s.send(Dgram.toDatagram(msg,
41-
hostAddress,
42-
ChatterServer.INPORT));
40+
s.send(Dgram.toDatagram(
41+
msg, hostAddress, ChatterServer.INPORT));
4342
// Block until it echoes back:
4443
s.receive(dp);
45-
// Print out the echoed contents:
44+
// Display the echoed contents:
4645
String rcvd = "Client #" + id +
4746
", rcvd from " +
4847
dp.getAddress() + ", " +

network/ChatterServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public ChatterServer() {
3131
"Echoed: " + rcvd;
3232
// Extract the address and port from the
3333
// received datagram to find out where to
34-
// send it back:
34+
// send it back to:
3535
DatagramPacket echo =
3636
Dgram.toDatagram(echoString,
3737
dp.getAddress(), dp.getPort());

network/Dgram.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +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-
// Converts back and forth
6-
// between Strings and DataGramPackets
5+
// Converts between Strings and DataGramPackets
76
import java.net.*;
87

98
public class Dgram {

network/MultiSimpleClient.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public SimpleClientThread(InetAddress addr) {
4646
// constructor:
4747
try {
4848
socket.close();
49-
} catch(IOException e2) {}
49+
} catch(IOException e2) {
50+
throw new RuntimeException(e2);
51+
}
5052
}
5153
// Otherwise the socket will be closed by
5254
// the run() method of the thread.
@@ -61,11 +63,14 @@ public void run() {
6163
}
6264
out.println("END");
6365
} catch(IOException e) {
66+
throw new RuntimeException(e);
6467
} finally {
6568
// Always close it:
6669
try {
6770
socket.close();
68-
} catch(IOException e) {}
71+
} catch(IOException e) {
72+
throw new RuntimeException(e);
73+
}
6974
threadcount--; // Ending this thread
7075
}
7176
}
@@ -77,11 +82,9 @@ public class MultiSimpleClient {
7782
main(String[] args) throws IOException,
7883
InterruptedException {
7984
new TimedAbort(5); // Terminate after 5 seconds
80-
InetAddress addr =
81-
InetAddress.getByName(null);
85+
InetAddress addr = InetAddress.getByName(null);
8286
while(true) {
83-
if(SimpleClientThread.threadCount()
84-
< MAX_THREADS)
87+
if(SimpleClientThread.threadCount() < MAX_THREADS)
8588
new SimpleClientThread(addr);
8689
Thread.sleep(100);
8790
}

network/MultiSimpleServer.java

Lines changed: 8 additions & 6 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-
// Uses multithreading to handle any number of clients
5+
// Uses threads to handle any number of clients
66
// {ValidateByHand}
77
import java.io.*;
88
import java.net.*;
@@ -25,10 +25,9 @@ public ServeOneSimple(Socket s)
2525
new BufferedWriter(
2626
new OutputStreamWriter(
2727
socket.getOutputStream())), true);
28-
// If any of the above calls throw an
29-
// exception, the caller is responsible for
30-
// closing the socket. Otherwise the thread
31-
// will close it.
28+
// If any of the above calls throw an exception,
29+
// the caller is responsible for closing the
30+
// socket. Otherwise the thread closes it.
3231
start(); // Calls run()
3332
}
3433
@Override
@@ -42,10 +41,13 @@ public void run() {
4241
}
4342
System.out.println("closing...");
4443
} catch (IOException e) {
44+
throw new RuntimeException(e);
4545
} finally {
4646
try {
4747
socket.close();
48-
} catch(IOException e) {}
48+
} catch(IOException e) {
49+
throw new RuntimeException(e);
50+
}
4951
}
5052
}
5153
}

validating/CountedList.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +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-
// A List that keeps track of how many
6-
// of itself are created.
5+
// Keeps track of how many of itself are created.
76
package validating;
87
import java.util.*;
98

validating/CountedListTest.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
public class CountedListTest {
1212
private CountedList list;
13+
@BeforeAll
14+
static void beforeAllMsg() {
15+
System.out.println(">>> Starting CountedListTest");
16+
}
17+
@AfterAll
18+
static void afterAllMsg() {
19+
System.out.println(">>> Finished CountedListTest");
20+
}
1321
@BeforeEach
1422
public void initialize() {
1523
list = new CountedList();
@@ -21,7 +29,6 @@ public void initialize() {
2129
public void cleanup() {
2230
System.out.println("Cleaning up " + list.getId());
2331
}
24-
// All tests are marked with the @Test annotation:
2532
@Test
2633
public void insert() {
2734
System.out.println("Running testInsert()");
@@ -38,16 +45,12 @@ public void replace() {
3845
assertEquals(list.size(), 3);
3946
assertEquals(list.get(1), "Replace");
4047
}
41-
// A helper method to reduce code duplication. As long
42-
// as it isn't annotated with @Test, it will not
43-
// be automatically executed by JUnit.
48+
// A helper method to simplify the code. As
49+
// long as it isn't annotated with @Test, it will
50+
// not be automatically executed by JUnit.
4451
private
4552
void compare(List<String> lst, String[] strs) {
46-
String[] array = lst.toArray(new String[0]);
47-
assertTrue(array.length == strs.length,
48-
"Arrays not the same length");
49-
for(int i = 0; i < array.length; i++)
50-
assertEquals(strs[i], array[i]);
53+
assertArrayEquals(lst.toArray(new String[0]), strs);
5154
}
5255
@Test
5356
public void order() {
@@ -72,15 +75,3 @@ public void addAll() {
7275
"An", "African", "Swallow" });
7376
}
7477
}
75-
/* Output:
76-
CountedList #0
77-
Running testAddAll()
78-
CountedList #1
79-
Running testInsert()
80-
CountedList #2
81-
Running testRemove()
82-
CountedList #3
83-
Running testOrder()
84-
CountedList #4
85-
Running testReplace()
86-
*/

validating/DynamicStringInverterTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ Stream<DynamicTest> testVersions(String id,
2222
inverter -> inverter.getClass().getSimpleName(),
2323
inverter -> {
2424
System.out.println(
25-
inverter.getClass().getSimpleName() + ": " + id
26-
);
25+
inverter.getClass().getSimpleName() +
26+
": " + id);
2727
try {
2828
if(test.apply(inverter) != "fail")
2929
System.out.println("Success");
3030
} catch(Exception | Error e) {
31-
System.out.println("Exception: " + e.getMessage());
31+
System.out.println(
32+
"Exception: " + e.getMessage());
3233
}
3334
}
3435
);

validating/FirstJUnit5Tests.java

Lines changed: 0 additions & 15 deletions
This file was deleted.

validating/GuavaAssertions.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// validating/GuavaAssertions.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+
// Assertions that are always enabled.
6+
import com.google.common.base.*;
7+
import static com.google.common.base.Verify.*;
8+
9+
public class GuavaAssertions {
10+
public static void main(String[] args) {
11+
verify(2 + 2 == 4);
12+
try {
13+
verify(1 + 2 == 4);
14+
} catch(VerifyException e) {
15+
System.out.println(e);
16+
}
17+
try {
18+
verify(1 + 2 == 4, "Bad math");
19+
} catch(VerifyException e) {
20+
System.out.println(e.getMessage());
21+
}
22+
try {
23+
verify(1 + 2 == 4, "Bad math: %s", "not 4");
24+
} catch(VerifyException e) {
25+
System.out.println(e.getMessage());
26+
}
27+
String s = "";
28+
s = verifyNotNull(s);
29+
s = null;
30+
try {
31+
verifyNotNull(s);
32+
} catch(VerifyException e) {
33+
System.out.println(e.getMessage());
34+
}
35+
try {
36+
verifyNotNull(s, "Shouldn't be null: %s", "arg s");
37+
} catch(VerifyException e) {
38+
System.out.println(e.getMessage());
39+
}
40+
}
41+
}
42+
/* Output:
43+
com.google.common.base.VerifyException
44+
Bad math
45+
Bad math: not 4
46+
expected a non-null reference
47+
Shouldn't be null: arg s
48+
*/

validating/GuavaPreconditions.java

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,121 @@
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
// Demonstrating Guava Preconditions
6+
import java.util.function.*;
67
import static com.google.common.base.Preconditions.*;
78

89
public class GuavaPreconditions {
9-
10+
void g(String s) {
11+
checkState(s.length() > 6);
12+
System.out.println("Success: " + s);
13+
}
14+
static void test(Consumer<String> c, String s) {
15+
try {
16+
System.out.println(s);
17+
c.accept(s);
18+
System.out.println("Success");
19+
} catch(Exception e) {
20+
String type = e.getClass().getSimpleName();
21+
String msg = e.getMessage();
22+
System.out.println(type +
23+
(msg == null ? "" : ": " + msg));
24+
}
25+
}
1026
public static void main(String[] args) {
27+
test((s) -> s = checkNotNull(s), "X");
28+
test((s) -> s = checkNotNull(s), null);
29+
test((s) -> s = checkNotNull(s, "s was null"), null);
30+
test((s) -> s = checkNotNull(
31+
s, "s was null, %s %s", "arg2", "arg3"), null);
32+
33+
test((s) -> checkArgument(s == "Fozzie"), "Fozzie");
34+
test((s) -> checkArgument(s == "Fozzie"), "X");
35+
test((s) -> checkArgument(s == "Fozzie"), null);
36+
test((s) -> checkArgument(
37+
s == "Fozzie", "Bear Left!"), null);
38+
test((s) -> checkArgument(
39+
s == "Fozzie", "Bear Left! %s Right!", "Frog"),
40+
null);
41+
42+
test((s) -> checkState(s.length() > 6), "Mortimer");
43+
test((s) -> checkState(s.length() > 6), "Mort");
44+
test((s) -> checkState(s.length() > 6), null);
45+
46+
test((s) ->
47+
checkElementIndex(6, s.length()), "Robert");
48+
test((s) ->
49+
checkElementIndex(6, s.length()), "Bob");
50+
test((s) ->
51+
checkElementIndex(6, s.length()), null);
52+
53+
test((s) ->
54+
checkPositionIndex(6, s.length()), "Robert");
55+
test((s) ->
56+
checkPositionIndex(6, s.length()), "Bob");
57+
test((s) ->
58+
checkPositionIndex(6, s.length()), null);
59+
60+
test((s) -> checkPositionIndexes(
61+
0, 6, s.length()), "Hieronymus");
62+
test((s) -> checkPositionIndexes(
63+
0, 10, s.length()), "Hieronymus");
64+
test((s) -> checkPositionIndexes(
65+
0, 11, s.length()), "Hieronymus");
66+
test((s) -> checkPositionIndexes(
67+
-1, 6, s.length()), "Hieronymus");
68+
test((s) -> checkPositionIndexes(
69+
7, 6, s.length()), "Hieronymus");
70+
test((s) -> checkPositionIndexes(
71+
0, 6, s.length()), null);
1172
}
1273
}
1374
/* Output:
75+
X
76+
Success
77+
null
78+
NullPointerException
79+
null
80+
NullPointerException: s was null
81+
null
82+
NullPointerException: s was null, arg2 arg3
83+
Fozzie
84+
Success
85+
X
86+
IllegalArgumentException
87+
null
88+
IllegalArgumentException
89+
null
90+
IllegalArgumentException: Bear Left!
91+
null
92+
IllegalArgumentException: Bear Left! Frog Right!
93+
Mortimer
94+
Success
95+
Mort
96+
IllegalStateException
97+
null
98+
NullPointerException
99+
Robert
100+
IndexOutOfBoundsException: index (6) must be less than size (6)
101+
Bob
102+
IndexOutOfBoundsException: index (6) must be less than size (3)
103+
null
104+
NullPointerException
105+
Robert
106+
Success
107+
Bob
108+
IndexOutOfBoundsException: index (6) must not be greater than size (3)
109+
null
110+
NullPointerException
111+
Hieronymus
112+
Success
113+
Hieronymus
114+
Success
115+
Hieronymus
116+
IndexOutOfBoundsException: end index (11) must not be greater than size (10)
117+
Hieronymus
118+
IndexOutOfBoundsException: start index (-1) must not be negative
119+
Hieronymus
120+
IndexOutOfBoundsException: end index (6) must not be less than start index (7)
121+
null
122+
NullPointerException
14123
*/

0 commit comments

Comments
 (0)