|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.eigenbase.test.DiffRepository
public class DiffRepository
A collection of resources used by tests.
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.
Nested Class Summary | |
---|---|
static interface |
DiffRepository.Filter
Callback to filter strings before returning them. |
Field Summary | |
---|---|
private DiffRepository |
baseRepos
|
private Document |
doc
|
private DocumentBuilder |
docBuilder
|
private DiffRepository.Filter |
filter
|
private File |
logFile
|
private static Map<Class,DiffRepository> |
mapClassToRepos
Holds one diff-repository per class. |
private File |
refFile
|
private static String |
ResourceNameAttr
|
private static String |
ResourceTag
|
private Element |
root
|
private static String |
RootTag
|
private static String |
TestCaseNameAttr
|
private static String |
TestCaseOverridesAttr
|
private static String |
TestCaseTag
|
Constructor Summary | |
---|---|
private |
DiffRepository(File refFile,
File logFile,
DiffRepository baseRepos,
DiffRepository.Filter filter)
Creates a DiffRepository. |
Method Summary | |
---|---|
void |
amend(String expected,
String actual)
|
void |
assertEquals(String tag,
String expected,
String actual)
|
void |
assertEqualsMulti(String[] tags,
String[] expecteds,
String[] actuals,
boolean ignoreNulls)
As assertEquals(String, String, String) , but checks multiple
values in parallel. |
String |
expand(String tag,
String text)
Expands a string containing one or more variables. |
private static File |
findFile(Class clazz,
String suffix)
|
private void |
flushDoc()
Flush the reference document to the file system. |
private String |
get(String testCaseName,
String resourceName)
Returns a given resource from a given testcase. |
private 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". |
private static File |
getFileBase(Class clazz)
Returns the base directory relative to which test logs are stored. |
private static File |
getFileBaseGivenRoot(Class clazz,
String root,
boolean searchParent)
|
private static Element |
getResourceElement(Element testCaseElement,
String resourceName)
Returns a given resource from a given testcase. |
private Element |
getTestCaseElement(String testCaseName,
boolean checkOverride)
Returns the <TestCase> element corresponding to the current test case. |
private static String |
getText(Element element)
Returns the text under an element. |
private static boolean |
isWhitespace(String text)
|
static DiffRepository |
lookup(Class clazz)
Finds the repository instance for a given class, with no base repository or filter. |
static DiffRepository |
lookup(Class clazz,
DiffRepository baseRepos)
Finds the repository instance for a given class and inheriting from a given repository. |
static DiffRepository |
lookup(Class clazz,
DiffRepository baseRepos,
DiffRepository.Filter filter)
Finds the repository instance for a given class. |
private static void |
removeAllChildren(Element element)
|
void |
set(String resourceName,
String value)
Sets the value of a given resource of the current testcase. |
private void |
update(String testCaseName,
String resourceName,
String value)
Creates a new document with a given resource. |
private static void |
write(Document doc,
Writer w)
Serializes an XML document as text. |
private static void |
writeNode(Node node,
org.eigenbase.xom.XMLOutput out)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private static final String RootTag
private static final String TestCaseTag
private static final String TestCaseNameAttr
private static final String TestCaseOverridesAttr
private static final String ResourceTag
private static final String ResourceNameAttr
private static final Map<Class,DiffRepository> mapClassToRepos
private final DiffRepository baseRepos
private final DocumentBuilder docBuilder
private Document doc
private final Element root
private final File refFile
private final File logFile
private final DiffRepository.Filter filter
Constructor Detail |
---|
private DiffRepository(File refFile, File logFile, DiffRepository baseRepos, DiffRepository.Filter filter)
refFile
- Reference filelogFile
- Log filebaseRepos
- Parent repository or nullfilter
- Filter or nullMethod Detail |
---|
private static File findFile(Class clazz, String suffix)
private static File getFileBase(Class clazz)
private static File getFileBaseGivenRoot(Class clazz, String root, boolean searchParent)
public 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 void amend(String expected, String actual)
private String get(String testCaseName, String resourceName)
testCaseName
- Name of test case, e.g. "testFoo"resourceName
- Name of resource, e.g. "sql", "plan"
private static String getText(Element element)
private Element getTestCaseElement(String testCaseName, boolean checkOverride)
testCaseName
- Name of test casecheckOverride
- Make sure that if an element overrides an element in
a base repository, it has overrides="true"
private String getCurrentTestCaseName(boolean fail)
fail
- Whether to fail if no method is found
public void assertEquals(String tag, String expected, String actual)
public void assertEqualsMulti(String[] tags, String[] expecteds, String[] actuals, boolean ignoreNulls)
assertEquals(String, String, String)
, but checks multiple
values in parallel.
If any of the values do not match, throws an AssertFailure
,
but still updates the other values. This is convenient, because if a unit
test needs to check N values, you can correct the logfile in 1 pass
through the test rather than N.
tags
- Array of tagsexpecteds
- Array of expected valuesactuals
- Array of actual valuesignoreNulls
- Whether to ignore entries for which expected[i] ==
nullprivate void update(String testCaseName, String resourceName, String value)
This method is synchronized, in case two threads are running test cases of this test at the same time.
testCaseName
- Test case nameresourceName
- Resource namevalue
- New value of resourceprivate void flushDoc()
private static Element getResourceElement(Element testCaseElement, String resourceName)
testCaseElement
- The enclosing TestCase element, e.g.
<TestCase name="testFoo">
.resourceName
- Name of resource, e.g. "sql", "plan"
private static void removeAllChildren(Element element)
private static void write(Document doc, Writer w)
FIXME: I'm sure there's a library call to do this, but I'm danged if I can find it. -- jhyde, 2006/2/9.
private static void writeNode(Node node, org.eigenbase.xom.XMLOutput out)
private static boolean isWhitespace(String text)
public static DiffRepository lookup(Class clazz)
clazz
- Testcase class
public static DiffRepository lookup(Class clazz, DiffRepository baseRepos)
clazz
- Testcase classbaseRepos
- Base class of test class
public static DiffRepository lookup(Class clazz, DiffRepository baseRepos, DiffRepository.Filter filter)
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.
Use the filter
parameter if you expect the test to
return results slightly different than in the repository. This happens
if the behavior of a derived test is slightly different than a base
test. If you do not specify a filter, no filtering will happen.
clazz
- Testcase classbaseRepos
- Base repositoryfilter
- Filters each string returned by the repository
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |