KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > ProjectTest


1 /*
2  * Copyright 2000-2005 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;
19
20 import org.apache.tools.ant.input.DefaultInputHandler;
21 import org.apache.tools.ant.input.InputHandler;
22 import org.apache.tools.ant.input.PropertyFileInputHandler;
23 import org.apache.tools.ant.taskdefs.condition.Os;
24 import org.apache.tools.ant.types.*;
25 import org.apache.tools.ant.util.JavaEnvUtils;
26
27 import java.io.File JavaDoc;
28
29 import junit.framework.AssertionFailedError;
30 import junit.framework.Test;
31 import junit.framework.TestCase;
32 import junit.framework.TestSuite;
33
34 /**
35  * Very limited test class for Project. Waiting to be extended.
36  *
37  */

38 public class ProjectTest extends TestCase {
39
40     private Project p;
41     private String JavaDoc root;
42     private MockBuildListener mbl;
43
44     public ProjectTest(String JavaDoc name) {
45         super(name);
46     }
47
48     public void setUp() {
49         p = new Project();
50         p.init();
51         root = new File JavaDoc(File.separator).getAbsolutePath();
52         mbl = new MockBuildListener(p);
53     }
54
55     public void testDataTypes() throws BuildException {
56         assertNull("dummy is not a known data type",
57                    p.createDataType("dummy"));
58         Object JavaDoc o = p.createDataType("fileset");
59         assertNotNull("fileset is a known type", o);
60         assertTrue("fileset creates FileSet", o instanceof FileSet);
61         assertTrue("PatternSet",
62                p.createDataType("patternset") instanceof PatternSet);
63         assertTrue("Path", p.createDataType("path") instanceof Path);
64     }
65
66     /**
67      * This test has been a starting point for moving the code to FileUtils.
68      */

69     public void testResolveFile() {
70         /*
71          * Start with simple absolute file names.
72          */

73         assertEquals(File.separator,
74                      p.resolveFile("/", null).getPath());
75         assertEquals(File.separator,
76                      p.resolveFile("\\", null).getPath());
77
78         if (!Os.isFamily("unix")) {
79         /*
80          * throw in drive letters
81          */

82         String JavaDoc driveSpec = "C:";
83         assertEquals(driveSpec + "\\",
84                      p.resolveFile(driveSpec + "/", null).getPath());
85         assertEquals(driveSpec + "\\",
86                      p.resolveFile(driveSpec + "\\", null).getPath());
87         String JavaDoc driveSpecLower = "c:";
88         assertEquals(driveSpec + "\\",
89                      p.resolveFile(driveSpecLower + "/", null).getPath());
90         assertEquals(driveSpec + "\\",
91                      p.resolveFile(driveSpecLower + "\\", null).getPath());
92         /*
93          * promised to eliminate consecutive slashes after drive letter.
94          */

95         assertEquals(driveSpec + "\\",
96                      p.resolveFile(driveSpec + "/////", null).getPath());
97         assertEquals(driveSpec + "\\",
98                      p.resolveFile(driveSpec + "\\\\\\\\\\\\", null).getPath());
99         }
100
101         /*
102          * Now test some relative file name magic.
103          */

104         assertEquals(localize("/1/2/3/4"),
105                      p.resolveFile("4", new File JavaDoc(localize("/1/2/3"))).getPath());
106         assertEquals(localize("/1/2/3/4"),
107                      p.resolveFile("./4", new File JavaDoc(localize("/1/2/3"))).getPath());
108         assertEquals(localize("/1/2/3/4"),
109                      p.resolveFile(".\\4", new File JavaDoc(localize("/1/2/3"))).getPath());
110         assertEquals(localize("/1/2/3/4"),
111                      p.resolveFile("./.\\4", new File JavaDoc(localize("/1/2/3"))).getPath());
112         assertEquals(localize("/1/2/3/4"),
113                      p.resolveFile("../3/4", new File JavaDoc(localize("/1/2/3"))).getPath());
114         assertEquals(localize("/1/2/3/4"),
115                      p.resolveFile("..\\3\\4", new File JavaDoc(localize("/1/2/3"))).getPath());
116         assertEquals(localize("/1/2/3/4"),
117                      p.resolveFile("../../5/.././2/./3/6/../4", new File JavaDoc(localize("/1/2/3"))).getPath());
118         assertEquals(localize("/1/2/3/4"),
119                      p.resolveFile("..\\../5/..\\./2/./3/6\\../4", new File JavaDoc(localize("/1/2/3"))).getPath());
120
121     }
122
123     /**
124      * adapt file separators to local conventions
125      */

126     private String JavaDoc localize(String JavaDoc path) {
127         path = root + path.substring(1);
128         return path.replace('\\', File.separatorChar).replace('/', File.separatorChar);
129     }
130
131
132     private void assertTaskDefFails(final Class JavaDoc taskClass,
133                                        final String JavaDoc message) {
134         final String JavaDoc dummyName = "testTaskDefinitionDummy";
135         try {
136             mbl.addBuildEvent(message, Project.MSG_ERR);
137             p.addTaskDefinition(dummyName, taskClass);
138             fail("expected BuildException(\""+message+"\", Project.MSG_ERR) when adding task " + taskClass);
139         }
140         catch(BuildException e) {
141             assertEquals(message, e.getMessage());
142             mbl.assertEmpty();
143             assertTrue(!p.getTaskDefinitions().containsKey(dummyName));
144         }
145     }
146
147     public void testAddTaskDefinition() {
148         p.addBuildListener(mbl);
149
150         p.addTaskDefinition("Ok", DummyTaskOk.class);
151         assertEquals(DummyTaskOk.class, p.getTaskDefinitions().get("Ok"));
152         p.addTaskDefinition("OkNonTask", DummyTaskOkNonTask.class);
153         assertEquals(DummyTaskOkNonTask.class, p.getTaskDefinitions().get("OkNonTask"));
154         mbl.assertEmpty();
155
156         assertTaskDefFails(DummyTaskPrivate.class, DummyTaskPrivate.class + " is not public");
157
158         try {
159             assertTaskDefFails(DummyTaskProtected.class,
160                                DummyTaskProtected.class + " is not public");
161         } catch (AssertionFailedError e) {
162             /*
163              * I don't understand this, but this is what happens with
164              * > java -fullversion
165              * java full version "Linux_JDK_1.1.8_v3_green_threads"
166              * from time to time
167              */

168             assertTrue(JavaEnvUtils.isJavaVersion(JavaEnvUtils.JAVA_1_1));
169             assertTaskDefFails(DummyTaskProtected.class,
170                                "No public no-arg constructor in "
171                                + DummyTaskProtected.class);
172         }
173
174         assertTaskDefFails(DummyTaskPackage.class, DummyTaskPackage.class + " is not public");
175
176         assertTaskDefFails(DummyTaskAbstract.class, DummyTaskAbstract.class + " is abstract");
177         assertTaskDefFails(DummyTaskInterface.class, DummyTaskInterface.class + " is abstract");
178
179         assertTaskDefFails(DummyTaskWithoutDefaultConstructor.class, "No public no-arg constructor in " + DummyTaskWithoutDefaultConstructor.class);
180         assertTaskDefFails(DummyTaskWithoutPublicConstructor.class, "No public no-arg constructor in " + DummyTaskWithoutPublicConstructor.class);
181
182         assertTaskDefFails(DummyTaskWithoutExecute.class, "No public execute() in " + DummyTaskWithoutExecute.class);
183         assertTaskDefFails(DummyTaskWithNonPublicExecute.class, "No public execute() in " + DummyTaskWithNonPublicExecute.class);
184
185         mbl.addBuildEvent("return type of execute() should be void but was \"int\" in " + DummyTaskWithNonVoidExecute.class, Project.MSG_WARN);
186         p.addTaskDefinition("NonVoidExecute", DummyTaskWithNonVoidExecute.class);
187         mbl.assertEmpty();
188         assertEquals(DummyTaskWithNonVoidExecute.class, p.getTaskDefinitions().get("NonVoidExecute"));
189     }
190
191     public void testInputHandler() {
192         InputHandler ih = p.getInputHandler();
193         assertNotNull(ih);
194         assertTrue(ih instanceof DefaultInputHandler);
195         InputHandler pfih = new PropertyFileInputHandler();
196         p.setInputHandler(pfih);
197         assertSame(pfih, p.getInputHandler());
198     }
199
200     public void testTaskDefinitionContainsKey() {
201         assertTrue(p.getTaskDefinitions().containsKey("echo"));
202     }
203
204     public void testTaskDefinitionContains() {
205         assertTrue(p.getTaskDefinitions().contains(org.apache.tools.ant.taskdefs.Echo.class));
206     }
207
208     private class DummyTaskPrivate extends Task {
209         public DummyTaskPrivate() {}
210         public void execute() {}
211     }
212
213     protected class DummyTaskProtected extends Task {
214         public DummyTaskProtected() {}
215         public void execute() {}
216     }
217
218 }
219
220 class DummyTaskPackage extends Task {
221     public DummyTaskPackage() {}
222     public void execute() {}
223 }
224
Popular Tags