1 19 20 package org.netbeans.modules.apisupport.project.universe; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.File ; 25 import java.net.URL ; 26 import java.util.Arrays ; 27 import java.util.Collections ; 28 import java.util.HashSet ; 29 import java.util.Set ; 30 import org.netbeans.modules.apisupport.project.TestBase; 31 import org.netbeans.modules.apisupport.project.Util; 32 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 33 import org.netbeans.spi.project.support.ant.PropertyProvider; 34 import org.netbeans.spi.project.support.ant.PropertyUtils; 35 import org.openide.filesystems.FileUtil; 36 37 41 public class NbPlatformTest extends TestBase { 42 43 private static final String ARTIFICIAL_DIR = "artificial/"; 44 45 public NbPlatformTest(String name) { 46 super(name); 47 } 48 49 public void testBasicUsage() throws Exception { 50 Set platforms = NbPlatform.getPlatforms(); 51 assertEquals("have two platforms", 2, platforms.size()); 52 NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT); 53 assertNotNull("have default platform", def); 54 assertEquals("can use a special method call for that", def, NbPlatform.getDefaultPlatform()); 55 NbPlatform custom = NbPlatform.getPlatformByID("custom"); 56 assertNotNull("have custom platform", custom); 57 assertNull("no such bogus platform", NbPlatform.getPlatformByID("bogus")); 58 assertEquals(new HashSet (Arrays.asList(new NbPlatform[] {def, custom})), platforms); 59 assertEquals("right default platform by dest dir", def, NbPlatform.getPlatformByDestDir(destDirF)); 60 assertEquals("right custom platform by dest dir", custom, NbPlatform.getPlatformByDestDir(resolveEEPFile("suite3/nbplatform"))); 61 assertFalse("bogus platform is not valid", NbPlatform.getPlatformByDestDir(file("nbbuild")).isValid()); 62 assertFalse("bogus platform is not default", NbPlatform.getPlatformByDestDir(file("nbbuild")).isDefault()); 63 assertEquals("right dest dir for default platform", destDirF, def.getDestDir()); 64 assertEquals("right dest dir for custom platform", resolveEEPFile("suite3/nbplatform"), custom.getDestDir()); 65 assertEquals("right name for default platform", NbPlatform.PLATFORM_ID_DEFAULT, def.getID()); 66 assertEquals("right name for custom platform", "custom", custom.getID()); 67 assertEquals("right sources for default platform", new HashSet (Arrays.asList(new URL [] { 68 Util.urlForDir(nbCVSRootFile()), 69 Util.urlForDir(resolveEEPFile("suite2")), 70 })), new HashSet (Arrays.asList(def.getSourceRoots()))); 71 assertEquals("right Javadoc for default platform", new HashSet (Arrays.asList(new URL [] { 72 Util.urlForJar(apisZip), 73 })), new HashSet (Arrays.asList(def.getJavadocRoots()))); 74 assertEquals("no sources for custom platform", Collections.EMPTY_SET, new HashSet (Arrays.asList(custom.getSourceRoots()))); 75 assertEquals("no Javadoc for custom platform", Collections.EMPTY_SET, new HashSet (Arrays.asList(custom.getJavadocRoots()))); 76 } 77 78 public void testGetSourceLocationOfModule() throws Exception { 79 NbPlatform p = NbPlatform.getDefaultPlatform(); 80 assertEquals("Right source location for beans.jar", file("beans"), p.getSourceLocationOfModule(file("nbbuild/netbeans/" + TestBase.CLUSTER_IDE + "/modules/org-netbeans-modules-beans.jar"))); 81 } 82 83 public void testIsPlatformDirectory() throws Exception { 84 assertTrue("nbbuild/netbeans is a platform", NbPlatform.isPlatformDirectory(destDirF)); 85 assertFalse(TestBase.CLUSTER_PLATFORM + " is not a platform", NbPlatform.isPlatformDirectory(file("nbbuild/netbeans/" + TestBase.CLUSTER_PLATFORM))); 86 assertFalse("nbbuild is not a platform", NbPlatform.isPlatformDirectory(file("nbbuild"))); 87 assertFalse("nbbuild/build.xml is not a platform", NbPlatform.isPlatformDirectory(file("nbbuild/build.xml"))); 88 assertFalse("nonexistent dir is not a platform", NbPlatform.isPlatformDirectory(file("nonexistent"))); 89 } 90 91 public void testComputeDisplayName() throws Exception { 92 String name = NbPlatform.computeDisplayName(destDirF); 93 assertTrue("name '" + name + "' mentions 'NetBeans IDE'", name.indexOf("NetBeans IDE") != -1); 95 } 96 97 public void testAddSourceRoot() throws Exception { 98 NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT); 99 doAddSourceRoot(def, ARTIFICIAL_DIR); 100 } 101 102 public void testRemoveRoots() throws Exception { 103 NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT); 104 URL url = doAddSourceRoot(def, ARTIFICIAL_DIR); 105 assertTrue("adding of new source root", Arrays.asList( def.getSourceRoots()).toString().indexOf(ARTIFICIAL_DIR) != -1); 107 def.removeSourceRoots(new URL [] {url}); 108 assertFalse("removing of new source root", Arrays.asList( def.getSourceRoots()).toString().indexOf(ARTIFICIAL_DIR) != -1); 110 } 111 112 public void testMovingSourceRoots() throws Exception { 113 NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT); 114 URL url = doAddSourceRoot(def, ARTIFICIAL_DIR); 115 116 def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT); 117 URL [] srcRoot = def.getSourceRoots(); 118 assertEquals("new url should be the last one", url, srcRoot[srcRoot.length - 1]); 119 def.moveSourceRootUp(srcRoot.length - 1); 120 srcRoot = def.getSourceRoots(); 121 assertEquals("new url should be moved up", url, srcRoot[srcRoot.length - 2]); 122 123 URL first = srcRoot[0]; 124 def.moveSourceRootDown(0); 125 srcRoot = def.getSourceRoots(); 126 assertEquals("first url should be moved to the second position", first, srcRoot[1]); 127 } 128 129 public void testAddJavadoc() throws Exception { 130 NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT); 131 doAddJavadocRoot(def, ARTIFICIAL_DIR); 132 } 133 134 public void testRemoveJavadocs() throws Exception { 135 NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT); 136 URL url = doAddJavadocRoot(def, ARTIFICIAL_DIR); 137 assertTrue("adding of new javadoc", Arrays.asList( def.getJavadocRoots()).toString().indexOf(ARTIFICIAL_DIR) != -1); 139 def.removeJavadocRoots(new URL [] {url}); 140 assertFalse("removing of new javadoc", Arrays.asList( def.getJavadocRoots()).toString().indexOf(ARTIFICIAL_DIR) != -1); 142 } 143 144 public void testMovingJavadocRoots() throws Exception { 145 NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT); 146 URL url = doAddJavadocRoot(def, ARTIFICIAL_DIR); 147 148 def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT); 149 URL [] jdRoot = def.getJavadocRoots(); 150 assertEquals("new url should be the last one", url, jdRoot[jdRoot.length - 1]); 151 def.moveJavadocRootUp(jdRoot.length - 1); 152 jdRoot = def.getJavadocRoots(); 153 assertEquals("new url should be moved up", url, jdRoot[jdRoot.length - 2]); 154 155 URL first = jdRoot[0]; 156 def.moveJavadocRootDown(0); 157 jdRoot = def.getJavadocRoots(); 158 assertEquals("first url should be moved to the second position", first, jdRoot[1]); 159 } 160 161 162 private URL doAddSourceRoot(NbPlatform plaf, String urlInWorkDir) throws Exception { 163 URL urlToAdd = new URL (getWorkDir().toURI().toURL(), urlInWorkDir); 164 plaf.addSourceRoot(urlToAdd); 165 NbPlatform.reset(); 167 plaf = NbPlatform.getPlatformByID(plaf.getID()); 168 assertTrue("adding of new sourceroot", Arrays.asList( 169 plaf.getSourceRoots()).toString().indexOf(urlInWorkDir) != -1); 170 return urlToAdd; 171 } 172 173 private URL doAddJavadocRoot(NbPlatform plaf, String urlInWorkDir) throws Exception { 174 URL urlToAdd = new URL (getWorkDir().toURI().toURL(), urlInWorkDir); 175 plaf.addJavadocRoot(urlToAdd); 176 NbPlatform.reset(); 178 plaf = NbPlatform.getPlatformByID(plaf.getID()); 179 assertTrue("adding of new javadoc", Arrays.asList( 180 plaf.getJavadocRoots()).toString().indexOf(urlInWorkDir) != -1); 181 return urlToAdd; 182 } 183 184 public void testRemovePlatform() throws Exception { 185 assertEquals("have two platforms", 2, NbPlatform.getPlatforms().size()); 186 NbPlatform custom = NbPlatform.getPlatformByID("custom"); 187 assertNotNull("have custom platform", custom); 188 NbPlatform.removePlatform(custom); 189 assertEquals("custom platform was deleted platforms", 1, NbPlatform.getPlatforms().size()); 190 assertNull("custom platform was deleted", NbPlatform.getPlatformByID("custom")); 191 } 192 193 public void testAddPlatform() throws Exception { 194 assertEquals("have two platforms", 2, NbPlatform.getPlatforms().size()); 195 NbPlatform custom = NbPlatform.getPlatformByID("custom"); 196 assertNotNull("have custom platform", custom); 197 NbPlatform.removePlatform(custom); 198 assertEquals("custom platform was deleted platforms", 1, NbPlatform.getPlatforms().size()); 199 assertNull("custom platform was deleted", NbPlatform.getPlatformByID("custom")); 200 NbPlatform.addPlatform(custom.getID(), custom.getDestDir(), "Some Label"); 201 NbPlatform.addPlatform(custom.getID() + 1, custom.getDestDir(), "Some Label 1"); 202 assertEquals("have two platforms", 3, NbPlatform.getPlatforms().size()); 203 assertNotNull("custom platform was added", NbPlatform.getPlatformByID("custom")); 204 assertNotNull("custom platform was added", NbPlatform.getPlatformByID("custom1")); 205 NbPlatform.reset(); 206 assertEquals("custom label", "Some Label 1", NbPlatform.getPlatformByID("custom1").getLabel()); 207 } 208 209 public void testIsLabelValid() throws Exception { 210 NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT); 211 assertFalse("already used label", NbPlatform.isLabelValid(def.getLabel())); 212 assertFalse("null label", NbPlatform.isLabelValid(null)); 213 assertTrue("valid label", NbPlatform.isLabelValid("whatever")); 214 } 215 216 public void testIsSupportedPlatform() throws Exception { 217 NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT); 218 assertTrue("platform supported", NbPlatform.isSupportedPlatform(def.getDestDir())); 219 } 220 221 public void testContains() throws Exception { 222 assertTrue("contains suite3/nbplatform", NbPlatform.contains(resolveEEPFile("suite3/nbplatform"))); 223 assertTrue("contains nbbuild/netbeans", NbPlatform.contains(destDirF)); 224 assertFalse("doesn't contains whatever/platform", NbPlatform.contains(file("whatever/platform"))); 225 } 226 227 public void testSourceRootsPersistence() throws Exception { 228 NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT); 229 assertTrue("platform supported", NbPlatform.isSupportedPlatform(def.getDestDir())); 230 File f1 = new File (getWorkDir(),"f1"); 231 File f2 = new File (getWorkDir(),"f2"); 232 if (!f1.exists()) { 233 assertTrue(f1.mkdir()); 234 } 235 if (!f2.exists()) { 236 assertTrue(f2.mkdir()); 237 } 238 URL [] us = new URL [] {f1.toURI().toURL(),f2.toURI().toURL()}; 239 String path = def.urlsToAntPath(us); 240 URL [] rus = def.findURLs(path); 241 assertEquals(us.length, rus.length); 242 for (int i = 0; i < us.length; i++) { 243 assertEquals(path, us[i].toExternalForm(), rus[i].toExternalForm()); 244 } 245 } 246 247 public void testHarnessVersionDetection() throws Exception { 248 NbPlatform p = NbPlatform.getDefaultPlatform(); 249 assertEquals("6.0 harness detected", NbPlatform.HARNESS_VERSION_60, p.getHarnessVersion()); 250 File testPlatform = new File (getWorkDir(), "test-platform"); 251 makePlatform(testPlatform); 252 p = NbPlatform.getPlatformByDestDir(testPlatform); 253 assertEquals("5.0 harness detected", NbPlatform.HARNESS_VERSION_50, p.getHarnessVersion()); 254 File defaultHarnessLocation = NbPlatform.getDefaultPlatform().getHarnessLocation(); 255 p = NbPlatform.addPlatform("test", testPlatform, defaultHarnessLocation, "Test"); 256 assertEquals("6.0 harness detected", NbPlatform.HARNESS_VERSION_60, p.getHarnessVersion()); 257 PropertyEvaluator eval = PropertyUtils.sequentialPropertyEvaluator(null, new PropertyProvider[] {PropertyUtils.globalPropertyProvider()}); 258 assertEquals(defaultHarnessLocation, FileUtil.normalizeFile(new File (eval.getProperty("nbplatform.test.harness.dir")))); 259 NbPlatform.reset(); 260 p = NbPlatform.getPlatformByID("test"); 261 assertNotNull(p); 262 assertEquals(testPlatform, p.getDestDir()); 263 assertEquals(defaultHarnessLocation, p.getHarnessLocation()); 264 assertEquals(NbPlatform.HARNESS_VERSION_60, p.getHarnessVersion()); 265 } 266 267 public void testSourceRootChangeFiring() throws Exception { 268 NbPlatform p = NbPlatform.getDefaultPlatform(); 269 SourceRootsPCL pcl = new SourceRootsPCL(); 270 p.addPropertyChangeListener(pcl); 271 assertFalse("source roots has not changed yet (sanity check)", pcl.sourcesChanged); 272 doAddSourceRoot(p, ARTIFICIAL_DIR); 273 assertTrue("source roots has changed", pcl.sourcesChanged); 274 } 275 276 private static final class SourceRootsPCL implements PropertyChangeListener { 277 278 boolean sourcesChanged; 279 280 public void propertyChange(PropertyChangeEvent evt) { 281 if (evt.getPropertyName() == NbPlatform.PROP_SOURCE_ROOTS) { 282 sourcesChanged = true; 283 } 284 } 285 286 } 287 288 290 } 291 | Popular Tags |