1 | A DESCRIPTION OF THE SELECTOR TEST FRAMEWORK
|
---|
2 |
|
---|
3 | When writing tests for selectors, I found that I wanted to have some
|
---|
4 | standard way of working with a set of files and testing whether one or
|
---|
5 | another of them was selected. To that end, I created a base class called
|
---|
6 | BaseSelectorTest that does most of the heavy lifting. Of course, you can
|
---|
7 | test your selectors any way you want, but if you want to reuse this code,
|
---|
8 | read on.
|
---|
9 |
|
---|
10 | What BaseSelectorTest does is use an ant build file
|
---|
11 | "src/etc/testcases/types/selector.xml" to copy a tree of files out of
|
---|
12 | "src/etc/testcases/taskdefs/expected" into a "selectortest" directories.
|
---|
13 | Then it takes a list of 12 of the files and directories in this tree, and
|
---|
14 | applies whatever selector you pass in to each one. It passes back to your
|
---|
15 | test a 12 character long string indicating which of the 12 files and
|
---|
16 | directories was selected, using 'T' for selected and 'F' for not selected.
|
---|
17 | In the Test class for your selector, you override the getInstance() method
|
---|
18 | to create your own type of selector, and set the elements of your selector
|
---|
19 | a variety of ways to ensure that the string of T's and F's returned when
|
---|
20 | the selector is applied to those 12 files is correct.
|
---|
21 |
|
---|
22 | So, for example, DepthSelectorTest.java extends BaseSelectorTest and has
|
---|
23 | the following code:
|
---|
24 |
|
---|
25 |
|
---|
26 | public BaseSelector getInstance() {
|
---|
27 | return new DepthSelector();
|
---|
28 | }
|
---|
29 |
|
---|
30 |
|
---|
31 | public void testSelectionBehaviour() {
|
---|
32 | DepthSelector s;
|
---|
33 | String results;
|
---|
34 |
|
---|
35 |
|
---|
36 | try {
|
---|
37 | makeBed();
|
---|
38 |
|
---|
39 |
|
---|
40 | s = (DepthSelector)getInstance();
|
---|
41 | s.setMin(20);
|
---|
42 | s.setMax(25);
|
---|
43 | results = selectionString(s);
|
---|
44 | assertEquals("FFFFFFFFFFFF", results);
|
---|
45 |
|
---|
46 |
|
---|
47 | s = (DepthSelector)getInstance();
|
---|
48 | s.setMin(0);
|
---|
49 | results = selectionString(s);
|
---|
50 | assertEquals("TTTTTTTTTTTT", results);
|
---|
51 |
|
---|
52 |
|
---|
53 | s = (DepthSelector)getInstance();
|
---|
54 | s.setMin(1);
|
---|
55 | results = selectionString(s);
|
---|
56 | assertEquals("FFFFFTTTTTTT", results);
|
---|
57 |
|
---|
58 |
|
---|
59 | The first test says that none of the 12 files or directories will match if
|
---|
60 | the depth range for the selector is between 20 and 25 (that would be one
|
---|
61 | deep directory tree!). The second says that all files and directories
|
---|
62 | match if the minimum depth is set to 0 and the maximum isn't specified. The
|
---|
63 | third test says that if the minumum depth is 1, the first 5 entries in the
|
---|
64 | list of 12 will not be selected and the rest will.
|
---|
65 |
|
---|
66 |
|
---|
67 | You can find the 12 files and directories that are tested for selection in
|
---|
68 | the BaseSelectorTest class. I used a fixed list so that if someone added
|
---|
69 | new files to the src/etc/testcases/types directory it wouldn't break my
|
---|
70 | tests:
|
---|
71 |
|
---|
72 |
|
---|
73 | protected String[] filenames = {".","asf-logo.gif.md5","asf-
|
---|
74 | logo.gif.bz2",
|
---|
75 | "asf-logo.gif.gz","copy.filterset.filtered","zip/asf-
|
---|
76 | logo.gif.zip",
|
---|
77 | "tar/asf-logo.gif.tar","tar/asf-logo-huge.tar.gz",
|
---|
78 | "tar/gz/asf-logo.gif.tar.gz","tar/bz2/asf-logo.gif.tar.bz2",
|
---|
79 | "tar/bz2/asf-logo-huge.tar.bz2","tar/bz2"};
|
---|
80 |
|
---|
81 |
|
---|
82 | If you wish to use this set of files and directories to test your selector,
|
---|
83 | you can reuse the BaseSelectorTest with no change to it.
|
---|
84 |
|
---|
85 | You may find you need to alter the build file so that you get some
|
---|
86 | variation in the files that your selector can work with. Most of the core
|
---|
87 | selectors have required that kind of modification. If you do that, make
|
---|
88 | sure that it doesn't alter the output strings on the other selector test,
|
---|
89 | or if it does that you update their expected return results.
|
---|
90 |
|
---|
91 | You may also want to alter the set of files you look at in a particular
|
---|
92 | selector test. Since the filelist in BaseSelectorTest is protected, you
|
---|
93 | should be able to override it as you need to. Or you can alter the fileset
|
---|
94 | in BaseSelectorTest itself, provided you update the test strings in all the
|
---|
95 | other unit tests.
|
---|
96 |
|
---|