At 06:31 PM 4/11/2003, Jay wrote:

I was thinking I should also do a test case. I have gotten lost trying to find
what's needed.

I know you aren't alone. I need to document this much better.

  I'm sure these questions have been answered before but so far
I have not had much luck finding a howto for doing a test case. I have looked
at the others similar in /testcases/org/apache/tools/ant/types/selectors for
example the ContainsSelector.  Where is the input file for that test, how do
I drive a specific test case?

Of course, you can write a test case any way you like. What I found was that I wanted to have some standard way of working with a set of files, and testing whether one or another of them was selected. To that end, I used a base class called BaseSelectorTest that does most of the heavy lifting.


What BaseSelectorTest does is copy a tree of files out of src/etc/testcases/types into selectortest and selectortest2 subdirectories (using the src/etc/testcases/types/selectors.xml build file). Then it takes a list of 12 of the files and directories in this tree, and applies whatever selector you pass in to each one. It passes back to your test a string indicating which of the 12 files and directories was selected, using 'T' for selected and 'F' for not selected. In the Test class for your selector, you override the getInstance() method to create your own type of selector, and set the elements of your selector a variety of ways to ensure that the string of T's and F's returned when the selector is applied to those 12 files is correct.

So, for example, DepthSelectorTest.java extends BaseSelectorTest and has the following code:

    public BaseSelector getInstance() {
        return new DepthSelector();
    }

    public void testSelectionBehaviour() {
        DepthSelector s;
        String results;

        try {
            makeBed();

            s = (DepthSelector)getInstance();
            s.setMin(20);
            s.setMax(25);
            results = selectionString(s);
            assertEquals("FFFFFFFFFFFF", results);

            s = (DepthSelector)getInstance();
            s.setMin(0);
            results = selectionString(s);
            assertEquals("TTTTTTTTTTTT", results);

            s = (DepthSelector)getInstance();
            s.setMin(1);
            results = selectionString(s);
            assertEquals("FFFFFTTTTTTT", results);

so the first test says that none of the 12 files or directories will match if the depth range is between 20 and 25 (that would be one deep directory tree!), the second says that they all match if the minimum depth is set to 0 and the maximum isn't specified, and the third that if the minumum depth is 1, the first 5 entries in the list will not be selected and the rest will.

You can find the 12 files and directories that are tested for selection in the BaseSelectorTest class. I used a fixed list so that if someone added new files to the src/etc/testcases/types directory it wouldn't break my tests:

    protected String[] filenames = {".","asf-logo.gif.md5","asf-logo.gif.bz2",
            "asf-logo.gif.gz","copy.filterset.filtered","zip/asf-logo.gif.zip",
            "tar/asf-logo.gif.tar","tar/asf-logo-huge.tar.gz",
            "tar/gz/asf-logo.gif.tar.gz","tar/bz2/asf-logo.gif.tar.bz2",
            "tar/bz2/asf-logo-huge.tar.bz2","tar/bz2"};

Clearer? Again, you don't have to do things this way. You can write your own tests any way that you want. If this framework saves you some work, go ahead and use it. If it is too confusing, write your own. Whatever works for you.




Reply via email to