1 19 20 package org.netbeans.api.java.platform; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 import org.netbeans.api.java.classpath.ClassPath; 33 import org.netbeans.api.project.TestUtil; 34 import org.netbeans.junit.NbTestCase; 35 import org.netbeans.modules.java.platform.JavaPlatformProvider; 36 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 37 import org.openide.filesystems.FileObject; 38 import org.openide.modules.SpecificationVersion; 39 import org.openide.util.Lookup; 40 import org.openide.util.lookup.Lookups; 41 42 45 public class JavaPlatformManagerTest extends NbTestCase { 46 47 public JavaPlatformManagerTest(String testName) { 48 super(testName); 49 } 50 51 protected void setUp() throws Exception { 52 super.setUp(); 53 TestUtil.setLookup(Lookups.fixed(new Object [] {new TestJavaPlatformProvider()})); 54 } 55 56 public void testGetDefaultPlatform() { 57 TestUtil.setLookup(new Object [0]); JavaPlatformManager manager = JavaPlatformManager.getDefault (); 59 assertNotNull (manager); 60 JavaPlatform p = manager.getDefaultPlatform(); 61 assertNotNull("some platform registered by default", p); 62 ClassPath cp = p.getBootstrapLibraries(); 63 assertNotNull("is 1.5+ JRE: " + cp, cp.findResource("java/lang/StringBuilder.class")); 64 assertFalse(p.getInstallFolders().isEmpty()); 65 assertNotNull(p.getDisplayName()); 67 assertNotNull(p.getSystemProperties().get("java.home")); 68 assertNotNull(p.getSourceFolders()); 69 assertNotNull(p.getJavadocFolders()); 70 cp = p.getStandardLibraries(); 71 assertNotNull("contains test CP: " + cp, cp.findResource(JavaPlatformManager.class.getName().replace('.', '/') + ".class")); 72 assertNotNull(p.getProperties()); 73 assertNotNull(p.getVendor()); 74 Specification spec = p.getSpecification(); 75 assertNotNull(spec); 76 assertNotNull(spec.getName()); 77 assertTrue(spec.getVersion().compareTo(new SpecificationVersion("1.5")) >= 0); 79 } 80 81 public void testGetInstalledPlatforms() { 82 JavaPlatformManager manager = JavaPlatformManager.getDefault(); 83 assertNotNull (manager); 84 TestJavaPlatformProvider provider = TestJavaPlatformProvider.getDefault (); 85 assertNotNull (provider); 86 JavaPlatform[] platforms = manager.getInstalledPlatforms(); 87 assertNotNull (platforms); 88 assertTrue (platforms.length == 0); 89 JavaPlatform platform = new TestJavaPlatform ("Testing Platform", 90 new Specification("j2se", new SpecificationVersion ("1.5"))); 91 provider.addPlatform (platform); 92 platforms = manager.getInstalledPlatforms(); 93 assertNotNull (platforms); 94 assertTrue (platforms.length == 1); 95 assertTrue (platforms[0]==platform); 96 provider.removePlatform(platform); 97 platforms = manager.getInstalledPlatforms(); 98 assertNotNull (platforms); 99 assertTrue (platforms.length == 0); 100 } 101 102 public void testGetPlatforms() { 103 JavaPlatformManager manager = JavaPlatformManager.getDefault(); 104 assertNotNull (manager); 105 TestJavaPlatformProvider provider = TestJavaPlatformProvider.getDefault (); 106 assertNotNull (provider); 107 JavaPlatform p1 = new TestJavaPlatform ("P1", new Specification("P1",new SpecificationVersion ("1.4"))); 108 JavaPlatform p2 = new TestJavaPlatform ("P2", new Specification("P2",new SpecificationVersion ("1.4"))); 109 JavaPlatform p3 = new TestJavaPlatform ("P3", new Specification("P3",new SpecificationVersion ("1.4"))); 110 JavaPlatform p4 = new TestJavaPlatform ("P4", new Specification("P4",new SpecificationVersion ("1.5"))); 111 JavaPlatform p5 = new TestJavaPlatform ("P5", new Specification("CDC",new SpecificationVersion("1.0"), new Profile[] { 112 new Profile ("PersonalJava", new SpecificationVersion ("1.0")), 113 new Profile ("RMI", new SpecificationVersion ("1.0")), 114 })); 115 JavaPlatform p6 = new TestJavaPlatform ("P6", new Specification("CDC", new SpecificationVersion("1.0"))); 116 JavaPlatform p7 = new TestJavaPlatform ("P7", new Specification("CDC",new SpecificationVersion("1.0"), new Profile[] { 117 new Profile ("PersonalJava", new SpecificationVersion ("1.0")) 118 })); 119 JavaPlatform p8 = new TestJavaPlatform ("P8", new Specification("CDC",new SpecificationVersion("1.0"), new Profile[] { 120 new Profile ("PersonalJava", new SpecificationVersion ("1.0")), 121 new Profile ("JNI", new SpecificationVersion ("1.0")), 122 new Profile ("GIOP", new SpecificationVersion ("1.0")) 123 })); 124 provider.addPlatform (p1); 125 provider.addPlatform (p2); 126 provider.addPlatform (p3); 127 provider.addPlatform (p4); 128 provider.addPlatform (p5); 129 provider.addPlatform (p6); 130 provider.addPlatform (p7); 131 provider.addPlatform (p8); 132 assertNotNull (manager.getInstalledPlatforms()); 133 assertTrue (manager.getInstalledPlatforms().length == 8); 134 JavaPlatform[] r = manager.getPlatforms("P1",null); 135 assertNotNull (r); 136 assertTrue (r.length == 1); 137 assertTrue (r[0] == p1); 138 r = manager.getPlatforms("P1", new Specification ("P1", new SpecificationVersion("1.4"))); 139 assertNotNull (r); 140 assertTrue (r.length == 1); 141 assertTrue (r[0] == p1); 142 r = manager.getPlatforms("P1", new Specification ("P1", null)); 143 assertNotNull (r); 144 assertTrue (r.length == 1); 145 assertTrue (r[0] == p1); 146 r = manager.getPlatforms(null, new Specification ("P1", null)); 147 assertNotNull (r); 148 assertTrue (r.length == 1); 149 assertTrue (r[0] == p1); 150 r = manager.getPlatforms(null, new Specification ("P1", new SpecificationVersion("1.4"))); 151 assertNotNull (r); 152 assertTrue (r.length == 1); 153 assertTrue (r[0] == p1); 154 r = manager.getPlatforms(null, new Specification (null,new SpecificationVersion("1.4"))); 155 assertNotNull (r); 156 assertTrue (r.length == 3); 157 assertEquivalent (r, new JavaPlatform[]{p1,p2,p3}); 158 r = manager.getPlatforms (null, new Specification ("CDC", new SpecificationVersion("1.0"))); assertNotNull (r); 161 assertTrue (r.length == 4); 162 assertEquivalent (r, new JavaPlatform[] {p5, p6, p7, p8}); 163 r = manager.getPlatforms (null, new Specification ("CDC", null, new Profile[] { new Profile ("PersonalJava",null), 165 new Profile ("RMI",null) 166 })); 167 assertNotNull (r); 168 assertTrue (r.length == 1); 169 assertTrue (r[0]==p5); 170 r = manager.getPlatforms (null, new Specification ("CDC",null,new Profile[] { new Profile (null,null) 172 })); 173 assertNotNull (r); 174 assertTrue (r.length == 3); 175 assertEquivalent (r, new JavaPlatform[] {p5,p7,p8}); 176 r = manager.getPlatforms (null, new Specification ("CDC",null,new Profile[] { new Profile ("PersonalJava",null), 178 new Profile (null,null) 179 })); 180 assertNotNull (r); 181 assertTrue (r.length == 3); 182 assertEquivalent (r, new JavaPlatform[] {p5,p7,p8}); 183 r = manager.getPlatforms (null, new Specification ("CDC",null,new Profile[] { new Profile ("PersonalJava",null) 185 })); 186 assertNotNull (r); 187 assertTrue (r.length == 1); 188 assertTrue (r[0] == p7); 189 r = manager.getPlatforms (null, new Specification ("CDC",null,new Profile[] { new Profile ("RMI",null), 191 new Profile (null,null) 192 })); 193 assertNotNull (r); 194 assertTrue (r.length == 1); 195 assertTrue (r[0] == p5); 196 r = manager.getPlatforms (null, new Specification ("CDC",null,new Profile[] { new Profile ("Gateway",null), 198 new Profile (null, null) 199 })); 200 assertNotNull (r); 201 assertTrue (r.length == 0); 202 r = manager.getPlatforms(null,null); assertNotNull(r); 204 assertTrue (r.length == 8); 205 assertEquivalent (r, new JavaPlatform[] {p1,p2,p3,p4,p5,p6,p7, p8}); 206 207 provider.removePlatform (p1); 209 provider.removePlatform (p2); 210 provider.removePlatform (p3); 211 provider.removePlatform (p4); 212 provider.removePlatform (p5); 213 provider.removePlatform (p6); 214 provider.removePlatform (p7); 215 provider.removePlatform (p8); 216 assertTrue (manager.getInstalledPlatforms().length == 0); 217 } 218 219 220 private static void assertEquivalent (JavaPlatform[] a, JavaPlatform[] b) { 221 assertTrue (a.length == b.length); 222 List l = Arrays.asList(a); 223 for (int i=0; i < b.length; i++) { 224 if (!l.contains(b[i])) { 225 assertTrue (false); 226 } 227 } 228 } 229 230 private static class TestJavaPlatform extends JavaPlatform { 231 232 private String id; 233 private Specification spec; 234 235 public TestJavaPlatform (String id, Specification spec) { 236 this.id = id; 237 this.spec = spec; 238 } 239 240 public ClassPath getBootstrapLibraries() { 241 return ClassPathSupport.createClassPath(new URL [0]); 242 } 243 244 public String getDisplayName() { 245 return this.id; 246 } 247 248 public Collection getInstallFolders() { 249 return Collections.EMPTY_LIST; 250 } 251 252 public List getJavadocFolders() { 253 return Collections.EMPTY_LIST; 254 } 255 256 public Map getProperties() { 257 return Collections.EMPTY_MAP; 258 } 259 260 public ClassPath getSourceFolders() { 261 return ClassPathSupport.createClassPath(Collections.EMPTY_LIST); 262 } 263 264 public Specification getSpecification() { 265 return this.spec; 266 } 267 268 public ClassPath getStandardLibraries() { 269 return ClassPathSupport.createClassPath(new URL [0]); 270 } 271 272 public String getVendor() { 273 return "Me"; 274 } 275 276 public FileObject findTool(String name) { 277 return null; 278 } 279 280 } 281 282 public static class TestJavaPlatformProvider implements JavaPlatformProvider { 283 284 private ArrayList listeners = new ArrayList (); 285 private List platforms = new ArrayList (); 286 287 288 static TestJavaPlatformProvider getDefault () { 289 return (TestJavaPlatformProvider) Lookup.getDefault ().lookup (TestJavaPlatformProvider.class); 290 } 291 292 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) { 293 assertNotNull (listener); 294 this.listeners.add (listener); 295 } 296 297 public void removePropertyChangeListener(PropertyChangeListener listener) { 298 assertNotNull (listener); 299 this.listeners.remove (listener); 300 } 301 302 public JavaPlatform[] getInstalledPlatforms() { 303 return (JavaPlatform[]) this.platforms.toArray (new JavaPlatform[platforms.size()]); 304 } 305 306 void addPlatform (JavaPlatform platform) { 307 this.platforms.add (platform); 308 this.firePropertyChange (); 309 } 310 311 void removePlatform (JavaPlatform platform) { 312 this.platforms.remove (platform); 313 this.firePropertyChange (); 314 } 315 316 private void firePropertyChange () { 317 Iterator it; 318 synchronized (this) { 319 it = ((Collection )this.listeners.clone()).iterator(); 320 } 321 PropertyChangeEvent event = new PropertyChangeEvent (this, PROP_INSTALLED_PLATFORMS, null, null); 322 while (it.hasNext()) { 323 ((PropertyChangeListener )it.next()).propertyChange(event); 324 } 325 } 326 327 public JavaPlatform getDefaultPlatform() { 328 if (platforms.size()>0) 329 return (JavaPlatform) platforms.get(0); 330 else 331 return null; 332 } 333 334 } 335 336 } 337 | Popular Tags |