1 19 package org.netbeans.modules.java.j2seproject.classpath; 20 21 import java.beans.Customizer ; 22 import java.beans.PropertyChangeListener ; 23 import java.io.OutputStream ; 24 import java.net.URL ; 25 import java.util.Arrays ; 26 import java.util.Collections ; 27 import java.util.jar.JarFile ; 28 import java.util.jar.JarOutputStream ; 29 import java.util.zip.ZipEntry ; 30 import java.util.zip.ZipOutputStream ; 31 import junit.framework.*; 32 import java.io.File ; 33 import java.io.IOException ; 34 import java.net.URI ; 35 import java.util.List ; 36 import org.netbeans.api.java.classpath.ClassPath; 37 import org.netbeans.api.java.project.JavaProjectConstants; 38 import org.netbeans.api.java.project.classpath.ProjectClassPathModifier; 39 import org.netbeans.api.project.FileOwnerQuery; 40 import org.netbeans.api.project.Project; 41 import org.netbeans.api.project.ProjectManager; 42 import org.netbeans.api.project.SourceGroup; 43 import org.netbeans.api.project.Sources; 44 import org.netbeans.api.project.TestUtil; 45 import org.netbeans.api.project.ant.AntArtifact; 46 import org.netbeans.api.project.libraries.Library; 47 import org.netbeans.api.project.libraries.LibraryManager; 48 import org.netbeans.junit.NbTestCase; 49 import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator; 50 import org.netbeans.modules.java.j2seproject.UpdateHelper; 51 import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties; 52 import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; 53 import org.netbeans.spi.project.ant.AntArtifactProvider; 54 import org.netbeans.spi.project.libraries.LibraryImplementation; 55 import org.netbeans.spi.project.libraries.LibraryProvider; 56 import org.netbeans.spi.project.libraries.LibraryTypeProvider; 57 import org.netbeans.spi.project.support.ant.AntProjectHelper; 58 import org.netbeans.spi.project.support.ant.EditableProperties; 59 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 60 import org.netbeans.spi.project.support.ant.PropertyUtils; 61 import org.netbeans.spi.project.support.ant.ReferenceHelper; 62 import org.openide.ErrorManager; 63 import org.openide.filesystems.FileLock; 64 import org.openide.filesystems.FileObject; 65 import org.openide.filesystems.FileUtil; 66 import org.openide.modules.SpecificationVersion; 67 import org.openide.util.Lookup; 68 import org.openide.util.Mutex; 69 import org.openide.util.MutexException; 70 71 75 public class J2SEProjectClassPathModifierTest extends NbTestCase { 76 77 private FileObject scratch; 78 private AntProjectHelper helper; 79 private PropertyEvaluator eval; 80 private FileObject src; 81 private FileObject test; 82 private Project prj; 83 84 public J2SEProjectClassPathModifierTest(String testName) { 85 super(testName); 86 } 87 88 protected void setUp() throws Exception { 89 super.setUp(); 90 TestUtil.setLookup(new Object [] { 91 new org.netbeans.modules.java.j2seproject.J2SEProjectType(), 92 new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(), 93 new TestLibraryProvider (), 94 }); 95 this.scratch = TestUtil.makeScratchDir(this); 96 FileObject projdir = scratch.createFolder("proj"); J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); this.helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"proj",null,null); this.eval = this.helper.getStandardPropertyEvaluator(); 100 J2SEProjectGenerator.setDefaultSourceLevel(null); 101 this.prj = FileOwnerQuery.getOwner(projdir); 102 assertNotNull (this.prj); 103 this.src = projdir.getFileObject("src"); 104 assertNotNull (this.src); 105 this.test = projdir.getFileObject("test"); 106 assertNotNull (this.test); 107 108 } 109 110 protected void tearDown() throws Exception { 111 } 112 113 public void testAddRemoveRoot () throws Exception { 114 final FileObject rootFolder = this.scratch.createFolder("Root"); 115 final FileObject jarFile = this.scratch.createData("archive","jar"); 116 FileLock lck = jarFile.lock(); 117 try { 118 ZipOutputStream jf = new ZipOutputStream (jarFile.getOutputStream(lck)); 119 try { 120 jf.putNextEntry(new ZipEntry ("Test.properties")); 121 }finally { 122 jf.close(); 123 } 124 } finally { 125 lck.releaseLock(); 126 } 127 final FileObject jarRoot = FileUtil.getArchiveRoot(jarFile); 128 ProjectClassPathModifier.addRoots (new URL [] {rootFolder.getURL()}, this.src, ClassPath.COMPILE); 129 String cp = this.eval.getProperty("javac.classpath"); 130 assertNotNull (cp); 131 String [] cpRoots = PropertyUtils.tokenizePath (cp); 132 assertNotNull (cpRoots); 133 assertEquals(1,cpRoots.length); 134 assertEquals(rootFolder,this.helper.resolveFileObject(cpRoots[0])); 135 ProjectClassPathModifier.removeRoots (new URL [] {rootFolder.getURL()},this.src, ClassPath.COMPILE); 136 cp = this.eval.getProperty("javac.classpath"); 137 assertNotNull (cp); 138 cpRoots = PropertyUtils.tokenizePath (cp); 139 assertNotNull (cpRoots); 140 assertEquals(0,cpRoots.length); 141 ProjectClassPathModifier.addRoots(new URL [] {jarRoot.getURL()},this.test,ClassPath.EXECUTE); 142 cp = this.eval.getProperty("run.test.classpath"); 143 assertNotNull (cp); 144 cpRoots = PropertyUtils.tokenizePath (cp); 145 assertNotNull (cpRoots); 146 assertEquals(4,cpRoots.length); 147 assertEquals(this.helper.resolveFileObject(cpRoots[3]),jarFile); 148 } 149 150 public void testAddRemoveArtifact () throws Exception { 151 FileObject projdir = scratch.createFolder("libPrj"); J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); AntProjectHelper helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"libProj",null,null); J2SEProjectGenerator.setDefaultSourceLevel(null); 155 Project libPrj = FileOwnerQuery.getOwner(projdir); 156 assertNotNull (this.prj); 157 AntArtifactProvider ap = (AntArtifactProvider) libPrj.getLookup().lookup(AntArtifactProvider.class); 158 AntArtifact[] aas = ap.getBuildArtifacts(); 159 AntArtifact output = null; 160 for (int i=0; i<aas.length; i++) { 161 if (JavaProjectConstants.ARTIFACT_TYPE_JAR.equals(aas[i].getType())) { 162 output = aas[i]; 163 break; 164 } 165 } 166 assertNotNull (output); 167 ProjectClassPathModifier.addAntArtifacts(new AntArtifact[] {output}, new URI [] {output.getArtifactLocations()[0]}, this.src, ClassPath.COMPILE); 168 String cp = this.eval.getProperty("javac.classpath"); 169 assertNotNull (cp); 170 String [] cpRoots = PropertyUtils.tokenizePath (cp); 171 assertNotNull (cpRoots); 172 assertEquals(1,cpRoots.length); 173 URI projectURI = URI.create(output.getProject().getProjectDirectory().getURL().toExternalForm()); 174 URI expected = projectURI.resolve(output.getArtifactLocations()[0]); 175 assertEquals(expected,this.helper.resolveFile(cpRoots[0]).toURI()); 176 ProjectClassPathModifier.removeAntArtifacts(new AntArtifact[] {output}, new URI [] {output.getArtifactLocations()[0]},this.src, ClassPath.COMPILE); 177 cp = this.eval.getProperty("javac.classpath"); 178 assertNotNull (cp); 179 cpRoots = PropertyUtils.tokenizePath (cp); 180 assertNotNull (cpRoots); 181 assertEquals(0,cpRoots.length); 182 } 183 184 public void testAddRemoveLibrary () throws Exception { 185 LibraryProvider lp = (LibraryProvider) Lookup.getDefault().lookup(LibraryProvider.class); 186 assertNotNull (lp); 187 LibraryImplementation[] impls = lp.getLibraries(); 188 assertNotNull (impls); 189 assertEquals(1,impls.length); 190 FileObject libRoot = this.scratch.createFolder("libRoot"); 191 impls[0].setContent("classpath",Collections.singletonList(libRoot.getURL())); 192 Library[] libs =LibraryManager.getDefault().getLibraries(); 193 assertNotNull (libs); 194 assertEquals(1,libs.length); 195 ProjectClassPathModifier.addLibraries(libs, this.src, ClassPath.COMPILE); 196 String cp = this.eval.getProperty("javac.classpath"); 197 assertNotNull (cp); 198 String [] cpRoots = PropertyUtils.tokenizePath (cp); 199 assertNotNull (cpRoots); 200 assertEquals(1,cpRoots.length); 201 assertEquals("${libs.Test.classpath}",cpRoots[0]); ProjectClassPathModifier.removeLibraries(libs,this.src, ClassPath.COMPILE); 203 cp = this.eval.getProperty("javac.classpath"); 204 assertNotNull (cp); 205 cpRoots = PropertyUtils.tokenizePath (cp); 206 assertNotNull (cpRoots); 207 assertEquals(0,cpRoots.length); 208 } 209 210 public void testClassPathExtenderCompatibility () throws Exception { 211 final FileObject rootFolder = this.scratch.createFolder("Root"); 212 final FileObject jarFile = this.scratch.createData("archive","jar"); 213 FileLock lck = jarFile.lock(); 214 try { 215 ZipOutputStream jf = new ZipOutputStream (jarFile.getOutputStream(lck)); 216 try { 217 jf.putNextEntry(new ZipEntry ("Test.properties")); 218 }finally { 219 jf.close(); 220 } 221 } finally { 222 lck.releaseLock(); 223 } 224 final FileObject jarRoot = FileUtil.getArchiveRoot(jarFile); 225 ProjectClassPathExtender extender = (ProjectClassPathExtender) this.prj.getLookup().lookup(ProjectClassPathExtender.class); 226 assertNotNull (extender); 227 extender.addArchiveFile(rootFolder); 228 extender.addArchiveFile(jarFile); 229 String cp = this.eval.getProperty("javac.classpath"); 230 assertNotNull (cp); 231 String [] cpRoots = PropertyUtils.tokenizePath (cp); 232 assertNotNull (cpRoots); 233 assertEquals(2,cpRoots.length); 234 assertEquals(rootFolder,this.helper.resolveFileObject(cpRoots[0])); 235 assertEquals(jarFile,this.helper.resolveFileObject(cpRoots[1])); 236 } 237 238 239 private static class TestLibraryProvider implements LibraryProvider { 240 241 private LibraryImplementation[] libs; 242 243 public void removePropertyChangeListener(PropertyChangeListener listener) { 244 } 245 246 public void addPropertyChangeListener(PropertyChangeListener listener) { 247 } 248 249 public LibraryImplementation[] getLibraries() { 250 if (libs == null) { 251 this.libs = new LibraryImplementation[] { new TestLibrary ("Test")}; 252 } 253 return this.libs; 254 } 255 256 } 257 258 private static class TestLibrary implements LibraryImplementation { 259 260 private String name; 261 private List cp = Collections.EMPTY_LIST; 262 private List src = Collections.EMPTY_LIST; 263 private List jdoc = Collections.EMPTY_LIST; 264 265 public TestLibrary (String name) { 266 this.name = name; 267 } 268 269 public void setName(String name) { 270 } 271 272 public void setLocalizingBundle(String resourceName) { 273 } 274 275 public void setDescription(String text) { 276 } 277 278 public List getContent(String volumeType) throws IllegalArgumentException { 279 if ("classpath".equals(volumeType)) { 280 return this.cp; 281 } 282 else if ("src".equals(volumeType)) { 283 return this.src; 284 } 285 else if ("jdoc".equals(volumeType)) { 286 return this.jdoc; 287 } 288 throw new IllegalArgumentException (); 289 } 290 291 public void removePropertyChangeListener(PropertyChangeListener l) { 292 } 293 294 public void addPropertyChangeListener(PropertyChangeListener l) { 295 } 296 297 public void setContent(String volumeType, List path) throws IllegalArgumentException { 298 if ("classpath".equals(volumeType)) { 299 this.cp = path; 300 } 301 else if ("src".equals(volumeType)) { 302 this.src = path; 303 } 304 else if ("jdoc".equals(volumeType)) { 305 this.jdoc = path; 306 } 307 else { 308 throw new IllegalArgumentException (); 309 } 310 } 311 312 public String getType() { 313 return "j2se"; 314 } 315 316 public String getName() { 317 return this.name; 318 } 319 320 public String getLocalizingBundle() { 321 return null; 322 } 323 324 public String getDescription() { 325 return null; 326 } 327 328 } 329 330 } 331 | Popular Tags |