1 | /*
|
---|
2 | * Copyright 2000-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.types.selectors;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.Project;
|
---|
22 | import org.apache.tools.ant.BuildFileTest;
|
---|
23 |
|
---|
24 | import junit.framework.TestCase;
|
---|
25 | import junit.framework.AssertionFailedError;
|
---|
26 |
|
---|
27 | import java.io.File;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Base test case for Selectors. Provides a shared test as well as
|
---|
31 | * a test bed for selecting on, and a helper method for determining
|
---|
32 | * whether selections are correct.
|
---|
33 | *
|
---|
34 | */
|
---|
35 | public abstract class BaseSelectorTest extends TestCase {
|
---|
36 |
|
---|
37 | private Project project;
|
---|
38 | private TaskdefForMakingBed tbed = null;
|
---|
39 | protected String basedirname = "src/etc/testcases/types";
|
---|
40 | protected String beddirname = basedirname + "/selectortest";
|
---|
41 | protected String mirrordirname = basedirname + "/selectortest2";
|
---|
42 | protected File basedir = new File(basedirname);
|
---|
43 | protected File beddir = new File(beddirname);
|
---|
44 | protected File mirrordir = new File(mirrordirname);
|
---|
45 | protected String[] filenames = {".","asf-logo.gif.md5","asf-logo.gif.bz2",
|
---|
46 | "asf-logo.gif.gz","copy.filterset.filtered","zip/asf-logo.gif.zip",
|
---|
47 | "tar/asf-logo.gif.tar","tar/asf-logo-huge.tar.gz",
|
---|
48 | "tar/gz/asf-logo.gif.tar.gz","tar/bz2/asf-logo.gif.tar.bz2",
|
---|
49 | "tar/bz2/asf-logo-huge.tar.bz2","tar/bz2"};
|
---|
50 | protected File[] files = new File[filenames.length];
|
---|
51 | protected File[] mirrorfiles = new File[filenames.length];
|
---|
52 |
|
---|
53 | public BaseSelectorTest(String name) {
|
---|
54 | super(name);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public void setUp() {
|
---|
58 | project = new Project();
|
---|
59 | project.init();
|
---|
60 | project.setBaseDir(basedir);
|
---|
61 | for (int x = 0; x < files.length; x++) {
|
---|
62 | files[x] = new File(beddir,filenames[x]);
|
---|
63 | mirrorfiles[x] = new File(mirrordir,filenames[x]);
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Override this in child classes to return a specific Selector
|
---|
69 | */
|
---|
70 | public abstract BaseSelector getInstance();
|
---|
71 |
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Return a preconfigured selector (with a set reference to
|
---|
75 | * project instance).
|
---|
76 | * @return the selector
|
---|
77 | */
|
---|
78 | public BaseSelector getSelector() {
|
---|
79 | BaseSelector selector = getInstance();
|
---|
80 | selector.setProject( getProject() );
|
---|
81 | return selector;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | public Project getProject() {
|
---|
86 | return project;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * This is a test that all Selectors derived from BaseSelector can
|
---|
91 | * use. It calls the setError() method and checks to ensure that a
|
---|
92 | * BuildException is thrown as a result.
|
---|
93 | */
|
---|
94 | public void testRespondsToError() {
|
---|
95 | BaseSelector s = getInstance();
|
---|
96 | if (s == null) {
|
---|
97 | return;
|
---|
98 | }
|
---|
99 | s.setError("test error");
|
---|
100 | try {
|
---|
101 | s.isSelected(beddir,filenames[0],files[0]);
|
---|
102 | fail("Cannot cause BuildException when setError() is called");
|
---|
103 | } catch (BuildException be) {
|
---|
104 | assertEquals("test error",
|
---|
105 | be.getMessage());
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * This is a helper method that takes a selector and calls its
|
---|
112 | * isSelected() method on each file in the testbed. It returns
|
---|
113 | * a string of "T"s amd "F"s
|
---|
114 | */
|
---|
115 | public String selectionString(FileSelector selector) {
|
---|
116 | return selectionString(beddir,files,selector);
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * This is a helper method that takes a selector and calls its
|
---|
121 | * isSelected() method on each file in the mirror testbed. This
|
---|
122 | * variation is used for dependency checks and to get around the
|
---|
123 | * limitations in the touch task when running JDK 1.1. It returns
|
---|
124 | * a string of "T"s amd "F"s.
|
---|
125 | */
|
---|
126 | public String mirrorSelectionString(FileSelector selector) {
|
---|
127 | return selectionString(mirrordir,mirrorfiles,selector);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Worker method for the two convenience methods above. Applies a
|
---|
132 | * selector on a set of files passed in and returns a string of
|
---|
133 | * "T"s amd "F"s from applying the selector to each file.
|
---|
134 | */
|
---|
135 | public String selectionString(File basedir, File[] files, FileSelector selector) {
|
---|
136 | StringBuffer buf = new StringBuffer();
|
---|
137 | for (int x = 0; x < files.length; x++) {
|
---|
138 | if (selector.isSelected(basedir,filenames[x],files[x])) {
|
---|
139 | buf.append('T');
|
---|
140 | }
|
---|
141 | else {
|
---|
142 | buf.append('F');
|
---|
143 | }
|
---|
144 | }
|
---|
145 | return buf.toString();
|
---|
146 | }
|
---|
147 |
|
---|
148 | /**
|
---|
149 | * Does the selection test for a given selector and prints the
|
---|
150 | * filenames of the differing files (selected but shouldn't,
|
---|
151 | * not selected but should).
|
---|
152 | * @param selector The selector to test
|
---|
153 | * @param expected The expected result
|
---|
154 | */
|
---|
155 | public void performTests(FileSelector selector, String expected) {
|
---|
156 | String result = selectionString(selector);
|
---|
157 | String diff = diff(expected, result);
|
---|
158 | String resolved = resolve(diff);
|
---|
159 | assertEquals("Differing files: " + resolved, result, expected);
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Checks which files are selected and shouldn't be or which
|
---|
164 | * are not selected but should.
|
---|
165 | * @param expected String containing 'F's and 'T's
|
---|
166 | * @param result String containing 'F's and 'T's
|
---|
167 | * @return Difference as String containing '-' (equal) and
|
---|
168 | * 'X' (difference).
|
---|
169 | */
|
---|
170 | public String diff(String expected, String result) {
|
---|
171 | int length1 = expected.length();
|
---|
172 | int length2 = result.length();
|
---|
173 | int min = (length1 > length2) ? length2 : length1;
|
---|
174 | StringBuffer sb = new StringBuffer();
|
---|
175 | for (int i=0; i<min; i++) {
|
---|
176 | sb.append(
|
---|
177 | (expected.charAt(i) == result.charAt(i))
|
---|
178 | ? "-"
|
---|
179 | : "X"
|
---|
180 | );
|
---|
181 | }
|
---|
182 | return sb.toString();
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Resolves a diff-String (@see diff()) against the (inherited) filenames-
|
---|
188 | * and files arrays.
|
---|
189 | * @param filelist Diff-String
|
---|
190 | * @return String containing the filenames for all differing files,
|
---|
191 | * separated with semicolons ';'
|
---|
192 | */
|
---|
193 | public String resolve(String filelist) {
|
---|
194 | StringBuffer sb = new StringBuffer();
|
---|
195 | int min = (filenames.length > filelist.length())
|
---|
196 | ? filelist.length()
|
---|
197 | : filenames.length;
|
---|
198 | for (int i=0; i<min; i++) {
|
---|
199 | if ('X'==filelist.charAt(i)) {
|
---|
200 | sb.append(filenames[i]);
|
---|
201 | sb.append(";");
|
---|
202 | }
|
---|
203 | }
|
---|
204 | return sb.toString();
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * <p>Creates a testbed. We avoid the dreaded "test" word so that we
|
---|
210 | * don't falsely identify this as a test to be run. The actual
|
---|
211 | * setting up of the testbed is done in the
|
---|
212 | * <code>src/etc/testcases/types/selectors.xml</code> build file.</p>
|
---|
213 | *
|
---|
214 | * <p>Note that the right way to call this is within a try block,
|
---|
215 | * with a finally clause that calls cleanupBed(). You place tests of
|
---|
216 | * the isSelected() method within the try block.</p>
|
---|
217 | */
|
---|
218 | protected void makeBed() {
|
---|
219 | tbed = new TaskdefForMakingBed("setupfiles");
|
---|
220 | tbed.setUp();
|
---|
221 | tbed.makeTestbed();
|
---|
222 | }
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * Cleans up the testbed by calling a target in the
|
---|
226 | * <code>src/etc/testcases/types/selectors.xml</code> file.
|
---|
227 | */
|
---|
228 | protected void cleanupBed() {
|
---|
229 | if (tbed != null) {
|
---|
230 | tbed.tearDown();
|
---|
231 | tbed = null;
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * <p>Creates a mirror of the testbed for use in dependency checks.</p>
|
---|
238 | *
|
---|
239 | * <p>Note that the right way to call this is within a try block,
|
---|
240 | * with a finally clause that calls cleanupMirror(). You place tests of
|
---|
241 | * the isSelected() method within the try block.</p>
|
---|
242 | */
|
---|
243 | protected void makeMirror() {
|
---|
244 | tbed = new TaskdefForMakingBed("mirrorfiles");
|
---|
245 | tbed.setUp();
|
---|
246 | tbed.makeMirror();
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Cleans up the mirror testbed by calling a target in the
|
---|
251 | * <code>src/etc/testcases/types/selectors.xml</code> file.
|
---|
252 | */
|
---|
253 | protected void cleanupMirror() {
|
---|
254 | if (tbed != null) {
|
---|
255 | tbed.deleteMirror();
|
---|
256 | tbed = null;
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | private class TaskdefForMakingBed extends BuildFileTest {
|
---|
261 |
|
---|
262 | TaskdefForMakingBed(String name) {
|
---|
263 | super(name);
|
---|
264 | }
|
---|
265 |
|
---|
266 | public void setUp() {
|
---|
267 | configureProject("src/etc/testcases/types/selectors.xml");
|
---|
268 | }
|
---|
269 |
|
---|
270 | public void tearDown() {
|
---|
271 | executeTarget("cleanup");
|
---|
272 | }
|
---|
273 |
|
---|
274 | public void makeTestbed() {
|
---|
275 | executeTarget("setupfiles");
|
---|
276 | }
|
---|
277 |
|
---|
278 | public void makeMirror() {
|
---|
279 | executeTarget("mirrorfiles");
|
---|
280 | }
|
---|
281 |
|
---|
282 | public void deleteMirror() {
|
---|
283 | executeTarget("cleanup.mirrorfiles");
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 |
|
---|
289 | }
|
---|