- Assertion
class
/**- DebugPrinter class
/**-
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