Line 0
Link Here
|
|
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2012 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 2012 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.spi.java.project.support; |
43 |
|
44 |
import java.beans.PropertyChangeListener; |
45 |
import java.net.URL; |
46 |
import java.util.Collection; |
47 |
import java.util.Collections; |
48 |
import java.util.HashMap; |
49 |
import java.util.List; |
50 |
import java.util.Map; |
51 |
import org.netbeans.api.annotations.common.NonNull; |
52 |
import org.netbeans.api.java.classpath.ClassPath; |
53 |
import org.netbeans.api.java.platform.JavaPlatform; |
54 |
import org.netbeans.api.java.platform.JavaPlatformManager; |
55 |
import org.netbeans.api.java.platform.Specification; |
56 |
import org.netbeans.junit.MockServices; |
57 |
import org.netbeans.junit.NbTestCase; |
58 |
import org.netbeans.modules.java.platform.JavaPlatformProvider; |
59 |
import org.openide.filesystems.FileObject; |
60 |
import org.openide.modules.SpecificationVersion; |
61 |
|
62 |
|
63 |
/** |
64 |
* |
65 |
* @author Tomas Zezula |
66 |
*/ |
67 |
public class PreferredProjectPlatformTest extends NbTestCase { |
68 |
|
69 |
private static final String J2SE = "j2se"; //NOI18N |
70 |
private static final String J2ME = "j2me"; //NOI18N |
71 |
|
72 |
public PreferredProjectPlatformTest(final String name) { |
73 |
super(name); |
74 |
} |
75 |
|
76 |
@Override |
77 |
protected void setUp() throws Exception { |
78 |
super.setUp(); |
79 |
MockServices.setServices(MockJavaPlatformProvider.class); |
80 |
} |
81 |
|
82 |
|
83 |
public void testPreferredPlatforms() { |
84 |
final JavaPlatform defaultPlatform = JavaPlatformManager.getDefault().getDefaultPlatform(); |
85 |
final JavaPlatform[] javaPlatforms = JavaPlatformManager.getDefault().getInstalledPlatforms(); |
86 |
final JavaPlatform[] nonDefaultJ2SEPlatforms = new JavaPlatform[javaPlatforms.length - 2]; |
87 |
JavaPlatform j2mePlatform = null; |
88 |
for (int i = 0, j = 0; i < javaPlatforms.length; i++ ) { |
89 |
if (!javaPlatforms[i].equals(defaultPlatform) && |
90 |
J2SE.equals(javaPlatforms[i].getSpecification().getName())) { |
91 |
nonDefaultJ2SEPlatforms[j++] = javaPlatforms[i]; |
92 |
} |
93 |
if (J2ME.equals(javaPlatforms[i].getSpecification().getName())) { |
94 |
j2mePlatform = javaPlatforms[i]; |
95 |
} |
96 |
} |
97 |
assertEquals(defaultPlatform, PreferredProjectPlatform.getPreferredPlatform(J2SE)); |
98 |
PreferredProjectPlatform.setPreferredPlatform(nonDefaultJ2SEPlatforms[0]); |
99 |
assertEquals(nonDefaultJ2SEPlatforms[0], PreferredProjectPlatform.getPreferredPlatform(J2SE)); |
100 |
PreferredProjectPlatform.setPreferredPlatform(nonDefaultJ2SEPlatforms[1]); |
101 |
assertEquals(nonDefaultJ2SEPlatforms[1], PreferredProjectPlatform.getPreferredPlatform(J2SE)); |
102 |
PreferredProjectPlatform.setPreferredPlatform(defaultPlatform); |
103 |
assertEquals(defaultPlatform, PreferredProjectPlatform.getPreferredPlatform(J2SE)); |
104 |
assertNull(PreferredProjectPlatform.getPreferredPlatform(J2ME)); |
105 |
PreferredProjectPlatform.setPreferredPlatform(j2mePlatform); |
106 |
assertEquals(j2mePlatform, PreferredProjectPlatform.getPreferredPlatform(J2ME)); |
107 |
} |
108 |
|
109 |
|
110 |
public static final class MockJavaPlatformProvider implements JavaPlatformProvider { |
111 |
|
112 |
private final JavaPlatform[] platforms; |
113 |
|
114 |
public MockJavaPlatformProvider() { |
115 |
this.platforms = new JavaPlatform[4]; |
116 |
this.platforms[0] = new MockJavaPlatform("default_platform", J2SE); //NOI18N |
117 |
this.platforms[1] = new MockJavaPlatform("nondefault_platform_1", J2SE); //NOI18N |
118 |
this.platforms[2] = new MockJavaPlatform("nondefault_platform_2", J2SE); //NOI18N |
119 |
this.platforms[3] = new MockJavaPlatform("j2me_platform", J2ME); //NOI18N |
120 |
} |
121 |
|
122 |
@Override |
123 |
public JavaPlatform[] getInstalledPlatforms() { |
124 |
return platforms; |
125 |
} |
126 |
|
127 |
@Override |
128 |
public JavaPlatform getDefaultPlatform() { |
129 |
return platforms[0]; |
130 |
} |
131 |
|
132 |
@Override |
133 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
134 |
} |
135 |
|
136 |
@Override |
137 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
138 |
} |
139 |
|
140 |
} |
141 |
|
142 |
private static final class MockJavaPlatform extends JavaPlatform { |
143 |
|
144 |
private final String displayName; |
145 |
private final String platformType; |
146 |
private final Map<String,String> props; |
147 |
|
148 |
MockJavaPlatform( |
149 |
@NonNull final String name, |
150 |
@NonNull final String platformType) { |
151 |
super(); |
152 |
this.displayName = name; |
153 |
this.props = new HashMap<String, String>(); |
154 |
this.props.put("platform.ant.name",name); //NOI18N |
155 |
this.platformType = platformType; |
156 |
} |
157 |
|
158 |
@Override |
159 |
public String getDisplayName() { |
160 |
return displayName; |
161 |
} |
162 |
|
163 |
@Override |
164 |
public Map<String, String> getProperties() { |
165 |
return props; |
166 |
} |
167 |
|
168 |
@Override |
169 |
public ClassPath getBootstrapLibraries() { |
170 |
return ClassPath.EMPTY; |
171 |
} |
172 |
|
173 |
@Override |
174 |
public ClassPath getStandardLibraries() { |
175 |
return ClassPath.EMPTY; |
176 |
} |
177 |
|
178 |
@Override |
179 |
public String getVendor() { |
180 |
return "me"; //NOI18N |
181 |
} |
182 |
|
183 |
@Override |
184 |
public Specification getSpecification() { |
185 |
return new Specification(platformType, new SpecificationVersion("1.6")); //NOI18N |
186 |
} |
187 |
|
188 |
@Override |
189 |
public Collection<FileObject> getInstallFolders() { |
190 |
return Collections.<FileObject>emptySet(); |
191 |
} |
192 |
|
193 |
@Override |
194 |
public FileObject findTool(String toolName) { |
195 |
return null; |
196 |
} |
197 |
|
198 |
@Override |
199 |
public ClassPath getSourceFolders() { |
200 |
return ClassPath.EMPTY; |
201 |
} |
202 |
|
203 |
@Override |
204 |
public List<URL> getJavadocFolders() { |
205 |
return Collections.<URL>emptyList(); |
206 |
} |
207 |
|
208 |
@Override |
209 |
public String toString() { |
210 |
return displayName; |
211 |
} |
212 |
|
213 |
} |
214 |
} |