public class TestContext extends Object
TestContext
is a singleton class which contains the information
necessary to run mondrian tests (otherwise we'd have to pass this
information into the constructor of TestCases).
The singleton instance (retrieved via the instance()
method)
contains a connection to the FoodMart database, and runs expressions in the
context of the Sales
cube.
Using the DelegatingTestContext
subclass, you can create derived
classes which use a different connection or a different cube.
Modifier and Type | Class and Description |
---|---|
static class |
TestContext.ConnectionInvocationHandler |
static class |
TestContext.DatabaseMetaDataInvocationHandler |
static class |
TestContext.HighCardDynamicSchemaProcessor
Schema processor that flags dimensions as high-cardinality if they
appear in the list of values in the
MondrianProperties.TestHighCardinalityDimensionList property. |
static class |
TestContext.SafeString
Wrapper around a string that indicates that all line endings have been
converted to platform-specific line endings.
|
static class |
TestContext.SnoopingSchemaProcessor |
Modifier | Constructor and Description |
---|---|
protected |
TestContext()
Creates a TestContext.
|
Modifier and Type | Method and Description |
---|---|
static String |
allHiers() |
static String |
allHiersExcept(String... hiers)
Generates a string containing all dimensions except those given.
|
void |
assertAxisReturns(String expression,
String expected)
Executes a query with a given expression on an axis, and asserts that it
returns the expected string.
|
void |
assertAxisThrows(String expression,
String pattern)
Executes a query with a given expression on an axis, and asserts that it
throws an error which matches a particular pattern.
|
static void |
assertEqualsVerbose(String expected,
String actual)
Checks that an actual string matches an expected string.
|
static void |
assertEqualsVerbose(String expected,
String actual,
boolean java,
String message)
Checks that an actual string matches an expected string.
|
static void |
assertEqualsVerbose(TestContext.SafeString safeExpected,
String actual,
boolean java,
String message)
Checks that an actual string matches an expected string.
|
void |
assertExprDependsOn(String expr,
String hierList)
Asserts that an MDX expression depends upon a given list of dimensions.
|
void |
assertExprReturns(String expression,
String expected)
Executes an expression and asserts that it returns a given result.
|
void |
assertExprThrows(String expression,
String pattern)
Executes an expression, and asserts that it gives an error which contains
a particular pattern.
|
void |
assertMatchesVerbose(Pattern expected,
String actual)
Checks that an actual string matches an expected pattern.
|
void |
assertMemberExprDependsOn(String expr,
String dimList)
Asserts that an MDX member-valued depends upon a given list of
dimensions.
|
void |
assertParameterizedExprReturns(String expr,
String expected,
Object... paramValues)
Asserts that an expression, with a given set of parameter bindings,
returns a given result.
|
void |
assertQueryReturns(String query,
String desiredResult)
Executes a query and checks that the result is a given string.
|
void |
assertQueryReturns(String message,
String query,
String desiredResult)
Executes a query and checks that the result is a given string,
displaying a message if result does not match desiredResult.
|
void |
assertQueryThrows(String queryString,
String pattern)
Executes a query, and asserts that it throws an exception which contains
the given pattern.
|
void |
assertSetExprDependsOn(String expr,
String dimList)
Asserts that an MDX set-valued expression depends upon a given list of
dimensions.
|
void |
assertSimpleQuery()
Executes a very simple query.
|
void |
assertSqlEquals(String expectedSql,
String actualSql,
int expectedRows)
Checks that expected SQL equals actual SQL.
|
static void |
assertStubbedEqualsVerbose(String expected,
String actual)
Checks that an actual string matches an expected string.
|
(package private) static Iterable<Cell> |
cellIter(CellSet cellSet)
Returns an iterator over cells in an olap4j cell set.
|
(package private) static Iterable<Cell> |
cellIter(Result result)
Returns an iterator over cells in a result.
|
static void |
checkThrowable(Throwable throwable,
String pattern) |
void |
close() |
String |
compileExpression(String expression,
boolean scalar)
Compiles a scalar expression in the context of the default cube.
|
static boolean |
contains(String[] a,
String s) |
static junit.framework.TestSuite |
copySuite(junit.framework.TestSuite suite,
Util.Functor1<Boolean,junit.framework.Test> testPattern)
Makes a copy of a suite, filtering certain tests.
|
TestContext |
create(String parameterDefs,
String cubeDefs,
String virtualCubeDefs,
String namedSetDefs,
String udfDefs,
String roleDefs)
Creates a TestContext which is based on a variant of the FoodMart
schema, which parameter, cube, named set, and user-defined function
definitions added.
|
TestContext |
createSubstitutingCube(String cubeName,
String dimensionDefs)
Creates a TestContext, adding hierarchy definitions to a cube definition.
|
TestContext |
createSubstitutingCube(String cubeName,
String dimensionDefs,
String memberDefs)
Creates a TestContext, adding hierarchy and calculated member definitions
to a cube definition.
|
TestContext |
createSubstitutingCube(String cubeName,
String dimensionDefs,
String measureDefs,
String memberDefs,
String namedSetDefs)
Creates a TestContext, adding hierarchy and calculated member definitions
to a cube definition.
|
TestContext |
createSubstitutingCube(String cubeName,
String dimensionDefs,
String measureDefs,
String memberDefs,
String namedSetDefs,
String defaultMeasure)
Overload that allows swapping the defaultMeasure.
|
boolean |
databaseIsValid()
Tests whether the database is valid.
|
Axis |
executeAxis(String expression)
Executes a query with a given expression on an axis, and returns the
whole axis.
|
Cell |
executeExprRaw(String expression)
Executes the expression in the context of the cube indicated by
cubeName , and returns the result as a Cell. |
CellSet |
executeOlap4jQuery(String queryString)
Executes a query using olap4j.
|
CellSet |
executeOlap4jXmlaQuery(String queryString) |
Result |
executeQuery(String queryString)
Executes a query.
|
Member |
executeSingletonAxis(String expression)
Executes a set expression which is expected to return 0 or 1 members.
|
ResultSet |
executeStatement(String queryString) |
void |
flushSchemaCache() |
static TestContext.SafeString |
fold(String string)
Replaces line-endings in a string with the platform-dependent
equivalent.
|
CacheControl |
getCacheControl()
Returns a
CacheControl . |
Connection |
getConnection()
Returns the connection to run queries.
|
Util.PropertyList |
getConnectionProperties() |
String |
getConnectString()
Returns the connect string by which the unit tests can talk to the
FoodMart database.
|
static String |
getDefaultConnectString()
Constructs a connect string by which the unit tests can talk to the
FoodMart database.
|
String |
getDefaultCubeName()
Returns the name of the default cube.
|
Dialect |
getDialect() |
static Dialect |
getFakeDialect(Dialect.DatabaseProduct product)
Creates a dialect without using a connection.
|
OlapConnection |
getOlap4jConnection() |
static String |
getRawFoodMartSchema()
Returns the definition of the "FoodMart" schema as stored in
FoodMart.xml . |
String |
getRawSchema()
Returns the definition of the schema.
|
String |
getSchema(String parameterDefs,
String cubeDefs,
String virtualCubeDefs,
String namedSetDefs,
String udfDefs,
String roleDefs)
Returns a the XML of the current schema with added parameters and cube
definitions.
|
List<Exception> |
getSchemaWarnings()
Creates a FoodMart connection with "Ignore=true" and returns the list
of warnings in the schema.
|
static String |
getStackTrace(Throwable e)
Converts a
Throwable to a stack trace. |
PrintWriter |
getWriter()
Returns the output writer.
|
static String |
hierarchyName(String dimension,
String hierarchy) |
static TestContext |
instance()
Retrieves the singleton (instantiating if necessary).
|
static String |
levelName(String dimension,
String hierarchy,
String level) |
static String |
repeatString(int count,
String format)
Returns count copies of a string.
|
static String |
stubAnonymousClasses(String str)
Replaces anonymous class names (/\$\d+/)
with a stub "$-anonymous-class-"
in constructions
"class mondrian.rest.package.name.ClassName$InnerClassNames".
|
static String |
toString(CellSet cellSet)
Converts a
CellSet to text in traditional format. |
static String |
toString(List<Position> positions)
Converts a set of positions into a string.
|
static String |
toString(Result result)
Converts a
Result to text in traditional format. |
static String |
unfold(String string)
Reverses the effect of
fold(java.lang.String) ; converts platform-specific line
endings in a string info linefeeds. |
String |
upgradeActual(String actual)
Massages the actual result of executing a query to handle differences in
unique names betweeen old and new behavior.
|
String |
upgradeQuery(String queryString)
Massages an MDX query to handle differences in
unique names betweeen old and new behavior.
|
TestContext |
withConnection(Connection connection)
Returns a
TestContext similar to this one, but which uses a given
connection. |
TestContext |
withCube(String cubeName)
Returns a TestContext similar to this one, but using the given cube as
default for tests such as
assertExprReturns(String, String) . |
TestContext |
withFreshConnection()
Returns a
TestContext similar to this one, but which uses a fresh
connection. |
TestContext |
withProperties(Util.PropertyList properties)
Creates a TestContext which is like this one but uses the given
connection properties.
|
TestContext |
withRole(String roleName)
Returns a TestContext similar to this one, but using the given role.
|
TestContext |
withScenario()
Returns a test context whose
getOlap4jConnection() method always
returns the same connection object, and which has an active
Scenario , thus enabling writeback. |
TestContext |
withSchema(String schema)
Creates a TestContext which contains the given schema text.
|
TestContext |
withSchemaPool(boolean usePool) |
TestContext |
withSchemaProcessor(Class<? extends DynamicSchemaProcessor> dynProcClass)
Returns a connection to the FoodMart database
with a dynamic schema processor and disables use of RolapSchema Pool.
|
protected static final String nl
public static TestContext instance()
public final String getConnectString()
In the base class, the result is the same as the static method
getDefaultConnectString()
. If a derived class overrides
getConnectionProperties()
, the result of this method
will change also.
public static String getDefaultConnectString()
MondrianProperties.TestConnectString
, if it is
set.MondrianProperties.FoodmartJdbcURL
is set, this
overrides the Jdbc
property.catalog
URL is unset or invalid, it assumes that
we are at the root of the source tree, and references
demo/FoodMart.xml
public void flushSchemaCache()
public Connection getConnection()
When invoked on the default TestContext instance, returns a connection to the FoodMart database.
public TestContext withSchemaProcessor(Class<? extends DynamicSchemaProcessor> dynProcClass)
public final TestContext withFreshConnection()
TestContext
similar to this one, but which uses a fresh
connection.withSchemaPool(boolean)
public TestContext withSchemaPool(boolean usePool)
public Util.PropertyList getConnectionProperties()
public String getSchema(String parameterDefs, String cubeDefs, String virtualCubeDefs, String namedSetDefs, String udfDefs, String roleDefs)
public static String getRawFoodMartSchema()
FoodMart.xml
.public String getRawSchema()
public Result executeQuery(String queryString)
queryString
- Query stringpublic ResultSet executeStatement(String queryString) throws SQLException
SQLException
public CellSet executeOlap4jQuery(String queryString) throws SQLException
SQLException
public CellSet executeOlap4jXmlaQuery(String queryString) throws SQLException
SQLException
static Iterable<Cell> cellIter(CellSet cellSet)
public void assertQueryThrows(String queryString, String pattern)
queryString
- Query stringpattern
- Pattern which exception must matchpublic void assertExprThrows(String expression, String pattern)
public String getDefaultCubeName()
Tests which evaluate scalar expressions, such as
assertExprReturns(String, String)
, generate queries against this
cube.
public Cell executeExprRaw(String expression)
cubeName
, and returns the result as a Cell.expression
- The expression to evaluatepublic void assertExprReturns(String expression, String expected)
public void assertParameterizedExprReturns(String expr, String expected, Object... paramValues)
expr
- Scalar MDX expressionexpected
- Expected resultparamValues
- Array of parameter names and valuespublic void assertAxisReturns(String expression, String expected)
public String upgradeActual(String actual)
Even though the new naming is not enabled by default, reference logs should be in terms of the new naming.
actual
- Actual resultMondrianProperties.SsasCompatibleNaming
public String upgradeQuery(String queryString)
The main difference addressed is with level naming. The problem arises when dimension, hierarchy and level have the same name:
So, upgradeQuery("[Gender]")
returns
"[Gender].[Gender]" for old behavior,
"[Gender].[Gender].[Gender]" for new behavior.
queryString
- Original queryMondrianProperties.SsasCompatibleNaming
public String compileExpression(String expression, boolean scalar)
expression
- The expression to evaluatescalar
- Whether the expression is scalarpublic Member executeSingletonAxis(String expression)
expression
- Expression stringpublic Axis executeAxis(String expression)
public void assertAxisThrows(String expression, String pattern)
public PrintWriter getWriter()
public void assertQueryReturns(String query, String desiredResult)
public void assertQueryReturns(String message, String query, String desiredResult)
public void assertSimpleQuery()
This forces the schema to be loaded and performs a basic sanity check. If this is a negative schema test, causes schema validation errors to be thrown.
public static void assertEqualsVerbose(String expected, String actual)
If they do not, throws a ComparisonFailure
and
prints the difference, including the actual string as an easily pasted
Java string literal.
public static void assertEqualsVerbose(String expected, String actual, boolean java, String message)
If they do not, throws a ComparisonFailure
and prints the
difference, including the actual string as an easily pasted Java string
literal.
expected
- Expected stringactual
- Actual stringjava
- Whether to generate actual string as a Java string literal
if the values are not equalmessage
- Message to display, optionalpublic static void assertEqualsVerbose(TestContext.SafeString safeExpected, String actual, boolean java, String message)
If they do not, throws a ComparisonFailure
and prints the
difference, including the actual string as an easily pasted Java string
literal.
safeExpected
- Expected string, where all line endings have been
converted into platform-specific line endingsactual
- Actual stringjava
- Whether to generate actual string as a Java string literal
if the values are not equalmessage
- Message to display, optionalpublic static void assertStubbedEqualsVerbose(String expected, String actual)
If they do not, throws a ComparisonFailure
and
prints the difference, including the actual string as an easily pasted
Java string literal.
public void assertMatchesVerbose(Pattern expected, String actual)
ComparisonFailure
and prints the
difference, including the actual string as an easily pasted Java string
literal.public static String getStackTrace(Throwable e)
Throwable
to a stack trace.public static String toString(Result result)
Result
to text in traditional format.
For more exotic formats, see
CellSetFormatter
.
result
- Query resultpublic static String toString(CellSet cellSet)
CellSet
to text in traditional format.
For more exotic formats, see
CellSetFormatter
.
cellSet
- Query resultpublic final TestContext withScenario()
getOlap4jConnection()
method always
returns the same connection object, and which has an active
Scenario
, thus enabling writeback.public static String toString(List<Position> positions)
public static junit.framework.TestSuite copySuite(junit.framework.TestSuite suite, Util.Functor1<Boolean,junit.framework.Test> testPattern)
suite
- Test suitetestPattern
- Regular expression of name of tests to includepublic void close()
public CacheControl getCacheControl()
CacheControl
.public static TestContext.SafeString fold(String string)
string
- String whose line endings are to be made platform-
dependent. Typically these are constant "expected
value" string expressions where the linefeed is
represented as linefeed "\n", but sometimes this method
will receive strings created dynamically where the line
endings are already appropriate for the platform.public static String unfold(String string)
fold(java.lang.String)
; converts platform-specific line
endings in a string info linefeeds.string
- String where all linefeeds have been converted to
platform-specific (CR+LF on Windows, LF on Unix/Linux)public Dialect getDialect()
public static Dialect getFakeDialect(Dialect.DatabaseProduct product)
product
- Database productpublic void assertSqlEquals(String expectedSql, String actualSql, int expectedRows)
public void assertSetExprDependsOn(String expr, String dimList)
public void assertMemberExprDependsOn(String expr, String dimList)
public void assertExprDependsOn(String expr, String hierList)
public final TestContext create(String parameterDefs, String cubeDefs, String virtualCubeDefs, String namedSetDefs, String udfDefs, String roleDefs)
parameterDefs
- Parameter definitions. If not null, the string is
is inserted into the schema XML in the appropriate place for
parameter definitions.cubeDefs
- Cube definition(s). If not null, the string is
is inserted into the schema XML in the appropriate place for
cube definitions.virtualCubeDefs
- Definitions of virtual cubes. If not null, the
string is inserted into the schema XML in the appropriate place for
virtual cube definitions.namedSetDefs
- Definitions of named sets. If not null, the string
is inserted into the schema XML in the appropriate place for
named set definitions.udfDefs
- Definitions of user-defined functions. If not null, the
string is inserted into the schema XML in the appropriate place for
UDF definitions.roleDefs
- Definitions of rolespublic final TestContext withSchema(String schema)
schema
- XML schema contentpublic TestContext withProperties(Util.PropertyList properties)
properties
- Connection propertiespublic final TestContext createSubstitutingCube(String cubeName, String dimensionDefs)
cubeName
- Name of a cube in the schema (cube must exist)dimensionDefs
- String defining dimensions, or nullpublic final TestContext createSubstitutingCube(String cubeName, String dimensionDefs, String memberDefs)
cubeName
- Name of a cube in the schema (cube must exist)dimensionDefs
- String defining dimensions, or nullmemberDefs
- String defining calculated members, or nullpublic final TestContext createSubstitutingCube(String cubeName, String dimensionDefs, String measureDefs, String memberDefs, String namedSetDefs)
cubeName
- Name of a cube in the schema (cube must exist)dimensionDefs
- String defining dimensions, or nullmeasureDefs
- String defining measures, or nullmemberDefs
- String defining calculated members, or nullnamedSetDefs
- String defining named set definitions, or nullpublic final TestContext createSubstitutingCube(String cubeName, String dimensionDefs, String measureDefs, String memberDefs, String namedSetDefs, String defaultMeasure)
public final TestContext withRole(String roleName)
roleName
- Role namepublic final TestContext withCube(String cubeName)
assertExprReturns(String, String)
.cubeName
- Cube namepublic final TestContext withConnection(Connection connection)
TestContext
similar to this one, but which uses a given
connection.connection
- Connectionpublic static String allHiersExcept(String... hiers)
assertExprDependsOn(String, String)
.public static String allHiers()
public List<Exception> getSchemaWarnings()
public OlapConnection getOlap4jConnection() throws SQLException
SQLException
public boolean databaseIsValid()
public static String repeatString(int count, String format)
String.format(java.lang.String, java.lang.Object...)
.count
- Number of copiesformat
- String templatepublic static String stubAnonymousClasses(String str)
stubAnonymousClasses("class mondrian.fun.Fun$21$1")
results
"class mondrian.fun.Fun$-anonymous-class-$-anonymous-class-"
.
String
s makes the comparison
independent on anonymous class names.