CS 1705 Library

cs1705
Class TestCase

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by cs1705.TestCase
All Implemented Interfaces:
Test

public class TestCase
extends TestCase

This class provides some customized behavior beyond the basics of 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. Subclasses that override tearDown() should also override setUp()--be sure to call super.setUp() in this case, or you'll see unexpected behavior!

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.

Version:
2007.09.12
Author:
Stephen Edwards

Constructor Summary
TestCase()
          Creates a new TestCase object.
TestCase(String name)
          Creates a new TestCase object.
 
Method Summary
static void assertFalse(boolean condition)
          Asserts that a condition is false.
static void assertFalse(String message, boolean condition)
          Asserts that a condition is false.
 void assertFuzzyEquals(String expected, String actual)
          Asserts that two Strings are equal, respecting preferences for what differences matter.
 void assertFuzzyEquals(String message, String expected, String actual)
          Asserts that two Strings are equal, respecting preferences for what differences matter.
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 substring)
          Determine whether one String contains another as a substring.
 boolean contains(String largerString, String[] substrings)
          Determine whether one String contains a sequence of other substrings in order.
 boolean containsRegex(String largerString, Pattern substring)
          Determine whether one String contains another as a substring, where the expected substring is specified as a regular expression.
 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 substring)
          Determine whether one String contains another as a substring, where the expected substring is specified as a regular expression.
 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.
 boolean fuzzyContains(String largerString, String substring)
          Determine whether one String contains another as a substring, respecting preferences for what differences matter.
 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 substring)
          Determine whether one String contains another as a substring, where the expected substring is specified as a regular expression, and 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 substring)
          Determine whether one String contains another as a substring, where the expected substring is specified as a regular expression, 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.
 PrintWriterWithHistory out()
          Get an output stream suitable for use in test cases.
 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().
 void setSystemIn(String contents)
          Set the contents of this test case's input stream, which can then be retrieved using in().
 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.
 
Methods inherited from class junit.framework.TestCase
countTestCases, getName, run, run, runBare, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, 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, fail, fail
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TestCase

public TestCase()
Creates a new TestCase object.


TestCase

public TestCase(String name)
Creates a new TestCase object.

Parameters:
name - The name of this test case
Method Detail

out

public PrintWriterWithHistory out()
Get an output stream suitable for use in test cases. You can pass this output stream to your own methods, and later call its 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.

Returns:
a PrintWriter suitable for use in test cases

systemOut

public PrintStreamWithHistory systemOut()
Get a version of System.out that records its history so you can compare against it later. The history of this stream gets cleared for every test case.

Returns:
a PrintStream connected to System.out that is suitable for use in test cases

systemErr

public PrintStreamWithHistory systemErr()
Get a version of System.err that records its history so you can compare against it later. The history of this stream gets cleared for every test case.

Returns:
a PrintStream connected to System.err that is suitable for use in test cases

in

public Scanner in()
Get an input stream containing the contents that you specify. Set the contents by calling setIn(String) or setIn(Scanner) to set the contents before you begin using this stream. This stream gets reset for every test case.

Returns:
a Scanner containing any contents you have specified.

setIn

public void setIn(String contents)
Set the contents of this test case's input stream, which can then be retrieved using in().

Parameters:
contents - The contents to use for the stream, which replace any that were there before.

setIn

public void setIn(Scanner contents)
Set the contents of this test case's input stream, which can then be retrieved using in().

Parameters:
contents - The contents to use for the stream, which replace any that were there before.

setSystemIn

public void setSystemIn(String contents)
Set the contents of this test case's input stream, which can then be retrieved using in().

Parameters:
contents - The contents to use for the stream, which replace any that were there before.

assertFuzzyEquals

public void assertFuzzyEquals(String expected,
                              String actual)
Asserts that two Strings are equal, respecting preferences for what differences matter. This method mirrors the static Assert.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.

Parameters:
expected - The expected value
actual - The value to test

assertFuzzyEquals

public void assertFuzzyEquals(String message,
                              String expected,
                              String actual)
Asserts that two Strings are equal, respecting preferences for what differences matter. This method mirrors the static Assert.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.

Parameters:
message - The message to use for a failed assertion
expected - The expected value
actual - The value to test

assertTrue

public static void assertTrue(String message,
                              boolean condition)
Asserts that a condition is true. If it isn't, it throws an AssertionFailedError with the given message. This is a special version of Assert.assertTrue(String,boolean) that issues special diagnostics when the assertion fails, if the given condition supports it.

Parameters:
message - The message to use for a failed assertion
condition - The condition to check

assertTrue

public static void assertTrue(boolean condition)
Asserts that a condition is true. If it isn't, it throws an AssertionFailedError with the given message. This is a special version of Assert.assertTrue(boolean) that issues special diagnostics when the assertion fails, if the given condition supports it.

Parameters:
condition - The condition to check

assertFalse

public static void assertFalse(String message,
                               boolean condition)
Asserts that a condition is false. If it isn't, it throws an AssertionFailedError with the given message. This is a special version of Assert.assertFalse(String,boolean) that issues special diagnostics when the assertion fails, if the given condition supports it.

Parameters:
message - The message to use for a failed assertion
condition - The condition to check

assertFalse

public static void assertFalse(boolean condition)
Asserts that a condition is false. If it isn't, it throws an AssertionFailedError with the given message. This is a special version of Assert.assertFalse(boolean) that issues special diagnostics when the assertion fails, if the given condition supports it.

Parameters:
condition - The condition to check

compact

public 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. For example, calling compact("hello there", 6, 3) will return "hel...ere".

Parameters:
content - The string to shorten
threshold - Strings longer than this will be compacted, while strings less than or equal to this limit will be returned unchanged
prefixLen - How many characters at the front and back of the string to keep. This number must be less than or equal to half the threshold
Returns:
The shortened version of the string

compact

public static String compact(String content)
Takes a string and, if it is too long, shortens it by replacing the middle with an ellipsis.

Parameters:
content - The string to shorten
Returns:
The shortened version of the string

equals

public boolean equals(String left,
                      String right)
Determines whether two Strings are equal. This method is identical to String.equals(Object), but is provided for symmetry with the other comparison predicates provided in this class. For assertion writing, remember that Assert.assertEquals(String,String) will produce more useful information on failure, however.

Parameters:
left - The first string to compare
right - The second string to compare
Returns:
True if the strings are equal

fuzzyEquals

public boolean fuzzyEquals(String left,
                           String right)
Determines whether two Strings are equal, respecting preferences for what differences matter. This method mirrors 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.

Parameters:
left - The first string to compare
right - The second string to compare
Returns:
True if the strings are equal

equalsRegex

public boolean equalsRegex(String actual,
                           String expected)
Determines whether a String exactly matches an expected regular expression. A null for the actual value is treated the same as an empty string for the purposes of matching. The regular expression must match the full string (all characters taken together). To match a substring, use 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.

Parameters:
actual - The value to test
expected - The expected value (interpreted as a regular expression Pattern)
Returns:
True if the actual matches the expected pattern

equalsRegex

public boolean equalsRegex(String actual,
                           Pattern expected)
Determines whether a String exactly matches an expected regular expression. A null for the actual value is treated the same as an empty string for the purposes of matching. The regular expression must match the full string (all characters taken together). To match a substring, use 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.

Parameters:
actual - The value to test
expected - The expected value
Returns:
True if the actual matches the expected pattern

fuzzyEqualsRegex

public boolean fuzzyEqualsRegex(String actual,
                                String expected)
Determines whether a String exactly matches an expected regular expression, respecting preferences for what differences matter. A null for the actual value is treated the same as an empty string for the purposes of matching. The regular expression must match the full string (all characters taken together). To match a substring, use 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.

Parameters:
actual - The value to test
expected - The expected value (interpreted as a regular expression Pattern)
Returns:
True if the actual matches the expected pattern

fuzzyEqualsRegex

public boolean fuzzyEqualsRegex(String actual,
                                Pattern expected)
Determines whether a String exactly matches an expected regular expression, respecting preferences for what differences matter. A null for the actual value is treated the same as an empty string for the purposes of matching. The regular expression must match the full string (all characters taken together). To match a substring, use fuzzyContainsRegex(String,String) instead.

Use stringNormalizer() to access and modify the StringNormalizer object's preferences for comparing strings.

Parameters:
actual - The value to test
expected - The expected value
Returns:
True if the actual matches the expected pattern

contains

public boolean contains(String largerString,
                        String substring)
Determine whether one String contains another as a substring. This method is identical to String.contains(CharSequence), but is provided for symmetry with the other comparison predicates provided in this class. If the largerString 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 substring is the second.

Parameters:
largerString - The target to look in
substring - The substring to look for
Returns:
True if the largerString contains the substring

contains

public boolean contains(String largerString,
                        String[] substrings)
Determine whether one String contains a sequence of other substrings in order. It looks for each element of the array of substrings 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.

Parameters:
largerString - The target to look in
substrings - The substrings to look for (in order)
Returns:
True if the largerString contains all of the specified substrings in order.

fuzzyContains

public boolean fuzzyContains(String largerString,
                             String substring)
Determine whether one String contains another as a substring, respecting preferences for what differences matter. If the larger string is null, this method returns false (since it can contain nothing).

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 substring is the second.

Parameters:
largerString - The target to look in
substring - The substring to look for
Returns:
True if the largerString contains the specified substring.

fuzzyContains

public boolean fuzzyContains(String largerString,
                             String[] substrings)
Determine whether one String contains a sequence of other substrings in order, respecting preferences for what differences matter. It looks for each element of the array of substrings 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).

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.

Parameters:
substrings - The substrings to look for (in order)
largerString - The target to look in
Returns:
True if the largerString contains all of the specified substrings in order.

containsRegex

public boolean containsRegex(String largerString,
                             String substring)
Determine whether one String contains another as a substring, where the expected substring is specified as a regular expression. If the largerString 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 substring is the second.

Parameters:
largerString - The target to look in
substring - The substring to look for (interpreted as a regular expression Pattern)
Returns:
True if the largerString contains the substring pattern

containsRegex

public boolean containsRegex(String largerString,
                             Pattern substring)
Determine whether one String contains another as a substring, where the expected substring is specified as a regular expression. If the largerString 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 substring is the second.

Parameters:
largerString - The target to look in
substring - The substring to look for
Returns:
True if the largerString contains the substring pattern

containsRegex

public 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. It looks for each element of the array of substrings 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.

Parameters:
largerString - The target to look in
substrings - An array of expected substrings (interpreted as regular expression Patterns), which must occur in the same order in the larger string
Returns:
True if the largerString contains all of the specified substrings in order.

containsRegex

public 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. It looks for each element of the array of substrings 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).

Parameters:
largerString - The target to look in
substrings - An array of expected substrings, which must occur in the same order in the larger string
Returns:
True if the largerString contains all of the specified substrings in order.

fuzzyContainsRegex

public boolean fuzzyContainsRegex(String largerString,
                                  String substring)
Determine whether one String contains another as a substring, where the expected substring is specified as a regular expression, and respecting preferences for what differences matter. This method behaves just like fuzzyContains(String,String), except that the first argument is interpreted as a regular expression. String normalization rules are only appled to the larger string, not to the regular expression.

Note that this predicate uses the opposite parameter ordering from JUnit assertions: The value to test is the first parameter, and the expected substring is the second.

Parameters:
largerString - The target to look in
substring - The substring to look for (interpreted as a regular expression Pattern)
Returns:
True if the largerString contains the substring pattern

fuzzyContainsRegex

public boolean fuzzyContainsRegex(String largerString,
                                  Pattern substring)
Determine whether one String contains another as a substring, where the expected substring is specified as a regular expression, and respecting preferences for what differences matter. This method behaves just like fuzzyContains(String,String), except that the first argument is interpreted as a regular expression. String normalization rules are only appled to the larger string, not to the regular expression.

Note that this predicate uses the opposite parameter ordering from JUnit assertions: The value to test is the first parameter, and the expected substring is the second.

Parameters:
largerString - The target to look in
substring - The substring to look for
Returns:
True if the largerString contains the substring pattern

fuzzyContainsRegex

public 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. This method behaves just like 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.

Parameters:
largerString - The target to look in
substrings - An array of expected substrings (interpreted as regular expression Patterns), which must occur in the same order in the larger string
Returns:
True if the largerString contains all of the specified substrings in order.

fuzzyContainsRegex

public 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. This method behaves just like 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.

Parameters:
largerString - The target to look in
substrings - An array of expected substrings, which must occur in the same order in the larger string
Returns:
True if the largerString contains all of the specified substrings in order.

Last updated: Wed, Apr 1, 2009 • 12:29 AM EDT

Copyright © 2009 Virginia Tech.