carfield.com.hkRunUnitTests.java2001-12-26T16:00:00Z2001-12-26T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: com:bruceeckel:test:RunUnitTests.java
// Discovering the unit test
// class and running each test.
package com.bruceeckel.test;
import java.lang.reflect.*;
import java.util.Iterator;
public class RunUnitTests {
public static void
require(boolean requirement, String errmsg) {
if(!requirement) {
System.err.println(errmsg);
System.exit(1);
}
}
public static void main(String[] args) {
require(args.length == 1,
"Usage: RunUnitTests qualified-class");
try {
Class c = Class.forName(args[0]);
// Only finds the inner classes
// declared in the current class:
Class[] classes = c.getDeclaredClasses();
Class ut = null;
for(int j = 0; j < classes.length; j++) {
// Skip inner classes that are
// not derived from UnitTest:
if(!UnitTest.class.
isAssignableFrom(classes[j]))
continue;
ut = classes[j];
break; // Finds the first test class only
}
// If it found an inner class,
// that class must be static:
if(ut != null)
require(
Modifier.isStatic(ut.getModifiers()),
"inner UnitTest class must be static");
// If it couldn't find the inner class,
// maybe it's a regular class (for black-
// box testing:
if(ut == null)
if(UnitTest.class.isAssignableFrom(c))
ut = c;
require(ut != null,
"No UnitTest class found");
require(
Modifier.isPublic(ut.getModifiers()),
"UnitTest class must be public");
Method[] methods = ut.getDeclaredMethods();
for(int k = 0; k < methods.length; k++) {
Method m = methods[k];
// Ignore overridden UnitTest methods:
if(m.getName().equals("cleanup"))
continue;
// Only public methods with no
// arguments and void return
// types will be used as test code:
if(m.getParameterTypes().length == 0 &&
m.getReturnType() == void.class &&
Modifier.isPublic(m.getModifiers())) {
// The name of the test is
// used in error messages:
UnitTest.testID = m.getName();
// A new instance of the
// test object is created and
// cleaned up for each test:
Object test = ut.newInstance();
m.invoke(test, new Object[0]);
((UnitTest)test).cleanup();
}
}
} catch(Exception e) {
e.printStackTrace(System.err);
// Any exception will return a nonzero
// value to the console, so that
// 'make' will abort:
System.err.println("Aborting make");
System.exit(1);
}
// After all tests in this class are run,
// display any results. If there were errors,
// abort 'make' by returning a nonzero value.
if(UnitTest.errors.size() != 0) {
Iterator it = UnitTest.errors.iterator();
while(it.hasNext())
System.err.println(it.next());
System.exit(1);
}
}
} ///:~
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2001-12-26T16:00:00ZUnitTest.java2001-12-26T16:00:00Z2001-12-26T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">//: com:bruceeckel:test:UnitTest.java
// The basic unit testing class
package com.bruceeckel.test;
import java.util.*;
public class UnitTest {
static String testID;
static List errors = new ArrayList();
// Override cleanup() if test object
// creation allocates non-memory
// resources that must be cleaned up:
protected void cleanup() {}
// Verify the truth of a condition:
protected final void affirm(boolean condition){
if(!condition)
errors.add("failed: " + testID);
}
} ///:~
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2001-12-26T16:00:00Zmakefile2001-12-26T16:00:00Z2001-12-26T16:00:00Z<br/><TEXTAREA name="code" class="" rows="16" cols="100"># From Thinking in Patterns (with Java) by Bruce Eckel
# At http://www.BruceEckel.com
# (c)2001 Bruce Eckel
# Copyright notice in Copyright.txt
# Automatically-generated MAKEFILE
# For examples in directory .\com\bruceeckel\test
# using the JDK 1.3 compiler
# Invoke with: make
HOME := ../../../
ifndef MAKECMDGOALS
MAKECMDGOALS := javac
endif
# Command.com is too weak to build this under Windows NT/2000:
ifeq ($(OS),Windows_NT)
COMSPEC=$(SYSTEMROOT)\system32\cmd.exe
endif
ifneq ($(MAKECMDGOALS),clean)
include $(HOME)/$(MAKECMDGOALS).mac
endif
.SUFFIXES : .class .java
.java.class :
$(JVC) $(JVCFLAGS) $<
javac: \
UnitTest.class \
RunUnitTests.class
jikes: \
UnitTest.class \
RunUnitTests.class
clean:
ifeq ($(notdir $(SHELL)),COMMAND.COM)
del *.class
else
rm -f *.class
endif
UnitTest.class: UnitTest.java
RunUnitTests.class: RunUnitTests.java
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2001-12-26T16:00:00Z