Started Guava Preconditions, new JUnit features

This commit is contained in:
Bruce Eckel 2016-08-15 17:10:52 -06:00
parent 363abe4586
commit a454e23de4
4 changed files with 39 additions and 31 deletions

View File

@ -294,6 +294,7 @@ project(':collections') {
configure(subprojects - project(':onjava')) {
dependencies {
compile project(':onjava')
compile group: 'com.google.guava', name: 'guava', version: '19.0'
compile "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}"
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.6.2'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'

View File

@ -0,0 +1,14 @@
// verifying/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.
// Demonstrating Guava Preconditions
import static com.google.common.base.Preconditions.*;
public class GuavaPreconditions {
public static void main(String[] args) {
}
}
/* Output:
*/

View File

@ -97,7 +97,7 @@ public class Queue {
private int i = 0;
public WhiteBoxTest() {
while(i < 5) // Preload with some data
queue.put("" + i++);
queue.put(Integer.toString(i++));
}
// Support methods:
private void showFullness() {
@ -118,7 +118,7 @@ public class Queue {
System.out.println(queue.get());
System.out.println(queue.get());
while(!queue.full())
queue.put("" + i++);
queue.put(Integer.toString(i++));
String msg = "";
try {
queue.put("");
@ -160,7 +160,7 @@ public class Queue {
public void circularity() {
System.out.println("testCircularity");
while(!queue.full())
queue.put("" + i++);
queue.put(Integer.toString(i++));
showFullness();
// White-box testing accesses private field:
assertTrue(queue.wrapped);
@ -168,7 +168,7 @@ public class Queue {
System.out.println(queue.get());
showEmptiness();
while(!queue.full())
queue.put("" + i++);
queue.put(Integer.toString(i++));
showFullness();
while(!queue.empty())
System.out.println(queue.get());

View File

@ -1,13 +1,11 @@
// verifying/JUnitDemo.java
// verifying/SimpleJUnit.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 ArrayList
import java.util.*;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static java.lang.System.out;
import org.junit.*;
import static org.junit.Assert.*;
// So we can see the list objects being created,
// and keep track of when they are cleaned up:
@ -15,33 +13,28 @@ class CountedList extends ArrayList<String> {
private static int counter = 0;
private int id = counter++;
public CountedList() {
out.println("CountedList #" + id);
System.out.println("CountedList #" + id);
}
public int getId() { return id; }
}
public class JUnitDemo {
private CountedList list = new CountedList();
// You can use the constructor instead of setUp():
public JUnitDemo() {
public class SimpleJUnit {
private CountedList list;
@Before
public void initialize() {
list = new CountedList();
System.out.println("Set up for " + list.getId());
for(int i = 0; i < 3; i++)
list.add("" + i);
list.add(Integer.toString(i));
}
// Thus, setUp() is optional, but is run right
// before the test:
protected void setUp() {
out.println("Set up for " + list.getId());
}
// tearDown() is also optional, and is called after
// each test. setUp() and tearDown() can be either
// protected or public:
public void tearDown() {
out.println("Tearing down " + list.getId());
@After
public void cleanup() {
System.out.println("Cleaning up " + list.getId());
}
// All tests are marked with the @Test annotation:
@Test
public void insert() {
out.println("Running testInsert()");
System.out.println("Running testInsert()");
assertEquals(list.size(), 3);
list.add(1, "Insert");
assertEquals(list.size(), 4);
@ -49,7 +42,7 @@ public class JUnitDemo {
}
@Test
public void replace() {
out.println("Running testReplace()");
System.out.println("Running testReplace()");
assertEquals(list.size(), 3);
list.set(1, "Replace");
assertEquals(list.size(), 3);
@ -68,12 +61,12 @@ public class JUnitDemo {
}
@Test
public void order() {
out.println("Running testOrder()");
System.out.println("Running testOrder()");
compare(list, new String[] { "0", "1", "2" });
}
@Test
public void remove() {
out.println("Running testRemove()");
System.out.println("Running testRemove()");
assertEquals(list.size(), 3);
list.remove(1);
assertEquals(list.size(), 2);
@ -81,7 +74,7 @@ public class JUnitDemo {
}
@Test
public void addAll() {
out.println("Running testAddAll()");
System.out.println("Running testAddAll()");
list.addAll(Arrays.asList(new String[] {
"An", "African", "Swallow"}));
assertEquals(list.size(), 6);
@ -91,7 +84,7 @@ public class JUnitDemo {
public static void main(String[] args) {
// Invoke JUnit on the class:
org.junit.runner.JUnitCore.runClasses(
JUnitDemo.class);
SimpleJUnit.class);
}
}
/* Output: