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;
020:
021: import org.apache.tools.ant.input.DefaultInputHandler;
022: import org.apache.tools.ant.input.InputHandler;
023: import org.apache.tools.ant.input.PropertyFileInputHandler;
024: import org.apache.tools.ant.taskdefs.condition.Os;
025: import org.apache.tools.ant.types.*;
026:
027: import java.io.File;
028: import junit.framework.TestCase;
029:
030: /**
031: * Very limited test class for Project. Waiting to be extended.
032: *
033: */
034: public class ProjectTest extends TestCase {
035:
036: private Project p;
037: private String root;
038: private MockBuildListener mbl;
039:
040: public ProjectTest(String name) {
041: super (name);
042: }
043:
044: public void setUp() {
045: p = new Project();
046: p.init();
047: root = new File(File.separator).getAbsolutePath().toUpperCase();
048: mbl = new MockBuildListener(p);
049: }
050:
051: public void testDataTypes() throws BuildException {
052: assertNull("dummy is not a known data type", p
053: .createDataType("dummy"));
054: Object o = p.createDataType("fileset");
055: assertNotNull("fileset is a known type", o);
056: assertTrue("fileset creates FileSet", o instanceof FileSet);
057: assertTrue("PatternSet",
058: p.createDataType("patternset") instanceof PatternSet);
059: assertTrue("Path", p.createDataType("path") instanceof Path);
060: }
061:
062: /**
063: * This test has been a starting point for moving the code to FileUtils.
064: */
065: public void testResolveFile() {
066: if (Os.isFamily("netware") || Os.isFamily("dos")) {
067: assertEqualsIgnoreDriveCase(localize(File.separator), p
068: .resolveFile("/", null).getPath());
069: assertEqualsIgnoreDriveCase(localize(File.separator), p
070: .resolveFile("\\", null).getPath());
071: /*
072: * throw in drive letters
073: */
074: String driveSpec = "C:";
075: String driveSpecLower = "c:";
076:
077: assertEqualsIgnoreDriveCase(driveSpecLower + "\\", p
078: .resolveFile(driveSpec + "/", null).getPath());
079: assertEqualsIgnoreDriveCase(driveSpecLower + "\\", p
080: .resolveFile(driveSpec + "\\", null).getPath());
081: assertEqualsIgnoreDriveCase(driveSpecLower + "\\", p
082: .resolveFile(driveSpecLower + "/", null).getPath());
083: assertEqualsIgnoreDriveCase(driveSpecLower + "\\", p
084: .resolveFile(driveSpecLower + "\\", null).getPath());
085: /*
086: * promised to eliminate consecutive slashes after drive letter.
087: */
088: assertEqualsIgnoreDriveCase(driveSpec + "\\", p
089: .resolveFile(driveSpec + "/////", null).getPath());
090: assertEqualsIgnoreDriveCase(driveSpec + "\\", p
091: .resolveFile(driveSpec + "\\\\\\\\\\\\", null)
092: .getPath());
093: } else {
094: /*
095: * Start with simple absolute file names.
096: */
097: assertEquals(File.separator, p.resolveFile("/", null)
098: .getPath());
099: assertEquals(File.separator, p.resolveFile("\\", null)
100: .getPath());
101: /*
102: * drive letters are not used, just to be considered as normal
103: * part of a name
104: */
105: String driveSpec = "C:";
106: String udir = System.getProperty("user.dir")
107: + File.separatorChar;
108: assertEquals(udir + driveSpec, p.resolveFile(
109: driveSpec + "/", null).getPath());
110: assertEquals(udir + driveSpec, p.resolveFile(
111: driveSpec + "\\", null).getPath());
112: String driveSpecLower = "c:";
113: assertEquals(udir + driveSpecLower, p.resolveFile(
114: driveSpecLower + "/", null).getPath());
115: assertEquals(udir + driveSpecLower, p.resolveFile(
116: driveSpecLower + "\\", null).getPath());
117: }
118: /*
119: * Now test some relative file name magic.
120: */
121: assertEquals(localize("/1/2/3/4"), p.resolveFile("4",
122: new File(localize("/1/2/3"))).getPath());
123: assertEquals(localize("/1/2/3/4"), p.resolveFile("./4",
124: new File(localize("/1/2/3"))).getPath());
125: assertEquals(localize("/1/2/3/4"), p.resolveFile(".\\4",
126: new File(localize("/1/2/3"))).getPath());
127: assertEquals(localize("/1/2/3/4"), p.resolveFile("./.\\4",
128: new File(localize("/1/2/3"))).getPath());
129: assertEquals(localize("/1/2/3/4"), p.resolveFile("../3/4",
130: new File(localize("/1/2/3"))).getPath());
131: assertEquals(localize("/1/2/3/4"), p.resolveFile("..\\3\\4",
132: new File(localize("/1/2/3"))).getPath());
133: assertEquals(localize("/1/2/3/4"), p.resolveFile(
134: "../../5/.././2/./3/6/../4",
135: new File(localize("/1/2/3"))).getPath());
136: assertEquals(localize("/1/2/3/4"), p.resolveFile(
137: "..\\../5/..\\./2/./3/6\\../4",
138: new File(localize("/1/2/3"))).getPath());
139:
140: }
141:
142: /**
143: * adapt file separators to local conventions
144: */
145: private String localize(String path) {
146: path = root + path.substring(1);
147: return path.replace('\\', File.separatorChar).replace('/',
148: File.separatorChar);
149: }
150:
151: /**
152: * convenience method
153: * the drive letter is in lower case under cygwin
154: * calling this method allows tests where FileUtils.normalize
155: * is called via resolveFile to pass under cygwin
156: */
157: private void assertEqualsIgnoreDriveCase(String s1, String s2) {
158: if ((Os.isFamily("dos") || Os.isFamily("netware"))
159: && s1.length() >= 1 && s2.length() >= 1) {
160: StringBuffer sb1 = new StringBuffer(s1);
161: StringBuffer sb2 = new StringBuffer(s2);
162: sb1.setCharAt(0, Character.toUpperCase(s1.charAt(0)));
163: sb2.setCharAt(0, Character.toUpperCase(s2.charAt(0)));
164: assertEquals(sb1.toString(), sb2.toString());
165: } else {
166: assertEquals(s1, s2);
167: }
168: }
169:
170: private void assertTaskDefFails(final Class taskClass,
171: final String message) {
172: final String dummyName = "testTaskDefinitionDummy";
173: try {
174: mbl.addBuildEvent(message, Project.MSG_ERR);
175: p.addTaskDefinition(dummyName, taskClass);
176: fail("expected BuildException(\"" + message
177: + "\", Project.MSG_ERR) when adding task "
178: + taskClass);
179: } catch (BuildException e) {
180: assertEquals(message, e.getMessage());
181: mbl.assertEmpty();
182: assertTrue(!p.getTaskDefinitions().containsKey(dummyName));
183: }
184: }
185:
186: public void testAddTaskDefinition() {
187: p.addBuildListener(mbl);
188:
189: p.addTaskDefinition("Ok", DummyTaskOk.class);
190: assertEquals(DummyTaskOk.class, p.getTaskDefinitions()
191: .get("Ok"));
192: p.addTaskDefinition("OkNonTask", DummyTaskOkNonTask.class);
193: assertEquals(DummyTaskOkNonTask.class, p.getTaskDefinitions()
194: .get("OkNonTask"));
195: mbl.assertEmpty();
196:
197: assertTaskDefFails(DummyTaskPrivate.class,
198: DummyTaskPrivate.class + " is not public");
199:
200: assertTaskDefFails(DummyTaskProtected.class,
201: DummyTaskProtected.class + " is not public");
202:
203: assertTaskDefFails(DummyTaskPackage.class,
204: DummyTaskPackage.class + " is not public");
205:
206: assertTaskDefFails(DummyTaskAbstract.class,
207: DummyTaskAbstract.class + " is abstract");
208: assertTaskDefFails(DummyTaskInterface.class,
209: DummyTaskInterface.class + " is abstract");
210:
211: assertTaskDefFails(DummyTaskWithoutDefaultConstructor.class,
212: "No public no-arg constructor in "
213: + DummyTaskWithoutDefaultConstructor.class);
214: assertTaskDefFails(DummyTaskWithoutPublicConstructor.class,
215: "No public no-arg constructor in "
216: + DummyTaskWithoutPublicConstructor.class);
217:
218: assertTaskDefFails(DummyTaskWithoutExecute.class,
219: "No public execute() in "
220: + DummyTaskWithoutExecute.class);
221: assertTaskDefFails(DummyTaskWithNonPublicExecute.class,
222: "No public execute() in "
223: + DummyTaskWithNonPublicExecute.class);
224:
225: mbl.addBuildEvent(
226: "return type of execute() should be void but was \"int\" in "
227: + DummyTaskWithNonVoidExecute.class,
228: Project.MSG_WARN);
229: p.addTaskDefinition("NonVoidExecute",
230: DummyTaskWithNonVoidExecute.class);
231: mbl.assertEmpty();
232: assertEquals(DummyTaskWithNonVoidExecute.class, p
233: .getTaskDefinitions().get("NonVoidExecute"));
234: }
235:
236: public void testInputHandler() {
237: InputHandler ih = p.getInputHandler();
238: assertNotNull(ih);
239: assertTrue(ih instanceof DefaultInputHandler);
240: InputHandler pfih = new PropertyFileInputHandler();
241: p.setInputHandler(pfih);
242: assertSame(pfih, p.getInputHandler());
243: }
244:
245: public void testTaskDefinitionContainsKey() {
246: assertTrue(p.getTaskDefinitions().containsKey("echo"));
247: }
248:
249: public void testTaskDefinitionContains() {
250: assertTrue(p.getTaskDefinitions().contains(
251: org.apache.tools.ant.taskdefs.Echo.class));
252: }
253:
254: public void testDuplicateTargets() {
255: // fail, because buildfile contains two targets with the same name
256: try {
257: BFT bft = new BFT("", "core/duplicate-target.xml");
258: } catch (BuildException ex) {
259: assertEquals("specific message",
260: "Duplicate target 'twice'", ex.getMessage());
261: return;
262: }
263: fail("Should throw BuildException about duplicate target");
264: }
265:
266: public void testDuplicateTargetsImport() {
267: // overriding target from imported buildfile is allowed
268: BFT bft = new BFT("", "core/duplicate-target2.xml");
269: bft.expectLog("once", "once from buildfile");
270: }
271:
272: private class DummyTaskPrivate extends Task {
273: public DummyTaskPrivate() {
274: }
275:
276: public void execute() {
277: }
278: }
279:
280: protected class DummyTaskProtected extends Task {
281: public DummyTaskProtected() {
282: }
283:
284: public void execute() {
285: }
286: }
287:
288: private class BFT extends org.apache.tools.ant.BuildFileTest {
289: BFT(String name, String buildfile) {
290: super (name);
291: this .buildfile = buildfile;
292: setUp();
293: }
294:
295: // avoid multiple configurations
296: boolean isConfigured = false;
297:
298: // the buildfile to use
299: String buildfile = "";
300:
301: public void setUp() {
302: if (!isConfigured) {
303: configureProject("src/etc/testcases/" + buildfile);
304: isConfigured = true;
305: }
306: }
307:
308: public void tearDown() {
309: }
310:
311: // call a target
312: public void doTarget(String target) {
313: if (!isConfigured)
314: setUp();
315: executeTarget(target);
316: }
317:
318: public org.apache.tools.ant.Project getProject() {
319: return super .getProject();
320: }
321: }//class-BFT
322:
323: }
324:
325: class DummyTaskPackage extends Task {
326: public DummyTaskPackage() {
327: }
328:
329: public void execute() {
330: }
331: }
|