public class DiffRepository extends Object
Loads files containing test input and output into memory. If there are differences, writes out a log file containing the actual output.
Typical usage is as follows:
package com.acme.test;
public class MyTest extends TestCase {
public DiffRepository getDiffRepos() {
return DiffRepository.lookup(MyTest.class);
}
public void testToUpper() {
getDiffRepos().assertEquals("${result}", "${string}");
}
public void testToLower() {
getDiffRepos().assertEquals("Multi-line\nstring", "${string}");
}
}
There is an accompanying reference file named after the class,
com/acme/test/MyTest.ref.xml
:
<Root>
<TestCase name="testToUpper">
<Resource name="string">
<![CDATA[String to be converted to upper case]]>
</Resource>
<Resource name="result">
<![CDATA[STRING TO BE CONVERTED TO UPPER CASE]]>
</Resource>
</TestCase>
<TestCase name="testToLower">
<Resource name="result">
<![CDATA[multi-line
string]]>
</Resource>
</TestCase>
</Root>
If any of the testcases fails, a log file is generated, called
com/acme/test/MyTest.log.xml
containing the actual output.
The log file is otherwise identical to the reference log, so once the
log file has been verified, it can simply be copied over to become the new
reference log.
If a resource or testcase does not exist, DiffRepository
creates them in the log file. Because DiffRepository is so forgiving, it is
very easy to create new tests and testcases.
The lookup(java.lang.Class)
method ensures that all test cases share the same
instance of the repository. This is important more than one one test case
fails. The shared instance ensures that the generated .log.xml
file contains the actual for both test cases.
Constructor and Description |
---|
DiffRepository(File refFile,
File logFile,
DiffRepository baseRepos,
String[] prefixes)
Creates a DiffRepository from a pair of files.
|
DiffRepository(URL refUrl)
Creates a read-only repository reading from a URL.
|
Modifier and Type | Method and Description |
---|---|
void |
amend(String expected,
String actual) |
void |
assertEquals(String tag,
String expected,
String actual) |
String |
expand(String tag,
String text)
Expands a string containing one or more variables.
|
String |
get(String testCaseName,
String resourceName) |
String |
get(String testCaseName,
String resourceName,
String dialectName)
Returns a given resource from a given testcase.
|
String |
getCurrentTestCaseName(boolean fail)
Returns the name of the current testcase by looking up the call
stack for a method whose name starts with "test", for example
"testFoo".
|
List<String> |
getTestCaseNames() |
static DiffRepository |
lookup(Class clazz)
Finds the repository instance for a given class, using the default
prefixes, and with no parent repository.
|
static DiffRepository |
lookup(Class clazz,
DiffRepository baseRepos,
String[] prefixes)
Finds the repository instance for a given class.
|
void |
set(String resourceName,
String value)
Sets the value of a given resource of the current testcase.
|
void |
setCurrentTestCaseName(String testCaseName)
Sets the name of the current test case.
|
public DiffRepository(File refFile, File logFile, DiffRepository baseRepos, String[] prefixes)
refFile
- File containing reference resultslogFile
- File to contain the actual output of the test runbaseRepos
- Base repository to inherit from, or nullprefixes
- List of directories to search in, or nullpublic DiffRepository(URL refUrl)
refUrl
- URL pointing to reference filepublic String expand(String tag, String text)
public void set(String resourceName, String value)
resourceName
- Name of the resource, e.g. "sql"value
- Value of the resourcepublic String get(String testCaseName, String resourceName, String dialectName)
testCaseName
- Name of test case, e.g. "testFoo"resourceName
- Name of resource, e.g. "sql", "plan"dialectName
- Name of sql dialect, e.g. "MYSQL", "LUCIDDB"public List<String> getTestCaseNames()
public void setCurrentTestCaseName(String testCaseName)
testCaseName
- name of test case to set as current,
or null to clearpublic String getCurrentTestCaseName(boolean fail)
fail
- Whether to fail if no method is foundpublic static DiffRepository lookup(Class clazz)
lookup(Class, DiffRepository, String[])
public static DiffRepository lookup(Class clazz, DiffRepository baseRepos, String[] prefixes)
It is important that all testcases in a class share the same repository instance. This ensures that, if two or more testcases fail, the log file will contains the actual results of both testcases.
The baseRepos
parameter is useful if the test is an
extension to a previous test. If the test class has a base class which
also has a repository, specify the repository here. DiffRepository will
look for resources in the base class if it cannot find them in this
repository. If test resources from testcases in the base class are
missing or incorrect, it will not write them to the log file -- you
probably need to fix the base test.
clazz
- Testcase classbaseRepos
- Base class of test classprefixes
- Array of directory names to look in; if null, the
default {"testsrc", "main"} is used