Ant Macros and Imports

This commit is contained in:
Bruce Eckel 2015-04-26 13:01:40 -07:00
parent eb26a0c8db
commit f266c9dd7d
4 changed files with 330 additions and 0 deletions

66
Ant-Common.xml Normal file
View File

@ -0,0 +1,66 @@
<project basedir="." name="Thinking in Java Director's Cut by Bruce Eckel">
<property name="chapter" value="CHAPTER?"/>
<description>
Ant build.xml for the source code for chapter ${chapter}
Thinking in Java Director's Cut by Bruce Eckel
Code available at http://www.MindView.net
See installation instructions in README.md
See copyright notice in CopyRight.txt
Ant available from http://ant.apache.org
To see options, type: ant -p
</description>
<macrodef name="jrun">
<attribute name="cls" default="NOT SET"/>
<sequential>
<echo>Running: @{cls}</echo>
<java classname="@{cls}"/>
<echo>Finished: @{cls}</echo>
<echo>--------------------------------</echo>
</sequential>
</macrodef>
<condition property="version1.8">
<equals arg1="1.8" arg2="${ant.java.version}"/>
</condition>
<target name="net_mindview_util">
<javac includeantruntime="false"
classpath="${java.class.path};${basedir};${basedir}/.."
srcdir="${basedir}/../net/mindview/util/">
<compilerarg value="-Xmaxerrs"/>
<compilerarg value="10"/>
</javac>
</target>
<target
depends="net_mindview_util"
description="Build all classes in this directory"
name="build">
<fail message="Java 8 required" unless="version1.8"/>
<echo message="Building '${chapter}'"/>
<javac includeantruntime="false"
classpath="${java.class.path};${basedir};${basedir}/.."
debug="true"
srcdir="${basedir}">
<compilerarg value="-Xmaxerrs"/>
<compilerarg value="10"/>
</javac>
<echo message="Build '${chapter}' succeeded"/>
</target>
<target description="delete all byproducts" name="clean">
<delete>
<fileset dir="${basedir}" includes="**/*.class"/>
<fileset dir="${basedir}" includes="**/*Output.txt"/>
<fileset dir="${basedir}" includes="**/log.txt"/>
<fileset dir="${basedir}" includes="failures"/>
</delete>
<echo message="clean successful"/>
</target>
</project>

139
patterns/build.xml Normal file
View File

@ -0,0 +1,139 @@
<?xml version="1.0" ?>
<project
basedir="."
default="build"
name="Thinking in Java, 4th Edition (Refreshed) by Bruce Eckel (chapter: patterns)">
<description>
build.xml for the source code for the patterns chapter of
Thinking in Java, 4th Edition (Refreshed) by Bruce Eckel
Source code available at http://www.MindView.net
See copyright notice in CopyRight.txt
Ant available from: http://ant.apache.org/
To see options, type: ant -p
</description>
<condition property="version1.8">
<equals arg1="1.8" arg2="${ant.java.version}"/>
</condition>
<target name="net_mindview_util">
<javac includeantruntime="false"
classpath="${java.class.path};${basedir};${basedir}/.."
srcdir="${basedir}/../net/mindview/util/">
<compilerarg value="-Xmaxerrs"/>
<compilerarg value="10"/>
</javac>
</target>
<target
depends="net_mindview_util"
description="Build all classes in this directory"
name="build">
<fail message="J2SE8 required" unless="version1.8"/>
<echo message="Building 'patterns'"/>
<javac includeantruntime="false"
classpath="${java.class.path};${basedir};${basedir}/.."
debug="true"
srcdir="${basedir}">
<compilerarg value="-Xmaxerrs"/>
<compilerarg value="10"/>
</javac>
<echo message="Build 'patterns' succeeded"/>
</target>
<target
depends="build"
description="Compile and run"
name="run">
<touch file="failures"/>
<antcall target="ActiveObjectDemo"/>
<antcall target="AtomicIntegerTest"/>
<antcall target="AtomicityTest"/>
<antcall target="AttemptLocking"/>
<antcall target="BankTellerSimulation"/>
<antcall target="BasicThreads"/>
<antcall target="CachedThreadPool"/>
<antcall target="CallableDemo"/>
<antcall target="CaptureUncaughtException"/>
<antcall target="CarBuilder"/>
<antcall target="CountDownLatchDemo"/>
<antcall target="CriticalSection"/>
<antcall target="DaemonFromFactory"/>
<antcall target="Daemons"/>
<antcall target="DaemonsDontRunFinally"/>
<antcall target="DeadlockingDiningPhilosophers"/>
<antcall target="DelayQueueDemo"/>
<antcall target="EvenGenerator"/>
<antcall target="ExceptionThread"/>
<antcall target="ExchangerDemo"/>
<antcall target="ExplicitCriticalSection"/>
<antcall target="FastSimulation"/>
<antcall target="FixedDiningPhilosophers"/>
<antcall target="FixedThreadPool"/>
<antcall target="GreenhouseScheduler"/>
<antcall target="HorseRace"/>
<antcall target="Interrupting"/>
<antcall target="Interrupting2"/>
<antcall target="InterruptingIdiom"/>
<antcall target="Joining"/>
<antcall target="ListComparisons"/>
<antcall target="MainThread"/>
<antcall target="MapComparisons"/>
<antcall target="MoreBasicThreads"/>
<antcall target="MultiLock"/>
<antcall target="NaiveExceptionHandling"/>
<antcall target="NIOInterruption"/>
<antcall target="NotifyVsNotifyAll"/>
<antcall target="OrnamentalGarden"/>
<antcall target="PipedIO"/>
<antcall target="PriorityBlockingQueueDemo"/>
<antcall target="ReaderWriterList"/>
<antcall target="Restaurant"/>
<antcall target="SelfManaged"/>
<antcall target="SemaphoreDemo"/>
<antcall target="SerialNumberChecker"/>
<antcall target="SettingDefaultHandler"/>
<antcall target="SimpleDaemons"/>
<antcall target="SimpleMicroBenchmark"/>
<antcall target="SimplePriorities"/>
<antcall target="SimpleThread"/>
<antcall target="SingleThreadExecutor"/>
<antcall target="SleepingTask"/>
<antcall target="SynchronizationComparisons"/>
<antcall target="SyncObject"/>
<antcall target="ThreadLocalVariableHolder"/>
<antcall target="ThreadVariations"/>
<antcall target="ToastOMatic"/>
<antcall target="RestaurantWithQueues"/>
<antcall target="WaxOMatic"/>
<antcall target="WaxOMatic2"/>
<antcall target="Atomicity"/>
<echo
message="* AtomicEvenGenerator must be run by hand *"/>
<echo message="* CloseResource must be run by hand *"/>
<echo message="* MutexEvenGenerator must be run by hand *"/>
<echo message="* ResponsiveUI must be run by hand *"/>
<echo
message="* SynchronizedEvenGenerator must be run by hand *"/>
<echo message="* TestBlockingQueues must be run by hand *"/>
<delete file="failures"/>
</target>
<target description="delete all byproducts" name="clean">
<delete>
<fileset dir="${basedir}" includes="**/*.class"/>
<fileset dir="${basedir}" includes="**/*Output.txt"/>
<fileset dir="${basedir}" includes="**/log.txt"/>
<fileset dir="${basedir}" includes="failures"/>
</delete>
<echo message="clean successful"/>
</target>
</project>

35
references/build.xml Normal file
View File

@ -0,0 +1,35 @@
<?xml version="1.0" ?>
<project default="run">
<property name="chapter" value="Passing and Returning Objects"/>
<import file="../Ant-Common.xml"/>
<target
depends="build"
description="Compile and run"
name="run">
<touch file="failures"/>
<jrun cls="AddingClone" />
<jrun cls="Alias1" />
<jrun cls="Alias2" />
<jrun cls="CheckCloneable" />
<jrun cls="Cloning" />
<jrun cls="Compete" />
<jrun cls="CopyConstructor" />
<jrun cls="DeepCopy" />
<jrun cls="HorrorFlick" />
<jrun cls="Immutable1" />
<jrun cls="Immutable2" />
<jrun cls="ImmutableInteger" />
<jrun cls="ImmutableStrings" />
<jrun cls="LocalCopy" />
<jrun cls="MutableInteger" />
<jrun cls="PassReferences" />
<jrun cls="Snake" />
<jrun cls="Stringer" />
<delete file="failures"/>
</target>
</project>

View File

@ -0,0 +1,90 @@
//: unittesting/JUnitDemo.java
// Simple use of JUnit to test ArrayList
// {Depends: junit.jar}
import java.util.*;
import org.junit.Test;
import org.junit.Assert.*;
// So we can see the list objects being created,
// and keep track of when they are 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 JUnitDemo {
private CountedList list = new CountedList();
// You can use the constructor instead of setUp():
public JUnitDemo() {
for(int i = 0; i < 3; i++)
list.add("" + i);
}
// Thus, setUp() is optional, but is run right
// before the test:
protected void setUp() {
System.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() {
System.out.println("Tearing down " + list.getId());
}
// All tests have method names beginning with "test":
@Test
public void testInsert() {
System.out.println("Running testInsert()");
org.junit.Assert.assertEquals(list.size(), 3);
list.add(1, "Insert");
org.junit.Assert.assertEquals(list.size(), 4);
org.junit.Assert.assertEquals(list.get(1), "Insert");
}
@Test
public void testReplace() {
System.out.println("Running testReplace()");
org.junit.Assert.assertEquals(list.size(), 3);
list.set(1, "Replace");
org.junit.Assert.assertEquals(list.size(), 3);
org.junit.Assert.assertEquals(list.get(1), "Replace");
}
// A "helper" method to reduce code duplication. As long
// as the name doesn't start with "test," it will not
// be automatically executed by JUnit.
private void compare(ArrayList lst, String[] strs) {
Object[] array = lst.toArray();
org.junit.Assert.assertTrue("Arrays not the same length",
array.length == strs.length);
for(int i = 0; i < array.length; i++)
org.junit.Assert.assertEquals(strs[i], (String)array[i]);
}
@Test
public void testOrder() {
System.out.println("Running testOrder()");
compare(list, new String[] { "0", "1", "2" });
}
@Test
public void testRemove() {
System.out.println("Running testRemove()");
org.junit.Assert.assertEquals(list.size(), 3);
list.remove(1);
org.junit.Assert.assertEquals(list.size(), 2);
compare(list, new String[] { "0", "2" });
}
@Test
public void testAddAll() {
System.out.println("Running testAddAll()");
list.addAll(Arrays.asList(new Object[] {
"An", "African", "Swallow"}));
org.junit.Assert.assertEquals(list.size(), 6);
compare(list, new String[] { "0", "1", "2",
"An", "African", "Swallow" });
}
public static void main(String[] args) {
// Invoke JUnit on the class:
org.junit.runner.JUnitCore.runClasses(JUnitDemo.class);
}
} ///:~