Begin Listing One - Assertion
class
/**
* A class for asserting program correctness.
* @version 1.0 Jan 23, 1999
* @author Chris Berry
*/
package util;
import java.io.*;
public class Assertion
{
public static void setHaltOnError(boolean TF)
{
sHaltOnError = TF;
}
public static void setPrintWriter(PrintWriter pw)
{
sOutfile = pw;
}
/** @conditional(DEBUG) */
public static void assert(boolean bool, String description)
throws Assertion.Exception
{
if (!bool)
{
if (sHaltOnError)
{
if (sOutfile == null)
sOutfile = new PrintWriter(sPrintStream, true);
sOutfile.println("Assertion [" + description + "] fired at:: ");
printStackTrace(new Throwable());
com.ms.debug.Debugger.breakpoint();
System.exit(1);
}
else
{
throw new Assertion.Exception(description);
}
}
}
/** @conditional(DEBUG) */
public static void assert(boolean bool) throws Assertion.Exception
{
assert(bool, "Failed assertion");
}
/** @conditional(DEBUG) */
public static void fail() throws Assertion.Exception
{
assert(true, "Explicit Failure");
}
/** @conditional(DEBUG) */
public static void fail(String description) throws Assertion.Exception
{
assert(true, description);
}
/** @conditional(DEBUG) */
public static void preCondition(boolean bool) throws Assertion.Exception
{
assert(bool, "Failed preCondition");
}
/** @conditional(DEBUG) */
public static void preCondition(boolean bool, String desc)
throws Assertion.Exception
{
assert(bool,desc);
}
/** @conditional(DEBUG) */
public static void postCondition(boolean bool)
throws Assertion.Exception
{
assert(bool, "Failed postCondition");
}
/** @conditional(DEBUG) */
public static void postCondition(boolean bool, String desc)
throws Assertion.Exception
{
assert(bool,desc);
}
public static String getStackTrace(Throwable tt)
{
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintWriter ps = new PrintWriter(os, true);
tt.printStackTrace(ps);
return os.toString();
}
public static void printStackTrace(Throwable tt)
{
if (sOutfile == null)
sOutfile = new PrintWriter(sPrintStream, true);
sOutfile.println(getStackTrace(tt));
}
public static void printMessage(Throwable tt)
{
if (sOutfile == null)
sOutfile = new PrintWriter(sPrintStream, true);
sOutfile.println("Unhandled Exception:: " + "message = " +
tt.getMessage());
}
// -----------------------------------------
// Data
// -----------------------------------------
private static boolean sHaltOnError = true;
private static PrintWriter sOutfile = null;
static public PrintStream sPrintStream = com.ms.debug.Debugger.out;
// -----------------------------------------
// Exceptions
// -----------------------------------------
static public class Exception extends RuntimeException
{
Exception()
{
this("Assertion exception");
}
Exception(String information)
{
super(information);
}
}
}
End Listing One
Begin
Listing Two - DebugPrinter class
/**
* A class for qualified debug printing
* @version 1.0 Jan 23, 1999
* @author Chris Berry
*/
package util;
import java.io.*;
public class DebugPrinter implements ISanityCheck
{
/** Note:: you can turn on/off global printing,
* yet still have control at a local print level.
*/
static public void setGlobalPrinting(boolean onOff)
{
sDoPrinting = onOff;
}
static public void setGlobalPrintWriter(PrintWriter pw)
{
Assertion.preCondition(pw != null);
sOutFile = pw;
}
/** @conditional(DEBUG) */
public void println(String str)
{
Assertion.preCondition(str != null);
Assertion.assert(isSane());
if (mDoPrinting)
{
if (sDoPrinting)
{
sOutFile.println(str);
}
else
{
mOutFile.println(str);
}
}
}
public void setPrinting(boolean onOff)
{
mDoPrinting = onOff;
}
public void setPrintWriter(PrintWriter pw)
{
Assertion.preCondition(pw != null);
mOutFile = pw;
}
public DebugPrinter(boolean doPrinting)
{
mDoPrinting = doPrinting;
mOutFile = new PrintWriter(sPrintStream, true);
}
public DebugPrinter(boolean doPrinting, PrintWriter pw)
{
Assertion.preCondition(pw != null);
mDoPrinting = doPrinting;
mOutFile = pw;
}
public DebugPrinter(boolean doPrinting, String filename)
{
mDoPrinting = doPrinting;
if (filename == null || filename.length() == 0)
mOutFile = new PrintWriter(sPrintStream, true);
else
{
try
{
FileOutputStream ffos = new FileOutputStream(filename, true);
mOutFile = new PrintWriter(ffos, true);
}
catch (IOException ex)
{
Assertion.fail(ex.getMessage());
}
}
}
public boolean isSane()
{
if ((mDoPrinting && sDoPrinting) && sOutFile == null)
return false;
if ((mDoPrinting && !sDoPrinting) && mOutFile == null)
return false;
return true;
}
// -----------------------------------------
// Data
// -----------------------------------------
static private boolean sDoPrinting = false;
static private PrintWriter sOutFile = null;
private boolean mDoPrinting = false;
private PrintWriter mOutFile = null;
static public PrintStream sPrintStream = com.ms.debug.Debugger.out;
// -----------------------------------------
// Unit Test Support
// -----------------------------------------
public static void main(String[] args)
{
PrintWriter outFile = null;
if (args.length == 0 || args[0] == null)
outFile = new PrintWriter(sPrintStream, true);
else
{
try
{
FileOutputStream ffos = new FileOutputStream(args[0], true);
outFile = new PrintWriter(ffos);
}
catch (IOException ex)
{
Assertion.fail(ex.getMessage());
}
}
unitTest(outFile);
outFile.close();
}
public static void unitTest(PrintWriter outFile)
{
Assertion.setHaltOnError(false);
Assertion.setPrintWriter(outFile);
DebugPrinter dp = new DebugPrinter(false);
DebugPrinter.UnitTester unitTester = dp.new UnitTester();
unitTester.test(outFile);
outFile.flush();
}
public class UnitTester
{
public void test(PrintWriter outFile)
{
outFile.println(">>>>>>>>>>>>>>>>>>>");
outFile.println("Begin DebugPrinter " + "Unit Test");
// Try to execute all of the individual Unit Tests.
try
{
setGlobalPrinting_test(outFile);
setGlobalPrintWriter_test(outFile);
setPrinting_test(outFile);
setPrintWriter_test(outFile);
ctor1_test_test(outFile);
ctor2_test_test(outFile);
ctor3_test_test(outFile);
println_test(outFile);
outFile.println("End DebugPrinter Unit " + "Test ... Successful");
}
// Catch any Exceptions not handled, or thrown
// by the Unit Tests. Any Execption that bubbles up to here
// represents failure.
catch (Exception ex)
{
outFile.println("Unhandled Exception:: " + ex.getMessage());
String trace = Assertion.getStackTrace(ex);
outFile.println(trace);
outFile.println("WARNING:: DebugPrinter" + " Unit Test... FAILED");
}
outFile.println("<<<<<<<<<<<<<<<<<<<<<<<");
}
void setGlobalPrinting_test(PrintWriter outFile)
{
boolean savedBool = sDoPrinting;
boolean testBool[] = { true, false };
for (int i = 0; i < testBool.length; i++)
{
setGlobalPrinting(testBool[i]);
Assertion.assert(sDoPrinting == testBool[i]);
}
setGlobalPrinting(savedBool);
outFile.println("setGlobalPrinting_test " + " successful");
}
void setGlobalPrintWriter_test(PrintWriter out)
{
PrintWriter savedPW = sOutFile;
PrintWriter[] testPW = { null, new PrintWriter(System.out) };
PrintWriter pw = null;
try
{
for (int i = 0; i < testPW.length; i++)
{
pw = testPW[i];
setGlobalPrintWriter(pw);
Assertion.assert(sOutFile == pw);
}
}
catch (Assertion.Exception ex)
{
Assertion.assert(pw == null);
}
setGlobalPrintWriter(savedPW);
out.println("setGlobalPrintWriter_test" + " successful");
}
void setPrinting_test(PrintWriter out)
{
boolean testBool[] = { true, false };
DebugPrinter OUT = new DebugPrinter(false);
for (int i = 0; i < testBool.length; i++)
{
OUT.setPrinting(testBool[i]);
Assertion.assert(OUT.mDoPrinting == testBool[i] );
}
out.println("setPrinting_test successful");
}
void setPrintWriter_test(PrintWriter out)
{
PrintWriter[] testPW = { null, new PrintWriter(System.out) };
DebugPrinter OUT = new DebugPrinter(false);
PrintWriter pw = null;
try
{
for (int i = 0; i < testPW.length; i++)
{
pw = testPW[i];
OUT.setPrintWriter(pw);
Assertion.assert(OUT.mOutFile == pw);
}
}
catch (Assertion.Exception ex)
{
Assertion.assert(pw == null);
}
out.println("setPrintWriter_test successful");
}
void ctor1_test_test(PrintWriter outFile)
{
DebugPrinter OUT = new DebugPrinter(true);
Assertion.assert(OUT.isSane());
Assertion.assert(OUT.mDoPrinting == true);
OUT = new DebugPrinter(false);
Assertion.assert(OUT.isSane());
Assertion.assert(OUT.mDoPrinting == false);
outFile.println("ctor1_test_test successful");
}
void ctor2_test_test(PrintWriter outFile)
{
boolean testBool[] = { true, false };
PrintWriter[] testPW = { null, new PrintWriter(System.out) };
Exception excep = null;
for (int i = 0; i < testBool.length; i++)
{
for (int j = 0; j < testPW.length; j++)
{
boolean hitException = false;
DebugPrinter OUT = null;
try
{
OUT = new DebugPrinter(testBool[i], testPW[j] );
}
catch (Exception exout)
{
hitException = true;
excep = exout;
}
if (hitException)
{
if (excep instanceof Assertion.Exception)
{
Assertion.assert((testPW[j] == null), "Unknown assert thrown");
}
else
Assertion.fail(excep.getMessage());
}
else
{
Assertion.assert(OUT.isSane());
Assertion.assert(OUT.mDoPrinting == testBool[i]);
Assertion.assert(OUT.mOutFile == testPW[j]);
}
}
}
outFile.println("ctor2_test_test successful");
}
void ctor3_test_test(PrintWriter outFile)
{
boolean testBool[] = { true, false };
String[] testStr = { null, ", "c:/temp/testDebugPrinter.out" };
Exception excep = null;
for (int i = 0; i < testBool.length; i++)
{
for (int j = 0; j < testStr.length; j++)
{
boolean hitException = false;
DebugPrinter OUT = null;
try
{
OUT = new DebugPrinter(testBool[i], testStr[j]);
}
catch (Exception exout)
{
hitException = true;
excep = exout;
}
if (hitException)
{
if (excep instanceof Assertion.Exception)
{
Assertion.assert((testStr[j] == null ||
testStr[j].length() == 0),
"Unknown assert thrown");
}
else
Assertion.fail(excep.getMessage());
}
else
{
Assertion.assert(OUT.isSane());
Assertion.assert(OUT.mDoPrinting == testBool[i]);
}
}
}
outFile.println("ctor3_test_test successful");
}
void println_test(PrintWriter outFile)
{
String filename = "c:/temp/testDebugPrinter.out";
File testFile = new File(filename);
if (testFile.exists())
testFile.delete();
DebugPrinter OUT = new DebugPrinter(true, filename);
Assertion.assert(OUT.isSane());
try
{
boolean save = sDoPrinting;
OUT.setGlobalPrinting(false);
OUT.println("This is a test");
BufferedReader br = new BufferedReader(new FileReader(testFile));
String str = br.readLine();
Assertion.assert((str.equals("This is a test")));
sDoPrinting = save;
}
catch (Exception ex)
{
Assertion.fail(ex.getMessage());
}
outFile.println("println_test successful");
}
} // End of inner class Tester.
}
End
Listing Two
Begin Listing Three -
Dog class
/**
* Contrived Example class to demonstrate Preemtive and Unit testing.
* @version 1.0 Jan 23, 1999
* @author Chris Berry
*/
import java.io.*;
import util.*;
public class Dog implements ISanityCheck
{
// -----------------------------------------
// Public Interface
// -----------------------------------------
public void eat(String typeOfFood, int amountToEat)
throws OutOfFoodException
{
sDebug.println("Dog.eat ==> amountToEat= " + amountToEat +
" typeOfFood= " + typeOfFood + " mIsBarking = " +
mIsBarking);
Assertion.preCondition(amountToEat >= 0);
Assertion.preCondition(!mIsBarking);
Assertion.preCondition(isTypeOfFood(typeOfFood));
int food = getFoodAmount(typeOfFood);
if (food == 0)
throw new OutOfFoodException();
else if (food < amountToEat)
food = 0;
else
food -= amountToEat;
setFoodAmount(typeOfFood, food);
sDebug.println("... food= " + food);
Assertion.postCondition(isSane());
}
public void feed(String typeOfFood, int amountToFeed)
{
sDebug.println("Dog.feed ==> amountToFeed= " + amountToFeed +
" typeOfFood= " + typeOfFood);
Assertion.preCondition(amountToFeed >= 0);
Assertion.preCondition(isTypeOfFood(typeOfFood));
int food = getFoodAmount(typeOfFood);
food += amountToFeed;
setFoodAmount(typeOfFood, food);
sDebug.println("...food= " + food);
Assertion.postCondition(isSane());
}
public void bark(boolean barkState)
{
mIsBarking = barkState;
}
public boolean isBarking()
{
return mIsBarking;
}
public boolean isSane()
{
if (mFood.length <= 0)
return false;
for (int i = 0; i < mFood.length; i++)
{
if (! mFood[i].isSane())
return false;
}
return true;
}
// -----------------------------------------
// Private Interface
// -----------------------------------------
private Food getFood(String typeOfFood)
{
Assertion.preCondition(typeOfFood != null &&
typeOfFood.length() > 0);
Food dogfood = null;
for (int i = 0; i < mFood.length; i++)
{
if (typeOfFood.equals(mFood[i].mType))
{
dogfood = mFood[i];
break;
}
}
return dogfood;
}
private boolean isTypeOfFood(String typeOfFood)
{
return ((getFood(typeOfFood) == null) ? false : true);
}
private void setFoodAmount(String typeOfFood, int amount)
{
Assertion.preCondition(isTypeOfFood(typeOfFood));
Assertion.preCondition(amount >= 0);
Food dogfood = getFood(typeOfFood);
dogfood.mAmount = amount;
}
private int getFoodAmount(String typeOfFood)
{
Assertion.preCondition(isTypeOfFood(typeOfFood));
Food dogfood = getFood(typeOfFood);
return dogfood.mAmount;
}
// -----------------------------------------
// Data
// -----------------------------------------
static private class Food implements ISanityCheck
{
Food(String type)
{
mType = type;
}
String mType = null;
int mAmount = 0;
public boolean isSane()
{
if (mAmount < 0)
return false;
if (mType == null || mType.length() < 0)
return false;
return true;
}
}
private Food[] mFood = { new Dog.Food("doggie biscuits"),
new Dog.Food("Ken-L-Ration") };
private boolean mIsBarking = false;
static private DebugPrinter sDebug = new DebugPrinter(false);
// -----------------------------------------
// Exceptions
// -----------------------------------------
static public class OutOfFoodException extends Exception
{
OutOfFoodException()
{
this("OutOfFoodException");
}
OutOfFoodException(String information)
{
super(information);
}
}
// -----------------------------------------
// Unit Test Support
// -----------------------------------------
static public void main(String[] args)
{
PrintWriter outFile = null;
if (args.length == 0 || args[0] == null)
outFile = new PrintWriter(com.ms.debug.Debugger.out, true);
else
{
try
{
FileOutputStream ffos = new FileOutputStream(args[0], true);
outFile = new PrintWriter(ffos);
}
catch (IOException ex)
{
Assertion.fail();
}
}
unitTest(outFile);
outFile.close();
}
public static void unitTest(PrintWriter outFile)
{
// Don't exit on errors
Assertion.setHaltOnError(false);
// For this Unit Test, turn on debug printing.
// This will yield richer output for Regression Testing.
sDebug.setPrinting(true);
sDebug.setPrintWriter(outFile);
Assertion.setPrintWriter(outFile);
// Note the peculiar syntax for creating our inner UnitTester.
Dog fido = new Dog();
Dog.UnitTester unitTester = fido.new UnitTester();
unitTester.test(outFile);
outFile.flush();
}
class UnitTester
{
void test(PrintWriter outFile)
{
outFile.println(">>>>>>>>>>>>>>>>>>>");
outFile.println("Begin Dog Unit Test");
// Try to execute all of the individual Unit Tests.
try
{
Dog_CTOR_test(outFile);
isBarking_test(outFile);
bark_test(outFile);
feed_test(outFile);
eat_test(outFile);
outFile.println("End Dog Unit Test..." + " Successful");
}
// Catch any Exceptions not handled, or thrown
// by the Unit Tests. Any Execption that bubbles up to here
// represents failure.
catch (Exception ex)
{
outFile.println("Unhandled Exception:: " + ex.getMessage());
String trace = Assertion.getStackTrace(ex);
outFile.println(trace);
outFile.println("WARNING ::::: Dog " + " Unit Test... FAILED");
}
outFile.println("<<<<<<<<<<<<<<<<<<<<<");
}
void Dog_CTOR_test(PrintWriter outFile)
{
Dog OUT = new Dog();
Assertion.assert(OUT.isSane());
Assertion.assert(!OUT.mIsBarking);
outFile.println("Dog_CTOR_test successful");
}
void isBarking_test(PrintWriter outFile)
{
Dog OUT = new Dog();
Assertion.assert(!OUT.isBarking());
OUT.mIsBarking = true;
Assertion.assert(OUT.isBarking());
outFile.println("isBarking_test successful");
}
void bark_test(PrintWriter outFile)
{
Dog OUT = new Dog();
OUT.bark(true);
Assertion.assert(OUT.isBarking());
OUT.bark(false);
Assertion.assert(!OUT.isBarking());
outFile.println("bark_test successful");
}
void feed_test(PrintWriter outFile)
{
int[] foodAmount = { Integer.MIN_VALUE, -1, 0, 1, 1000,
Integer.MAX_VALUE };
String[] foodType = { null, ", "unknown", "doggie biscuits",
"Ken-L-Ration" };
Exception excep = null;
for (int i = 0; i < foodAmount.length; i++)
{
for (int j = 0; j < foodType.length; j++)
{
boolean hitException = false;
Dog OUT = new Dog();
try
{
OUT.feed(foodType[j], foodAmount[i]);
}
catch (Exception exout)
{
hitException = true;
excep = exout;
}
if (hitException)
{
if (excep instanceof Assertion.Exception)
{
Assertion.assert((foodAmount[i] <= 0 ||
foodType[j] == null ||
foodType[j].length() == 0 ||
foodType[j].equals("unknown")),
"Unknown assert thrown");
}
else
Assertion.fail();
}
else
{
int currentAmt = OUT.getFoodAmount(foodType[j]);
Assertion.assert(currentAmt == foodAmount[i]);
}
}
}
outFile.println("feed_test successful");
}
void eat_test(PrintWriter outFile)
{
int[] foodAmount = { Integer.MIN_VALUE, -1, 0, 1, 1000,
Integer.MAX_VALUE };
String[] foodType = { null, ", "unknown",
"doggie biscuits", "Ken-L-Ration" };
int[] meal = { 0, 1, 1000, 2000 };
boolean[] barkYN = { true, false };
Exception excep = null;
for (int i=0; i < foodAmount.length; i++)
{
for (int j=0; j < meal.length; j++)
{
for (int k=0; k < barkYN.length; k++)
{
for (int l=0; l < foodType.length; l++)
{
boolean hitException = false;
Dog OUT = new Dog();
int oldFood = 0;
try
{
OUT.bark( barkYN[k]);
OUT.feed(foodType[l], meal[j]);
oldFood = OUT.getFoodAmount(foodType[l]);
OUT.eat(foodType[l], foodAmount[i]);
}
catch (Exception exout)
{
hitException = true;
excep = exout;
}
if (hitException)
{
if (excep instanceof Assertion.Exception)
Assertion.assert((foodAmount[i] <= 0 ||
OUT.isBarking() ||
foodType[l] == null ||
foodType[l].length() == 0 ||
foodType[l].equals("unknown")),
"Unknown assert thrown");
else if (excep instanceof OutOfFoodException)
Assertion.assert((OUT.getFoodAmount(foodType[l]) == 0));
else
Assertion.fail();
}
else
{
Assertion.assert((
OUT.getFoodAmount(foodType[l]) == (oldFood-foodAmount[i]) ||
OUT.getFoodAmount(foodType[l]) == 0));
}
}
}
}
}
outFile.println("eat_test successful");
}
} // End of inner class Tester.
}
End
Listing Three