Skip to content

Commit f266c9d

Browse files
committed
Ant Macros and Imports
1 parent eb26a0c commit f266c9d

4 files changed

Lines changed: 330 additions & 0 deletions

File tree

Ant-Common.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<project basedir="." name="Thinking in Java Director's Cut by Bruce Eckel">
2+
3+
<property name="chapter" value="CHAPTER?"/>
4+
5+
<description>
6+
Ant build.xml for the source code for chapter ${chapter}
7+
Thinking in Java Director's Cut by Bruce Eckel
8+
Code available at http://www.MindView.net
9+
See installation instructions in README.md
10+
See copyright notice in CopyRight.txt
11+
12+
Ant available from http://ant.apache.org
13+
14+
To see options, type: ant -p
15+
</description>
16+
17+
<macrodef name="jrun">
18+
<attribute name="cls" default="NOT SET"/>
19+
<sequential>
20+
<echo>Running: @{cls}</echo>
21+
<java classname="@{cls}"/>
22+
<echo>Finished: @{cls}</echo>
23+
<echo>--------------------------------</echo>
24+
</sequential>
25+
</macrodef>
26+
27+
<condition property="version1.8">
28+
<equals arg1="1.8" arg2="${ant.java.version}"/>
29+
</condition>
30+
31+
<target name="net_mindview_util">
32+
<javac includeantruntime="false"
33+
classpath="${java.class.path};${basedir};${basedir}/.."
34+
srcdir="${basedir}/../net/mindview/util/">
35+
<compilerarg value="-Xmaxerrs"/>
36+
<compilerarg value="10"/>
37+
</javac>
38+
</target>
39+
40+
<target
41+
depends="net_mindview_util"
42+
description="Build all classes in this directory"
43+
name="build">
44+
<fail message="Java 8 required" unless="version1.8"/>
45+
<echo message="Building '${chapter}'"/>
46+
<javac includeantruntime="false"
47+
classpath="${java.class.path};${basedir};${basedir}/.."
48+
debug="true"
49+
srcdir="${basedir}">
50+
<compilerarg value="-Xmaxerrs"/>
51+
<compilerarg value="10"/>
52+
</javac>
53+
<echo message="Build '${chapter}' succeeded"/>
54+
</target>
55+
56+
<target description="delete all byproducts" name="clean">
57+
<delete>
58+
<fileset dir="${basedir}" includes="**/*.class"/>
59+
<fileset dir="${basedir}" includes="**/*Output.txt"/>
60+
<fileset dir="${basedir}" includes="**/log.txt"/>
61+
<fileset dir="${basedir}" includes="failures"/>
62+
</delete>
63+
<echo message="clean successful"/>
64+
</target>
65+
66+
</project>

patterns/build.xml

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?xml version="1.0" ?>
2+
3+
<project
4+
basedir="."
5+
default="build"
6+
name="Thinking in Java, 4th Edition (Refreshed) by Bruce Eckel (chapter: patterns)">
7+
8+
<description>
9+
build.xml for the source code for the patterns chapter of
10+
Thinking in Java, 4th Edition (Refreshed) by Bruce Eckel
11+
Source code available at http://www.MindView.net
12+
See copyright notice in CopyRight.txt
13+
14+
Ant available from: http://ant.apache.org/
15+
16+
To see options, type: ant -p
17+
</description>
18+
19+
<condition property="version1.8">
20+
<equals arg1="1.8" arg2="${ant.java.version}"/>
21+
</condition>
22+
23+
<target name="net_mindview_util">
24+
<javac includeantruntime="false"
25+
classpath="${java.class.path};${basedir};${basedir}/.."
26+
srcdir="${basedir}/../net/mindview/util/">
27+
<compilerarg value="-Xmaxerrs"/>
28+
<compilerarg value="10"/>
29+
</javac>
30+
</target>
31+
32+
<target
33+
depends="net_mindview_util"
34+
description="Build all classes in this directory"
35+
name="build">
36+
<fail message="J2SE8 required" unless="version1.8"/>
37+
<echo message="Building 'patterns'"/>
38+
<javac includeantruntime="false"
39+
classpath="${java.class.path};${basedir};${basedir}/.."
40+
debug="true"
41+
srcdir="${basedir}">
42+
<compilerarg value="-Xmaxerrs"/>
43+
<compilerarg value="10"/>
44+
</javac>
45+
<echo message="Build 'patterns' succeeded"/>
46+
</target>
47+
48+
49+
<target
50+
depends="build"
51+
description="Compile and run"
52+
name="run">
53+
<touch file="failures"/>
54+
<antcall target="ActiveObjectDemo"/>
55+
<antcall target="AtomicIntegerTest"/>
56+
<antcall target="AtomicityTest"/>
57+
<antcall target="AttemptLocking"/>
58+
<antcall target="BankTellerSimulation"/>
59+
<antcall target="BasicThreads"/>
60+
<antcall target="CachedThreadPool"/>
61+
<antcall target="CallableDemo"/>
62+
<antcall target="CaptureUncaughtException"/>
63+
<antcall target="CarBuilder"/>
64+
<antcall target="CountDownLatchDemo"/>
65+
<antcall target="CriticalSection"/>
66+
<antcall target="DaemonFromFactory"/>
67+
<antcall target="Daemons"/>
68+
<antcall target="DaemonsDontRunFinally"/>
69+
<antcall target="DeadlockingDiningPhilosophers"/>
70+
<antcall target="DelayQueueDemo"/>
71+
<antcall target="EvenGenerator"/>
72+
<antcall target="ExceptionThread"/>
73+
<antcall target="ExchangerDemo"/>
74+
<antcall target="ExplicitCriticalSection"/>
75+
<antcall target="FastSimulation"/>
76+
<antcall target="FixedDiningPhilosophers"/>
77+
<antcall target="FixedThreadPool"/>
78+
<antcall target="GreenhouseScheduler"/>
79+
<antcall target="HorseRace"/>
80+
<antcall target="Interrupting"/>
81+
<antcall target="Interrupting2"/>
82+
<antcall target="InterruptingIdiom"/>
83+
<antcall target="Joining"/>
84+
<antcall target="ListComparisons"/>
85+
<antcall target="MainThread"/>
86+
<antcall target="MapComparisons"/>
87+
<antcall target="MoreBasicThreads"/>
88+
<antcall target="MultiLock"/>
89+
<antcall target="NaiveExceptionHandling"/>
90+
<antcall target="NIOInterruption"/>
91+
<antcall target="NotifyVsNotifyAll"/>
92+
<antcall target="OrnamentalGarden"/>
93+
<antcall target="PipedIO"/>
94+
<antcall target="PriorityBlockingQueueDemo"/>
95+
<antcall target="ReaderWriterList"/>
96+
<antcall target="Restaurant"/>
97+
<antcall target="SelfManaged"/>
98+
<antcall target="SemaphoreDemo"/>
99+
<antcall target="SerialNumberChecker"/>
100+
<antcall target="SettingDefaultHandler"/>
101+
<antcall target="SimpleDaemons"/>
102+
<antcall target="SimpleMicroBenchmark"/>
103+
<antcall target="SimplePriorities"/>
104+
<antcall target="SimpleThread"/>
105+
<antcall target="SingleThreadExecutor"/>
106+
<antcall target="SleepingTask"/>
107+
<antcall target="SynchronizationComparisons"/>
108+
<antcall target="SyncObject"/>
109+
<antcall target="ThreadLocalVariableHolder"/>
110+
<antcall target="ThreadVariations"/>
111+
<antcall target="ToastOMatic"/>
112+
<antcall target="RestaurantWithQueues"/>
113+
<antcall target="WaxOMatic"/>
114+
<antcall target="WaxOMatic2"/>
115+
<antcall target="Atomicity"/>
116+
<echo
117+
message="* AtomicEvenGenerator must be run by hand *"/>
118+
<echo message="* CloseResource must be run by hand *"/>
119+
<echo message="* MutexEvenGenerator must be run by hand *"/>
120+
<echo message="* ResponsiveUI must be run by hand *"/>
121+
<echo
122+
message="* SynchronizedEvenGenerator must be run by hand *"/>
123+
<echo message="* TestBlockingQueues must be run by hand *"/>
124+
<delete file="failures"/>
125+
</target>
126+
127+
<target description="delete all byproducts" name="clean">
128+
<delete>
129+
<fileset dir="${basedir}" includes="**/*.class"/>
130+
<fileset dir="${basedir}" includes="**/*Output.txt"/>
131+
<fileset dir="${basedir}" includes="**/log.txt"/>
132+
<fileset dir="${basedir}" includes="failures"/>
133+
</delete>
134+
<echo message="clean successful"/>
135+
</target>
136+
137+
</project>
138+
139+

references/build.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" ?>
2+
3+
<project default="run">
4+
<property name="chapter" value="Passing and Returning Objects"/>
5+
<import file="../Ant-Common.xml"/>
6+
7+
<target
8+
depends="build"
9+
description="Compile and run"
10+
name="run">
11+
<touch file="failures"/>
12+
<jrun cls="AddingClone" />
13+
<jrun cls="Alias1" />
14+
<jrun cls="Alias2" />
15+
<jrun cls="CheckCloneable" />
16+
<jrun cls="Cloning" />
17+
<jrun cls="Compete" />
18+
<jrun cls="CopyConstructor" />
19+
<jrun cls="DeepCopy" />
20+
<jrun cls="HorrorFlick" />
21+
<jrun cls="Immutable1" />
22+
<jrun cls="Immutable2" />
23+
<jrun cls="ImmutableInteger" />
24+
<jrun cls="ImmutableStrings" />
25+
<jrun cls="LocalCopy" />
26+
<jrun cls="MutableInteger" />
27+
<jrun cls="PassReferences" />
28+
<jrun cls="Snake" />
29+
<jrun cls="Stringer" />
30+
<delete file="failures"/>
31+
</target>
32+
33+
</project>
34+
35+

unittesting/JUnitDemo.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//: unittesting/JUnitDemo.java
2+
// Simple use of JUnit to test ArrayList
3+
// {Depends: junit.jar}
4+
import java.util.*;
5+
import org.junit.Test;
6+
import org.junit.Assert.*;
7+
8+
// So we can see the list objects being created,
9+
// and keep track of when they are cleaned up:
10+
class CountedList extends ArrayList {
11+
private static int counter = 0;
12+
private int id = counter++;
13+
public CountedList() {
14+
System.out.println("CountedList #" + id);
15+
}
16+
public int getId() { return id; }
17+
}
18+
19+
public class JUnitDemo {
20+
private CountedList list = new CountedList();
21+
// You can use the constructor instead of setUp():
22+
public JUnitDemo() {
23+
for(int i = 0; i < 3; i++)
24+
list.add("" + i);
25+
}
26+
// Thus, setUp() is optional, but is run right
27+
// before the test:
28+
protected void setUp() {
29+
System.out.println("Set up for " + list.getId());
30+
}
31+
// tearDown() is also optional, and is called after
32+
// each test. setUp() and tearDown() can be either
33+
// protected or public:
34+
public void tearDown() {
35+
System.out.println("Tearing down " + list.getId());
36+
}
37+
// All tests have method names beginning with "test":
38+
@Test
39+
public void testInsert() {
40+
System.out.println("Running testInsert()");
41+
org.junit.Assert.assertEquals(list.size(), 3);
42+
list.add(1, "Insert");
43+
org.junit.Assert.assertEquals(list.size(), 4);
44+
org.junit.Assert.assertEquals(list.get(1), "Insert");
45+
}
46+
@Test
47+
public void testReplace() {
48+
System.out.println("Running testReplace()");
49+
org.junit.Assert.assertEquals(list.size(), 3);
50+
list.set(1, "Replace");
51+
org.junit.Assert.assertEquals(list.size(), 3);
52+
org.junit.Assert.assertEquals(list.get(1), "Replace");
53+
}
54+
// A "helper" method to reduce code duplication. As long
55+
// as the name doesn't start with "test," it will not
56+
// be automatically executed by JUnit.
57+
private void compare(ArrayList lst, String[] strs) {
58+
Object[] array = lst.toArray();
59+
org.junit.Assert.assertTrue("Arrays not the same length",
60+
array.length == strs.length);
61+
for(int i = 0; i < array.length; i++)
62+
org.junit.Assert.assertEquals(strs[i], (String)array[i]);
63+
}
64+
@Test
65+
public void testOrder() {
66+
System.out.println("Running testOrder()");
67+
compare(list, new String[] { "0", "1", "2" });
68+
}
69+
@Test
70+
public void testRemove() {
71+
System.out.println("Running testRemove()");
72+
org.junit.Assert.assertEquals(list.size(), 3);
73+
list.remove(1);
74+
org.junit.Assert.assertEquals(list.size(), 2);
75+
compare(list, new String[] { "0", "2" });
76+
}
77+
@Test
78+
public void testAddAll() {
79+
System.out.println("Running testAddAll()");
80+
list.addAll(Arrays.asList(new Object[] {
81+
"An", "African", "Swallow"}));
82+
org.junit.Assert.assertEquals(list.size(), 6);
83+
compare(list, new String[] { "0", "1", "2",
84+
"An", "African", "Swallow" });
85+
}
86+
public static void main(String[] args) {
87+
// Invoke JUnit on the class:
88+
org.junit.runner.JUnitCore.runClasses(JUnitDemo.class);
89+
}
90+
} ///:~

0 commit comments

Comments
 (0)