1 19 20 package org.netbeans.modules.java.freeform; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.lang.reflect.Method ; 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.Collections ; 31 import java.util.HashSet ; 32 import java.util.List ; 33 import java.util.Set ; 34 import java.util.SortedSet ; 35 import java.util.TreeSet ; 36 import java.util.concurrent.CountDownLatch ; 37 import java.util.concurrent.TimeUnit ; 38 import java.util.regex.Matcher ; 39 import java.util.regex.Pattern ; 40 import org.netbeans.api.java.classpath.ClassPath; 41 import org.netbeans.api.java.classpath.GlobalPathRegistry; 42 import org.netbeans.api.java.project.JavaProjectConstants; 43 import org.netbeans.api.project.Project; 44 import org.netbeans.api.project.ProjectManager; 45 import org.netbeans.modules.ant.freeform.FreeformProject; 46 import org.netbeans.modules.ant.freeform.FreeformProjectGenerator; 47 import org.netbeans.modules.ant.freeform.TestBase; 48 import org.netbeans.modules.ant.freeform.spi.support.Util; 49 import org.netbeans.spi.project.AuxiliaryConfiguration; 50 import org.netbeans.spi.project.support.ant.AntProjectHelper; 51 import org.netbeans.spi.project.support.ant.EditableProperties; 52 import org.netbeans.spi.project.ui.ProjectOpenedHook; 53 import org.openide.filesystems.FileLock; 54 import org.openide.filesystems.FileObject; 55 import org.openide.filesystems.FileUtil; 56 import org.openide.util.Mutex; 57 import org.w3c.dom.Document ; 58 import org.w3c.dom.Element ; 59 60 73 78 public class ClasspathsTest extends TestBase { 79 80 public ClasspathsTest(String name) { 81 super(name); 82 } 83 84 protected void setUp() throws Exception { 85 super.setUp(); 86 assertNotNull("Must have built ant/freeform unit tests first, INCLUDING copying non-*.java resources to the classes build directory", 87 ClasspathsTest.class.getResource("/META-INF/services/org.openide.modules.InstalledFileLocator")); 88 } 89 90 public void testSourcePath() throws Exception { 91 ClassPath cp = ClassPath.getClassPath(myAppJava, ClassPath.SOURCE); 92 assertNotNull("have some SOURCE classpath for src/", cp); 93 FileObject[] roots = cp.getRoots(); 94 assertEquals("have one entry in " + cp, 1, roots.length); 95 assertEquals("that is src/", simple.getProjectDirectory().getFileObject("src"), roots[0]); 96 cp = ClassPath.getClassPath(specialTaskJava, ClassPath.SOURCE); 97 assertNotNull("have some SOURCE classpath for antsrc/", cp); 98 roots = cp.getRoots(); 99 assertEquals("have one entry", 1, roots.length); 100 assertEquals("that is antsrc/", simple.getProjectDirectory().getFileObject("antsrc"), roots[0]); 101 cp = ClassPath.getClassPath(buildProperties, ClassPath.SOURCE); 102 assertNull("have no SOURCE classpath for build.properties", cp); 103 } 104 105 public void testCompileClasspath() throws Exception { 106 ClassPath cp = ClassPath.getClassPath(myAppJava, ClassPath.COMPILE); 107 assertNotNull("have some COMPILE classpath for src/", cp); 108 assertEquals("have two entries in " + cp, 2, cp.entries().size()); 109 assertEquals("have two roots in " + cp, 2, cp.getRoots().length); 110 assertNotNull("found WeakSet in " + cp, cp.findResource("org/openide/util/WeakSet.class")); 111 assertNotNull("found NullInputStream", cp.findResource("org/openide/util/io/NullInputStream.class")); 112 cp = ClassPath.getClassPath(specialTaskJava, ClassPath.COMPILE); 113 assertNotNull("have some COMPILE classpath for antsrc/", cp); 114 assertEquals("have one entry", 1, cp.getRoots().length); 115 assertNotNull("found Task", cp.findResource("org/apache/tools/ant/Task.class")); 116 cp = ClassPath.getClassPath(buildProperties, ClassPath.COMPILE); 117 assertNull("have no COMPILE classpath for build.properties", cp); 118 } 119 120 public void testExecuteClasspath() throws Exception { 121 ClassPath cp = ClassPath.getClassPath(myAppJava, ClassPath.EXECUTE); 122 assertNotNull("have some EXECUTE classpath for src/", cp); 123 Set <String > roots = new TreeSet <String >(); 124 roots.add(urlForJar("lib/lib1.jar")); 125 roots.add(urlForJar("lib/lib2.jar")); 126 roots.add(urlForJar("build/simple-app.jar")); 128 roots.add(urlForFolder("build/classes")); 129 assertEquals("right EXECUTE cp for src/", roots.toString(), urlsOfCp(cp).toString()); 130 cp = ClassPath.getClassPath(specialTaskJava, ClassPath.EXECUTE); 131 assertNotNull("have some EXECUTE classpath for antsrc/", cp); 132 assertEquals("have two entries (ant.jar + build/antclasses) in " + cp, 2, cp.entries().size()); 134 assertNotNull("found Task", cp.findResource("org/apache/tools/ant/Task.class")); 135 cp = ClassPath.getClassPath(buildProperties, ClassPath.EXECUTE); 136 assertNull("have no EXECUTE classpath for build.properties", cp); 137 } 138 139 public void testBootClasspath() throws Exception { 140 ClassPath cp = ClassPath.getClassPath(myAppJava, ClassPath.BOOT); 141 assertNotNull("have some BOOT classpath for src/", cp); 142 assertEquals("and it is JDK 1.4", "1.4", specOfBootClasspath(cp)); 143 ClassPath cp2 = ClassPath.getClassPath(specialTaskJava, ClassPath.BOOT); 144 assertNotNull("have some BOOT classpath for antsrc/", cp2); 145 assertEquals("and it is JDK 1.4", "1.4", specOfBootClasspath(cp2)); 146 149 cp = ClassPath.getClassPath(buildProperties, ClassPath.BOOT); 150 assertNull("have no BOOT classpath for build.properties", cp); 151 } 152 153 private static String specOfBootClasspath(ClassPath cp) { 154 List <ClassPath.Entry> entries = cp.entries(); 155 if (entries.size() != 1) { 156 return null; 157 } 158 ClassPath.Entry entry = entries.get(0); 159 String u = entry.getURL().toExternalForm(); 160 Pattern p = Pattern.compile("jar:file:/c:/java/([0-9.]+)/jre/lib/rt\\.jar!/"); 162 Matcher m = p.matcher(u); 163 if (m.matches()) { 164 return m.group(1); 165 } else { 166 return null; 167 } 168 } 169 170 public void testGlobalPathRegistryUsage() throws Exception { 171 GlobalPathRegistry gpr = GlobalPathRegistry.getDefault(); 172 assertEquals("no BOOT classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.BOOT)); 173 assertEquals("no COMPILE classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.COMPILE)); 174 assertEquals("no EXECUTE classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.EXECUTE)); 175 assertEquals("no SOURCE classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.SOURCE)); 176 ProjectOpenedHook hook = simple.getLookup().lookup(ProjectOpenedHook.class); 177 assertNotNull("have a ProjectOpenedHook", hook); 178 Method opened = ProjectOpenedHook.class.getDeclaredMethod("projectOpened"); 179 opened.setAccessible(true); 180 opened.invoke(hook); 181 Set <ClassPath> boot = gpr.getPaths(ClassPath.BOOT); 182 Set <ClassPath> compile = gpr.getPaths(ClassPath.COMPILE); 183 Set <ClassPath> execute = gpr.getPaths(ClassPath.EXECUTE); 184 Set <ClassPath> source = gpr.getPaths(ClassPath.SOURCE); 185 Set <ClassPath> expected = new HashSet <ClassPath>(); 186 expected.add(ClassPath.getClassPath(myAppJava, ClassPath.BOOT)); 187 expected.add(ClassPath.getClassPath(specialTaskJava, ClassPath.BOOT)); 188 assertEquals("correct set of BOOT classpaths", expected, boot); 189 expected = new HashSet <ClassPath>(); 190 expected.add(ClassPath.getClassPath(myAppJava, ClassPath.COMPILE)); 191 expected.add(ClassPath.getClassPath(specialTaskJava, ClassPath.COMPILE)); 192 assertEquals("correct set of COMPILE classpaths", expected, compile); 193 expected = new HashSet <ClassPath>(); 194 expected.add(ClassPath.getClassPath(myAppJava, ClassPath.EXECUTE)); 195 expected.add(ClassPath.getClassPath(specialTaskJava, ClassPath.EXECUTE)); 196 assertEquals("correct set of EXECUTE classpaths", expected, execute); 197 expected = new HashSet <ClassPath>(); 198 expected.add(ClassPath.getClassPath(myAppJava, ClassPath.SOURCE)); 199 expected.add(ClassPath.getClassPath(specialTaskJava, ClassPath.SOURCE)); 200 assertEquals("correct set of SOURCE classpaths", expected, source); 201 Method closed = ProjectOpenedHook.class.getDeclaredMethod("projectClosed"); 202 closed.setAccessible(true); 203 closed.invoke(hook); 204 assertEquals("again no BOOT classpaths", Collections.EMPTY_SET, gpr.getPaths(ClassPath.BOOT)); 205 assertEquals("again no COMPILE classpaths", Collections.EMPTY_SET, gpr.getPaths(ClassPath.COMPILE)); 206 assertEquals("again no EXECUTE classpaths", Collections.EMPTY_SET, gpr.getPaths(ClassPath.EXECUTE)); 207 assertEquals("again no SOURCE classpaths", Collections.EMPTY_SET, gpr.getPaths(ClassPath.SOURCE)); 208 } 209 210 public void testCompileClasspathChanges() throws Exception { 211 clearWorkDir(); 212 FreeformProject simple2 = copyProject(simple); 213 FileObject myAppJava2 = simple2.getProjectDirectory().getFileObject("src/org/foo/myapp/MyApp.java"); 214 assertNotNull("found MyApp.java", myAppJava2); 215 ClassPath cp = ClassPath.getClassPath(myAppJava2, ClassPath.COMPILE); 216 assertNotNull("have some COMPILE classpath for src/", cp); 217 assertEquals("have two entries in " + cp, 2, cp.entries().size()); 218 assertEquals("have two roots in " + cp, 2, cp.getRoots().length); 219 assertNotNull("found WeakSet in " + cp, cp.findResource("org/openide/util/WeakSet.class")); 220 assertNotNull("found NullInputStream", cp.findResource("org/openide/util/io/NullInputStream.class")); 221 TestPCL l = new TestPCL(); 222 cp.addPropertyChangeListener(l); 223 EditableProperties props = new EditableProperties(); 224 FileObject buildProperties = simple2.getProjectDirectory().getFileObject("build.properties"); 225 assertNotNull("have build.properties", buildProperties); 226 InputStream is = buildProperties.getInputStream(); 227 try { 228 props.load(is); 229 } finally { 230 is.close(); 231 } 232 assertEquals("right original src.cp", "${lib.dir}/lib1.jar:${lib.dir}/lib2.jar", props.getProperty("src.cp")); 233 props.setProperty("src.cp", "${lib.dir}/lib1.jar"); 234 FileLock lock = buildProperties.lock(); 235 try { 236 final OutputStream os = buildProperties.getOutputStream(lock); 237 try { 238 props.store(os); 239 } finally { 240 ProjectManager.mutex().readAccess(new Mutex.ExceptionAction<Void >() { 242 public Void run() throws Exception { 243 os.close(); 244 return null; 245 } 246 }); 247 } 248 } finally { 249 lock.releaseLock(); 250 } 251 assertEquals("ROOTS fired", new HashSet <String >(Arrays.asList(ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS)), l.changed); 252 assertEquals("have one entry in " + cp, 1, cp.entries().size()); 253 assertEquals("have one root in " + cp, 1, cp.getRoots().length); 254 assertNotNull("found WeakSet in " + cp, cp.findResource("org/openide/util/WeakSet.class")); 255 assertNull("did not find NullInputStream", cp.findResource("org/openide/util/io/NullInputStream.class")); 256 } 257 258 public void testCompilationUnitChanges() throws Exception { 259 clearWorkDir(); 260 FreeformProject simple2 = copyProject(simple); 261 AntProjectHelper helper2 = simple2.helper(); 262 FileObject myAppJava = simple2.getProjectDirectory().getFileObject("src/org/foo/myapp/MyApp.java"); 263 assertNotNull("found MyApp.java", myAppJava); 264 ClassPath cpSrc = ClassPath.getClassPath(myAppJava, ClassPath.COMPILE); 265 assertNotNull("have some COMPILE classpath for src/", cpSrc); 266 assertEquals("have two entries in " + cpSrc, 2, cpSrc.entries().size()); 267 FileObject mySpecialTask = simple2.getProjectDirectory().getFileObject("antsrc/org/foo/ant/SpecialTask.java"); 268 assertNotNull("found SpecialTask.java", mySpecialTask); 269 ClassPath cpAnt = ClassPath.getClassPath(mySpecialTask, ClassPath.COMPILE); 270 assertNotNull("have some COMPILE classpath for antsrc/", cpAnt); 271 assertEquals("have one entry in " + cpAnt, 1, cpAnt.entries().size()); 272 TestPCL srcL = new TestPCL(); 273 cpSrc.addPropertyChangeListener(srcL); 274 TestPCL antL = new TestPCL(); 275 cpAnt.addPropertyChangeListener(antL); 276 277 ProjectOpenedHook hook = simple2.getLookup().lookup(ProjectOpenedHook.class); 278 assertNotNull("have a ProjectOpenedHook", hook); 279 GlobalPathRegistry gpr = GlobalPathRegistry.getDefault(); 280 assertEquals("no COMPILE classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.COMPILE)); 281 Method opened = ProjectOpenedHook.class.getDeclaredMethod("projectOpened"); 282 opened.setAccessible(true); 283 opened.invoke(hook); 284 Set <ClassPath> compile = gpr.getPaths(ClassPath.COMPILE); 285 Set <ClassPath> expected = new HashSet <ClassPath>(); 286 expected.add(cpSrc); 287 expected.add(cpAnt); 288 assertEquals("correct set of COMPILE classpaths", expected, compile); 289 290 AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(helper2); 291 List <JavaProjectGenerator.JavaCompilationUnit> units = JavaProjectGenerator.getJavaCompilationUnits(helper2, aux); 292 assertEquals("two compilation units", 2, units.size()); 293 JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit(); 294 cu.packageRoots = new ArrayList <String >(); 295 cu.packageRoots.add("${src.dir}"); 296 cu.packageRoots.add("${ant.src.dir}"); 297 cu.classpath = new ArrayList <JavaProjectGenerator.JavaCompilationUnit.CP>(); 298 JavaProjectGenerator.JavaCompilationUnit.CP cucp = new JavaProjectGenerator.JavaCompilationUnit.CP(); 299 cucp.mode = "compile"; 300 cucp.classpath = "${src.cp}:${ant.src.cp}"; 301 cu.classpath.add(cucp); 302 List <JavaProjectGenerator.JavaCompilationUnit> newUnits = new ArrayList <JavaProjectGenerator.JavaCompilationUnit>(); 303 newUnits.add(cu); 304 JavaProjectGenerator.putJavaCompilationUnits(helper2, aux, newUnits); 305 306 ClassPath cpSrc2 = ClassPath.getClassPath(myAppJava, ClassPath.COMPILE); 307 assertNotNull("have some COMPILE classpath for src/", cpSrc2); 308 assertEquals("have three entries in " + cpSrc2, 3, cpSrc2.entries().size()); 309 assertEquals("original classpath is empty now", 0, cpSrc.entries().size()); 310 ClassPath cpAnt2 = ClassPath.getClassPath(mySpecialTask, ClassPath.COMPILE); 311 assertNotNull("have some COMPILE classpath for antsrc/", cpAnt2); 312 assertEquals("have three entries in " + cpAnt2, 3, cpAnt2.entries().size()); 313 assertEquals("original classpath is empty now", 0, cpAnt.entries().size()); 314 assertEquals("classpath for src/ and antsrc/ are the same. " + cpAnt2 + " "+cpSrc2, cpAnt2, cpSrc2); 315 316 assertEquals("ROOTS fired", new HashSet <String >(Arrays.asList(ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS)), srcL.changed); 317 srcL.reset(); 318 assertEquals("ROOTS fired", new HashSet <String >(Arrays.asList(ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS)), antL.changed); 319 antL.reset(); 320 321 compile = gpr.getPaths(ClassPath.COMPILE); 322 expected = new HashSet <ClassPath>(); 323 expected.add(cpSrc); 324 expected.add(cpAnt); 325 expected.add(cpAnt2); 326 assertEquals("correct set of COMPILE classpaths", expected, compile); 327 328 TestPCL cpL = new TestPCL(); 329 cpAnt2.addPropertyChangeListener(cpL); 330 331 JavaProjectGenerator.putJavaCompilationUnits(helper2, aux, units); 332 ClassPath cpSrc3 = ClassPath.getClassPath(myAppJava, ClassPath.COMPILE); 333 assertNotNull("have some COMPILE classpath for src/", cpSrc3); 334 assertEquals("have two entries in " + cpSrc3, 2, cpSrc3.entries().size()); 335 ClassPath cpAnt3 = ClassPath.getClassPath(mySpecialTask, ClassPath.COMPILE); 336 assertNotNull("have some COMPILE classpath for antsrc/", cpAnt3); 337 assertEquals("have one entry in " + cpAnt3, 1, cpAnt3.entries().size()); 338 assertEquals("original classpath instance was reused", cpSrc, cpSrc3); 339 assertEquals("original classpath instance was reused", cpAnt, cpAnt3); 340 assertEquals("classpath of single compilation unit is empty now", 0, cpAnt2.entries().size()); 341 342 assertEquals("ROOTS fired", new HashSet (Arrays.asList(new String [] {ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS})), cpL.changed); 343 cpL.reset(); 344 assertEquals("ROOTS fired", new HashSet (Arrays.asList(new String [] {ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS})), srcL.changed); 345 srcL.reset(); 346 assertEquals("ROOTS fired", new HashSet (Arrays.asList(new String [] {ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS})), antL.changed); 347 antL.reset(); 348 349 compile = gpr.getPaths(ClassPath.COMPILE); 350 assertEquals("all three classpaths still there", expected, compile); 351 Method closed = ProjectOpenedHook.class.getDeclaredMethod("projectClosed"); 352 closed.setAccessible(true); 353 closed.invoke(hook); 354 compile = gpr.getPaths(ClassPath.COMPILE); 355 expected = new HashSet (); 356 assertEquals("correct set of COMPILE classpaths", expected, compile); 357 } 358 359 public void testDeadlock77015() throws Exception { 360 final CountDownLatch l = new CountDownLatch (2); 361 362 Classpaths.TESTING_LATCH = l; 363 364 new Thread () { 365 public void run() { 366 ClassPath.getClassPath(myAppJava, ClassPath.SOURCE); 367 } 368 }.start(); 369 370 Thread t = new Thread () { 371 public void run() { 372 ProjectManager.mutex().writeAccess(new Mutex.Action<Void >() { 373 public Void run() { 374 Element el = simple.getPrimaryConfigurationData(); 375 AuxiliaryConfiguration c = simple.getLookup().lookup(AuxiliaryConfiguration.class); 376 377 l.countDown(); 378 try { 379 l.await(2, TimeUnit.SECONDS); 380 } catch (InterruptedException e) { 381 e.printStackTrace(); 382 } 383 384 c.putConfigurationFragment(el.getOwnerDocument().createElementNS("http://something/X", "A"), false); 385 386 return null; 387 } 388 }); 389 } 390 }; 391 392 t.start(); 393 t.join(); 394 } 395 396 private String urlForJar(String path) throws Exception { 398 return FileUtil.getArchiveRoot(simple.helper().resolveFile(path).toURI().toURL()).toExternalForm(); 399 } 400 private String urlForFolder(String path) throws Exception { 401 String s = simple.helper().resolveFile(path).toURI().toURL().toExternalForm(); 402 if (s.endsWith("/")) { 403 return s; 404 } else { 405 return s + "/"; 406 } 407 } 408 private Set <String > urlsOfCp(ClassPath cp) { 409 Set <String > s = new TreeSet <String >(); 410 for (ClassPath.Entry entry : cp.entries()) { 411 s.add(entry.getURL().toExternalForm()); 412 } 413 return s; 414 } 415 416 public void testIncludesExcludes() throws Exception { 417 clearWorkDir(); 418 File d = getWorkDir(); 419 AntProjectHelper helper = FreeformProjectGenerator.createProject(d, d, "prj", null); 420 Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory()); 421 FileUtil.createData(new File (d, "s/relevant/included/file")); 422 FileUtil.createData(new File (d, "s/relevant/excluded/file")); 423 FileUtil.createData(new File (d, "s/ignored/file")); 424 Element data = Util.getPrimaryConfigurationData(helper); 425 Document doc = data.getOwnerDocument(); 426 Element sf = (Element ) data.insertBefore(doc.createElementNS(Util.NAMESPACE, "folders"), Util.findElement(data, "view", Util.NAMESPACE)). 427 appendChild(doc.createElementNS(Util.NAMESPACE, "source-folder")); 428 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "label")).appendChild(doc.createTextNode("Sources")); 429 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "type")).appendChild(doc.createTextNode(JavaProjectConstants.SOURCES_TYPE_JAVA)); 430 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "location")).appendChild(doc.createTextNode("s")); 431 Util.putPrimaryConfigurationData(helper, data); 432 Element jd = doc.createElementNS(JavaProjectNature.NS_JAVA_2, JavaProjectNature.EL_JAVA); 433 jd.appendChild(doc.createElementNS(JavaProjectNature.NS_JAVA_2, "compilation-unit")). 434 appendChild(doc.createElementNS(JavaProjectNature.NS_JAVA_2, "package-root")). 435 appendChild(doc.createTextNode("s")); 436 p.getLookup().lookup(AuxiliaryConfiguration.class).putConfigurationFragment(jd, true); 437 ProjectManager.getDefault().saveProject(p); 438 FileObject s = helper.resolveFileObject("s"); 439 ClassPath cp = ClassPath.getClassPath(s, ClassPath.SOURCE); 440 FileObject[] roots = cp.getRoots(); 441 assertEquals(1, roots.length); 442 assertEquals(s, roots[0]); 443 assertEquals("ignored{file} relevant{excluded{file} included{file}}", expand(cp, s)); 444 EditableProperties ep = new EditableProperties(); 446 ep.put("includes", "relevant/"); 447 ep.put("excludes", "**/excluded/"); 448 helper.putProperties("config.properties", ep); 449 data = Util.getPrimaryConfigurationData(helper); 450 doc = data.getOwnerDocument(); 451 data.getElementsByTagName("properties").item(0). 452 appendChild(doc.createElementNS(Util.NAMESPACE, "property-file")). 453 appendChild(doc.createTextNode("config.properties")); 454 Util.putPrimaryConfigurationData(helper, data); 455 ProjectManager.getDefault().saveProject(p); 456 data = Util.getPrimaryConfigurationData(helper); 457 doc = data.getOwnerDocument(); 458 sf = (Element ) data.getElementsByTagName("source-folder").item(0); 459 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "includes")). 460 appendChild(doc.createTextNode("${includes}")); 461 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "excludes")). 462 appendChild(doc.createTextNode("${excludes}")); 463 Util.putPrimaryConfigurationData(helper, data); 464 ProjectManager.getDefault().saveProject(p); 465 assertEquals("relevant{included{file}}", expand(cp, s)); 466 TestPCL l = new TestPCL(); 468 cp.addPropertyChangeListener(l); 469 ep = helper.getProperties("config.properties"); 470 ep.remove("includes"); 471 helper.putProperties("config.properties", ep); 472 ProjectManager.getDefault().saveProject(p); 473 assertEquals("ignored{file} relevant{included{file}}", expand(cp, s)); 474 assertEquals(Collections.singleton(ClassPath.PROP_INCLUDES), l.changed); 475 ep = helper.getProperties("config.properties"); 477 ep.put("includes", "relevant/included/"); 478 helper.putProperties("config.properties", ep); 479 ProjectManager.getDefault().saveProject(p); 480 assertEquals("relevant{included{file}}", expand(cp, s)); 481 } 482 private static String expand(ClassPath cp, FileObject d) { 483 SortedSet <String > subs = new TreeSet <String >(); 484 for (FileObject kid : d.getChildren()) { 485 if (!cp.contains(kid)) { 486 continue; 487 } 488 String sub = kid.getNameExt(); 489 if (kid.isFolder()) { 490 sub += '{' + expand(cp, kid) + '}'; 491 } 492 subs.add(sub); 493 } 494 StringBuilder b = new StringBuilder (); 495 for (String sub : subs) { 496 if (b.length() > 0) { 497 b.append(' '); 498 } 499 b.append(sub); 500 } 501 return b.toString(); 502 } 503 504 public void testIncludesFiredJustOnce() throws Exception { 505 clearWorkDir(); 506 File d = getWorkDir(); 507 AntProjectHelper helper = FreeformProjectGenerator.createProject(d, d, "prj", null); 508 Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory()); 509 FileObject src1 = FileUtil.createFolder(new File (d, "src1")); 510 FileObject src2 = FileUtil.createFolder(new File (d, "src2")); 511 Element data = Util.getPrimaryConfigurationData(helper); 512 Document doc = data.getOwnerDocument(); 513 data.getElementsByTagName("properties").item(0). 514 appendChild(doc.createElementNS(Util.NAMESPACE, "property-file")). 515 appendChild(doc.createTextNode("config.properties")); 516 Element folders = (Element ) data.insertBefore(doc.createElementNS(Util.NAMESPACE, "folders"), Util.findElement(data, "view", Util.NAMESPACE)); 517 Element sf = (Element ) folders.appendChild(doc.createElementNS(Util.NAMESPACE, "source-folder")); 518 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "label")).appendChild(doc.createTextNode("Sources #1")); 519 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "type")).appendChild(doc.createTextNode(JavaProjectConstants.SOURCES_TYPE_JAVA)); 520 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "location")).appendChild(doc.createTextNode("src1")); 521 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "includes")). 522 appendChild(doc.createTextNode("${includes}")); 523 sf = (Element ) folders.appendChild(doc.createElementNS(Util.NAMESPACE, "source-folder")); 524 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "label")).appendChild(doc.createTextNode("Sources #2")); 525 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "type")).appendChild(doc.createTextNode(JavaProjectConstants.SOURCES_TYPE_JAVA)); 526 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "location")).appendChild(doc.createTextNode("src2")); 527 sf.appendChild(doc.createElementNS(Util.NAMESPACE, "includes")). 528 appendChild(doc.createTextNode("${includes}")); 529 Util.putPrimaryConfigurationData(helper, data); 530 Element jd = doc.createElementNS(JavaProjectNature.NS_JAVA_2, JavaProjectNature.EL_JAVA); 531 Element cu = (Element ) jd.appendChild(doc.createElementNS(JavaProjectNature.NS_JAVA_2, "compilation-unit")); 532 cu.appendChild(doc.createElementNS(JavaProjectNature.NS_JAVA_2, "package-root")). 533 appendChild(doc.createTextNode("src1")); 534 cu.appendChild(doc.createElementNS(JavaProjectNature.NS_JAVA_2, "package-root")). 535 appendChild(doc.createTextNode("src2")); 536 p.getLookup().lookup(AuxiliaryConfiguration.class).putConfigurationFragment(jd, true); 537 ProjectManager.getDefault().saveProject(p); 538 ClassPath cp = ClassPath.getClassPath(src1, ClassPath.SOURCE); 539 FileObject[] roots = cp.getRoots(); 540 assertEquals(2, roots.length); 541 assertEquals(src1, roots[0]); 542 assertEquals(src2, roots[1]); 543 ClassPath.Entry cpe2 = cp.entries().get(1); 544 assertEquals(src2.getURL(), cpe2.getURL()); 545 assertTrue(cpe2.includes("stuff/")); 546 assertTrue(cpe2.includes("whatever/")); 547 class L implements PropertyChangeListener { 548 int cnt; 549 public void propertyChange(PropertyChangeEvent e) { 550 if (ClassPath.PROP_INCLUDES.equals(e.getPropertyName())) { 551 cnt++; 552 } 553 } 554 } 555 L l = new L(); 556 cp.addPropertyChangeListener(l); 557 EditableProperties ep = new EditableProperties(); 558 ep.put("includes", "whatever/"); 559 helper.putProperties("config.properties", ep); 560 ProjectManager.getDefault().saveProject(p); 561 assertEquals(1, l.cnt); 562 assertFalse(cpe2.includes("stuff/")); 563 assertTrue(cpe2.includes("whatever/")); 564 ep.setProperty("includes", "whateverelse/"); 565 helper.putProperties("config.properties", ep); 566 ProjectManager.getDefault().saveProject(p); 567 assertEquals(2, l.cnt); 568 assertFalse(cpe2.includes("stuff/")); 569 assertFalse(cpe2.includes("whatever/")); 570 ep.remove("includes"); 571 helper.putProperties("config.properties", ep); 572 ProjectManager.getDefault().saveProject(p); 573 assertEquals(3, l.cnt); 574 assertTrue(cpe2.includes("stuff/")); 575 assertTrue(cpe2.includes("whatever/")); 576 } 577 578 } 579 | Popular Tags |