Class DiffRepository

java.lang.Object
org.apache.calcite.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. A test case class defines a method


 package com.acme.test;
  
 public class MyTest extends TestCase {
   public DiffRepository getDiffRepos() {
     return DiffRepository.lookup(MyTest.class);
   }
  
   @Test void testToUpper() {
     getDiffRepos().assertEquals("${result}", "${string}");
   }
  
   @Test void testToLower() {
     getDiffRepos().assertEquals("Multi-line\nstring", "${string}");
   }
 }
 

There is an accompanying reference file named after the class, src/test/resources/com/acme/test/MyTest.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 test cases fails, a log file is generated, called build/diffrepo/test/com/acme/test/MyTest_actual.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:

cp build/diffrepo/test/com/acme/test/MyTest_actual.xml src/test/resources/com/acme/test/MyTest.xml

If a resource or test case does not exist, DiffRepository creates them in the log file. Because DiffRepository is so forgiving, it is very easy to create new tests and test cases.

The lookup(java.lang.Class<?>) method ensures that all test cases share the same instance of the repository. This is important more than one test case fails. The shared instance ensures that the generated build/diffrepo/test/com/acme/test/MyTest_actual.xml file contains the actual for both test cases.

  • Method Details

    • checkActualAndReferenceFiles

      public void checkActualAndReferenceFiles()
    • castNonNull

      public static DiffRepository castNonNull(@Nullable DiffRepository diffRepos)
      Returns the diff repository, checking that it is not null.

      If it is null, throws IllegalArgumentException with a message informing people that they need to change their test configuration.

    • 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 test case.
      Parameters:
      resourceName - Name of the resource, e.g. "sql"
      value - Value of the resource
    • amend

      public void amend(String expected, String actual)
    • 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, with no base repository or filter.
      Parameters:
      clazz - Test case class
      Returns:
      The diff repository shared between test cases in this class.
    • lookup

      public static DiffRepository lookup(Class<?> clazz, DiffRepository baseRepository, DiffRepository.Filter filter, int indent)
      Finds the repository instance for a given class.

      It is important that all test cases in a class share the same repository instance. This ensures that, if two or more test cases fail, the log file will contains the actual results of both test cases.

      The baseRepository 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 test cases 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.

      Parameters:
      clazz - Test case class
      baseRepository - Base repository
      filter - Filters each string returned by the repository
      indent - Indent of the XML file (usually 2)
      Returns:
      The diff repository shared between test cases in this class