001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.types.selectors;
020:
021: import java.io.File;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.tools.ant.BuildException;
026: import org.apache.tools.ant.BuildFileTest;
027: import org.apache.tools.ant.Project;
028:
029: /**
030: * Base test case for Selectors. Provides a shared test as well as
031: * a test bed for selecting on, and a helper method for determining
032: * whether selections are correct.
033: *
034: */
035: public abstract class BaseSelectorTest extends TestCase {
036:
037: private Project project;
038: private TaskdefForMakingBed tbed = null;
039: protected String basedirname = "src/etc/testcases/types";
040: protected String beddirname = basedirname + "/selectortest";
041: protected String mirrordirname = basedirname + "/selectortest2";
042: protected File basedir = new File(System.getProperty("root"),
043: basedirname);
044: protected File beddir = new File(System.getProperty("root"),
045: beddirname);
046: protected File mirrordir = new File(System.getProperty("root"),
047: mirrordirname);
048: protected String[] filenames = { ".", "asf-logo.gif.md5",
049: "asf-logo.gif.bz2", "asf-logo.gif.gz",
050: "copy.filterset.filtered", "zip/asf-logo.gif.zip",
051: "tar/asf-logo.gif.tar", "tar/asf-logo-huge.tar.gz",
052: "tar/gz/asf-logo.gif.tar.gz",
053: "tar/bz2/asf-logo.gif.tar.bz2",
054: "tar/bz2/asf-logo-huge.tar.bz2", "tar/bz2" };
055: protected File[] files = new File[filenames.length];
056: protected File[] mirrorfiles = new File[filenames.length];
057:
058: public BaseSelectorTest(String name) {
059: super (name);
060: }
061:
062: public void setUp() {
063: project = new Project();
064: project.init();
065: project.setBaseDir(basedir);
066: for (int x = 0; x < files.length; x++) {
067: files[x] = new File(beddir, filenames[x]);
068: mirrorfiles[x] = new File(mirrordir, filenames[x]);
069: }
070: }
071:
072: /**
073: * Override this in child classes to return a specific Selector
074: */
075: public abstract BaseSelector getInstance();
076:
077: /**
078: * Return a preconfigured selector (with a set reference to
079: * project instance).
080: * @return the selector
081: */
082: public BaseSelector getSelector() {
083: BaseSelector selector = getInstance();
084: selector.setProject(getProject());
085: return selector;
086: }
087:
088: public Project getProject() {
089: return project;
090: }
091:
092: /**
093: * This is a test that all Selectors derived from BaseSelector can
094: * use. It calls the setError() method and checks to ensure that a
095: * BuildException is thrown as a result.
096: */
097: public void testRespondsToError() {
098: BaseSelector s = getInstance();
099: if (s == null) {
100: return;
101: }
102: s.setError("test error");
103: try {
104: s.isSelected(beddir, filenames[0], files[0]);
105: fail("Cannot cause BuildException when setError() is called");
106: } catch (BuildException be) {
107: assertEquals("test error", be.getMessage());
108: }
109: }
110:
111: /**
112: * This is a helper method that takes a selector and calls its
113: * isSelected() method on each file in the testbed. It returns
114: * a string of "T"s amd "F"s
115: */
116: public String selectionString(FileSelector selector) {
117: return selectionString(beddir, files, selector);
118: }
119:
120: /**
121: * This is a helper method that takes a selector and calls its
122: * isSelected() method on each file in the mirror testbed. This
123: * variation is used for dependency checks and to get around the
124: * limitations in the touch task when running JDK 1.1. It returns
125: * a string of "T"s amd "F"s.
126: */
127: public String mirrorSelectionString(FileSelector selector) {
128: return selectionString(mirrordir, mirrorfiles, selector);
129: }
130:
131: /**
132: * Worker method for the two convenience methods above. Applies a
133: * selector on a set of files passed in and returns a string of
134: * "T"s amd "F"s from applying the selector to each file.
135: */
136: public String selectionString(File basedir, File[] files,
137: FileSelector selector) {
138: StringBuffer buf = new StringBuffer();
139: for (int x = 0; x < files.length; x++) {
140: if (selector.isSelected(basedir, filenames[x], files[x])) {
141: buf.append('T');
142: } else {
143: buf.append('F');
144: }
145: }
146: return buf.toString();
147: }
148:
149: /**
150: * Does the selection test for a given selector and prints the
151: * filenames of the differing files (selected but shouldn't,
152: * not selected but should).
153: * @param selector The selector to test
154: * @param expected The expected result
155: */
156: public void performTests(FileSelector selector, String expected) {
157: String result = selectionString(selector);
158: String diff = diff(expected, result);
159: String resolved = resolve(diff);
160: assertEquals("Differing files: " + resolved, result, expected);
161: }
162:
163: /**
164: * Checks which files are selected and shouldn't be or which
165: * are not selected but should.
166: * @param expected String containing 'F's and 'T's
167: * @param result String containing 'F's and 'T's
168: * @return Difference as String containing '-' (equal) and
169: * 'X' (difference).
170: */
171: public String diff(String expected, String result) {
172: int length1 = expected.length();
173: int length2 = result.length();
174: int min = (length1 > length2) ? length2 : length1;
175: StringBuffer sb = new StringBuffer();
176: for (int i = 0; i < min; i++) {
177: sb.append((expected.charAt(i) == result.charAt(i)) ? "-"
178: : "X");
179: }
180: return sb.toString();
181: }
182:
183: /**
184: * Resolves a diff-String (@see diff()) against the (inherited) filenames-
185: * and files arrays.
186: * @param filelist Diff-String
187: * @return String containing the filenames for all differing files,
188: * separated with semicolons ';'
189: */
190: public String resolve(String filelist) {
191: StringBuffer sb = new StringBuffer();
192: int min = (filenames.length > filelist.length()) ? filelist
193: .length() : filenames.length;
194: for (int i = 0; i < min; i++) {
195: if ('X' == filelist.charAt(i)) {
196: sb.append(filenames[i]);
197: sb.append(";");
198: }
199: }
200: return sb.toString();
201: }
202:
203: /**
204: * <p>Creates a testbed. We avoid the dreaded "test" word so that we
205: * don't falsely identify this as a test to be run. The actual
206: * setting up of the testbed is done in the
207: * <code>src/etc/testcases/types/selectors.xml</code> build file.</p>
208: *
209: * <p>Note that the right way to call this is within a try block,
210: * with a finally clause that calls cleanupBed(). You place tests of
211: * the isSelected() method within the try block.</p>
212: */
213: protected void makeBed() {
214: tbed = new TaskdefForMakingBed("setupfiles");
215: tbed.setUp();
216: tbed.makeTestbed();
217: }
218:
219: /**
220: * Cleans up the testbed by calling a target in the
221: * <code>src/etc/testcases/types/selectors.xml</code> file.
222: */
223: protected void cleanupBed() {
224: if (tbed != null) {
225: tbed.tearDown();
226: tbed = null;
227: }
228: }
229:
230: /**
231: * <p>Creates a mirror of the testbed for use in dependency checks.</p>
232: *
233: * <p>Note that the right way to call this is within a try block,
234: * with a finally clause that calls cleanupMirror(). You place tests of
235: * the isSelected() method within the try block.</p>
236: */
237: protected void makeMirror() {
238: tbed = new TaskdefForMakingBed("mirrorfiles");
239: tbed.setUp();
240: tbed.makeMirror();
241: }
242:
243: /**
244: * Cleans up the mirror testbed by calling a target in the
245: * <code>src/etc/testcases/types/selectors.xml</code> file.
246: */
247: protected void cleanupMirror() {
248: if (tbed != null) {
249: tbed.deleteMirror();
250: tbed = null;
251: }
252: }
253:
254: private class TaskdefForMakingBed extends BuildFileTest {
255:
256: TaskdefForMakingBed(String name) {
257: super (name);
258: }
259:
260: public void setUp() {
261: configureProject("src/etc/testcases/types/selectors.xml");
262: }
263:
264: public void tearDown() {
265: executeTarget("cleanup");
266: }
267:
268: public void makeTestbed() {
269: executeTarget("setupfiles");
270: }
271:
272: public void makeMirror() {
273: executeTarget("mirrorfiles");
274: }
275:
276: public void deleteMirror() {
277: executeTarget("cleanup.mirrorfiles");
278: }
279: }
280:
281: }
|