public abstract class TestCase2
extends junit.framework.TestCase
TestCase
that supports logging, loading test
resources, and hierarchical properties.
Note: When using Maven (or Ant), you need to be aware that that
your project.xml
has an impact on which files are
recognized as tests to be submitted to a test runner. For example,
it is common practice that all files beginning or ending with
"Test" will be submitted to JUnit, though I suggest that the
practice of naming things "FooTestCase" will serve you better.
Also note that the maven test plugin explictly skips files whose
name matches the pattern "*AbstractTestSuite".
Modifier and Type | Class and Description |
---|---|
protected static class |
TestCase2.MyProperties
Helper class gives us access to the default
Properties . |
protected class |
TestCase2.RandomType
Used to create objects of random type.
|
Modifier and Type | Field and Description |
---|---|
protected TestCase2.RandomType |
_randomType |
protected static org.apache.log4j.Logger |
log
The default
Logger for this class, which is named
"junit.framework.Test". |
Modifier and Type | Method and Description |
---|---|
static void |
assertEquals(boolean[] expected,
boolean[] actual) |
static void |
assertEquals(byte[] expected,
byte[] actual)
Compares byte[]s by value (not reference).
|
static void |
assertEquals(char[] expected,
char[] actual) |
static void |
assertEquals(double[] expected,
double[] actual) |
static void |
assertEquals(double expected,
double actual)
Provides a proper basis for comparing floating point numbers (the values
must be within 10 ulps of one another).
|
static void |
assertEquals(float[] expected,
float[] actual) |
static void |
assertEquals(float expected,
float actual)
Provides a proper basis for comparing floating point numbers (the values
must be within 10 ulps of one another).
|
static void |
assertEquals(int[] expected,
int[] actual) |
static void |
assertEquals(long[] expected,
long[] actual) |
static void |
assertEquals(Object[] expected,
Object[] actual) |
static void |
assertEquals(short[] expected,
short[] actual) |
static void |
assertEquals(String msg,
boolean[] expected,
boolean[] actual)
Compares arrays of primitive values.
|
static void |
assertEquals(String msg,
byte[] expected,
byte[] actual)
Compares byte[]s by value (not reference).
|
static void |
assertEquals(String msg,
char[] expected,
char[] actual) |
static void |
assertEquals(String msg,
double[] expected,
double[] actual)
TODO Use smarter floating point comparison and offer parameter
for controlling the floating point comparison.
|
static void |
assertEquals(String message,
double expected,
double actual)
Provides a proper basis for comparing floating point numbers (the values
must be within 10 ulps of one another).
|
static void |
assertEquals(String msg,
float[] expected,
float[] actual)
TODO Use smarter floating point comparison and offer parameter
for controlling the floating point comparison.
|
static void |
assertEquals(String message,
float expected,
float actual)
Provides a proper basis for comparing floating point numbers (the values
must be within 10 ulps of one another).
|
static void |
assertEquals(String msg,
int[] expected,
int[] actual) |
static void |
assertEquals(String msg,
long[] expected,
long[] actual) |
static void |
assertEquals(String msg,
Object[] expected,
Object[] actual)
Compares arrays of
Object s. |
static void |
assertEquals(String msg,
short[] expected,
short[] actual) |
static void |
assertEqualsWithinUlps(String message,
double expected,
double actual,
long maxUlps)
Provides a proper basis for comparing floating point numbers (the values
must be within maxUlps of one another).
|
static void |
assertSameArray(Object[] expected,
Object[] actual)
Helper method verifies that the arrays are consistent in length
and that their elements are consistent under
Object.equals( Object other ) . |
static void |
assertSameArray(String message,
Object[] expected,
Object[] actual)
Helper method verifies that the arrays are consistent in length
and that their elements are consistent under
Object.equals( Object other ) . |
static void |
assertSameBigDecimal(BigDecimal expected,
BigDecimal actual)
This uses
BigDecimal.compareTo( Object other ) , which
considers that two BigDecimal s that are equal in
value but have a different scale are the same. |
static void |
assertSameBigDecimal(String message,
BigDecimal expected,
BigDecimal actual)
This uses
BigDecimal.compareTo( Object other ) , which
considers that two BigDecimal s that are equal in
value but have a different scale are the same. |
static void |
assertSameBigInteger(BigInteger expected,
BigInteger actual) |
static void |
assertSameBigInteger(String message,
BigInteger expected,
BigInteger actual) |
static void |
assertSameIterator(Object[] expected,
Iterator actual)
Method verifies that the actual
Iterator
produces the expected objects in the expected order. |
static void |
assertSameIterator(String msg,
Object[] expected,
Iterator actual)
Method verifies that the actual
Iterator
produces the expected objects in the expected order. |
static void |
assertSameIteratorAnyOrder(Object[] expected,
Iterator actual)
Verifies that the iterator visits the specified objects in some arbitrary
ordering and that the iterator is exhausted once all expected objects
have been visited.
|
static void |
assertSameIteratorAnyOrder(String msg,
Object[] expected,
Iterator actual)
Verifies that the iterator visits the specified objects in some arbitrary
ordering and that the iterator is exhausted once all expected objects
have been visited.
|
static void |
assertSameValue(Object expected,
Object actual)
Test helper that can correctly compare arrays of primitives and
arrays of objects as well as primitives and objects.
|
static void |
assertSameValue(String msg,
Object expected,
Object actual)
Test helper that can correctly compare arrays of primitives and
arrays of objects as well as primitives and objects.
|
protected void |
assertZeroUlps(double d1,
double d2)
Verify zero ULPs difference between the values.
|
protected void |
assertZeroUlps(float f1,
float f2)
Verify zero ULPs difference between the values.
|
static void |
fail(String message,
Throwable t)
This convenience method provides a version of
TestCase.fail(
String Message ) that also excepts a Throwable cause
for the test failure. |
static Throwable |
getInnerCause(Throwable t,
Class cls)
Examines a stack trace for an instance of the specified cause nested to
any level within that stack trace.
|
int |
getNormalInt(int range)
Returns a random integer normally distributed in [0:range].
|
static String |
getProjectBuildPath()
Returns the project build directory, even in a multiproject
build.
|
Properties |
getProperties()
Reads in the configuration properties for the test from a
variety of resources and returns a properties hierarchy.
|
Object |
getRandomObject(boolean allowNull)
Returns an object with a random type and random value.
|
Object |
getRandomObject(Random rnd,
boolean allowNull)
Returns an object with a random type and random value.
|
static int[] |
getRandomOrder(int n)
Test helper produces a random sequence of indices in the range [0:n-1]
suitable for visiting the elements of an array of n elements in a random
order.
|
String |
getRandomString(int len,
int id)
Returns a random but unique string of Unicode characters with a maximum
length of len and a minimum length.
|
InputStream |
getTestInputStream(String resourceName)
Returns an
InputStream that may be used to read from a
test resource found along the CLASSPATH. |
String |
getTestResource(String resourceName)
Convenience method for
getTestResource( String
resourceName, String encoding ) that uses the platform
specific default encoding, which is NOT recommended since it is
non-portable. |
String |
getTestResource(String resourceName,
String encoding)
Read a test character resource from along the CLASSPATH.
|
protected static long |
getUlps(double A,
double B)
Derived from
Comparing floating point numbers by Bruce Dawson.
|
protected static int |
getUlps(float A,
float B)
Derived from
Comparing floating point numbers by Bruce Dawson.
|
boolean |
isDEBUG() |
static boolean |
isDEBUG(org.apache.log4j.Logger log)
Utility method returns true iff the effective logger level is
DEBUG or less.
|
boolean |
isINFO() |
static boolean |
isINFO(org.apache.log4j.Logger log)
Utility method returns true iff the effective logger level is
INFO or less.
|
static boolean |
isInnerCause(Throwable t,
Class cls)
Examines a stack trace for an instance of the specified cause nested to
any level within that stack trace.
|
protected void |
logProperties(TestCase2.MyProperties properties,
int level)
Recursively logs properties together with the resource or file
in which they were defined at the DEBUG level.
|
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, countTestCases, createResult, fail, fail, failNotEquals, failNotSame, failSame, format, getName, run, run, runBare, runTest, setName, setUp, tearDown, toString
protected static final org.apache.log4j.Logger log
Logger
for this class, which is named
"junit.framework.Test".protected TestCase2.RandomType _randomType
public TestCase2()
public TestCase2(String name)
public static void fail(String message, Throwable t)
TestCase.fail(
String Message )
that also excepts a Throwable
cause
for the test failure.public static void assertEquals(boolean[] expected, boolean[] actual)
public static void assertEquals(String msg, boolean[] expected, boolean[] actual)
public static void assertEquals(byte[] expected, byte[] actual)
Compares byte[]s by value (not reference).
Note: This method will only be invoked if both arguments can be typed as
byte[] by the compiler. If either argument is not strongly typed, you
MUST case it to a byte[] or TestCase.assertEquals(Object, Object)
will be
invoked instead.
expected
- actual
- public static void assertEquals(String msg, byte[] expected, byte[] actual)
Compares byte[]s by value (not reference).
Note: This method will only be invoked if both arguments can be typed as
byte[] by the compiler. If either argument is not strongly typed, you
MUST case it to a byte[] or TestCase.assertEquals(Object, Object)
will be
invoked instead.
msg
- expected
- actual
- public static void assertEquals(char[] expected, char[] actual)
public static void assertEquals(String msg, char[] expected, char[] actual)
public static void assertEquals(short[] expected, short[] actual)
public static void assertEquals(String msg, short[] expected, short[] actual)
public static void assertEquals(int[] expected, int[] actual)
public static void assertEquals(String msg, int[] expected, int[] actual)
public static void assertEquals(long[] expected, long[] actual)
public static void assertEquals(String msg, long[] expected, long[] actual)
public static void assertEquals(float[] expected, float[] actual)
public static void assertEquals(String msg, float[] expected, float[] actual)
public static void assertEquals(double[] expected, double[] actual)
public static void assertEquals(String msg, double[] expected, double[] actual)
public static void assertEquals(String msg, Object[] expected, Object[] actual)
Object
s. The length of the arrays
must agree, and each array element must agree. However the
class of the arrays does NOT need to agree, e.g., an Object[]
MAY compare as equals with a String[].public static void assertSameValue(Object expected, Object actual)
public static void assertSameValue(String msg, Object expected, Object actual)
ClassCastException
if
actual is of a different primitive array type. Change
the code to throw AssertionFailedError
instead.public static void assertSameIterator(Object[] expected, Iterator actual)
Iterator
produces the expected objects in the expected order. Objects
are compared using Object.equals( Object other )
. Errors
are reported if too few or too many objects are produced, etc.public static void assertSameIterator(String msg, Object[] expected, Iterator actual)
Iterator
produces the expected objects in the expected order. Objects
are compared using Object.equals( Object other )
. Errors
are reported if too few or too many objects are produced, etc.public static void assertSameIteratorAnyOrder(Object[] expected, Iterator actual)
public static void assertSameIteratorAnyOrder(String msg, Object[] expected, Iterator actual)
public static int[] getRandomOrder(int n)
public int getNormalInt(int range)
public String getRandomString(int len, int id)
len
- The maximum length of the string. Each generated literal will
have a mean length of len/2
and the lengths
will be distributed using a normal distribution (bell curve).id
- A unique index used to obtain a unique string. Typically this
is a one up identifier.public Object getRandomObject(boolean allowNull)
allowNull
- When true, a null object reference may be
returned.public Object getRandomObject(Random rnd, boolean allowNull)
rnd
- The random generator.allowNull
- When true, a null object reference may be
returned.public InputStream getTestInputStream(String resourceName)
InputStream
that may be used to read from a
test resource found along the CLASSPATH.resourceName
- A resource identified by a series of '/'
delimited path components without a leading
'/'. Such resources are normally part of the JAR in which the
test is bundled and correspond to the package naming convention
with the exception that the '/' delimiter is used in place of
the '.' delimiter used in package names.AssertionError
- If the resource could not be read for
any reason.ClassLoader.getResourceAsStream( String resourceName )
public String getTestResource(String resourceName, String encoding)
resourceName
- A resource identified by a series of path
components beginning without a leading '/'.
Such resources are normally part of the JAR in which the test
is bundled.encoding
- The character set encoding that will be used to
read the text resource. If null
then the platform
default Java encoding will be used (which is not recommended as
it is non-portable).AssertionError
- If the resource could not be read for
any reason.ClassLoader.getResourceAsStream( String resourceName )
public String getTestResource(String resourceName)
getTestResource( String
resourceName, String encoding )
that uses the platform
specific default encoding, which is NOT recommended since it is
non-portable.public Properties getProperties()
Properties
object, then the lower levels of the
hierarchy are recursively searched.The hierarchy is constructed from the following properties files in the following order. The first property file in this list corresponds to the top of the property hierarchy. The last property file in this list corresponds to the bottom of the property hierarchy. The property resources are:
Properties
object that supplies bindings for
property names according to the described hierarchy among
property resources. The returned Properties
is NOT
cached.
TODO This does not handle the recursive truncation of the
class name to search for "test.properties" yet.protected void logProperties(TestCase2.MyProperties properties, int level)
This method is invoked by getProperties()
.
public static String getProjectBuildPath() throws UnsupportedOperationException
Note that the current working directory in a multiproject build is normally NOT the same as the individual project directory when running the unit tests. This method is designed to give you access to project local resources (whether consumed or created) during unit test, even when running a multiproject goal.
In order for this method to succeed, you MUST declare maven.junit.sysproperties="maven.build.dir ..." when you invoke the test suite. See the properties page for the test-plugin for more information on how to specify this property.
UnsupportedOperationException
- if the project build
directory could NOT be determined since the
maven.junit.sysproperties property was NOT defined.public static final boolean isDEBUG(org.apache.log4j.Logger log)
log
- The logger.public static final boolean isINFO(org.apache.log4j.Logger log)
log
- The logger.public final boolean isDEBUG()
public final boolean isINFO()
protected static int getUlps(float A, float B)
A
- A floating point value.B
- Another floating point valueprotected static long getUlps(double A, double B)
A
- A double precision floating point value.B
- Another double precision floating point valueprotected void assertZeroUlps(float f1, float f2)
f1
- f2
- protected void assertZeroUlps(double d1, double d2)
d1
- d2
- public static void assertEquals(float expected, float actual)
getUlps(float, float)
public static void assertEquals(String message, float expected, float actual)
getUlps(float, float)
public static void assertEquals(double expected, double actual)
expected
- actual
- getUlps(double, double)
public static void assertEquals(String message, double expected, double actual)
expected
- actual
- getUlps(double, double)
public static void assertEqualsWithinUlps(String message, double expected, double actual, long maxUlps)
expected
- actual
- maxUlps
- The maximum #of ulps difference which will be considered
as equivalent.getUlps(double, double)
public static void assertSameBigInteger(BigInteger expected, BigInteger actual)
public static void assertSameBigInteger(String message, BigInteger expected, BigInteger actual)
public static void assertSameBigDecimal(BigDecimal expected, BigDecimal actual)
BigDecimal.compareTo( Object other )
, which
considers that two BigDecimal
s that are equal in
value but have a different scale are the same.
(This is NOT true of BigDecimal.equals( Object other )
,
which also requires the values to have the same scale.)public static void assertSameBigDecimal(String message, BigDecimal expected, BigDecimal actual)
BigDecimal.compareTo( Object other )
, which
considers that two BigDecimal
s that are equal in
value but have a different scale are the same.
(This is NOT true of BigDecimal.equals( Object other )
,
which also requires the values to ave the same scale.)public static void assertSameArray(Object[] expected, Object[] actual)
Object.equals( Object other )
.public static void assertSameArray(String message, Object[] expected, Object[] actual)
Object.equals( Object other )
.public static Throwable getInnerCause(Throwable t, Class cls)
t
- The stack trace.cls
- The class of exception that you are looking for in the stack
trace.null
otherwise.IllegalArgumentException
- if any parameter is null.public static boolean isInnerCause(Throwable t, Class cls)
t
- The stack trace.cls
- The class of exception that you are looking for in the stack
trace.true
iff an exception that is an instance of that
class iff one exists in the stack trace.IllegalArgumentException
- if any parameter is null.Copyright © 2006–2019 SYSTAP, LLC DBA Blazegraph. All rights reserved.