org.jutil.junit
Class JutilTest

org.jutil.junit.JutilTest

public abstract class JutilTest

This is an extension for the junit TestCase class.

This class adds revision checking to junit tests. The revision of a library class is kept in a static string, which is automatically updated by CVS (or another revision control management system). Each testclass passes a Revision object to the constructor of this class, which represents the revision that is tested by that testclass. When the library class is changed, the revisions will differ, and a RevisionError will be thrown. This way a developer knows that he must check whether or not the testclass must be adapted.

At this moment this class can only work with cvs revisions. Put the following code in the library class:


 public final static String CVS_REVISION ="$Revision: 1.4 $";
 

CVS will now automatically adapt the CVS_REVISION field when the cvs revision is changed.

The test class either pass a Class to a constructor to indicated the tested class , or for Jutil.org testclasses, use the constructor which determines the testclass itself. For the Jutil.org testclasses, the following scheme is used:

org.jutiltest.X.TestY -> org.jutil.X.T

A typical constructor of a testclass will look like this:


 super(nameForJunit, new CVSRevision("x.x.x"));
 

Of course any Revision can be passed, but most people use CVS as source control mechanism.

Version:
$Revision: 1.4 $
Author:
Marko van Dooren

Field Summary
static java.lang.String CVS_REVISION
           
 
Constructor Summary
JutilTest(java.lang.String name, java.lang.Class testedClass, Revision testedRevision)
          public behavior

pre testedRevision != null;
pre class != null;

// The tested class of this JutilTest is set to the given class.
post getTestedClass() == testedClass;
// The cvs revision of the tested class is assigned to getTestedClassRevision()
post getTestedClassRevision().equals(
new CVSRevision(
getTestedClass().getField("CVS_REVISION").get(null).toString()
)
);
post getTestedRevision() == testedRevision;

// Throws an AssertionFailedError if the actual class does not contain a field named CVS_REVISION.
signals (AssertionFailedError) (\forall Field f; new Vector(getTestedClass().getFields()).contains(f);
! f.getName().equals("CVS_REVISION"));
// Throws a RevisionError if the tested revision doesn't match the revision of the tested class.
signals (RevisionError) ! getTestedRevision().equals(getTestedClassRevision());
Initialize a new JutilTest with the given name, tested class and tested revision.
JutilTest(java.lang.String name, Revision testedRevision)
          public invariant getTestedRevision().equals(getTestedClassRevision());

public behavior

pre testedRevision != null;

// The tested class is the corresponding class in the org.jutil package
// with the name X, when the name of the testclass is TestX
post getTestedClass().getName().equals(
new Pattern("org\\.jutiltest\\").replacer("org.jutil").replace(
new Pattern("\\.Test[^\\.]*").replacer("").replace(
getClass().getName()
)
)
);
// The cvs revision of the tested class is assigned to getTestedClassRevision()
post getTestedClassRevision().equals(
new CVSRevision(
getTestedClass().getField("CVS_REVISION").get(null).toString()
)
);
post getTestedRevision == testedRevision;

// Throws an AssertionFailedError if the actual class does not contain a field named CVS_REVISION.
signals (AssertionFailedError) (\forall Field f; new Vector(getTestedClass().getFields()).contains(f);
! f.getName().equals("CVS_REVISION"));
// The default tested claas is not found.
signals (AssertionFailedError) (* There is no class with the name
new Pattern("org\\.jutiltest\\").replacer("org.jutil").replace(
new Pattern("\\.Test[^\\.]*").replacer("").replace(
getClass().getName()
)
)
*);
// Throws a RevisionError if the tested revision doesn't match the revision of the tested class.
signals (RevisionError) ! getTestedRevision().equals(getTestedClassRevision());
 
Method Summary
 java.lang.Class getTestedClass()
          public behavior

post \result != null;
Return the class that is tested by this test class.
 Revision getTestedClassRevision()
          public behavior

post \result != null;
Return the field representing the revision of the tested class.
 Revision getTestedRevision()
          public behavior

post \result != null;
Return the revision of the tested class that is tested by this class.
 

Field Detail

CVS_REVISION

public static final java.lang.String CVS_REVISION
Constructor Detail

JutilTest

public JutilTest(java.lang.String name,
                 Revision testedRevision)
public invariant getTestedRevision().equals(getTestedClassRevision());

public behavior

pre testedRevision != null;

// The tested class is the corresponding class in the org.jutil package
// with the name X, when the name of the testclass is TestX
post getTestedClass().getName().equals(
new Pattern("org\\.jutiltest\\").replacer("org.jutil").replace(
new Pattern("\\.Test[^\\.]*").replacer("").replace(
getClass().getName()
)
)
);
// The cvs revision of the tested class is assigned to getTestedClassRevision()
post getTestedClassRevision().equals(
new CVSRevision(
getTestedClass().getField("CVS_REVISION").get(null).toString()
)
);
post getTestedRevision == testedRevision;

// Throws an AssertionFailedError if the actual class does not contain a field named CVS_REVISION.
signals (AssertionFailedError) (\forall Field f; new Vector(getTestedClass().getFields()).contains(f);
! f.getName().equals("CVS_REVISION"));
// The default tested claas is not found.
signals (AssertionFailedError) (* There is no class with the name
new Pattern("org\\.jutiltest\\").replacer("org.jutil").replace(
new Pattern("\\.Test[^\\.]*").replacer("").replace(
getClass().getName()
)
)
*);
// Throws a RevisionError if the tested revision doesn't match the revision of the tested class.
signals (RevisionError) ! getTestedRevision().equals(getTestedClassRevision());

Initialize a new JutilTest with the given name and tested revision.

This is a convenience constructor for Jutil.org test classes. The tested class will be set to the corresponding class in the library. This means, the class with package org.jutil.X when the package of this class is org.jutiltest.X and with the name X when the name of this testclass is TestX.

If you want to set the tested class yourself, use the other constructor.

Parameters:
name - The name of the method that has to be invoked by default.
testedRevision - The revision of the tested class that is tested by this class.

JutilTest

public JutilTest(java.lang.String name,
                 java.lang.Class testedClass,
                 Revision testedRevision)
public behavior

pre testedRevision != null;
pre class != null;

// The tested class of this JutilTest is set to the given class.
post getTestedClass() == testedClass;
// The cvs revision of the tested class is assigned to getTestedClassRevision()
post getTestedClassRevision().equals(
new CVSRevision(
getTestedClass().getField("CVS_REVISION").get(null).toString()
)
);
post getTestedRevision() == testedRevision;

// Throws an AssertionFailedError if the actual class does not contain a field named CVS_REVISION.
signals (AssertionFailedError) (\forall Field f; new Vector(getTestedClass().getFields()).contains(f);
! f.getName().equals("CVS_REVISION"));
// Throws a RevisionError if the tested revision doesn't match the revision of the tested class.
signals (RevisionError) ! getTestedRevision().equals(getTestedClassRevision());
Initialize a new JutilTest with the given name, tested class and tested revision.
Parameters:
name - The name of the method that has to be invoked by default.
testedClass - The class that is tested by this testclass.
testedRevision - The revision of the tested class that is tested by this class.
Method Detail

getTestedRevision

public Revision getTestedRevision()
public behavior

post \result != null;
Return the revision of the tested class that is tested by this class. This is not necessarily the same revision as the revision of the tested class.

getTestedClassRevision

public Revision getTestedClassRevision()
public behavior

post \result != null;
Return the field representing the revision of the tested class.

getTestedClass

public java.lang.Class getTestedClass()
public behavior

post \result != null;
Return the class that is tested by this test class.