1 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 ; 28 29 import junit.framework.AssertionFailedError; 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 38 public class ProjectTest extends TestCase { 39 40 private Project p; 41 private String root; 42 private MockBuildListener mbl; 43 44 public ProjectTest(String name) { 45 super(name); 46 } 47 48 public void setUp() { 49 p = new Project(); 50 p.init(); 51 root = new File (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 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 69 public void testResolveFile() { 70 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 82 String driveSpec = "C:"; 83 assertEquals(driveSpec + "\\", 84 p.resolveFile(driveSpec + "/", null).getPath()); 85 assertEquals(driveSpec + "\\", 86 p.resolveFile(driveSpec + "\\", null).getPath()); 87 String driveSpecLower = "c:"; 88 assertEquals(driveSpec + "\\", 89 p.resolveFile(driveSpecLower + "/", null).getPath()); 90 assertEquals(driveSpec + "\\", 91 p.resolveFile(driveSpecLower + "\\", null).getPath()); 92 95 assertEquals(driveSpec + "\\", 96 p.resolveFile(driveSpec + "/////", null).getPath()); 97 assertEquals(driveSpec + "\\", 98 p.resolveFile(driveSpec + "\\\\\\\\\\\\", null).getPath()); 99 } 100 101 104 assertEquals(localize("/1/2/3/4"), 105 p.resolveFile("4", new File (localize("/1/2/3"))).getPath()); 106 assertEquals(localize("/1/2/3/4"), 107 p.resolveFile("./4", new File (localize("/1/2/3"))).getPath()); 108 assertEquals(localize("/1/2/3/4"), 109 p.resolveFile(".\\4", new File (localize("/1/2/3"))).getPath()); 110 assertEquals(localize("/1/2/3/4"), 111 p.resolveFile("./.\\4", new File (localize("/1/2/3"))).getPath()); 112 assertEquals(localize("/1/2/3/4"), 113 p.resolveFile("../3/4", new File (localize("/1/2/3"))).getPath()); 114 assertEquals(localize("/1/2/3/4"), 115 p.resolveFile("..\\3\\4", new File (localize("/1/2/3"))).getPath()); 116 assertEquals(localize("/1/2/3/4"), 117 p.resolveFile("../../5/.././2/./3/6/../4", new File (localize("/1/2/3"))).getPath()); 118 assertEquals(localize("/1/2/3/4"), 119 p.resolveFile("..\\../5/..\\./2/./3/6\\../4", new File (localize("/1/2/3"))).getPath()); 120 121 } 122 123 126 private String localize(String 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 taskClass, 133 final String message) { 134 final String 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 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 |