Added
Link Here
|
1 |
/* |
2 |
* J2SEProjectClassPathModifierTest.java |
3 |
* JUnit based test |
4 |
* |
5 |
* Created on May 2, 2006, 9:45 PM |
6 |
*/ |
7 |
|
8 |
package org.netbeans.modules.java.j2seproject.classpath; |
9 |
|
10 |
import java.beans.Customizer; |
11 |
import java.beans.PropertyChangeListener; |
12 |
import java.io.OutputStream; |
13 |
import java.net.URL; |
14 |
import java.util.Arrays; |
15 |
import java.util.Collections; |
16 |
import java.util.jar.JarFile; |
17 |
import java.util.jar.JarOutputStream; |
18 |
import java.util.zip.ZipEntry; |
19 |
import java.util.zip.ZipOutputStream; |
20 |
import junit.framework.*; |
21 |
import java.io.File; |
22 |
import java.io.IOException; |
23 |
import java.net.URI; |
24 |
import java.util.List; |
25 |
import org.netbeans.api.java.classpath.ClassPath; |
26 |
import org.netbeans.api.java.project.JavaProjectConstants; |
27 |
import org.netbeans.api.project.FileOwnerQuery; |
28 |
import org.netbeans.api.project.Project; |
29 |
import org.netbeans.api.project.ProjectManager; |
30 |
import org.netbeans.api.project.SourceGroup; |
31 |
import org.netbeans.api.project.Sources; |
32 |
import org.netbeans.api.project.TestUtil; |
33 |
import org.netbeans.api.project.ant.AntArtifact; |
34 |
import org.netbeans.api.project.libraries.Library; |
35 |
import org.netbeans.api.project.libraries.LibraryManager; |
36 |
import org.netbeans.junit.NbTestCase; |
37 |
import org.netbeans.modules.java.j2seproject.J2SEProjectGenerator; |
38 |
import org.netbeans.modules.java.j2seproject.UpdateHelper; |
39 |
import org.netbeans.modules.java.j2seproject.ui.customizer.J2SEProjectProperties; |
40 |
import org.netbeans.spi.java.project.classpath.ProjectClassPathExtender; |
41 |
import org.netbeans.spi.java.project.classpath.ProjectClassPathModifier; |
42 |
import org.netbeans.spi.project.ant.AntArtifactProvider; |
43 |
import org.netbeans.spi.project.libraries.LibraryImplementation; |
44 |
import org.netbeans.spi.project.libraries.LibraryProvider; |
45 |
import org.netbeans.spi.project.libraries.LibraryTypeProvider; |
46 |
import org.netbeans.spi.project.support.ant.AntProjectHelper; |
47 |
import org.netbeans.spi.project.support.ant.EditableProperties; |
48 |
import org.netbeans.spi.project.support.ant.PropertyEvaluator; |
49 |
import org.netbeans.spi.project.support.ant.PropertyUtils; |
50 |
import org.netbeans.spi.project.support.ant.ReferenceHelper; |
51 |
import org.openide.ErrorManager; |
52 |
import org.openide.filesystems.FileLock; |
53 |
import org.openide.filesystems.FileObject; |
54 |
import org.openide.filesystems.FileUtil; |
55 |
import org.openide.modules.SpecificationVersion; |
56 |
import org.openide.util.Lookup; |
57 |
import org.openide.util.Mutex; |
58 |
import org.openide.util.MutexException; |
59 |
|
60 |
/** |
61 |
* |
62 |
* @author tom |
63 |
*/ |
64 |
public class J2SEProjectClassPathModifierTest extends NbTestCase { |
65 |
|
66 |
private FileObject scratch; |
67 |
private AntProjectHelper helper; |
68 |
private PropertyEvaluator eval; |
69 |
private Project prj; |
70 |
|
71 |
public J2SEProjectClassPathModifierTest(String testName) { |
72 |
super(testName); |
73 |
} |
74 |
|
75 |
protected void setUp() throws Exception { |
76 |
super.setUp(); |
77 |
TestUtil.setLookup(new Object[] { |
78 |
new org.netbeans.modules.java.j2seproject.J2SEProjectType(), |
79 |
new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(), |
80 |
new TestLibraryProvider (), |
81 |
}); |
82 |
this.scratch = TestUtil.makeScratchDir(this); |
83 |
FileObject projdir = scratch.createFolder("proj"); //NOI18N |
84 |
J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); //NOI18N |
85 |
this.helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"proj",null,null); //NOI18N |
86 |
this.eval = this.helper.getStandardPropertyEvaluator(); |
87 |
J2SEProjectGenerator.setDefaultSourceLevel(null); |
88 |
this.prj = FileOwnerQuery.getOwner(projdir); |
89 |
assertNotNull (this.prj); |
90 |
} |
91 |
|
92 |
protected void tearDown() throws Exception { |
93 |
} |
94 |
|
95 |
public void testAddRemoveRoot () throws Exception { |
96 |
final FileObject rootFolder = this.scratch.createFolder("Root"); |
97 |
final FileObject jarFile = this.scratch.createData("archive","jar"); |
98 |
FileLock lck = jarFile.lock(); |
99 |
try { |
100 |
ZipOutputStream jf = new ZipOutputStream (jarFile.getOutputStream(lck)); |
101 |
try { |
102 |
jf.putNextEntry(new ZipEntry("Test.properties")); |
103 |
}finally { |
104 |
jf.close(); |
105 |
} |
106 |
} finally { |
107 |
lck.releaseLock(); |
108 |
} |
109 |
final FileObject jarRoot = FileUtil.getArchiveRoot(jarFile); |
110 |
ProjectClassPathModifier modifier = (ProjectClassPathModifier) this.prj.getLookup().lookup(ProjectClassPathModifier.class); |
111 |
assertNotNull (modifier); |
112 |
Sources sources = (Sources) this.prj.getLookup().lookup(Sources.class); |
113 |
assertNotNull (sources); |
114 |
SourceGroup[] sgs = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); |
115 |
assertNotNull (sgs); |
116 |
assertEquals(2,sgs.length); |
117 |
SourceGroup srcSg=null, testSg = null; |
118 |
for (int i=0; i<sgs.length; i++) { |
119 |
if ("src".equals(sgs[i].getRootFolder().getName())) { |
120 |
srcSg = sgs[i]; |
121 |
} |
122 |
if ("test".equals(sgs[i].getRootFolder().getName())) { |
123 |
testSg = sgs[i]; |
124 |
} |
125 |
} |
126 |
assertNotNull (srcSg); |
127 |
assertNotNull (testSg); |
128 |
modifier.addRoot (rootFolder.getURL(),srcSg,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 |
modifier.removeRoot (rootFolder.getURL(),srcSg,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 |
modifier.addRoot(jarRoot.getURL(),testSg,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"); //NOI18N |
152 |
J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); //NOI18N |
153 |
AntProjectHelper helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"libProj",null,null); //NOI18N |
154 |
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 modifier = (ProjectClassPathModifier) this.prj.getLookup().lookup(ProjectClassPathModifier.class); |
168 |
assertNotNull (modifier); |
169 |
Sources sources = (Sources) this.prj.getLookup().lookup(Sources.class); |
170 |
assertNotNull (sources); |
171 |
SourceGroup[] sgs = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); |
172 |
assertNotNull (sgs); |
173 |
assertEquals(2,sgs.length); |
174 |
SourceGroup srcSg=null; |
175 |
for (int i=0; i<sgs.length; i++) { |
176 |
if ("src".equals(sgs[i].getRootFolder().getName())) { |
177 |
srcSg = sgs[i]; |
178 |
break; |
179 |
} |
180 |
} |
181 |
assertNotNull (srcSg); |
182 |
modifier.addAntArtifact(output,output.getArtifactLocations()[0],srcSg,ClassPath.COMPILE); |
183 |
String cp = this.eval.getProperty("javac.classpath"); |
184 |
assertNotNull (cp); |
185 |
String[] cpRoots = PropertyUtils.tokenizePath (cp); |
186 |
assertNotNull (cpRoots); |
187 |
assertEquals(1,cpRoots.length); |
188 |
URI projectURI = URI.create(output.getProject().getProjectDirectory().getURL().toExternalForm()); |
189 |
URI expected = projectURI.resolve(output.getArtifactLocations()[0]); |
190 |
assertEquals(expected,this.helper.resolveFile(cpRoots[0]).toURI()); |
191 |
modifier.removeAntArtifact(output,output.getArtifactLocations()[0],srcSg,ClassPath.COMPILE); |
192 |
cp = this.eval.getProperty("javac.classpath"); |
193 |
assertNotNull (cp); |
194 |
cpRoots = PropertyUtils.tokenizePath (cp); |
195 |
assertNotNull (cpRoots); |
196 |
assertEquals(0,cpRoots.length); |
197 |
} |
198 |
|
199 |
public void testAddRemoveLibrary () throws Exception { |
200 |
LibraryProvider lp = (LibraryProvider) Lookup.getDefault().lookup(LibraryProvider.class); |
201 |
assertNotNull (lp); |
202 |
LibraryImplementation[] impls = lp.getLibraries(); |
203 |
assertNotNull (impls); |
204 |
assertEquals(1,impls.length); |
205 |
FileObject libRoot = this.scratch.createFolder("libRoot"); |
206 |
impls[0].setContent("classpath",Collections.singletonList(libRoot.getURL())); |
207 |
Library[] libs =LibraryManager.getDefault().getLibraries(); |
208 |
assertNotNull (libs); |
209 |
assertEquals(1,libs.length); |
210 |
ProjectClassPathModifier modifier = (ProjectClassPathModifier) this.prj.getLookup().lookup(ProjectClassPathModifier.class); |
211 |
assertNotNull (modifier); |
212 |
Sources sources = (Sources) this.prj.getLookup().lookup(Sources.class); |
213 |
assertNotNull (sources); |
214 |
SourceGroup[] sgs = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); |
215 |
assertNotNull (sgs); |
216 |
assertEquals(2,sgs.length); |
217 |
SourceGroup srcSg=null; |
218 |
for (int i=0; i<sgs.length; i++) { |
219 |
if ("src".equals(sgs[i].getRootFolder().getName())) { |
220 |
srcSg = sgs[i]; |
221 |
break; |
222 |
} |
223 |
} |
224 |
assertNotNull (srcSg); |
225 |
modifier.addLibrary(libs[0],srcSg,ClassPath.COMPILE); |
226 |
String cp = this.eval.getProperty("javac.classpath"); |
227 |
assertNotNull (cp); |
228 |
String[] cpRoots = PropertyUtils.tokenizePath (cp); |
229 |
assertNotNull (cpRoots); |
230 |
assertEquals(1,cpRoots.length); |
231 |
assertEquals("${libs.Test.classpath}",cpRoots[0]); //There is no build.properties filled, the libraries are not resolved |
232 |
modifier.removeLibrary(libs[0],srcSg,ClassPath.COMPILE); |
233 |
cp = this.eval.getProperty("javac.classpath"); |
234 |
assertNotNull (cp); |
235 |
cpRoots = PropertyUtils.tokenizePath (cp); |
236 |
assertNotNull (cpRoots); |
237 |
assertEquals(0,cpRoots.length); |
238 |
} |
239 |
|
240 |
public void testClassPathExtenderCompatibility () throws Exception { |
241 |
final FileObject rootFolder = this.scratch.createFolder("Root"); |
242 |
final FileObject jarFile = this.scratch.createData("archive","jar"); |
243 |
FileLock lck = jarFile.lock(); |
244 |
try { |
245 |
ZipOutputStream jf = new ZipOutputStream (jarFile.getOutputStream(lck)); |
246 |
try { |
247 |
jf.putNextEntry(new ZipEntry("Test.properties")); |
248 |
}finally { |
249 |
jf.close(); |
250 |
} |
251 |
} finally { |
252 |
lck.releaseLock(); |
253 |
} |
254 |
final FileObject jarRoot = FileUtil.getArchiveRoot(jarFile); |
255 |
ProjectClassPathExtender extender = (ProjectClassPathExtender) this.prj.getLookup().lookup(ProjectClassPathExtender.class); |
256 |
assertNotNull (extender); |
257 |
extender.addArchiveFile(rootFolder); |
258 |
extender.addArchiveFile(jarFile); |
259 |
String cp = this.eval.getProperty("javac.classpath"); |
260 |
assertNotNull (cp); |
261 |
String[] cpRoots = PropertyUtils.tokenizePath (cp); |
262 |
assertNotNull (cpRoots); |
263 |
assertEquals(2,cpRoots.length); |
264 |
assertEquals(rootFolder,this.helper.resolveFileObject(cpRoots[0])); |
265 |
assertEquals(jarFile,this.helper.resolveFileObject(cpRoots[1])); |
266 |
} |
267 |
|
268 |
|
269 |
private static class TestLibraryProvider implements LibraryProvider { |
270 |
|
271 |
private LibraryImplementation[] libs; |
272 |
|
273 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
274 |
} |
275 |
|
276 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
277 |
} |
278 |
|
279 |
public LibraryImplementation[] getLibraries() { |
280 |
if (libs == null) { |
281 |
this.libs = new LibraryImplementation[] { new TestLibrary ("Test")}; |
282 |
} |
283 |
return this.libs; |
284 |
} |
285 |
|
286 |
} |
287 |
|
288 |
private static class TestLibrary implements LibraryImplementation { |
289 |
|
290 |
private String name; |
291 |
private List cp = Collections.EMPTY_LIST; |
292 |
private List src = Collections.EMPTY_LIST; |
293 |
private List jdoc = Collections.EMPTY_LIST; |
294 |
|
295 |
public TestLibrary (String name) { |
296 |
this.name = name; |
297 |
} |
298 |
|
299 |
public void setName(String name) { |
300 |
} |
301 |
|
302 |
public void setLocalizingBundle(String resourceName) { |
303 |
} |
304 |
|
305 |
public void setDescription(String text) { |
306 |
} |
307 |
|
308 |
public List getContent(String volumeType) throws IllegalArgumentException { |
309 |
if ("classpath".equals(volumeType)) { |
310 |
return this.cp; |
311 |
} |
312 |
else if ("src".equals(volumeType)) { |
313 |
return this.src; |
314 |
} |
315 |
else if ("jdoc".equals(volumeType)) { |
316 |
return this.jdoc; |
317 |
} |
318 |
throw new IllegalArgumentException (); |
319 |
} |
320 |
|
321 |
public void removePropertyChangeListener(PropertyChangeListener l) { |
322 |
} |
323 |
|
324 |
public void addPropertyChangeListener(PropertyChangeListener l) { |
325 |
} |
326 |
|
327 |
public void setContent(String volumeType, List path) throws IllegalArgumentException { |
328 |
if ("classpath".equals(volumeType)) { |
329 |
this.cp = path; |
330 |
} |
331 |
else if ("src".equals(volumeType)) { |
332 |
this.src = path; |
333 |
} |
334 |
else if ("jdoc".equals(volumeType)) { |
335 |
this.jdoc = path; |
336 |
} |
337 |
else { |
338 |
throw new IllegalArgumentException (); |
339 |
} |
340 |
} |
341 |
|
342 |
public String getType() { |
343 |
return "j2se"; |
344 |
} |
345 |
|
346 |
public String getName() { |
347 |
return this.name; |
348 |
} |
349 |
|
350 |
public String getLocalizingBundle() { |
351 |
return null; |
352 |
} |
353 |
|
354 |
public String getDescription() { |
355 |
return null; |
356 |
} |
357 |
|
358 |
} |
359 |
|
360 |
} |