Trying to get everything to build with JUnit5

This commit is contained in:
Bruce Eckel 2016-08-24 16:28:10 -06:00
parent 2ca00a4fa5
commit 336d85c4cc
22 changed files with 238 additions and 198 deletions

View File

@ -191,11 +191,11 @@ subprojects {
main {
java {
srcDir projectDir
exclude "*Test.java"
// exclude "*Test.java"
exclude "*Tests.java"
exclude "*JUnit.java"
exclude "StringInverter*.java"
exclude "Queue.java"
// exclude "Queue.java"
}
resources {
srcDir projectDir
@ -207,11 +207,11 @@ subprojects {
srcDir projectDir
}
}
test {
/* test {
java {
srcDir projectDir
}
}
}*/
}
/*test {

5
generics/Amphibian.java Normal file
View File

@ -0,0 +1,5 @@
// generics/Amphibian.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.
public class Amphibian {}

View File

@ -6,29 +6,6 @@ import java.util.*;
import java.util.function.*;
import onjava.*;
class Shape {
private static long counter = 0;
private final long id = counter++;
@Override
public String toString() {
return getClass().getSimpleName() + " " + id;
}
public void rotate() {
System.out.println(this + " rotate");
}
public void resize(int newSize) {
System.out.println(this + " resize " + newSize);
}
}
class Square extends Shape {}
class FilledList<T> extends ArrayList<T> {
public FilledList(Supplier<T> gen, int size) {
Suppliers.fill(this, gen, size);
}
}
public class ApplyTest {
public static void main(String[] args) throws Exception {
List<Shape> shapes =

28
generics/FilledList.java Normal file
View File

@ -0,0 +1,28 @@
// generics/FilledList.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.
import java.util.*;
import java.util.function.*;
import onjava.*;
class FilledList<T> extends ArrayList<T> {
public FilledList(Supplier<T> gen, int size) {
Suppliers.fill(this, gen, size);
}
public FilledList(T t, int size) {
for(int i = 0; i < size; i++)
this.add(t);
}
public static void main(String[] args) {
List<String> list = new FilledList<>("Hello", 4);
System.out.println(list);
// Supplier version:
List<Integer> ilist = new FilledList<>(() -> 47, 4);
System.out.println(ilist);
}
}
/* Output:
[Hello, Hello, Hello, Hello]
[47, 47, 47, 47]
*/

View File

@ -1,23 +0,0 @@
// generics/FilledListMaker.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.
import java.util.*;
public class FilledListMaker<T> {
List<T> create(T t, int n) {
List<T> result = new ArrayList<>();
for(int i = 0; i < n; i++)
result.add(t);
return result;
}
public static void main(String[] args) {
FilledListMaker<String> stringMaker =
new FilledListMaker<>();
List<String> list = stringMaker.create("Hello", 4);
System.out.println(list);
}
}
/* Output:
[Hello, Hello, Hello, Hello]
*/

View File

@ -3,6 +3,7 @@
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {CompileTimeError} (Will not compile)
package generics;
interface Payable<T> {}

19
generics/Shape.java Normal file
View File

@ -0,0 +1,19 @@
// generics/Shape.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.
public class Shape {
private static long counter = 0;
private final long id = counter++;
@Override
public String toString() {
return getClass().getSimpleName() + " " + id;
}
public void rotate() {
System.out.println(this + " rotate");
}
public void resize(int newSize) {
System.out.println(this + " resize " + newSize);
}
}

View File

@ -2,7 +2,7 @@
// (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.
// A different kind of collection that is Iterable
// A different kind of Iterable collection
import java.util.*;
public class SimpleQueue<T> implements Iterable<T> {

5
generics/Square.java Normal file
View File

@ -0,0 +1,5 @@
// generics/Square.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.
public class Square extends Shape {}

View File

@ -4,9 +4,6 @@
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
import onjava.*;
class Amphibian {}
class Vehicle {}
public class TupleTest {
static Tuple2<String, Integer> f() {
// Autoboxing converts the int to Integer:

View File

@ -3,6 +3,7 @@
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
// {CompileTimeError} (Will not compile)
import java.util.*;
public class UseList<W, T> {
void f(List<T> v) {}

5
generics/Vehicle.java Normal file
View File

@ -0,0 +1,5 @@
// generics/Vehicle.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.
public class Vehicle {}

View File

@ -1,125 +0,0 @@
// references/DeepCopy.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.
// Cloning a composed object
// (Install libraries from junit.org)
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
class DepthReading implements Cloneable {
private double depth;
public DepthReading(double depth) {
this.depth = depth;
}
@Override
public DepthReading clone() {
try {
return (DepthReading)super.clone();
} catch(CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
public double getDepth() { return depth; }
public void setDepth(double depth) {
this.depth = depth;
}
@Override
public String toString() {
return String.valueOf(depth);
}
}
class TemperatureReading implements Cloneable {
private long time;
private double temperature;
public TemperatureReading(double temperature) {
time = System.currentTimeMillis();
this.temperature = temperature;
}
@Override
public TemperatureReading clone() {
try {
return (TemperatureReading)super.clone();
} catch(CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
public double getTemperature() {
return temperature;
}
public void setTemperature(double temp) {
this.temperature = temp;
}
@Override
public String toString() {
return String.valueOf(temperature);
}
}
class OceanReading implements Cloneable {
private DepthReading depth;
private TemperatureReading temperature;
public
OceanReading(double tdata, double ddata) {
temperature = new TemperatureReading(tdata);
depth = new DepthReading(ddata);
}
@Override
public OceanReading clone() {
OceanReading or = null;
try {
or = (OceanReading)super.clone();
} catch(CloneNotSupportedException e) {
throw new RuntimeException(e);
}
// Must clone references:
or.depth = (DepthReading)or.depth.clone();
or.temperature =
(TemperatureReading)or.temperature.clone();
return or;
}
public TemperatureReading getTemperatureReading() {
return temperature;
}
public void
setTemperatureReading(TemperatureReading tr) {
temperature = tr;
}
public DepthReading getDepthReading() {
return depth;
}
public void setDepthReading(DepthReading dr) {
this.depth = dr;
}
@Override
public String toString() {
return "temperature: " + temperature +
", depth: " + depth;
}
}
public class DeepCopy {
@Test
public void testClone() {
OceanReading reading =
new OceanReading(33.9, 100.5);
// Now clone it:
OceanReading clone = reading.clone();
TemperatureReading tr =
clone.getTemperatureReading();
tr.setTemperature(tr.getTemperature() + 1);
clone.setTemperatureReading(tr);
DepthReading dr = clone.getDepthReading();
dr.setDepth(dr.getDepth() + 1);
clone.setDepthReading(dr);
assertEquals(reading.toString(),
"temperature: 33.9, depth: 100.5");
assertEquals(clone.toString(),
"temperature: 34.9, depth: 101.5");
}
public static void main(String[] args) {
org.junit.runner.JUnitCore.runClasses(
DeepCopy.class);
}
}

View File

@ -0,0 +1,28 @@
// references/DeepCopyTest.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.
package references;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
public class DeepCopyTest {
@Test
public void testClone() {
OceanReading reading =
new OceanReading(33.9, 100.5);
// Now clone it:
OceanReading clone = reading.clone();
TemperatureReading tr =
clone.getTemperatureReading();
tr.setTemperature(tr.getTemperature() + 1);
clone.setTemperatureReading(tr);
DepthReading dr = clone.getDepthReading();
dr.setDepth(dr.getDepth() + 1);
clone.setDepthReading(dr);
assertEquals(reading.toString(),
"temperature: 33.9, depth: 100.5");
assertEquals(clone.toString(),
"temperature: 34.9, depth: 101.5");
}
}

View File

@ -0,0 +1,29 @@
// references/DepthReading.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.
// Cloning a composed object
package references;
public class DepthReading implements Cloneable {
private double depth;
public DepthReading(double depth) {
this.depth = depth;
}
@Override
public DepthReading clone() {
try {
return (DepthReading)super.clone();
} catch(CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
public double getDepth() { return depth; }
public void setDepth(double depth) {
this.depth = depth;
}
@Override
public String toString() {
return String.valueOf(depth);
}
}

View File

@ -0,0 +1,48 @@
// references/OceanReading.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.
// Cloning a composed object
package references;
public class OceanReading implements Cloneable {
private DepthReading depth;
private TemperatureReading temperature;
public
OceanReading(double tdata, double ddata) {
temperature = new TemperatureReading(tdata);
depth = new DepthReading(ddata);
}
@Override
public OceanReading clone() {
OceanReading or = null;
try {
or = (OceanReading)super.clone();
} catch(CloneNotSupportedException e) {
throw new RuntimeException(e);
}
// Must clone references:
or.depth = (DepthReading)or.depth.clone();
or.temperature =
(TemperatureReading)or.temperature.clone();
return or;
}
public TemperatureReading getTemperatureReading() {
return temperature;
}
public void
setTemperatureReading(TemperatureReading tr) {
temperature = tr;
}
public DepthReading getDepthReading() {
return depth;
}
public void setDepthReading(DepthReading dr) {
this.depth = dr;
}
@Override
public String toString() {
return "temperature: " + temperature +
", depth: " + depth;
}
}

View File

@ -0,0 +1,33 @@
// references/TemperatureReading.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.
// Cloning a composed object
package references;
public class TemperatureReading implements Cloneable {
private long time;
private double temperature;
public TemperatureReading(double temperature) {
time = System.currentTimeMillis();
this.temperature = temperature;
}
@Override
public TemperatureReading clone() {
try {
return (TemperatureReading)super.clone();
} catch(CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
public double getTemperature() {
return temperature;
}
public void setTemperature(double temp) {
this.temperature = temp;
}
@Override
public String toString() {
return String.valueOf(temperature);
}
}

View File

@ -0,0 +1,17 @@
// verifying/CountedList.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.
// A List that keeps track of how many
// of itself are created.
package verifying;
import java.util.*;
public class CountedList extends ArrayList<String> {
private static int counter = 0;
private int id = counter++;
public CountedList() {
System.out.println("CountedList #" + id);
}
public int getId() { return id; }
}

View File

@ -1,25 +1,15 @@
// verifying/SimpleJUnit.java
// verifying/CountedListTest.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
// Simple use of JUnit to test CountedList.
package verifying;
import java.util.*;
//import verifying.CountedList;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
// Keeps track of list objects as they are
// created and cleaned up:
class CountedList extends ArrayList<String> {
private static int counter = 0;
private int id = counter++;
public CountedList() {
System.out.println("CountedList #" + id);
}
public int getId() { return id; }
}
public class SimpleJUnit {
public class CountedListTest {
private CountedList list;
@BeforeEach
public void initialize() {

View File

@ -13,10 +13,6 @@ public class Queue {
out = 0; // Next gettable object
// Has it wrapped around the circular queue?
private boolean wrapped = false;
public static class
QueueException extends RuntimeException {
public QueueException(String why) { super(why); }
}
public Queue(int size) {
data = new Object[size];
// Must be true after construction:

View File

@ -0,0 +1,9 @@
// verifying/QueueException.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.
package verifying;
public class QueueException extends RuntimeException {
public QueueException(String why) { super(why); }
}

View File

@ -3,7 +3,7 @@
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
package verifying;
import verifying.Queue.*;
import verifying.Queue;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;