org.apache.avalon.fortress.testcase
Class FortressTestCase
java.lang.Object
junit.framework.Assert
junit.framework.TestCase
org.apache.avalon.fortress.testcase.FortressTestCase
- All Implemented Interfaces:
- junit.framework.Test
- public class FortressTestCase
- extends junit.framework.TestCase
JUnit TestCase for Avalon Components in Fortress.
This class extends the JUnit TestCase class to setup an environment which makes it possible to easily test Avalon Components. The following methods and instance variables are exposed for convenience testing:
- m_serviceManager
- This instance variable contains an initialized ServiceLocator which can be used to lookup Components configured in the test configuration file. (see below)
- getLogger()
- This method returns the default logger for this test case
The following test case configuration can be used as a basis for new tests. Detailed are explanations of the configuration elements can be found after the example. The example will log all logger output to the console and to a log file.
<testcase>
<annotation>
<![CDATA[
<title>{Name of test}</title>
<para>
{Description of test}
The configuration is specified in the file located in
<parameter>avalon-excalibur/src/test/{path and name of conf file}.xtext</parameter>.
</para>
]]>
</annotation>
<logger log-level="INFO">
<factories>
<factory type="stream" class="org.apache.avalon.excalibur.logger.factory.StreamTargetFactory"/>
<factory type="file" class="org.apache.avalon.excalibur.logger.factory.FileTargetFactory"/>
</factories>
<targets>
<stream id="console">
<stream>System.out</stream>
<format type="avalon">
%7.7{priority} %23.23{time:yyyy-MM-dd' 'HH:mm:ss.SSS} [%30.30{category}] (%{context}): %{message}\n%{throwable}
</format>
</stream>
<file id="log-file">
<filename>TEST-{full test class name}.log</filename>
<format type="avalon">
%7.7{priority} %23.23{time:yyyy-MM-dd' 'HH:mm:ss.SSS} [%30.30{category}] (%{context}): %{message}\n%{throwable}
</format>
</file>
</targets>
<categories>
<category name="test" log-level="INFO">
<log-target id-ref="console"/>
<log-target id-ref="log-file"/>
</category>
<category name="jdbc" log-level="INFO">
<log-target id-ref="console"/>
<log-target id-ref="log-file"/>
</category>
</categories>
</logger>
<context>
<entry name="foo" value="bar"/>
<entry name="baz" class="my.context.Class"/>
</context>
<roles>
<role name="org.apache.avalon.excalibur.datasource.DataSourceComponent"
<component shorthand="jdbc"
class="org.apache.avalon.excalibur.datasource.JdbcDataSource"
handler="org.apache.avalon.fortress.impl.handler.ThreadSafeComponentHandler"/>
</role>
</roles>
<components>
<jdbc name="personell" logger="jdbc">
<pool-controller min="5" max="10"/>
<jdbc name="personnel"/>
<dburl>jdbc:odbc:test</dburl>
<user>test</user>
<password>test</password>
<driver>sun.jdbc.odbc.JdbcOdbcDriver</driver>
</jdbc>
</components>
</testcase>
Element Explanation:
- testcase
- Defines a test case configuration. Must contain one each of the following elements:
annotation, logger, context, roles, and components
- .
- annotation
- Defines a test annotation. This element should define a block of XML enclosed within a CDATA element. The XML should be made up of a
title element, naming the test, and a para element which is used to describe the test.
- logger
-
Configures the logger used by the test cases and the components used by the tests. The
logger element takes two optional attributes:
- logger
- Uses to name the logger which is used to bootstrap the LogKit logger. (Defaults to
"lm")
- log-level
- Because the logger used by the LogKit must be created before the Log Kit Manager is initialized, it must be fully configured before the
logger element is parsed. This attribute allows the Log Kit's log priority to be set. This log level will also become the default for the Role Manager, Service Manager, and all components if they do not have category elements declated in the logger element. (Defaults to "INFO")
The loggers used by test cases and components can be easily configured from within this file. The default test configuration, shown above, includes a "test" category. This category is used to configure the default logger for all test cases. If it is set to "DEBUG", then all test debug logging will be enabled. To enalble debug logging for a single test case, a child category must be defined for the "testCheckTotals" test case as follows:
<categories>
<category name="test" log-level="INFO">
<log-target id-ref="console"/>
<log-target id-ref="log-file"/>
<category name="testCheckTotals" log-level="DEBUG">
<log-target id-ref="console"/>
<log-target id-ref="log-file"/>
</category>
</category>
</categories>
For general information on how to configure the Logger Manager, please refer to the Log Kit documentation.
- context
- Allows context properties to be set in the context passed to any Contextualizable components.
- roles
- Roles configuration for the Components configured in the
components element. The logger used by the RoleManager can be configured using a logger attribute, which defaults to "rm". By default this logger will have the same log level and formatting as the LogKit logger. It can be configured by adding a category within the logger element.
- components
- Used to configure any Components used by the test cases. The logger used by the ServiceLocator can be configured using a
logger attribute, which defaults to "cm". By default this logger will have the same log level and formatting as the LogKit logger. It can be configured by adding a category within the logger element.
- Author:
| Fields inherited from class junit.framework.TestCase |
|
| Method Summary |
protected void |
addContext(DefaultContext context)
This method may be overwritten by subclasses to put additional objects into the context programmatically. |
private void |
done()
Disposes Fortress |
private java.lang.String |
getBaseClassName(java.lang.Class clazz)
Exctract the base class name of a class. |
protected Logger |
getLogger()
Return the logger |
protected boolean |
hasService(java.lang.String key)
|
protected java.lang.Object |
lookup(java.lang.String key)
|
protected void |
prepare()
Initializes Fortress. |
protected void |
prepare(java.io.InputStream testconf)
Initializes Fortress. |
protected void |
release(java.lang.Object object)
|
void |
run(junit.framework.TestResult result)
Override run so that we can have code that is run once. |
private void |
setCurrentLogger(java.lang.String name)
Sets the logger which will be returned by getLogger and getLogEnabledLogger |
private Context |
setupContext(Configuration conf, Context parentContext)
Set up a context according to the xtest configuration specifications context element. |
| Methods inherited from class junit.framework.TestCase |
countTestCases, createResult, getName, name, run, runBare, runTest, setName, setUp, tearDown, toString |
| Methods inherited from class junit.framework.Assert |
assert, assert, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertNotNull, assertNotNull, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
FORMAT
private static final java.lang.String FORMAT
-
- See Also:
- Constant Field Values
m_logger
private Logger m_logger
-
m_containerManager
private DefaultContainerManager m_containerManager
-
m_container
private DefaultContainer m_container
-
m_loggerManager
private LoggerManager m_loggerManager
-
m_serviceManager
private ServiceManager m_serviceManager
-
m_tests
private static java.util.HashMap m_tests
-
FortressTestCase
public FortressTestCase(java.lang.String name)
hasService
protected final boolean hasService(java.lang.String key)
-
lookup
protected final java.lang.Object lookup(java.lang.String key)
throws ServiceException
-
- Throws:
ServiceException
release
protected final void release(java.lang.Object object)
-
getLogger
protected Logger getLogger()
-
Return the logger
-
run
public final void run(junit.framework.TestResult result)
-
Override
run so that we can have code that is run once.
-
prepare
protected void prepare()
throws java.lang.Exception
-
Initializes Fortress. The configuration file is determined by the class name plus .xtest appended, all '.' replaced by '/' and loaded as a resource via classpath
-
- Throws:
java.lang.Exception
prepare
protected final void prepare(java.io.InputStream testconf)
throws java.lang.Exception
-
Initializes Fortress.
-
- Parameters:
testconf - The configuration file is passed as an InputStream A common way to supply a InputStream is to overide the prepare() method in the sub class, do there whatever is needed to get the right InputStream object supplying a conformant xtest configuartion and pass it to this prepare method. the mentioned initialize method is also the place to set a different logging priority to the member variable m_logPriority.
- Throws:
java.lang.Exception
setCurrentLogger
private void setCurrentLogger(java.lang.String name)
-
Sets the logger which will be returned by getLogger and getLogEnabledLogger
-
setupContext
private Context setupContext(Configuration conf,
Context parentContext)
throws java.lang.Exception
-
Set up a context according to the xtest configuration specifications context element. A method addContext(DefaultContext context) is called here to enable subclasses to put additional objects into the context programmatically.
-
- Throws:
java.lang.Exception
addContext
protected void addContext(DefaultContext context)
-
This method may be overwritten by subclasses to put additional objects into the context programmatically.
-
getBaseClassName
private java.lang.String getBaseClassName(java.lang.Class clazz)
-
Exctract the base class name of a class.
-
done
private void done()
-
Disposes Fortress
-
Please see keelframework.org for information about new Keel releases.