From 3c1db41ac7c7a94c4530a64c42b1b5b5278f5263 Mon Sep 17 00:00:00 2001
From: Jan Materne This is the category that Ant provides specifically for youto
+ This is the category that Ant provides specifically for you to
define your own Selectors. Anywhere you want to use your selector
you use the For a robust component (and selectors are (Project)Component´s) tests are
+ necessary. For testing Tasks we use JUnit TestCases - more specific
+ org.apache.tools.ant.BuildFileTest extends junit.framework.TestCase.
+ Some of its features like configure the (test) project by reading its buildfile and
+ execute targets we need for selector tests also. Therefore we use that BuildFileTest.
+ But testing selectors requires some more work: having a set of files, instantiate
+ and configure the selector, check the selection work and more. Because we usually
+ extend BaseExtendSelector its features have to be tested also (e.g. setError()).
+ That´s why we have a base class for doing our selector tests:
+ org.apache.tools.ant.types.selectors.BaseSelectorTest. This class extends TestCase and therefore can included in the set of Ant´s
+ unit tests. It holds an instance of preconfigured BuildFileTest. Configuration
+ is done by parsing the src/etc/testcases/types/selectors.xml. BaseSelectorTest
+ then gives us helper methods for handling multiple selections. Because the term "testcase" or "testenvironment" are so often used, this
+ special testenvironment got a new name: bed. Like you initialize the
+ test environment by calling setUp() and cleaning by calling tearDown() (or like
+ to make your bed before go sleeping) you have to do that work with your
+ bed by calling makeBed() respecitive cleanupBed(). A usual test scenario is
+ <custom>
element and specify
the class name of your selector within it. See the
@@ -86,7 +86,8 @@
org.apache.tools.ant.types.selectors.SelectorContainer
.
This is an interface, so you will also have to add an implementation
for the method in the classes which implement it, namely
- org.apache.tools.ant.types.AbstractFileSet
and
+ org.apache.tools.ant.types.AbstractFileSet
,
+ org.apache.tools.ant.taskdefs.MatchingTask
and
org.apache.tools.ant.types.selectors.BaseSelectorContainer
.
Once it is in there, it will be available everywhere that core
selectors are appropriate.Testing Selectors
+
+
+
+
For common way of instantiation you have to override the getInstance() + simply by returning a new object of your selector. For easier "selection and verification work" + BaseSelectorTest provides the method performTests() which + iterates over all files (and directories) in the String array filenames + and checks whether the given selector returns the expected result. If an error + occured (especially the selector does not return the expected result) the test + fails and the failing filenames are logged.
+ +An example test would be:
+package org.apache.tools.ant.types.selectors; + +public class MySelectorTest extends BaseSelectorTest { + + public MySelectorTest(String name) { + super(name); + } + + public BaseSelector getInstance() { + return new MySelector(); + } + + public void testCase1() { + try { + // initialize test environment 'bed' + makeBed(); + + // Configure the selector + MySelector s = (MySelector)getSelector(); + s.addParam("key1", "value1"); + s.addParam("key2", "value2"); + s.setXX(true); + s.setYY("a value"); + + // do the tests + performTests(s, "FTTTTTTTTTTT"); // First is not selected - rest is + + } finally { + // cleanup the environment + cleanupBed(); + } + } +} ++ As an example of an error JUnit could log
+ [junit] FAILED + [junit] Error for files: .;copy.filterset.filtered;tar/gz/asf-logo.gif.tar.gz + [junit] expected:<FTTTFTTTF...> but was:<TTTTTTTTT...> + [junit] junit.framework.ComparisonFailure: Error for files: .;copy.filterset.filtered;tar/gz/asf-logo.gif.tar.gz + [junit] expected:<FTTTFTTTF...> but was:<TTTTTTTTT...> + [junit] at junit.framework.Assert.assertEquals(Assert.java:81) + [junit] at org.apache.tools.ant.types.selectors.BaseSelectorTest.performTest(BaseSelectorTest.java:194) ++ +
Described above the test class should provide a getInstance() + method. But that isn´t used here. The used getSelector() method is + implemented in the base class and gives an instance of an Ant Project to + the selector. This is usually done inside normal build file runs, but not + inside this special environment, so this method gives the selector the + ability to use its own Project object (getProject()), for example + for logging.
+ + +During development and maybe later you sometimes need the output of information.
+ Therefore Logging is needed. Because the selector extends BaseExtendSelector or directly
+ BaseSelector it is an Ant DataType and therefore a ProjectComponent.
+ That means that you have access to the project object and its logging capability.
+ ProjectComponent itself provides log methods which will do the
+ access to the project instance. Logging is therefore done simply with:
+
+ log( "message" ); ++ or +
+ log( "message" , loglevel ); ++ where the loglevel is one of the values
Copyright © 2002 Apache Software +
Copyright © 2002-2003 Apache Software Foundation. All rights Reserved.
-