Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2013 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2013 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.modules.java.api.common.ui; |
43 |
|
44 |
import org.netbeans.modules.java.api.common.ui.PlatformFilter; |
45 |
import java.beans.PropertyChangeListener; |
46 |
import java.io.File; |
47 |
import java.net.URL; |
48 |
import java.util.ArrayList; |
49 |
import java.util.Collection; |
50 |
import java.util.Collections; |
51 |
import java.util.List; |
52 |
import java.util.Map; |
53 |
import org.netbeans.api.java.classpath.ClassPath; |
54 |
import org.netbeans.api.java.platform.JavaPlatform; |
55 |
import org.netbeans.api.java.platform.Specification; |
56 |
import org.netbeans.api.project.Project; |
57 |
import org.netbeans.junit.NbTestCase; |
58 |
import org.netbeans.modules.java.platform.JavaPlatformProvider; |
59 |
import org.netbeans.spi.java.classpath.support.ClassPathSupport; |
60 |
import org.netbeans.spi.project.support.ant.AntBasedTestUtil; |
61 |
import org.openide.filesystems.FileObject; |
62 |
import org.openide.filesystems.FileUtil; |
63 |
import org.openide.modules.SpecificationVersion; |
64 |
import org.openide.util.Lookup; |
65 |
import org.openide.util.lookup.Lookups; |
66 |
import org.openide.util.test.MockLookup; |
67 |
|
68 |
|
69 |
public class PlatformFilterTest extends NbTestCase { |
70 |
|
71 |
private static final String PRESERVE = "ToBePreserved"; |
72 |
private static final String FILTEROUT = "ToBeFilteredOut"; |
73 |
private MockProject prj1; |
74 |
private MockProject prj2; |
75 |
private JavaPlatform platform1; |
76 |
private JavaPlatform platform2; |
77 |
private JavaPlatform platform3; |
78 |
|
79 |
public PlatformFilterTest(final String name) { |
80 |
super(name); |
81 |
} |
82 |
|
83 |
@Override |
84 |
protected void setUp() throws Exception { |
85 |
super.setUp(); |
86 |
platform1 = new TestPlatform("DefaultPlatform" + PRESERVE); |
87 |
platform2 = new TestPlatform("Platform" + PRESERVE); |
88 |
platform3 = new TestPlatform("Platform" + FILTEROUT); |
89 |
MockLookup.setInstances( |
90 |
AntBasedTestUtil.testAntBasedProjectType(), |
91 |
new PlatformFilterTest.TestPlatformProvider( |
92 |
platform1, |
93 |
platform2, |
94 |
platform3 |
95 |
)); |
96 |
clearWorkDir(); |
97 |
final FileObject projectDir1 = FileUtil.createFolder( |
98 |
new File(getWorkDir(),"project1")); //NOI18N |
99 |
prj1 = new PlatformFilterTest.MockProject(projectDir1); |
100 |
final FileObject projectDir2 = FileUtil.createFolder( |
101 |
new File(getWorkDir(),"project2")); //NOI18N |
102 |
prj2 = new PlatformFilterTest.MockProjectExtended(projectDir2); |
103 |
} |
104 |
|
105 |
public void testMockProject1() { |
106 |
Collection<JavaPlatform> platforms = prj1.getAvailablePlatforms(); |
107 |
assertEquals(platforms.size(), 3); |
108 |
assertTrue(platforms.contains(platform1)); |
109 |
assertTrue(platforms.contains(platform2)); |
110 |
assertTrue(platforms.contains(platform3)); |
111 |
} |
112 |
|
113 |
public void testMockProject2() { |
114 |
Collection<JavaPlatform> platforms = prj2.getAvailablePlatforms(); |
115 |
assertEquals(platforms.size(), 2); |
116 |
assertTrue(platforms.contains(platform1)); |
117 |
assertTrue(platforms.contains(platform2)); |
118 |
assertFalse(platforms.contains(platform3)); |
119 |
} |
120 |
|
121 |
private static class MockProject implements Project { |
122 |
|
123 |
private final FileObject projectDir; |
124 |
|
125 |
MockProject(final FileObject projectDir) { |
126 |
assert projectDir != null; |
127 |
this.projectDir = projectDir; |
128 |
} |
129 |
|
130 |
public Collection<JavaPlatform> getAvailablePlatforms() { |
131 |
List<JavaPlatform> result = new ArrayList<>(); |
132 |
final Collection<? extends PlatformFilter> filters = getLookup().lookupAll(PlatformFilter.class); |
133 |
JavaPlatform[] platforms = TestPlatformProvider.getDefault().getInstalledPlatforms(); |
134 |
if(platforms != null) { |
135 |
for(int i = 0; i < platforms.length; i++) { |
136 |
boolean accepted = true; |
137 |
if(filters != null) { |
138 |
for(PlatformFilter filter : filters) { |
139 |
if(!filter.accept(platforms[i])) { |
140 |
accepted = false; |
141 |
break; |
142 |
} |
143 |
} |
144 |
} |
145 |
if(accepted) { |
146 |
result.add(platforms[i]); |
147 |
} |
148 |
} |
149 |
} |
150 |
return result; |
151 |
} |
152 |
|
153 |
@Override |
154 |
public FileObject getProjectDirectory() { |
155 |
return projectDir; |
156 |
} |
157 |
|
158 |
@Override |
159 |
public Lookup getLookup() { |
160 |
return Lookups.fixed(this); |
161 |
} |
162 |
|
163 |
} |
164 |
|
165 |
private static class MockProjectExtended extends MockProject { |
166 |
|
167 |
MockProjectExtended(final FileObject projectDir) { |
168 |
super(projectDir); |
169 |
} |
170 |
|
171 |
@Override |
172 |
public Lookup getLookup() { |
173 |
return Lookups.fixed(this, getFilter()); |
174 |
} |
175 |
|
176 |
private PlatformFilter getFilter() { |
177 |
return new PlatformFilter() { |
178 |
@Override |
179 |
public boolean accept(JavaPlatform platform) { |
180 |
return !platform.getDisplayName().endsWith(FILTEROUT); |
181 |
} |
182 |
}; |
183 |
} |
184 |
|
185 |
} |
186 |
|
187 |
|
188 |
private static class TestPlatformProvider implements JavaPlatformProvider { |
189 |
|
190 |
private JavaPlatform[] platforms; |
191 |
|
192 |
public static JavaPlatformProvider getDefault() { |
193 |
return MockLookup.getDefault().lookup(JavaPlatformProvider.class); |
194 |
} |
195 |
|
196 |
public TestPlatformProvider(JavaPlatform... platforms) { |
197 |
if(platforms.length == 0) { |
198 |
this.platforms = new JavaPlatform[]{new TestPlatform("DefaultPlatform")}; |
199 |
} else { |
200 |
this.platforms = platforms; |
201 |
} |
202 |
} |
203 |
|
204 |
@Override |
205 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
206 |
} |
207 |
|
208 |
@Override |
209 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
210 |
} |
211 |
|
212 |
@Override |
213 |
public JavaPlatform[] getInstalledPlatforms() { |
214 |
return platforms; |
215 |
} |
216 |
|
217 |
@Override |
218 |
public JavaPlatform getDefaultPlatform() { |
219 |
if (platforms.length == 0) { |
220 |
platforms = new JavaPlatform[]{new TestPlatform("DefaultPlatform")}; |
221 |
} |
222 |
return platforms[0]; |
223 |
} |
224 |
} |
225 |
|
226 |
private static class TestPlatform extends JavaPlatform { |
227 |
|
228 |
private String name; |
229 |
|
230 |
public TestPlatform(String name) { |
231 |
this.name = name; |
232 |
} |
233 |
|
234 |
@Override |
235 |
public FileObject findTool(String toolName) { |
236 |
return null; |
237 |
} |
238 |
|
239 |
@Override |
240 |
public String getVendor() { |
241 |
return "me"; |
242 |
} |
243 |
|
244 |
@Override |
245 |
public ClassPath getStandardLibraries() { |
246 |
return ClassPathSupport.createClassPath(new URL[0]); |
247 |
} |
248 |
|
249 |
@Override |
250 |
public Specification getSpecification() { |
251 |
return new Specification("j2se", new SpecificationVersion("1.7")); |
252 |
} |
253 |
|
254 |
@Override |
255 |
public ClassPath getSourceFolders() { |
256 |
return null; |
257 |
} |
258 |
|
259 |
@Override |
260 |
public Map<String, String> getProperties() { |
261 |
return Collections.singletonMap("platform.ant.name", getDisplayName()); |
262 |
} |
263 |
|
264 |
@Override |
265 |
public List<URL> getJavadocFolders() { |
266 |
return null; |
267 |
} |
268 |
|
269 |
@Override |
270 |
public Collection<FileObject> getInstallFolders() { |
271 |
return null; |
272 |
} |
273 |
|
274 |
@Override |
275 |
public String getDisplayName() { |
276 |
return name == null ? "DefaultTestPlatform" : name; |
277 |
} |
278 |
|
279 |
@Override |
280 |
public ClassPath getBootstrapLibraries() { |
281 |
return ClassPathSupport.createClassPath(new URL[0]); |
282 |
} |
283 |
} |
284 |
} |