Started Guava Preconditions, new JUnit features
This commit is contained in:
parent
363abe4586
commit
a454e23de4
@ -294,6 +294,7 @@ project(':collections') {
|
|||||||
configure(subprojects - project(':onjava')) {
|
configure(subprojects - project(':onjava')) {
|
||||||
dependencies {
|
dependencies {
|
||||||
compile project(':onjava')
|
compile project(':onjava')
|
||||||
|
compile group: 'com.google.guava', name: 'guava', version: '19.0'
|
||||||
compile "org.openjdk.jmh:jmh-core:${jmh.jmhVersion}"
|
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-api', version: '2.6.2'
|
||||||
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'
|
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.6.2'
|
||||||
|
14
verifying/GuavaPreconditions.java
Normal file
14
verifying/GuavaPreconditions.java
Normal 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:
|
||||||
|
*/
|
@ -97,7 +97,7 @@ public class Queue {
|
|||||||
private int i = 0;
|
private int i = 0;
|
||||||
public WhiteBoxTest() {
|
public WhiteBoxTest() {
|
||||||
while(i < 5) // Preload with some data
|
while(i < 5) // Preload with some data
|
||||||
queue.put("" + i++);
|
queue.put(Integer.toString(i++));
|
||||||
}
|
}
|
||||||
// Support methods:
|
// Support methods:
|
||||||
private void showFullness() {
|
private void showFullness() {
|
||||||
@ -118,7 +118,7 @@ public class Queue {
|
|||||||
System.out.println(queue.get());
|
System.out.println(queue.get());
|
||||||
System.out.println(queue.get());
|
System.out.println(queue.get());
|
||||||
while(!queue.full())
|
while(!queue.full())
|
||||||
queue.put("" + i++);
|
queue.put(Integer.toString(i++));
|
||||||
String msg = "";
|
String msg = "";
|
||||||
try {
|
try {
|
||||||
queue.put("");
|
queue.put("");
|
||||||
@ -160,7 +160,7 @@ public class Queue {
|
|||||||
public void circularity() {
|
public void circularity() {
|
||||||
System.out.println("testCircularity");
|
System.out.println("testCircularity");
|
||||||
while(!queue.full())
|
while(!queue.full())
|
||||||
queue.put("" + i++);
|
queue.put(Integer.toString(i++));
|
||||||
showFullness();
|
showFullness();
|
||||||
// White-box testing accesses private field:
|
// White-box testing accesses private field:
|
||||||
assertTrue(queue.wrapped);
|
assertTrue(queue.wrapped);
|
||||||
@ -168,7 +168,7 @@ public class Queue {
|
|||||||
System.out.println(queue.get());
|
System.out.println(queue.get());
|
||||||
showEmptiness();
|
showEmptiness();
|
||||||
while(!queue.full())
|
while(!queue.full())
|
||||||
queue.put("" + i++);
|
queue.put(Integer.toString(i++));
|
||||||
showFullness();
|
showFullness();
|
||||||
while(!queue.empty())
|
while(!queue.empty())
|
||||||
System.out.println(queue.get());
|
System.out.println(queue.get());
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
// verifying/JUnitDemo.java
|
// verifying/SimpleJUnit.java
|
||||||
// (c)2016 MindView LLC: see Copyright.txt
|
// (c)2016 MindView LLC: see Copyright.txt
|
||||||
// We make no guarantees that this code is fit for any purpose.
|
// We make no guarantees that this code is fit for any purpose.
|
||||||
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
|
||||||
// Simple use of JUnit to test ArrayList
|
// Simple use of JUnit to test ArrayList
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import org.junit.Test;
|
import org.junit.*;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.*;
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static java.lang.System.out;
|
|
||||||
|
|
||||||
// So we can see the list objects being created,
|
// So we can see the list objects being created,
|
||||||
// and keep track of when they are cleaned up:
|
// and keep track of when they are cleaned up:
|
||||||
@ -15,33 +13,28 @@ class CountedList extends ArrayList<String> {
|
|||||||
private static int counter = 0;
|
private static int counter = 0;
|
||||||
private int id = counter++;
|
private int id = counter++;
|
||||||
public CountedList() {
|
public CountedList() {
|
||||||
out.println("CountedList #" + id);
|
System.out.println("CountedList #" + id);
|
||||||
}
|
}
|
||||||
public int getId() { return id; }
|
public int getId() { return id; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class JUnitDemo {
|
public class SimpleJUnit {
|
||||||
private CountedList list = new CountedList();
|
private CountedList list;
|
||||||
// You can use the constructor instead of setUp():
|
@Before
|
||||||
public JUnitDemo() {
|
public void initialize() {
|
||||||
|
list = new CountedList();
|
||||||
|
System.out.println("Set up for " + list.getId());
|
||||||
for(int i = 0; i < 3; i++)
|
for(int i = 0; i < 3; i++)
|
||||||
list.add("" + i);
|
list.add(Integer.toString(i));
|
||||||
}
|
}
|
||||||
// Thus, setUp() is optional, but is run right
|
@After
|
||||||
// before the test:
|
public void cleanup() {
|
||||||
protected void setUp() {
|
System.out.println("Cleaning up " + list.getId());
|
||||||
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());
|
|
||||||
}
|
}
|
||||||
// All tests are marked with the @Test annotation:
|
// All tests are marked with the @Test annotation:
|
||||||
@Test
|
@Test
|
||||||
public void insert() {
|
public void insert() {
|
||||||
out.println("Running testInsert()");
|
System.out.println("Running testInsert()");
|
||||||
assertEquals(list.size(), 3);
|
assertEquals(list.size(), 3);
|
||||||
list.add(1, "Insert");
|
list.add(1, "Insert");
|
||||||
assertEquals(list.size(), 4);
|
assertEquals(list.size(), 4);
|
||||||
@ -49,7 +42,7 @@ public class JUnitDemo {
|
|||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void replace() {
|
public void replace() {
|
||||||
out.println("Running testReplace()");
|
System.out.println("Running testReplace()");
|
||||||
assertEquals(list.size(), 3);
|
assertEquals(list.size(), 3);
|
||||||
list.set(1, "Replace");
|
list.set(1, "Replace");
|
||||||
assertEquals(list.size(), 3);
|
assertEquals(list.size(), 3);
|
||||||
@ -68,12 +61,12 @@ public class JUnitDemo {
|
|||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void order() {
|
public void order() {
|
||||||
out.println("Running testOrder()");
|
System.out.println("Running testOrder()");
|
||||||
compare(list, new String[] { "0", "1", "2" });
|
compare(list, new String[] { "0", "1", "2" });
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void remove() {
|
public void remove() {
|
||||||
out.println("Running testRemove()");
|
System.out.println("Running testRemove()");
|
||||||
assertEquals(list.size(), 3);
|
assertEquals(list.size(), 3);
|
||||||
list.remove(1);
|
list.remove(1);
|
||||||
assertEquals(list.size(), 2);
|
assertEquals(list.size(), 2);
|
||||||
@ -81,7 +74,7 @@ public class JUnitDemo {
|
|||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
public void addAll() {
|
public void addAll() {
|
||||||
out.println("Running testAddAll()");
|
System.out.println("Running testAddAll()");
|
||||||
list.addAll(Arrays.asList(new String[] {
|
list.addAll(Arrays.asList(new String[] {
|
||||||
"An", "African", "Swallow"}));
|
"An", "African", "Swallow"}));
|
||||||
assertEquals(list.size(), 6);
|
assertEquals(list.size(), 6);
|
||||||
@ -91,7 +84,7 @@ public class JUnitDemo {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// Invoke JUnit on the class:
|
// Invoke JUnit on the class:
|
||||||
org.junit.runner.JUnitCore.runClasses(
|
org.junit.runner.JUnitCore.runClasses(
|
||||||
JUnitDemo.class);
|
SimpleJUnit.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Output:
|
/* Output:
|
Loading…
x
Reference in New Issue
Block a user