Added
Link Here
|
1 |
/* |
2 |
* Sun Public License Notice |
3 |
* |
4 |
* The contents of this file are subject to the Sun Public License |
5 |
* Version 1.0 (the "License"). You may not use this file except in |
6 |
* compliance with the License. A copy of the License is available at |
7 |
* http://www.sun.com/ |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
13 |
|
14 |
package org.netbeans.spi.java.project.classpath.support; |
15 |
|
16 |
|
17 |
import java.io.File; |
18 |
import java.io.IOException; |
19 |
import java.net.URL; |
20 |
import java.util.ArrayList; |
21 |
import java.util.Arrays; |
22 |
import java.util.Collection; |
23 |
import java.util.Collections; |
24 |
import java.util.List; |
25 |
import java.util.Map; |
26 |
import java.util.Properties; |
27 |
import org.netbeans.api.java.classpath.ClassPath; |
28 |
import org.netbeans.api.project.Project; |
29 |
import org.netbeans.api.project.ProjectManager; |
30 |
import org.netbeans.junit.NbTestCase; |
31 |
import org.netbeans.spi.java.classpath.ClassPathFactory; |
32 |
import org.netbeans.spi.java.classpath.ClassPathImplementation; |
33 |
import org.netbeans.spi.java.classpath.support.ClassPathSupport; |
34 |
import org.netbeans.spi.project.support.ant.AntBasedTestUtil; |
35 |
import org.netbeans.spi.project.support.ant.PropertyEvaluator; |
36 |
import org.netbeans.spi.project.support.ant.PropertyUtils; |
37 |
import org.openide.filesystems.FileObject; |
38 |
import org.netbeans.api.project.TestUtil; |
39 |
import org.netbeans.spi.project.support.ant.AntProjectHelper; |
40 |
import org.netbeans.spi.project.support.ant.EditableProperties; |
41 |
import org.netbeans.spi.project.support.ant.ProjectGenerator; |
42 |
import org.openide.filesystems.FileUtil; |
43 |
import org.openide.filesystems.Repository; |
44 |
import org.openide.util.Lookup; |
45 |
|
46 |
/** |
47 |
* Tests for {@link ProjectClassPathImplementation}. |
48 |
* @author Tomas Zezula |
49 |
*/ |
50 |
public class ProjectClassPathImplementationTest extends NbTestCase { |
51 |
|
52 |
private static final String PROP_NAME_1 = "classpath1"; //NOI18N |
53 |
private static final String PROP_NAME_2 = "classpath2"; //NOI18N |
54 |
|
55 |
public ProjectClassPathImplementationTest(String testName) { |
56 |
super(testName); |
57 |
} |
58 |
|
59 |
private FileObject scratch; |
60 |
private FileObject projdir; |
61 |
private FileObject[] cpRoots1; |
62 |
private FileObject[] cpRoots2; |
63 |
private AntProjectHelper helper; |
64 |
private PropertyEvaluator evaluator; |
65 |
|
66 |
protected void setUp() throws Exception { |
67 |
super.setUp(); |
68 |
TestUtil.setLookup(new Object[] { |
69 |
new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(), |
70 |
AntBasedTestUtil.testAntBasedProjectType(), |
71 |
}, ProjectClassPathImplementation.class.getClassLoader()); |
72 |
} |
73 |
|
74 |
protected void tearDown() throws Exception { |
75 |
scratch = null; |
76 |
projdir = null; |
77 |
cpRoots1 = null; |
78 |
cpRoots2 = null; |
79 |
helper = null; |
80 |
evaluator = null; |
81 |
TestUtil.setLookup(Lookup.EMPTY); |
82 |
super.tearDown(); |
83 |
} |
84 |
|
85 |
|
86 |
private void prepareProject () throws IOException { |
87 |
scratch = TestUtil.makeScratchDir(this); |
88 |
projdir = scratch.createFolder("proj"); //NOI18N |
89 |
cpRoots1 = new FileObject[2]; |
90 |
cpRoots1[0] = scratch.createFolder("cpRoot1"); //NOI18N |
91 |
cpRoots1[1] = scratch.createFolder("cpRoot2"); //NOI18N |
92 |
cpRoots2[0] = scratch.createFolder("cpRoot3"); //NOI18N |
93 |
cpRoots2[1] = scratch.createFolder("cpRoot4"); //NOI18N |
94 |
helper = ProjectGenerator.createProject(projdir, "test"); //NOI18N |
95 |
evaluator = helper.getStandardPropertyEvaluator(); |
96 |
setClassPath(new String[] {}, new FileObject[][] {cpRoots1, cpRoots2}); |
97 |
} |
98 |
|
99 |
public void testBootClassPathImplementation () throws Exception { |
100 |
prepareProject(); |
101 |
ClassPathImplementation cpImpl = ProjectClassPathSupport.createPropertyBasedClassPathImplementation(helper, evaluator, new String[] {PROP_NAME_1, PROP_NAME_2}); |
102 |
ClassPath cp = ClassPathFactory.createClassPath(cpImpl); |
103 |
FileObject[] fo = cp.getRoots(); |
104 |
List expected = new ArrayList (); |
105 |
expected.addAll(Arrays.asList(cpRoots1)); |
106 |
expected.addAll(Arrays.asList(cpRoots2)); |
107 |
assertEquals ("Wrong ClassPath roots",expected, Arrays.asList(fo)); //NOI18N |
108 |
cpRoots1 = new FileObject[] {cpRoots1[0]}; |
109 |
setClassPath(new String[] {PROP_NAME_1}, new FileObject[][]{cpRoots1}); |
110 |
fo = cp.getRoots(); |
111 |
expected = new ArrayList (); |
112 |
expected.addAll(Arrays.asList(cpRoots1)); |
113 |
expected.addAll(Arrays.asList(cpRoots2)); |
114 |
assertEquals ("Wrong ClassPath roots",expected, Arrays.asList(fo)); //NOI18N |
115 |
cpRoots2 = new FileObject[] {cpRoots2[0]}; |
116 |
setClassPath(new String[] {PROP_NAME_2}, new FileObject[][]{cpRoots2}); |
117 |
fo = cp.getRoots(); |
118 |
expected = new ArrayList (); |
119 |
expected.addAll(Arrays.asList(cpRoots1)); |
120 |
expected.addAll(Arrays.asList(cpRoots2)); |
121 |
assertEquals ("Wrong ClassPath roots",expected, Arrays.asList(fo)); //NOI18N |
122 |
} |
123 |
|
124 |
|
125 |
|
126 |
private void setClassPath (String[] propNames, FileObject[][] cpRoots) { |
127 |
EditableProperties props = helper.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); |
128 |
for (int i=0; i< propNames.length; i++) { |
129 |
props.setProperty (propNames[i],toPath(cpRoots[i])); |
130 |
} |
131 |
helper.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, props); |
132 |
} |
133 |
|
134 |
|
135 |
private static String toPath (FileObject[] cpRoots) { |
136 |
StringBuffer result = new StringBuffer (); |
137 |
for (int i=0; i<cpRoots.length; i++) { |
138 |
if (i>0) { |
139 |
result.append(':'); //NOI18N |
140 |
} |
141 |
File f = FileUtil.toFile (cpRoots[i]); |
142 |
result.append (f.getAbsolutePath()); |
143 |
} |
144 |
return result.toString(); |
145 |
} |
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
} |