public class TestCase
extends junit.framework.TestCase
TestCase
to support testing of I/O driven
programs and flexible/fuzzy comparison of strings. In most cases, it
can be used as a completely transparent drop-in replacement for its
parent class.
Fuzzy string comparisons in this class default to standard rules in
StringNormalizer
(excluding the OPT_* rules). You can use
the stringNormalizer()
method to access the normalizer and
set your own comparison options, however.
Modifier and Type | Class and Description |
---|---|
static class |
TestCase.AssertMethodCalledIncorrectly
A custom error class that represents a mistake in calling the wrong
assert method for a given situation.
|
Constructor and Description |
---|
TestCase()
Creates a new TestCase object.
|
TestCase(String name)
Creates a new TestCase object.
|
Modifier and Type | Method and Description |
---|---|
static void |
assertEquals(Number expected,
Number actual)
There is no assertion to compare ints with floats or doubles, or
to compare two floats or two doubles without specifying a tolerance,
but autoboxing will allow you to compare them as objects, which is
never desired.
|
static void |
assertEquals(String message,
Number expected,
Number actual)
There is no assertion to compare ints with floats or doubles, or
to compare two floats or two doubles without specifying a tolerance,
but autoboxing will allow you to compare them as objects, which is
never desired.
|
static void |
assertEquals(String expected,
String actual)
Asserts that two Strings are equal.
|
static void |
assertEquals(String message,
String expected,
String actual)
Asserts that two Strings are equal.
|
static void |
assertFalse(boolean condition)
Asserts that a condition is false.
|
static void |
assertFalse(String message,
boolean condition)
Asserts that a condition is false.
|
static void |
assertFuzzyEquals(String expected,
String actual)
Asserts that two Strings are equal, respecting preferences for what
differences matter.
|
static void |
assertFuzzyEquals(String message,
String expected,
String actual)
Asserts that two Strings are equal, respecting preferences for what
differences matter.
|
static void |
assertSame(Number expected,
Number actual)
There is no assertion to compare primitive values using assertSame(),
but autoboxing will allow you to compare them as objects, which is
never desired.
|
static void |
assertSame(String message,
Number expected,
Number actual)
There is no assertion to compare primitive values using assertSame(),
but autoboxing will allow you to compare them as objects, which is
never desired.
|
static void |
assertTrue(boolean condition)
Asserts that a condition is true.
|
static void |
assertTrue(String message,
boolean condition)
Asserts that a condition is true.
|
static String |
compact(String content)
Takes a string and, if it is too long, shortens it by replacing the
middle with an ellipsis.
|
static String |
compact(String content,
int threshold,
int prefixLen)
Takes a string and, if it is too long, shortens it by replacing the
middle with an ellipsis.
|
boolean |
contains(String largerString,
String... substrings)
Determine whether one String contains a sequence of other substrings
in order.
|
boolean |
containsRegex(String largerString,
Pattern... substrings)
Determine whether one String contains a sequence of other substrings
in order, where the expected substrings are specified as a regular
expressions.
|
boolean |
containsRegex(String largerString,
String... substrings)
Determine whether one String contains a sequence of other substrings
in order, where the expected substrings are specified as a regular
expressions.
|
boolean |
equals(String left,
String right)
Determines whether two Strings are equal.
|
boolean |
equalsRegex(String actual,
Pattern expected)
Determines whether a String exactly matches an expected regular
expression.
|
boolean |
equalsRegex(String actual,
String expected)
Determines whether a String exactly matches an expected regular
expression.
|
String |
fromFile(File file)
Load the contents of a text file and return it as a string
(normalizing newlines to unix-style line ending conventions).
|
String |
fromFile(String file)
Load the contents of a text file and return it as a string
(normalizing newlines to unix-style line ending conventions).
|
boolean |
fuzzyContains(String largerString,
String... substrings)
Determine whether one String contains a sequence of other substrings
in order, respecting preferences for what differences matter.
|
boolean |
fuzzyContainsRegex(String largerString,
Pattern... substrings)
Determine whether one String contains a sequence of other substrings
in order, where the expected substrings are specified as regular
expressions, and respecting preferences for what differences matter.
|
boolean |
fuzzyContainsRegex(String largerString,
String... substrings)
Determine whether one String contains a sequence of other substrings
in order, where the expected substrings are specified as regular
expressions, and respecting preferences for what differences matter.
|
boolean |
fuzzyEquals(String left,
String right)
Determines whether two Strings are equal, respecting preferences for
what differences matter.
|
boolean |
fuzzyEqualsRegex(String actual,
Pattern expected)
Determines whether a String exactly matches an expected regular
expression, respecting preferences for what differences matter.
|
boolean |
fuzzyEqualsRegex(String actual,
String expected)
Determines whether a String exactly matches an expected regular
expression, respecting preferences for what differences matter.
|
Scanner |
in()
Get an input stream containing the contents that you specify.
|
static void |
installExitHandler()
This method is for internal use only and should not be called
by other code.
|
protected void |
instrumentIO()
An internal helper that ensures input/output buffering is in place
before each test case.
|
String |
multiline(String... lines)
Combine a list of strings provided as arguments into a single
string, following each "line" with a line terminator.
|
PrintWriterWithHistory |
out()
Get an output stream suitable for use in test cases.
|
void |
prepareIO()
Resets IO and output reasons, then instruments IO for next method.
|
protected void |
resetIO()
An internal helper that resets all of the input/output buffering
after each test case.
|
static void |
resetSystemIO()
An internal helper that resets the system part of the input/output
buffering after each test case.
|
void |
setIn(Scanner contents)
Set the contents of this test case's input stream, which can then
be retrieved using
in() . |
void |
setIn(String... contents)
Set the contents of this test case's input stream, which can then
be retrieved using
in() . |
static void |
setNormalizeLineEndings(boolean value)
Control whether strings manipulated in this test case will have
their line terminator sequences automatically normalized to unix-style
line terminators (default is true).
|
void |
setSystemIn(String... contents)
Set the contents of
System.in for this test case. |
protected void |
setUp()
Sets up the fixture, for example, open a network connection.
|
static void |
setUseFuzzyStringComparisons(boolean value)
Control whether assertEquals() on strings will use fuzzy comparisons
(using the
stringNormalizer() to ignore things like
punctuation differences, capitalization differences, etc.--see the
StringNormalizer documentation for how to control what differences
are ignored). |
protected static StringNormalizer |
stringNormalizer()
Access the string normalizer that this test case uses in
fuzzy string comparisons.
|
PrintStreamWithHistory |
systemErr()
Get a version of
System.err that records its history
so you can compare against it later. |
PrintStreamWithHistory |
systemOut()
Get a version of
System.out that records its history
so you can compare against it later. |
protected void |
tearDown()
Tears down the fixture, for example, close a network connection.
|
static void |
uninstallExitHandler()
This method is for internal use only and should not be called
by other code.
|
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, countTestCases, createResult, fail, fail, failNotEquals, failNotSame, failSame, format, getName, run, run, runBare, runTest, setName, toString
public TestCase()
public TestCase(String name)
name
- The name of this test caseprotected void setUp() throws Exception
setUp
in class junit.framework.TestCase
Exception
protected void tearDown() throws Exception
tearDown
in class junit.framework.TestCase
Exception
protected void instrumentIO()
protected void resetIO()
public PrintWriterWithHistory out()
PrintWriterWithHistory.getHistory()
method to extract all the output in the form of a string. The contents
of this stream get cleared for every test case.PrintWriter
suitable for use in test casespublic PrintStreamWithHistory systemOut()
System.out
that records its history
so you can compare against it later. The history of this
stream gets cleared for every test case.PrintStream
connected to System.out that
is suitable for use in test casespublic PrintStreamWithHistory systemErr()
System.err
that records its history
so you can compare against it later. The history of this
stream gets cleared for every test case.PrintStream
connected to System.err that
is suitable for use in test casespublic Scanner in()
setIn(String...)
or
setIn(Scanner)
to set the contents before you begin
using this stream. This stream gets reset for every test case.Scanner
containing any contents you
have specified.public void setIn(String... contents)
in()
.contents
- The contents to use for the stream, which replace
any that were there before. The contents can be provided as a
single string, or as a series of strings that should be merged
using multiline(String...)
. Line terminator sequences
in the contents will be normalized to unix-style line endings.public void setIn(Scanner contents)
in()
.contents
- The contents to use for the stream, which replace
any that were there before.public void setSystemIn(String... contents)
System.in
for this test case.contents
- The contents to use for the stream, which replace
any that were there before. The contents can be provided as a
single string, or as a series of strings that should be merged
using multiline(String...)
. Line terminator sequences
in the contents will be normalized to unix-style line endings.public String multiline(String... lines)
lines
- The series of lines to be combined (a line terminator
will be added after each one).public String fromFile(File file)
file
- The file to read from.public String fromFile(String file)
file
- The name of the file to read from.protected static StringNormalizer stringNormalizer()
assertFuzzyEquals(String, String)
,
StringNormalizer
,
StringNormalizer.addStandardRules()
public static void setNormalizeLineEndings(boolean value)
value
- True if line endings should be automatically converted
to unix-style, or false if they should be left alone.public static void setUseFuzzyStringComparisons(boolean value)
stringNormalizer()
to ignore things like
punctuation differences, capitalization differences, etc.--see the
StringNormalizer documentation for how to control what differences
are ignored). The default is false, meaning that fuzzy comparisons
must be explicitly requested using one of the "fuzzy"-named methods.
Calling this method with the parameter true will cause conventional
assertEquals() calls to use fuzzy string normalization in comparisons
as well, making assertEquals() and similar methods behave the same
as assertFuzzyEquals().value
- True if string comparisons should be fuzzy in
assertEquals().public static void assertFuzzyEquals(String expected, String actual)
TestCase.assertEquals(String,String)
method, augmenting its behavior with the ability to make "fuzzy"
string comparisons that ignore things like differences in spacing,
punctuation, or capitalization. Use
stringNormalizer()
to access and modify the
StringNormalizer
object's preferences for comparing
strings.expected
- The expected valueactual
- The value to testpublic static void assertFuzzyEquals(String message, String expected, String actual)
TestCase.assertEquals(String,String)
method, augmenting its behavior with the ability to make "fuzzy"
string comparisons that ignore things like differences in spacing,
punctuation, or capitalization. Use
stringNormalizer()
to access and modify the
StringNormalizer
object's preferences for comparing
strings.message
- The message to use for a failed assertionexpected
- The expected valueactual
- The value to testpublic static void assertTrue(String message, boolean condition)
TestCase.assertTrue(String,boolean)
that issues special diagnostics when the assertion fails, if
the given condition supports it.message
- The message to use for a failed assertioncondition
- The condition to checkpublic static void assertTrue(boolean condition)
TestCase.assertTrue(boolean)
that issues special diagnostics when the assertion fails, if
the given condition supports it.condition
- The condition to checkpublic static void assertFalse(String message, boolean condition)
TestCase.assertFalse(String,boolean)
that issues special diagnostics when the assertion fails, if
the given condition supports it.message
- The message to use for a failed assertioncondition
- The condition to checkpublic static void assertFalse(boolean condition)
TestCase.assertFalse(boolean)
that issues special diagnostics when the assertion fails, if
the given condition supports it.condition
- The condition to checkpublic static void assertSame(String message, Number expected, Number actual)
message
- The message to use if the assertion failsexpected
- The expected valueactual
- The actual valuepublic static void assertSame(Number expected, Number actual)
expected
- The expected valueactual
- The actual valuepublic static void assertEquals(String message, Number expected, Number actual)
message
- The message to use if the assertion failsexpected
- The expected valueactual
- The actual valuepublic static void assertEquals(Number expected, Number actual)
expected
- The expected valueactual
- The actual valuepublic static void assertEquals(String message, String expected, String actual)
message
- The message to use if the assertion failsexpected
- The expected valueactual
- The actual valuepublic static void assertEquals(String expected, String actual)
expected
- The expected valueactual
- The actual valuepublic static String compact(String content, int threshold, int prefixLen)
compact("hello
there", 6, 3)
will return "hel...ere".content
- The string to shortenthreshold
- Strings longer than this will be compacted, while
strings less than or equal to this limit will be returned
unchangedprefixLen
- How many characters at the front and back of the
string to keep. This number must be less than or equal to half
the thresholdpublic static String compact(String content)
content
- The string to shortenpublic boolean equals(String left, String right)
String.equals(Object)
, but is provided for symmetry with
the other comparison predicates provided in this class. For
assertion writing, remember that
TestCase.assertEquals(String,String)
will
produce more useful information on failure, however.left
- The first string to compareright
- The second string to comparepublic boolean fuzzyEquals(String left, String right)
equals(String,String)
, augmenting its behavior with the
ability to make "fuzzy" string comparisons that ignore things like
differences in spacing, punctuation, or capitalization. It is also
identical to assertFuzzyEquals(String,String)
, except that it
returns the boolean result of the comparison instead of making a
test case assertion. Use
stringNormalizer()
to access and modify the
StringNormalizer
object's preferences for comparing
strings. For assertion writing, remember that
assertFuzzyEquals(String,String)
will
produce more useful information on failure, however.left
- The first string to compareright
- The second string to comparepublic boolean equalsRegex(String actual, String expected)
containsRegex(String,String...)
instead.
Note that this predicate uses the opposite parameter ordering from JUnit assertions: The value to test is the first parameter, and the expected pattern is the second.
actual
- The value to testexpected
- The expected value (interpreted as a regular
expression Pattern
)public boolean equalsRegex(String actual, Pattern expected)
containsRegex(String,Pattern...)
instead.
Note that this predicate uses the opposite parameter ordering from JUnit assertions: The value to test is the first parameter, and the expected pattern is the second.
actual
- The value to testexpected
- The expected valuepublic boolean fuzzyEqualsRegex(String actual, String expected)
fuzzyContainsRegex(String,String...)
instead.
Note that this predicate uses the opposite parameter ordering from JUnit assertions: The value to test is the first parameter, and the expected pattern is the second.
Use
stringNormalizer()
to access and modify the
StringNormalizer
object's preferences for comparing
strings.
actual
- The value to testexpected
- The expected value (interpreted as a regular
expression Pattern
)public boolean fuzzyEqualsRegex(String actual, Pattern expected)
fuzzyContainsRegex(String,String...)
instead.
Use
stringNormalizer()
to access and modify the
StringNormalizer
object's preferences for comparing
strings.
actual
- The value to testexpected
- The expected valuepublic boolean contains(String largerString, String... substrings)
String.contains(CharSequence)
. If you provide more than
one substring, it looks for each such element in turn
in the larger string, making sure they are all found in the proper order
(each substring must strictly follow the previous one, although there
can be any amount of intervening characters between any two substrings
in the array). If the larger string is null, this method returns
false (since it can contain nothing).
Note that this predicate uses the opposite parameter ordering from JUnit assertions: The value to test is the first parameter, and the expected substrings are the second.
largerString
- The target to look insubstrings
- One or more substrings to look for (in order)public boolean fuzzyContains(String largerString, String... substrings)
This method makes "fuzzy" string comparisons that ignore things
like differences in spacing, punctuation, or capitalization. Use
stringNormalizer()
to access and modify the
StringNormalizer
object's preferences for comparing
strings.
Note that this predicate uses the opposite parameter ordering from JUnit assertions: The value to test is the first parameter, and the expected substrings are the second.
largerString
- The target to look insubstrings
- The substrings to look for (in order)public boolean containsRegex(String largerString, String... substrings)
Note that this predicate uses the opposite parameter ordering from JUnit assertions: The value to test is the first parameter, and the expected substrings are the second.
largerString
- The target to look insubstrings
- A sequence of expected substrings (interpreted as
regular expression Pattern
s), which must
occur in the same order in the larger stringpublic boolean containsRegex(String largerString, Pattern... substrings)
largerString
- The target to look insubstrings
- A sequence of expected regular expressions, which
must occur in the same order in the larger stringpublic boolean fuzzyContainsRegex(String largerString, String... substrings)
fuzzyContains(String,String...)
,
except that the second argument is interpreted as an array of regular
expressions. String normalization rules are only appled to the
larger string, not to the regular expressions.largerString
- The target to look insubstrings
- An array of expected substrings (interpreted as
regular expression Pattern
s), which must
occur in the same order in the larger stringpublic boolean fuzzyContainsRegex(String largerString, Pattern... substrings)
fuzzyContains(String,String...)
,
except that the second argument is interpreted as an array of regular
expressions. String normalization rules are only appled to the
larger string, not to the regular expressions.largerString
- The target to look insubstrings
- An array of expected substrings, which must
occur in the same order in the larger stringpublic void prepareIO()
public static void resetSystemIO()
public static void installExitHandler()
public static void uninstallExitHandler()