From 336d85c4cca4f3a73eb2de2ee7c0fbfd7dea4ab6 Mon Sep 17 00:00:00 2001 From: Bruce Eckel Date: Wed, 24 Aug 2016 16:28:10 -0600 Subject: [PATCH] Trying to get everything to build with JUnit5 --- build.gradle | 8 +- generics/Amphibian.java | 5 + generics/ApplyTest.java | 23 ---- generics/FilledList.java | 28 ++++ generics/FilledListMaker.java | 23 ---- generics/MultipleInterfaceVariants.java | 1 + generics/Shape.java | 19 +++ generics/SimpleQueue.java | 2 +- generics/Square.java | 5 + generics/TupleTest.java | 3 - generics/UseList.java | 1 + generics/Vehicle.java | 5 + references/DeepCopy.java | 125 ------------------ references/DeepCopyTest.java | 28 ++++ references/DepthReading.java | 29 ++++ references/OceanReading.java | 48 +++++++ references/TemperatureReading.java | 33 +++++ verifying/CountedList.java | 17 +++ ...{SimpleJUnit.java => CountedListTest.java} | 18 +-- verifying/Queue.java | 4 - verifying/QueueException.java | 9 ++ verifying/QueueTest.java | 2 +- 22 files changed, 238 insertions(+), 198 deletions(-) create mode 100644 generics/Amphibian.java create mode 100644 generics/FilledList.java delete mode 100644 generics/FilledListMaker.java create mode 100644 generics/Shape.java create mode 100644 generics/Square.java create mode 100644 generics/Vehicle.java delete mode 100644 references/DeepCopy.java create mode 100644 references/DeepCopyTest.java create mode 100644 references/DepthReading.java create mode 100644 references/OceanReading.java create mode 100644 references/TemperatureReading.java create mode 100644 verifying/CountedList.java rename verifying/{SimpleJUnit.java => CountedListTest.java} (86%) create mode 100644 verifying/QueueException.java diff --git a/build.gradle b/build.gradle index bae50d8a..11614814 100644 --- a/build.gradle +++ b/build.gradle @@ -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 { diff --git a/generics/Amphibian.java b/generics/Amphibian.java new file mode 100644 index 00000000..6809ef71 --- /dev/null +++ b/generics/Amphibian.java @@ -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 {} diff --git a/generics/ApplyTest.java b/generics/ApplyTest.java index 2bf45ad0..aa273cd8 100644 --- a/generics/ApplyTest.java +++ b/generics/ApplyTest.java @@ -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 extends ArrayList { - public FilledList(Supplier gen, int size) { - Suppliers.fill(this, gen, size); - } -} - public class ApplyTest { public static void main(String[] args) throws Exception { List shapes = diff --git a/generics/FilledList.java b/generics/FilledList.java new file mode 100644 index 00000000..efffa3bb --- /dev/null +++ b/generics/FilledList.java @@ -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 extends ArrayList { + public FilledList(Supplier 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 list = new FilledList<>("Hello", 4); + System.out.println(list); + // Supplier version: + List ilist = new FilledList<>(() -> 47, 4); + System.out.println(ilist); + } +} +/* Output: +[Hello, Hello, Hello, Hello] +[47, 47, 47, 47] +*/ diff --git a/generics/FilledListMaker.java b/generics/FilledListMaker.java deleted file mode 100644 index 85393113..00000000 --- a/generics/FilledListMaker.java +++ /dev/null @@ -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 { - List create(T t, int n) { - List result = new ArrayList<>(); - for(int i = 0; i < n; i++) - result.add(t); - return result; - } - public static void main(String[] args) { - FilledListMaker stringMaker = - new FilledListMaker<>(); - List list = stringMaker.create("Hello", 4); - System.out.println(list); - } -} -/* Output: -[Hello, Hello, Hello, Hello] -*/ diff --git a/generics/MultipleInterfaceVariants.java b/generics/MultipleInterfaceVariants.java index 035c7956..8badc843 100644 --- a/generics/MultipleInterfaceVariants.java +++ b/generics/MultipleInterfaceVariants.java @@ -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 {} diff --git a/generics/Shape.java b/generics/Shape.java new file mode 100644 index 00000000..42363dee --- /dev/null +++ b/generics/Shape.java @@ -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); + } +} diff --git a/generics/SimpleQueue.java b/generics/SimpleQueue.java index 87f64384..385b438b 100644 --- a/generics/SimpleQueue.java +++ b/generics/SimpleQueue.java @@ -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 implements Iterable { diff --git a/generics/Square.java b/generics/Square.java new file mode 100644 index 00000000..9aa16d80 --- /dev/null +++ b/generics/Square.java @@ -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 {} diff --git a/generics/TupleTest.java b/generics/TupleTest.java index fee02b25..35126980 100644 --- a/generics/TupleTest.java +++ b/generics/TupleTest.java @@ -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 f() { // Autoboxing converts the int to Integer: diff --git a/generics/UseList.java b/generics/UseList.java index 29a7e53f..e526b743 100644 --- a/generics/UseList.java +++ b/generics/UseList.java @@ -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 { void f(List v) {} diff --git a/generics/Vehicle.java b/generics/Vehicle.java new file mode 100644 index 00000000..0e742ca4 --- /dev/null +++ b/generics/Vehicle.java @@ -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 {} diff --git a/references/DeepCopy.java b/references/DeepCopy.java deleted file mode 100644 index ba4db549..00000000 --- a/references/DeepCopy.java +++ /dev/null @@ -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); - } -} diff --git a/references/DeepCopyTest.java b/references/DeepCopyTest.java new file mode 100644 index 00000000..93e0daa2 --- /dev/null +++ b/references/DeepCopyTest.java @@ -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"); + } +} diff --git a/references/DepthReading.java b/references/DepthReading.java new file mode 100644 index 00000000..6de99757 --- /dev/null +++ b/references/DepthReading.java @@ -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); + } +} diff --git a/references/OceanReading.java b/references/OceanReading.java new file mode 100644 index 00000000..cfd6aafa --- /dev/null +++ b/references/OceanReading.java @@ -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; + } +} diff --git a/references/TemperatureReading.java b/references/TemperatureReading.java new file mode 100644 index 00000000..b533497f --- /dev/null +++ b/references/TemperatureReading.java @@ -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); + } +} diff --git a/verifying/CountedList.java b/verifying/CountedList.java new file mode 100644 index 00000000..23b47f67 --- /dev/null +++ b/verifying/CountedList.java @@ -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 { + private static int counter = 0; + private int id = counter++; + public CountedList() { + System.out.println("CountedList #" + id); + } + public int getId() { return id; } +} diff --git a/verifying/SimpleJUnit.java b/verifying/CountedListTest.java similarity index 86% rename from verifying/SimpleJUnit.java rename to verifying/CountedListTest.java index 5037979e..8ed45d52 100644 --- a/verifying/SimpleJUnit.java +++ b/verifying/CountedListTest.java @@ -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 { - 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() { diff --git a/verifying/Queue.java b/verifying/Queue.java index ca46f450..a2ccd694 100644 --- a/verifying/Queue.java +++ b/verifying/Queue.java @@ -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: diff --git a/verifying/QueueException.java b/verifying/QueueException.java new file mode 100644 index 00000000..276be650 --- /dev/null +++ b/verifying/QueueException.java @@ -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); } +} diff --git a/verifying/QueueTest.java b/verifying/QueueTest.java index 8982132d..ba937447 100644 --- a/verifying/QueueTest.java +++ b/verifying/QueueTest.java @@ -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.*;