mondrian.test
Class DiffRepository

java.lang.Object
  extended by mondrian.test.DiffRepository

public class DiffRepository
extends Object

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:

  1. A testcase class defines a method
    
     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.

    Author:
    jhyde

    Constructor Summary
    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.
     
    Method Summary
     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.
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     

    Constructor Detail

    DiffRepository

    public DiffRepository(File refFile,
                          File logFile,
                          DiffRepository baseRepos,
                          String[] prefixes)
    Creates a DiffRepository from a pair of files.

    Parameters:
    refFile - File containing reference results
    logFile - File to contain the actual output of the test run
    baseRepos - Base repository to inherit from, or null
    prefixes - List of directories to search in, or null

    DiffRepository

    public DiffRepository(URL refUrl)
    Creates a read-only repository reading from a URL.

    Parameters:
    refUrl - URL pointing to reference file
    Method Detail

    expand

    public String expand(String tag,
                         String text)
    Expands a string containing one or more variables. (Currently only works if there is one variable.)


    set

    public void set(String resourceName,
                    String value)
    Sets the value of a given resource of the current testcase.

    Parameters:
    resourceName - Name of the resource, e.g. "sql"
    value - Value of the resource

    amend

    public void amend(String expected,
                      String actual)

    get

    public String get(String testCaseName,
                      String resourceName)

    get

    public String get(String testCaseName,
                      String resourceName,
                      String dialectName)
    Returns a given resource from a given testcase.

    Parameters:
    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"
    Returns:
    The value of the resource, or null if not found

    getTestCaseNames

    public List<String> getTestCaseNames()
    Returns:
    a list of the names of all test cases defined in the repository file

    setCurrentTestCaseName

    public void setCurrentTestCaseName(String testCaseName)
    Sets the name of the current test case. For use in tests created via dynamic suite() methods. Caller should pass test case name from setUp(), and null from tearDown() to clear.

    Parameters:
    testCaseName - name of test case to set as current, or null to clear

    getCurrentTestCaseName

    public 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".

    Parameters:
    fail - Whether to fail if no method is found
    Returns:
    Name of current testcase, or null if not found

    assertEquals

    public void assertEquals(String tag,
                             String expected,
                             String actual)

    lookup

    public static DiffRepository lookup(Class clazz)
    Finds the repository instance for a given class, using the default prefixes, and with no parent repository.

    See Also:
    lookup(Class, DiffRepository, String[])

    lookup

    public static DiffRepository lookup(Class clazz,
                                        DiffRepository baseRepos,
                                        String[] prefixes)
    Finds the repository instance for a given class.

    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.

    Parameters:
    clazz - Testcase class
    baseRepos - Base class of test class
    prefixes - Array of directory names to look in; if null, the default {"testsrc", "main"} is used
    Returns:
    The diff repository shared between testcases in this class.

    Get Mondrian at SourceForge.net. Fast, secure and free Open Source software downloads