Skip to content

Commit a454e23

Browse files
committed
Started Guava Preconditions, new JUnit features
1 parent 363abe4 commit a454e23

File tree

4 files changed

+39
-31
lines changed

4 files changed

+39
-31
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ project(':collections') {
294294
configure(subprojects - project(':onjava')) {
295295
dependencies {
296296
compile project(':onjava')
297+
compile group: 'com.google.guava', name: 'guava', version: '19.0'
297298
compile "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}"
298299
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2'
299300
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'

verifying/GuavaPreconditions.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// verifying/GuavaPreconditions.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+
// Demonstrating Guava Preconditions
6+
import static com.google.common.base.Preconditions.*;
7+
8+
public class GuavaPreconditions {
9+
10+
public static void main(String[] args) {
11+
}
12+
}
13+
/* Output:
14+
*/

verifying/Queue.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static class WhiteBoxTest {
9797
private int i = 0;
9898
public WhiteBoxTest() {
9999
while(i < 5) // Preload with some data
100-
queue.put("" + i++);
100+
queue.put(Integer.toString(i++));
101101
}
102102
// Support methods:
103103
private void showFullness() {
@@ -118,7 +118,7 @@ public void full() {
118118
System.out.println(queue.get());
119119
System.out.println(queue.get());
120120
while(!queue.full())
121-
queue.put("" + i++);
121+
queue.put(Integer.toString(i++));
122122
String msg = "";
123123
try {
124124
queue.put("");
@@ -160,15 +160,15 @@ public void nullPut() {
160160
public void circularity() {
161161
System.out.println("testCircularity");
162162
while(!queue.full())
163-
queue.put("" + i++);
163+
queue.put(Integer.toString(i++));
164164
showFullness();
165165
// White-box testing accesses private field:
166166
assertTrue(queue.wrapped);
167167
while(!queue.empty())
168168
System.out.println(queue.get());
169169
showEmptiness();
170170
while(!queue.full())
171-
queue.put("" + i++);
171+
queue.put(Integer.toString(i++));
172172
showFullness();
173173
while(!queue.empty())
174174
System.out.println(queue.get());

verifying/JUnitDemo.java renamed to verifying/SimpleJUnit.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,48 @@
1-
// verifying/JUnitDemo.java
1+
// verifying/SimpleJUnit.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 ArrayList
66
import java.util.*;
7-
import org.junit.Test;
8-
import static org.junit.Assert.assertEquals;
9-
import static org.junit.Assert.assertTrue;
10-
import static java.lang.System.out;
7+
import org.junit.*;
8+
import static org.junit.Assert.*;
119

1210
// So we can see the list objects being created,
1311
// and keep track of when they are cleaned up:
1412
class CountedList extends ArrayList<String> {
1513
private static int counter = 0;
1614
private int id = counter++;
1715
public CountedList() {
18-
out.println("CountedList #" + id);
16+
System.out.println("CountedList #" + id);
1917
}
2018
public int getId() { return id; }
2119
}
2220

23-
public class JUnitDemo {
24-
private CountedList list = new CountedList();
25-
// You can use the constructor instead of setUp():
26-
public JUnitDemo() {
21+
public class SimpleJUnit {
22+
private CountedList list;
23+
@Before
24+
public void initialize() {
25+
list = new CountedList();
26+
System.out.println("Set up for " + list.getId());
2727
for(int i = 0; i < 3; i++)
28-
list.add("" + i);
28+
list.add(Integer.toString(i));
2929
}
30-
// Thus, setUp() is optional, but is run right
31-
// before the test:
32-
protected void setUp() {
33-
out.println("Set up for " + list.getId());
34-
}
35-
// tearDown() is also optional, and is called after
36-
// each test. setUp() and tearDown() can be either
37-
// protected or public:
38-
public void tearDown() {
39-
out.println("Tearing down " + list.getId());
30+
@After
31+
public void cleanup() {
32+
System.out.println("Cleaning up " + list.getId());
4033
}
4134
// All tests are marked with the @Test annotation:
4235
@Test
4336
public void insert() {
44-
out.println("Running testInsert()");
37+
System.out.println("Running testInsert()");
4538
assertEquals(list.size(), 3);
4639
list.add(1, "Insert");
4740
assertEquals(list.size(), 4);
4841
assertEquals(list.get(1), "Insert");
4942
}
5043
@Test
5144
public void replace() {
52-
out.println("Running testReplace()");
45+
System.out.println("Running testReplace()");
5346
assertEquals(list.size(), 3);
5447
list.set(1, "Replace");
5548
assertEquals(list.size(), 3);
@@ -68,20 +61,20 @@ void compare(ArrayList<String> lst, String[] strs) {
6861
}
6962
@Test
7063
public void order() {
71-
out.println("Running testOrder()");
64+
System.out.println("Running testOrder()");
7265
compare(list, new String[] { "0", "1", "2" });
7366
}
7467
@Test
7568
public void remove() {
76-
out.println("Running testRemove()");
69+
System.out.println("Running testRemove()");
7770
assertEquals(list.size(), 3);
7871
list.remove(1);
7972
assertEquals(list.size(), 2);
8073
compare(list, new String[] { "0", "2" });
8174
}
8275
@Test
8376
public void addAll() {
84-
out.println("Running testAddAll()");
77+
System.out.println("Running testAddAll()");
8578
list.addAll(Arrays.asList(new String[] {
8679
"An", "African", "Swallow"}));
8780
assertEquals(list.size(), 6);
@@ -91,7 +84,7 @@ public void addAll() {
9184
public static void main(String[] args) {
9285
// Invoke JUnit on the class:
9386
org.junit.runner.JUnitCore.runClasses(
94-
JUnitDemo.class);
87+
SimpleJUnit.class);
9588
}
9689
}
9790
/* Output:

0 commit comments

Comments
 (0)