FileDocCategorySizeDatePackage
BaseSelectorTest.javaAPI DocApache Ant 1.709507Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.types.selectors

BaseSelectorTest

public abstract class BaseSelectorTest extends TestCase
Base test case for Selectors. Provides a shared test as well as a test bed for selecting on, and a helper method for determining whether selections are correct.

Fields Summary
private org.apache.tools.ant.Project
project
private TaskdefForMakingBed
tbed
protected String
basedirname
protected String
beddirname
protected String
mirrordirname
protected File
basedir
protected File
beddir
protected File
mirrordir
protected String[]
filenames
protected File[]
files
protected File[]
mirrorfiles
Constructors Summary
public BaseSelectorTest(String name)


       
        super(name);
    
Methods Summary
protected voidcleanupBed()
Cleans up the testbed by calling a target in the src/etc/testcases/types/selectors.xml file.

        if (tbed != null) {
            tbed.tearDown();
            tbed = null;
        }
    
protected voidcleanupMirror()
Cleans up the mirror testbed by calling a target in the src/etc/testcases/types/selectors.xml file.

        if (tbed != null) {
            tbed.deleteMirror();
            tbed = null;
        }
    
public java.lang.Stringdiff(java.lang.String expected, java.lang.String result)
Checks which files are selected and shouldn't be or which are not selected but should.

param
expected String containing 'F's and 'T's
param
result String containing 'F's and 'T's
return
Difference as String containing '-' (equal) and 'X' (difference).

        int length1 = expected.length();
        int length2 = result.length();
        int min = (length1 > length2) ? length2 : length1;
        StringBuffer sb = new StringBuffer();
        for (int i=0; i<min; i++) {
            sb.append(
                  (expected.charAt(i) == result.charAt(i))
                ? "-"
                : "X"
            );
        }
        return sb.toString();
    
public abstract BaseSelectorgetInstance()
Override this in child classes to return a specific Selector

public org.apache.tools.ant.ProjectgetProject()

        return project;
    
public BaseSelectorgetSelector()
Return a preconfigured selector (with a set reference to project instance).

return
the selector

        BaseSelector selector = getInstance();
        selector.setProject( getProject() );
        return selector;
    
protected voidmakeBed()

Creates a testbed. We avoid the dreaded "test" word so that we don't falsely identify this as a test to be run. The actual setting up of the testbed is done in the src/etc/testcases/types/selectors.xml build file.

Note that the right way to call this is within a try block, with a finally clause that calls cleanupBed(). You place tests of the isSelected() method within the try block.

        tbed = new TaskdefForMakingBed("setupfiles");
        tbed.setUp();
        tbed.makeTestbed();
    
protected voidmakeMirror()

Creates a mirror of the testbed for use in dependency checks.

Note that the right way to call this is within a try block, with a finally clause that calls cleanupMirror(). You place tests of the isSelected() method within the try block.

        tbed = new TaskdefForMakingBed("mirrorfiles");
        tbed.setUp();
        tbed.makeMirror();
    
public java.lang.StringmirrorSelectionString(FileSelector selector)
This is a helper method that takes a selector and calls its isSelected() method on each file in the mirror testbed. This variation is used for dependency checks and to get around the limitations in the touch task when running JDK 1.1. It returns a string of "T"s amd "F"s.

        return selectionString(mirrordir,mirrorfiles,selector);
    
public voidperformTests(FileSelector selector, java.lang.String expected)
Does the selection test for a given selector and prints the filenames of the differing files (selected but shouldn't, not selected but should).

param
selector The selector to test
param
expected The expected result

        String result = selectionString(selector);
        String diff = diff(expected, result);
        String resolved = resolve(diff);
        assertEquals("Differing files: " + resolved, result, expected);
    
public java.lang.Stringresolve(java.lang.String filelist)
Resolves a diff-String (@see diff()) against the (inherited) filenames- and files arrays.

param
filelist Diff-String
return
String containing the filenames for all differing files, separated with semicolons ';'

        StringBuffer sb = new StringBuffer();
        int min = (filenames.length > filelist.length())
                ? filelist.length()
                : filenames.length;
        for (int i=0; i<min; i++) {
            if ('X"==filelist.charAt(i)) {
                sb.append(filenames[i]);
                sb.append(";");
            }
        }
        return sb.toString();
    
public java.lang.StringselectionString(FileSelector selector)
This is a helper method that takes a selector and calls its isSelected() method on each file in the testbed. It returns a string of "T"s amd "F"s

        return selectionString(beddir,files,selector);
    
public java.lang.StringselectionString(java.io.File basedir, java.io.File[] files, FileSelector selector)
Worker method for the two convenience methods above. Applies a selector on a set of files passed in and returns a string of "T"s amd "F"s from applying the selector to each file.

        StringBuffer buf = new StringBuffer();
        for (int x = 0; x < files.length; x++) {
            if (selector.isSelected(basedir,filenames[x],files[x])) {
                buf.append('T");
            }
            else {
                buf.append('F");
            }
        }
        return buf.toString();
    
public voidsetUp()

        project = new Project();
        project.init();
        project.setBaseDir(basedir);
        for (int x = 0; x < files.length; x++) {
            files[x] = new File(beddir,filenames[x]);
            mirrorfiles[x] = new File(mirrordir,filenames[x]);
        }
    
public voidtestRespondsToError()
This is a test that all Selectors derived from BaseSelector can use. It calls the setError() method and checks to ensure that a BuildException is thrown as a result.

        BaseSelector s = getInstance();
        if (s == null) {
            return;
        }
        s.setError("test error");
        try {
            s.isSelected(beddir,filenames[0],files[0]);
            fail("Cannot cause BuildException when setError() is called");
        } catch (BuildException be) {
            assertEquals("test error",
                         be.getMessage());
        }