1 19 20 package org.netbeans.modules.java.j2seproject; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.OutputStream ; 25 import java.io.PrintWriter ; 26 import java.io.Reader ; 27 import java.io.StringReader ; 28 import java.io.StringWriter ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.Properties ; 32 import java.util.jar.Attributes ; 33 import java.util.jar.JarFile ; 34 import org.apache.tools.ant.module.api.support.ActionUtils; 35 import org.netbeans.api.project.Project; 36 import org.netbeans.api.project.ProjectManager; 37 import org.netbeans.api.project.ant.AntArtifact; 38 import org.netbeans.junit.MockServices; 39 import org.netbeans.junit.NbTestCase; 40 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties; 41 import org.netbeans.spi.project.ant.AntArtifactProvider; 42 import org.netbeans.spi.project.support.ant.AntProjectHelper; 43 import org.netbeans.spi.project.support.ant.EditableProperties; 44 import org.netbeans.spi.project.support.ant.ReferenceHelper; 45 import org.openide.execution.ExecutorTask; 46 import org.openide.filesystems.FileObject; 47 import org.openide.filesystems.FileUtil; 48 import org.openide.modules.InstalledFileLocator; 49 import org.openide.modules.SpecificationVersion; 50 import org.openide.windows.IOProvider; 51 import org.openide.windows.InputOutput; 52 import org.openide.windows.OutputListener; 53 import org.openide.windows.OutputWriter; 54 55 60 public final class BuildImplTest extends NbTestCase { 61 62 public BuildImplTest(String name) { 63 super(name); 64 } 65 66 private File junitJar; 67 68 protected void setUp() throws Exception { 69 super.setUp(); 70 clearWorkDir(); 71 output.clear(); 72 outputType.clear(); 73 String junitJarProp = System.getProperty("test.junit.jar"); 74 assertNotNull("must set test.junit.jar", junitJarProp); 75 junitJar = new File (junitJarProp); 76 assertTrue("file " + junitJar + " exists", junitJar.isFile()); 77 MockServices.setServices(IOP.class, IFL.class); 78 } 79 80 private AntProjectHelper setupProject(String subFolder, int numberOfSourceFiles, boolean generateTests) throws Exception { 81 File proj = getWorkDir(); 82 if (subFolder != null) { 83 proj = new File (getWorkDir(), subFolder); 84 } 85 J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); AntProjectHelper aph = J2SEProjectGenerator.createProject(proj, subFolder != null ? subFolder + getName() : getName(), (String )null, (String )null); 87 J2SEProjectGenerator.setDefaultSourceLevel(null); 88 FileObject root = aph.getProjectDirectory(); 89 for (int i=0; i<numberOfSourceFiles; i++) { 90 generateJava(root, "src/pkg/Source" + i + ".java", false); 91 if (generateTests) { 92 generateJava(root, "test/pkg/Source" + i + "Test.java", true); 93 } 94 } 95 return aph; 96 } 97 98 private AntProjectHelper setupProject(int numberOfSourceFiles, boolean generateTests) throws Exception { 99 clearWorkDir(); 100 return setupProject(null, numberOfSourceFiles, generateTests); 101 } 102 103 private void generateJava(FileObject root, String path, boolean test) throws Exception { 104 String name = path.replaceFirst("^.+/", "").replaceFirst("\\..+$", ""); 105 if (test) { 106 writeFile(root, path, 107 "package pkg;\n" + 108 "import junit.framework.TestCase;\n" + 109 "public class " + name + " extends TestCase {\n" + 110 "public " + name + "() { }\n"+ 111 "public void testDoSomething() { System.out.println(\"" + name + " test executed\"); }\n" + 112 "}\n"); 113 } else { 114 writeFile(root, path, 115 "package pkg;\n" + 116 "public class " + name + " {\n" + 117 "public boolean doSomething() { return true; }\n" + 118 "public static void main(String[] args) { System.err.println(\"" + name + " main class executed\"); }\n" + 119 "}\n"); 120 } 121 } 122 123 private FileObject writeFile(FileObject root, String path, String body) throws Exception { 124 FileObject fo = FileUtil.createData(root, path); 125 OutputStream os = fo.getOutputStream(); 126 PrintWriter pw = new PrintWriter (os); 127 pw.print(body); 128 pw.flush(); 129 os.close(); 130 return fo; 131 } 132 133 private Properties getProperties() { 134 Properties p = new Properties (); 135 p.setProperty("libs.junit.classpath", junitJar.getAbsolutePath()); 136 return p; 137 } 138 139 public void testDefaultTargets() throws Exception { 140 AntProjectHelper aph = setupProject(1, true); 141 FileObject buildXml = aph.getProjectDirectory().getFileObject("build.xml"); 142 assertNotNull("Must have build.xml", buildXml); 143 Properties p = getProperties(); 144 assertBuildSuccess(ActionUtils.runTarget(buildXml, null, p)); 145 assertTrue("Default target must test project", output.contains("test:")); 146 assertTrue("Default target must jar project", output.contains("jar:")); 147 assertTrue("Default target must build javadoc", output.contains("javadoc:")); 148 149 FileObject fo = aph.getProjectDirectory(); 150 assertNotNull("build/classes/pkg/Source0.class must exist", fo.getFileObject("build/classes/pkg/Source0.class")); 151 assertNotNull("build/test/classes/pkg/Source0Test.class must exist", fo.getFileObject("build/test/classes/pkg/Source0Test.class")); 152 assertNotNull("dist/testDefaultTargets.jar must exist", fo.getFileObject("dist/testDefaultTargets.jar")); 153 assertNotNull("dist/javadoc/index.html must exist", fo.getFileObject("dist/javadoc/index.html")); 154 } 155 156 public void testCompile() throws Exception { 157 AntProjectHelper aph = setupProject(2, true); 158 FileObject buildXml = aph.getProjectDirectory().getFileObject("build.xml"); 159 assertNotNull("Must have build.xml", buildXml); 160 Properties p = getProperties(); 161 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"compile"}, p)); 162 assertTrue("compile target was not executed", output.contains("compile:")); 163 164 FileObject fo = aph.getProjectDirectory(); 165 assertNotNull("build/classes/pkg/Source0.class must exist", fo.getFileObject("build/classes/pkg/Source0.class")); 166 assertNotNull("build/classes/pkg/Source1.class must exist", fo.getFileObject("build/classes/pkg/Source1.class")); 167 assertNull("build/test folder should not be created", fo.getFileObject("build/test")); 168 assertNull("dist folder should not be created", fo.getFileObject("dist")); 169 } 170 171 public void testCompileSingle() throws Exception { 172 AntProjectHelper aph = setupProject(3, true); 173 FileObject buildXml = aph.getProjectDirectory().getFileObject("build.xml"); 174 assertNotNull("Must have build.xml", buildXml); 175 Properties p = getProperties(); 176 p.setProperty("javac.includes", "pkg/Source2.java"); 177 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"compile-single"}, p)); 178 assertTrue("compile-single target was not executed", output.contains("compile-single:")); 179 180 FileObject fo = aph.getProjectDirectory(); 181 assertNotNull("build/classes/pkg/Source2.class must exist", fo.getFileObject("build/classes/pkg/Source2.class")); 182 assertEquals("Only one class should be compiled", 1, fo.getFileObject("build/classes/pkg").getChildren().length); 183 assertNull("build/test folder should not be created", fo.getFileObject("build/test")); 184 assertNull("dist folder should not be created", fo.getFileObject("dist")); 185 } 186 187 public void testIncludesExcludes() throws Exception { 188 AntProjectHelper aph = setupProject(12, true); 189 EditableProperties ep = aph.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); 190 ep.setProperty(J2SEProjectProperties.INCLUDES, "**/*1*"); 191 ep.setProperty(J2SEProjectProperties.EXCLUDES, "**/*0*"); 192 aph.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep); 193 ProjectManager.getDefault().saveAllProjects(); 194 FileObject dir = aph.getProjectDirectory(); 195 FileUtil.createData(dir, "src/data0.xml"); 196 FileUtil.createData(dir, "src/data1.xml"); 197 FileUtil.createData(dir, "src/data2.xml"); 198 FileUtil.createData(dir, "src/data10.xml"); 199 FileUtil.createData(dir, "src/data11.xml"); 200 generateJava(dir, "test/pkg/Utils1.java", true); 201 FileObject buildXml = dir.getFileObject("build.xml"); 202 assertNotNull(buildXml); 203 Properties p = getProperties(); 204 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String [] {"test"}, p)); 205 assertNull(dir.getFileObject("build/classes/pkg/Source0.class")); 206 assertNotNull(dir.getFileObject("build/classes/pkg/Source1.class")); 207 assertNull(dir.getFileObject("build/classes/pkg/Source2.class")); 208 assertNull(dir.getFileObject("build/classes/pkg/Source10.class")); 209 assertNotNull(dir.getFileObject("build/classes/pkg/Source11.class")); 210 assertNull(dir.getFileObject("build/classes/data0.xml")); 211 assertNotNull(dir.getFileObject("build/classes/data1.xml")); 212 assertNull(dir.getFileObject("build/classes/data2.xml")); 213 assertNull(dir.getFileObject("build/classes/data10.xml")); 214 assertNotNull(dir.getFileObject("build/classes/data11.xml")); 215 assertNull(dir.getFileObject("build/test/classes/pkg/Source0Test.class")); 216 assertNotNull(dir.getFileObject("build/test/classes/pkg/Source1Test.class")); 217 assertNull(dir.getFileObject("build/test/classes/pkg/Source2Test.class")); 218 assertNull(dir.getFileObject("build/test/classes/pkg/Source10Test.class")); 219 assertNotNull(dir.getFileObject("build/test/classes/pkg/Source11Test.class")); 220 assertNotNull(dir.getFileObject("build/test/classes/pkg/Utils1.class")); 221 assertFalse(output.contains("Source0Test test executed")); 222 assertTrue(output.contains("Source1Test test executed")); 223 assertFalse(output.contains("Source2Test test executed")); 224 assertFalse(output.contains("Source10Test test executed")); 225 assertTrue(output.contains("Source11Test test executed")); 226 assertFalse(output.contains("Utils1 test executed")); 227 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String [] {"javadoc"}, p)); 228 assertNull(dir.getFileObject("dist/javadoc/pkg/Source0.html")); 229 assertNotNull(dir.getFileObject("dist/javadoc/pkg/Source1.html")); 230 assertNull(dir.getFileObject("dist/javadoc/pkg/Source2.html")); 231 assertNull(dir.getFileObject("dist/javadoc/pkg/Source10.html")); 232 assertNotNull(dir.getFileObject("dist/javadoc/pkg/Source11.html")); 233 p.setProperty("javac.includes", "pkg/Source4.java"); 234 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String [] {"compile-single"}, p)); 235 assertNotNull(dir.getFileObject("build/classes/pkg/Source4.class")); 236 p.setProperty("javac.includes", "pkg/Source4Test.java"); 237 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String [] {"compile-test-single"}, p)); 238 assertNotNull(dir.getFileObject("build/test/classes/pkg/Source4Test.class")); 239 p.setProperty("javac.includes", "pkg/Source7Test.java"); 240 p.setProperty("test.includes", "pkg/Source7Test.java"); 241 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String [] {"test-single"}, p)); 242 assertNotNull(dir.getFileObject("build/test/classes/pkg/Source7Test.class")); 243 assertTrue(output.contains("Source7Test test executed")); 244 writeFile(dir, "src/RefersToExcluded1a.java", "class RefersToExcluded1a {{new pkg.Source11();}}"); 245 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String [] {"compile"}, p)); 246 writeFile(dir, "src/RefersToExcluded1b.java", "class RefersToExcluded1b {{new pkg.Source10();}}"); 247 assertBuildFailure(ActionUtils.runTarget(buildXml, new String [] {"compile"}, p)); 248 } 249 250 private void touch(FileObject f, FileObject ref) throws Exception { 251 File ff = FileUtil.toFile(f); 252 long older = ff.lastModified(); 253 if (ref != null) { 254 older = Math.max(older, FileUtil.toFile(ref).lastModified()); 255 } 256 for (long pause = 1; pause < 9999; pause *= 2) { 257 Thread.sleep(pause); 258 ff.setLastModified(System.currentTimeMillis()); 259 if (ff.lastModified() > older) { 260 return; 261 } 262 } 263 fail("Did not manage to touch " + ff); 264 } 265 266 267 public void testCompileWithDependencyAnalysis() throws Exception { 268 AntProjectHelper aph = setupProject(0, false); 269 FileObject buildXml = aph.getProjectDirectory().getFileObject("build.xml"); 270 FileObject d = aph.getProjectDirectory(); 271 FileObject x = writeFile(d, "src/p/X.java", "package p; public class X {static {Y.y1();}}"); 272 FileObject y = writeFile(d, "src/p/Y.java", "package p; public class Y {static void y1() {}}"); 273 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String [] {"compile"}, getProperties())); 274 writeFile(d, "src/p/Y.java", "package p; public class Y {static void y2() {}}"); 275 touch(y, d.getFileObject("build/classes/p/Y.class")); 276 assertBuildFailure(ActionUtils.runTarget(buildXml, new String [] {"compile"}, getProperties())); 277 writeFile(d, "src/p/X.java", "package p; public class X {static {Y.y2();}}"); 278 touch(x, null); 279 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String [] {"compile"}, getProperties())); 280 FileObject yt = writeFile(d, "test/p/YTest.java", "package p; public class YTest extends junit.framework.TestCase {public void testY() {Y.y2();}}"); 281 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String [] {"compile-test"}, getProperties())); 282 writeFile(d, "src/p/X.java", "package p; public class X {static {Y.y1();}}"); 283 touch(x, d.getFileObject("build/classes/p/X.class")); 284 writeFile(d, "src/p/Y.java", "package p; public class Y {static void y1() {}}"); 285 touch(y, d.getFileObject("build/classes/p/Y.class")); 286 assertBuildFailure(ActionUtils.runTarget(buildXml, new String [] {"compile-test"}, getProperties())); 287 writeFile(d, "test/p/YTest.java", "package p; public class YTest extends junit.framework.TestCase {public void testY() {Y.y1();}}"); 288 touch(yt, null); 289 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String [] {"compile-test"}, getProperties())); 290 } 291 292 public void testRun() throws Exception { 293 AntProjectHelper aph = setupProject(3, true); 294 FileObject buildXml = aph.getProjectDirectory().getFileObject("build.xml"); 295 assertNotNull("Must have build.xml", buildXml); 296 Properties p = getProperties(); 297 p.setProperty("main.class", "pkg.Source1"); 298 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"run"}, p)); 299 assertTrue("compile target was not executed", output.contains("compile:")); 300 assertTrue("run target was not executed", output.contains("run:")); 301 assertTrue("main class was not executed", output.contains("Source1 main class executed")); 302 303 FileObject fo = aph.getProjectDirectory(); 304 assertNotNull("build/classes/pkg/Source0.class must exist", fo.getFileObject("build/classes/pkg/Source0.class")); 305 assertNotNull("build/classes/pkg/Source1.class must exist", fo.getFileObject("build/classes/pkg/Source1.class")); 306 assertNotNull("build/classes/pkg/Source2.class must exist", fo.getFileObject("build/classes/pkg/Source2.class")); 307 assertNull("build/test folder should not be created", fo.getFileObject("build/test")); 308 assertNull("dist folder should not be created", fo.getFileObject("dist")); 309 } 310 311 public void testRunSingle() throws Exception { 312 AntProjectHelper aph = setupProject(3, true); 313 FileObject buildXml = aph.getProjectDirectory().getFileObject("build.xml"); 314 assertNotNull("Must have build.xml", buildXml); 315 Properties p = getProperties(); 316 p.setProperty("main.class", "pkg.Source0"); 317 p.setProperty("javac.includes", "pkg/Source2.java"); 318 p.setProperty("run.class", "pkg.Source2"); 319 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"run-single"}, p)); 320 assertTrue("compile-single target was not executed", output.contains("compile-single:")); 321 assertTrue("run target was not executed", output.contains("run-single:")); 322 assertTrue("main class was not executed", output.contains("Source2 main class executed")); 323 324 FileObject fo = aph.getProjectDirectory(); 325 assertNotNull("build/classes/pkg/Source2.class must exist", fo.getFileObject("build/classes/pkg/Source2.class")); 326 assertEquals("Only one class should be compiled", 1, fo.getFileObject("build/classes/pkg").getChildren().length); 327 assertNull("build/test folder should not be created", fo.getFileObject("build/test")); 328 assertNull("dist folder should not be created", fo.getFileObject("dist")); 329 } 330 331 public void testJar() throws Exception { 332 AntProjectHelper aph = setupProject(2, true); 333 FileObject buildXml = aph.getProjectDirectory().getFileObject("build.xml"); 334 assertNotNull("Must have build.xml", buildXml); 335 Properties p = getProperties(); 336 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"jar"}, p)); 337 assertTrue("compile target was not executed", output.contains("compile:")); 338 assertTrue("jar target was not executed", output.contains("jar:")); 339 340 FileObject fo = aph.getProjectDirectory(); 341 assertNotNull("build/classes/pkg/Source0.class must exist", fo.getFileObject("build/classes/pkg/Source0.class")); 342 assertNotNull("build/classes/pkg/Source1.class must exist", fo.getFileObject("build/classes/pkg/Source1.class")); 343 assertNull("build/test folder should not be created", fo.getFileObject("build/test")); 344 assertNotNull("dist/testJar.jar must exist", fo.getFileObject("dist/testJar.jar")); 345 assertNull("dist/javadoc fodler should not be created", fo.getFileObject("dist/javadoc")); 346 Attributes mf = getJarManifest(fo.getFileObject("dist/testJar.jar")); 347 assertNull("Main-class was not set", mf.getValue("Main-class")); 348 349 351 writeFile(aph.getProjectDirectory(), "manifest/manifest.mf", 352 "Manifest-Version: 1.0\n" + 353 "Something: s.o.m.e\n\n"); 354 p.setProperty("manifest.file", "manifest/manifest.mf"); 355 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"jar"}, p)); 356 assertTrue("compile target was not executed", output.contains("compile:")); 357 assertTrue("jar target was not executed", output.contains("jar:")); 358 assertNull("build/test folder should not be created", fo.getFileObject("build/test")); 359 assertNotNull("dist/testJar.jar must exist", fo.getFileObject("dist/testJar.jar")); 360 assertNull("dist/javadoc fodler should not be created", fo.getFileObject("dist/javadoc")); 361 mf = getJarManifest(fo.getFileObject("dist/testJar.jar")); 362 assertEquals("Specified manifest was not used", "s.o.m.e", mf.getValue("Something")); 363 assertNull("Main-class was not set", mf.getValue("Main-class")); 364 365 367 p.setProperty("main.class", "some.clazz.Main"); 368 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"jar"}, p)); 369 assertTrue("compile target was not executed", output.contains("compile:")); 370 assertTrue("jar target was not executed", output.contains("jar:")); 371 assertNull("build/test folder should not be created", fo.getFileObject("build/test")); 372 assertNotNull("dist/testJar.jar must exist", fo.getFileObject("dist/testJar.jar")); 373 assertNull("dist/javadoc fodler should not be created", fo.getFileObject("dist/javadoc")); 374 mf = getJarManifest(fo.getFileObject("dist/testJar.jar")); 375 assertEquals("Specified manifest was not used", "s.o.m.e", mf.getValue("Something")); 376 assertEquals("Main-class was not set", "some.clazz.Main", mf.getValue("Main-class")); 377 } 378 379 public void testJavadoc() throws Exception { 380 AntProjectHelper aph = setupProject(3, true); 381 FileObject buildXml = aph.getProjectDirectory().getFileObject("build.xml"); 382 assertNotNull("Must have build.xml", buildXml); 383 Properties p = getProperties(); 384 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"javadoc"}, p)); 385 assertTrue("javadoc target was not executed", output.contains("javadoc:")); 386 387 FileObject fo = aph.getProjectDirectory(); 388 assertNull("build folder should not be created", fo.getFileObject("build")); 389 assertNull("dist/testJavadoc.jar should not exist", fo.getFileObject("dist/testJavadoc.jar")); 390 assertNotNull("dist/javadoc/index.html must exist", fo.getFileObject("dist/javadoc/index.html")); 391 } 392 393 public void testTest() throws Exception { 394 AntProjectHelper aph = setupProject(2, true); 395 FileObject buildXml = aph.getProjectDirectory().getFileObject("build.xml"); 396 assertNotNull("Must have build.xml", buildXml); 397 Properties p = getProperties(); 398 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"test"}, p)); 399 assertTrue("compile target was not executed", output.contains("compile:")); 400 assertTrue("compile-test target was not executed", output.contains("compile-test:")); 401 assertTrue("test target was not executed", output.contains("test:")); 402 assertTrue("test 0 was not executed", output.contains("Source0Test test executed")); 403 assertTrue("test 1 was not executed", output.contains("Source1Test test executed")); 404 405 FileObject fo = aph.getProjectDirectory(); 406 assertNotNull("build/classes/pkg/Source0.class must exist", fo.getFileObject("build/classes/pkg/Source0.class")); 407 assertNotNull("build/classes/pkg/Source1.class must exist", fo.getFileObject("build/classes/pkg/Source1.class")); 408 assertNotNull("build/test/classes/pkg/Source0Test.class must exist", fo.getFileObject("build/test/classes/pkg/Source0Test.class")); 409 assertNotNull("build/test/classes/pkg/Source1Test.class must exist", fo.getFileObject("build/test/classes/pkg/Source1Test.class")); 410 assertNull("dist folder should not be created", fo.getFileObject("dist")); 411 } 412 413 public void testCompileSingleTest() throws Exception { 414 AntProjectHelper aph = setupProject(3, true); 415 FileObject buildXml = aph.getProjectDirectory().getFileObject("build.xml"); 416 assertNotNull("Must have build.xml", buildXml); 417 Properties p = getProperties(); 418 p.setProperty("javac.includes", "pkg/Source2Test.java"); 419 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"compile-test-single"}, p)); 420 assertTrue("compile-single target was not executed", output.contains("compile:")); 421 assertTrue("compile-single target was not executed", output.contains("compile-test-single:")); 422 423 FileObject fo = aph.getProjectDirectory(); 424 assertNotNull("build/classes/pkg/Source0.class must exist", fo.getFileObject("build/classes/pkg/Source0.class")); 425 assertNotNull("build/classes/pkg/Source1.class must exist", fo.getFileObject("build/classes/pkg/Source1.class")); 426 assertNotNull("build/classes/pkg/Source2.class must exist", fo.getFileObject("build/classes/pkg/Source2.class")); 427 assertNotNull("build/test/classes/pkg/Source2Test.class must exist", fo.getFileObject("build/test/classes/pkg/Source2Test.class")); 428 assertEquals("Only one test class should be compiled", 1, fo.getFileObject("build/test/classes/pkg").getChildren().length); 429 assertNull("dist folder should not be created", fo.getFileObject("dist")); 430 } 431 432 public void testRunSingleTest() throws Exception { 433 AntProjectHelper aph = setupProject(3, true); 434 FileObject buildXml = aph.getProjectDirectory().getFileObject("build.xml"); 435 assertNotNull("Must have build.xml", buildXml); 436 Properties p = getProperties(); 437 p.setProperty("javac.includes", "pkg/Source2Test.java"); 438 p.setProperty("test.includes", "pkg/Source2Test.java"); 439 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"test-single"}, p)); 440 assertTrue("compile target was not executed", output.contains("compile:")); 441 assertTrue("run target was not executed", output.contains("compile-test-single:")); 442 assertTrue("run target was not executed", output.contains("test-single:")); 443 assertTrue("test was not executed", output.contains("Source2Test test executed")); 444 445 FileObject fo = aph.getProjectDirectory(); 446 assertNotNull("build/classes/pkg/Source0.class must exist", fo.getFileObject("build/classes/pkg/Source0.class")); 447 assertNotNull("build/classes/pkg/Source1.class must exist", fo.getFileObject("build/classes/pkg/Source1.class")); 448 assertNotNull("build/classes/pkg/Source2.class must exist", fo.getFileObject("build/classes/pkg/Source2.class")); 449 assertNotNull("build/test/classes/pkg/Source2Test.class must exist", fo.getFileObject("build/test/classes/pkg/Source2Test.class")); 450 assertEquals("Only one test class should be compiled", 1, fo.getFileObject("build/test/classes/pkg").getChildren().length); 451 assertNull("dist folder should not be created", fo.getFileObject("dist")); 452 } 453 454 public void testSubprojects() throws Exception { 455 clearWorkDir(); 456 AntProjectHelper aph1 = setupProject("p1", 1, false); 457 AntProjectHelper aph2 = setupProject("p2", 1, false); 458 Project proj1 = ProjectManager.getDefault().findProject(aph1.getProjectDirectory()); 459 Project proj2 = ProjectManager.getDefault().findProject(aph2.getProjectDirectory()); 460 ReferenceHelper refHelper = ((J2SEProject)proj1).getReferenceHelper(); 461 AntArtifactProvider aap = proj2.getLookup().lookup(AntArtifactProvider.class); 462 AntArtifact[] aa = aap.getBuildArtifacts(); 463 assertTrue("Project should have an artifact", aa.length > 0); 464 assertTrue("Reference was not added?", refHelper.addReference(aa[0])); 465 ProjectManager.getDefault().saveAllProjects(); 466 FileObject fo = aph1.getProjectDirectory(); 467 assertNull("build folder cannot exist", fo.getFileObject("build")); 468 assertNull("dist folder cannot exist", fo.getFileObject("dist")); 469 fo = aph2.getProjectDirectory(); 470 assertNull("build folder cannot exist", fo.getFileObject("build")); 471 assertNull("dist folder cannot exist", fo.getFileObject("dist")); 472 473 FileObject buildXml = aph1.getProjectDirectory().getFileObject("build.xml"); 474 assertNotNull("Must have build.xml", buildXml); 475 Properties p = getProperties(); 476 p.setProperty("no.dependencies", "true"); 477 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"jar"}, p)); 478 assertTrue("jar target was not executed", output.contains("jar:")); 479 output.remove("jar:"); 480 assertFalse("subproject's jar should not be executed", output.contains("jar:")); 481 fo = aph1.getProjectDirectory(); 482 assertNotNull("build folder must exist", fo.getFileObject("build")); 483 assertNotNull("dist folder must exist", fo.getFileObject("dist")); 484 fo = aph2.getProjectDirectory(); 485 assertNull("build folder cannot exist", fo.getFileObject("build")); 486 assertNull("dist folder cannot exist", fo.getFileObject("dist")); 487 488 p.setProperty("no.dependencies", "false"); 489 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"jar"}, p)); 490 assertTrue("jar target was not executed", output.contains("jar:")); 491 output.remove("jar:"); 492 assertTrue("subproject's jar target was not executed", output.contains("jar:")); 493 fo = aph1.getProjectDirectory(); 494 assertNotNull("build folder must exist", fo.getFileObject("build")); 495 assertNotNull("dist folder must exist", fo.getFileObject("dist")); 496 fo = aph2.getProjectDirectory(); 497 assertNotNull("build folder must exist", fo.getFileObject("build")); 498 assertNotNull("dist folder must exist", fo.getFileObject("dist")); 499 500 p.setProperty("no.dependencies", "true"); 501 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"clean"}, p)); 502 assertTrue("clean target was not executed", output.contains("clean:")); 503 output.remove("clean:"); 504 assertFalse("subproject's clean should not be executed", output.contains("clean:")); 505 fo = aph1.getProjectDirectory(); 506 fo.refresh(); 507 assertNull("build folder cannot exist", fo.getFileObject("build")); 508 assertNull("dist folder cannot exist", fo.getFileObject("dist")); 509 fo = aph2.getProjectDirectory(); 510 fo.refresh(); 511 assertNotNull("build folder must exist", fo.getFileObject("build")); 512 assertNotNull("dist folder must exist", fo.getFileObject("dist")); 513 514 p.setProperty("no.dependencies", "false"); 515 assertBuildSuccess(ActionUtils.runTarget(buildXml, new String []{"clean"}, p)); 516 assertTrue("clean target was not executed", output.contains("clean:")); 517 output.remove("clean:"); 518 assertTrue("subproject's clean target was not executed", output.contains("clean:")); 519 fo = aph1.getProjectDirectory(); 520 fo.refresh(); 521 assertNull("build folder must be removed", fo.getFileObject("build")); 522 assertNull("dist folder must be removed", fo.getFileObject("dist")); 523 fo = aph2.getProjectDirectory(); 524 fo.refresh(); 525 assertNull("build folder must be removed", fo.getFileObject("build")); 526 assertNull("dist folder must be removed", fo.getFileObject("dist")); 527 } 528 529 530 private Attributes getJarManifest(FileObject fo) throws Exception { 531 File f = FileUtil.toFile(fo); 532 JarFile jf = new JarFile (f); 533 Attributes attrs = (Attributes )jf.getManifest().getMainAttributes().clone(); 534 jf.close(); 535 return attrs; 536 } 537 538 private void assertBuildSuccess(ExecutorTask task) { 539 if (task.result() != 0) { 540 dumpOutput(); 541 fail("target failed"); 542 } 543 } 544 545 private void assertBuildFailure(ExecutorTask task) { 546 if (task.result() == 0) { 547 dumpOutput(); 548 fail("target failed"); 549 } 550 } 551 552 private void dumpOutput() { 553 List <String > output = new ArrayList <String >(this.output); 554 System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); 555 for (String line : output) { 556 System.out.println(line); 557 } 558 System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"); 559 } 560 561 @SuppressWarnings ("deprecation") 562 public static final class IOP extends IOProvider implements InputOutput { 563 564 public IOP() {} 565 566 public InputOutput getIO(String name, boolean newIO) { 567 return this; 568 } 569 570 public OutputWriter getStdOut() { 571 throw new UnsupportedOperationException (); 572 } 573 574 public OutputWriter getOut() { 575 return new OW(false); 576 } 577 578 public OutputWriter getErr() { 579 return new OW(true); 580 } 581 582 public Reader getIn() { 583 return new StringReader (""); 584 } 585 586 public Reader flushReader() { 587 return getIn(); 588 } 589 590 public void closeInputOutput() {} 591 592 public boolean isClosed() { 593 return false; 594 } 595 596 public boolean isErrSeparated() { 597 return false; 598 } 599 600 public boolean isFocusTaken() { 601 return false; 602 } 603 604 public void select() {} 605 606 public void setErrSeparated(boolean value) {} 607 608 public void setErrVisible(boolean value) {} 609 610 public void setFocusTaken(boolean value) {} 611 612 public void setInputVisible(boolean value) {} 613 614 public void setOutputVisible(boolean value) {} 615 616 } 617 618 private static final List <String > output = new ArrayList <String >(); 619 private static final List <String > outputType = new ArrayList <String >(); 620 621 private static final String TYPE_ERR = "err"; 622 private static final String TYPE_OK = "ok"; 623 624 private static final class OW extends OutputWriter { 625 626 private final boolean err; 627 628 public OW(boolean err) { 629 super(new StringWriter ()); 630 this.err = err; 631 } 632 633 public void println(String s, OutputListener l) throws IOException { 634 message(s, l != null); 635 } 636 637 public void println(String x) { 638 message(x, false); 639 } 640 641 private void message(String msg, boolean hyperlinked) { 642 output.add(msg); 643 String type = err ? TYPE_ERR : TYPE_OK; 644 outputType.add(type); 645 } 646 647 public void reset() throws IOException {} 648 649 } 650 651 652 public static final class IFL extends InstalledFileLocator { 653 public IFL() {} 654 public File locate(String relativePath, String codeNameBase, boolean localized) { 655 if (relativePath.equals("ant/nblib/bridge.jar")) { 656 String path = System.getProperty("test.bridge.jar"); 657 assertNotNull("must set test.bridge.jar", path); 658 return new File (path); 659 } else if (relativePath.equals("ant")) { 660 String path = System.getProperty("test.ant.home"); 661 assertNotNull("must set test.ant.home", path); 662 return new File (path); 663 } else if (relativePath.startsWith("ant/")) { 664 String path = System.getProperty("test.ant.home"); 665 assertNotNull("must set test.ant.home", path); 666 return new File (path, relativePath.substring(4).replace('/', File.separatorChar)); 667 } else { 668 return null; 669 } 670 } 671 } 672 673 } 674 | Popular Tags |