001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project.universe;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.io.File;
047: import java.io.IOException;
048: import java.net.URL;
049: import java.util.Arrays;
050: import java.util.Collections;
051: import java.util.HashSet;
052: import java.util.Set;
053: import org.netbeans.modules.apisupport.project.TestBase;
054: import org.netbeans.modules.apisupport.project.Util;
055: import org.netbeans.spi.project.support.ant.PropertyEvaluator;
056: import org.netbeans.spi.project.support.ant.PropertyUtils;
057: import org.openide.filesystems.FileUtil;
058:
059: /**
060: * Test functionality of {@link NbPlatform}.
061: * @author Jesse Glick
062: */
063: public class NbPlatformTest extends TestBase {
064:
065: private static final String ARTIFICIAL_DIR = "artificial/";
066:
067: public NbPlatformTest(String name) {
068: super (name);
069: }
070:
071: public void testBasicUsage() throws Exception {
072: Set<NbPlatform> platforms = NbPlatform.getPlatforms();
073: assertEquals("have two platforms", 2, platforms.size());
074: NbPlatform def = NbPlatform
075: .getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
076: assertNotNull("have default platform", def);
077: assertEquals("can use a special method call for that", def,
078: NbPlatform.getDefaultPlatform());
079: NbPlatform custom = NbPlatform.getPlatformByID("custom");
080: assertNotNull("have custom platform", custom);
081: assertNull("no such bogus platform", NbPlatform
082: .getPlatformByID("bogus"));
083: assertEquals(
084: new HashSet<NbPlatform>(Arrays.asList(def, custom)),
085: platforms);
086: assertEquals("right default platform by dest dir", def,
087: NbPlatform.getPlatformByDestDir(destDirF));
088: assertEquals(
089: "right custom platform by dest dir",
090: custom,
091: NbPlatform
092: .getPlatformByDestDir(resolveEEPFile("suite3/nbplatform")));
093: assertFalse("bogus platform is not valid", NbPlatform
094: .getPlatformByDestDir(file("nbbuild")).isValid());
095: assertFalse("bogus platform is not default", NbPlatform
096: .getPlatformByDestDir(file("nbbuild")).isDefault());
097: assertEquals("right dest dir for default platform", destDirF,
098: def.getDestDir());
099: assertEquals("right dest dir for custom platform",
100: resolveEEPFile("suite3/nbplatform"), custom
101: .getDestDir());
102: assertEquals("right name for default platform",
103: NbPlatform.PLATFORM_ID_DEFAULT, def.getID());
104: assertEquals("right name for custom platform", "custom", custom
105: .getID());
106: assertEquals("right sources for default platform",
107: new HashSet<URL>(Arrays.asList(Util
108: .urlForDir(nbRootFile()), Util
109: .urlForDir(resolveEEPFile("suite2")))),
110: new HashSet<URL>(Arrays.asList(def.getSourceRoots())));
111: assertEquals(
112: "right Javadoc for default platform",
113: new HashSet<URL>(Arrays.asList(Util.urlForJar(apisZip))),
114: new HashSet<URL>(Arrays.asList(def.getJavadocRoots())));
115: assertEquals("no sources for custom platform", Collections
116: .emptySet(), new HashSet<URL>(Arrays.asList(custom
117: .getSourceRoots())));
118: assertEquals("no Javadoc for custom platform", Collections
119: .emptySet(), new HashSet<URL>(Arrays.asList(custom
120: .getJavadocRoots())));
121: }
122:
123: public void testGetSourceLocationOfModule() throws Exception {
124: NbPlatform p = NbPlatform.getDefaultPlatform();
125: assertEquals("Right source location for image.jar",
126: file("image"),
127: p.getSourceLocationOfModule(file("nbbuild/netbeans/"
128: + TestBase.CLUSTER_IDE
129: + "/modules/org-netbeans-modules-image.jar")));
130: }
131:
132: public void testIsPlatformDirectory() throws Exception {
133: assertTrue("nbbuild/netbeans is a platform", NbPlatform
134: .isPlatformDirectory(destDirF));
135: assertFalse(TestBase.CLUSTER_PLATFORM + " is not a platform",
136: NbPlatform.isPlatformDirectory(file("nbbuild/netbeans/"
137: + TestBase.CLUSTER_PLATFORM)));
138: assertFalse("nbbuild is not a platform", NbPlatform
139: .isPlatformDirectory(file("nbbuild")));
140: assertFalse("nbbuild/build.xml is not a platform", NbPlatform
141: .isPlatformDirectory(file("nbbuild/build.xml")));
142: assertFalse("nonexistent dir is not a platform", NbPlatform
143: .isPlatformDirectory(file("nonexistent")));
144: }
145:
146: public void testComputeDisplayName() throws Exception {
147: String name = NbPlatform.computeDisplayName(destDirF);
148: //System.out.println("name: " + name);
149: assertTrue("name '" + name + "' mentions 'NetBeans IDE'", name
150: .indexOf("NetBeans IDE") != -1);
151: }
152:
153: public void testAddSourceRoot() throws Exception {
154: NbPlatform def = NbPlatform
155: .getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
156: doAddSourceRoot(def, ARTIFICIAL_DIR);
157: }
158:
159: public void testRemoveRoots() throws Exception {
160: NbPlatform def = NbPlatform
161: .getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
162: URL url = doAddSourceRoot(def, ARTIFICIAL_DIR);
163: assertTrue("adding of new source root", Arrays.asList( // double check
164: def.getSourceRoots()).toString()
165: .indexOf(ARTIFICIAL_DIR) != -1);
166: def.removeSourceRoots(new URL[] { url });
167: assertFalse("removing of new source root", Arrays.asList( // double check
168: def.getSourceRoots()).toString()
169: .indexOf(ARTIFICIAL_DIR) != -1);
170: }
171:
172: public void testMovingSourceRoots() throws Exception {
173: NbPlatform def = NbPlatform
174: .getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
175: URL url = doAddSourceRoot(def, ARTIFICIAL_DIR);
176:
177: def = NbPlatform
178: .getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
179: URL[] srcRoot = def.getSourceRoots();
180: assertEquals("new url should be the last one", url,
181: srcRoot[srcRoot.length - 1]);
182: def.moveSourceRootUp(srcRoot.length - 1);
183: srcRoot = def.getSourceRoots();
184: assertEquals("new url should be moved up", url,
185: srcRoot[srcRoot.length - 2]);
186:
187: URL first = srcRoot[0];
188: def.moveSourceRootDown(0);
189: srcRoot = def.getSourceRoots();
190: assertEquals(
191: "first url should be moved to the second position",
192: first, srcRoot[1]);
193: }
194:
195: public void testAddJavadoc() throws Exception {
196: NbPlatform def = NbPlatform
197: .getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
198: doAddJavadocRoot(def, ARTIFICIAL_DIR);
199: }
200:
201: public void testRemoveJavadocs() throws Exception {
202: NbPlatform def = NbPlatform
203: .getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
204: URL url = doAddJavadocRoot(def, ARTIFICIAL_DIR);
205: assertTrue("adding of new javadoc", Arrays.asList( // double check
206: def.getJavadocRoots()).toString().indexOf(
207: ARTIFICIAL_DIR) != -1);
208: def.removeJavadocRoots(new URL[] { url });
209: assertFalse("removing of new javadoc", Arrays.asList( // double check
210: def.getJavadocRoots()).toString().indexOf(
211: ARTIFICIAL_DIR) != -1);
212: }
213:
214: public void testMovingJavadocRoots() throws Exception {
215: NbPlatform def = NbPlatform
216: .getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
217: URL url = doAddJavadocRoot(def, ARTIFICIAL_DIR);
218:
219: def = NbPlatform
220: .getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
221: URL[] jdRoot = def.getJavadocRoots();
222: assertEquals("new url should be the last one", url,
223: jdRoot[jdRoot.length - 1]);
224: def.moveJavadocRootUp(jdRoot.length - 1);
225: jdRoot = def.getJavadocRoots();
226: assertEquals("new url should be moved up", url,
227: jdRoot[jdRoot.length - 2]);
228:
229: URL first = jdRoot[0];
230: def.moveJavadocRootDown(0);
231: jdRoot = def.getJavadocRoots();
232: assertEquals(
233: "first url should be moved to the second position",
234: first, jdRoot[1]);
235: }
236:
237: /** Add and tests URL and returns it. */
238: private URL doAddSourceRoot(NbPlatform plaf, String urlInWorkDir)
239: throws Exception {
240: URL urlToAdd = new URL(getWorkDir().toURI().toURL(),
241: urlInWorkDir);
242: plaf.addSourceRoot(urlToAdd);
243: // reload platform
244: NbPlatform.reset();
245: plaf = NbPlatform.getPlatformByID(plaf.getID());
246: assertTrue("adding of new sourceroot",
247: Arrays.asList(plaf.getSourceRoots()).toString()
248: .indexOf(urlInWorkDir) != -1);
249: return urlToAdd;
250: }
251:
252: private URL doAddJavadocRoot(NbPlatform plaf, String urlInWorkDir)
253: throws Exception {
254: URL urlToAdd = new URL(getWorkDir().toURI().toURL(),
255: urlInWorkDir);
256: plaf.addJavadocRoot(urlToAdd);
257: // reload platform
258: NbPlatform.reset();
259: plaf = NbPlatform.getPlatformByID(plaf.getID());
260: assertTrue("adding of new javadoc", Arrays.asList(
261: plaf.getJavadocRoots()).toString()
262: .indexOf(urlInWorkDir) != -1);
263: return urlToAdd;
264: }
265:
266: public void testRemovePlatform() throws Exception {
267: assertEquals("have two platforms", 2, NbPlatform.getPlatforms()
268: .size());
269: NbPlatform custom = NbPlatform.getPlatformByID("custom");
270: assertNotNull("have custom platform", custom);
271: NbPlatform.removePlatform(custom);
272: assertEquals("custom platform was deleted platforms", 1,
273: NbPlatform.getPlatforms().size());
274: assertNull("custom platform was deleted", NbPlatform
275: .getPlatformByID("custom"));
276: }
277:
278: public void testAddPlatform() throws Exception {
279: assertEquals("have two platforms", 2, NbPlatform.getPlatforms()
280: .size());
281: NbPlatform custom = NbPlatform.getPlatformByID("custom");
282: assertNotNull("have custom platform", custom);
283: NbPlatform.removePlatform(custom);
284: assertEquals("custom platform was deleted platforms", 1,
285: NbPlatform.getPlatforms().size());
286: assertNull("custom platform was deleted", NbPlatform
287: .getPlatformByID("custom"));
288: NbPlatform.addPlatform(custom.getID(), custom.getDestDir(),
289: "Some Label");
290: NbPlatform.addPlatform(custom.getID() + 1, custom.getDestDir(),
291: "Some Label 1");
292: try {
293: NbPlatform.addPlatform(custom.getID(), custom.getDestDir(),
294: "Duplicate");
295: fail("should have rejected this");
296: } catch (IOException x) {
297: // OK
298: }
299: assertEquals("have two platforms", 3, NbPlatform.getPlatforms()
300: .size());
301: assertNotNull("custom platform was added", NbPlatform
302: .getPlatformByID("custom"));
303: assertNotNull("custom platform was added", NbPlatform
304: .getPlatformByID("custom1"));
305: NbPlatform.reset();
306: assertEquals("custom label", "Some Label 1", NbPlatform
307: .getPlatformByID("custom1").getLabel());
308: }
309:
310: public void testIsLabelValid() throws Exception {
311: NbPlatform def = NbPlatform
312: .getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
313: assertFalse("already used label", NbPlatform.isLabelValid(def
314: .getLabel()));
315: assertFalse("null label", NbPlatform.isLabelValid(null));
316: assertTrue("valid label", NbPlatform.isLabelValid("whatever"));
317: }
318:
319: public void testIsSupportedPlatform() throws Exception {
320: NbPlatform def = NbPlatform
321: .getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
322: assertTrue("platform supported", NbPlatform
323: .isSupportedPlatform(def.getDestDir()));
324: }
325:
326: public void testContains() throws Exception {
327: assertTrue("contains suite3/nbplatform", NbPlatform
328: .contains(resolveEEPFile("suite3/nbplatform")));
329: assertTrue("contains nbbuild/netbeans", NbPlatform
330: .contains(destDirF));
331: assertFalse("doesn't contains whatever/platform", NbPlatform
332: .contains(file("whatever/platform")));
333: }
334:
335: public void testSourceRootsPersistence() throws Exception {
336: NbPlatform def = NbPlatform
337: .getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
338: assertTrue("platform supported", NbPlatform
339: .isSupportedPlatform(def.getDestDir()));
340: File f1 = new File(getWorkDir(), "f1");
341: File f2 = new File(getWorkDir(), "f2");
342: if (!f1.exists()) {
343: assertTrue(f1.mkdir());
344: }
345: if (!f2.exists()) {
346: assertTrue(f2.mkdir());
347: }
348: URL[] us = { f1.toURI().toURL(), f2.toURI().toURL() };
349: String path = NbPlatform.urlsToAntPath(us);
350: URL[] rus = NbPlatform.findURLs(path);
351: assertEquals(path, Arrays.asList(us), Arrays.asList(rus));
352: }
353:
354: public void testHarnessVersionDetection() throws Exception {
355: NbPlatform p = NbPlatform.getDefaultPlatform();
356: assertEquals("6.1 harness detected",
357: NbPlatform.HARNESS_VERSION_61, p.getHarnessVersion());
358: File testPlatform = new File(getWorkDir(), "test-platform");
359: makePlatform(testPlatform);
360: p = NbPlatform.getPlatformByDestDir(testPlatform);
361: assertEquals("5.0 harness detected",
362: NbPlatform.HARNESS_VERSION_50, p.getHarnessVersion());
363: File defaultHarnessLocation = NbPlatform.getDefaultPlatform()
364: .getHarnessLocation();
365: p = NbPlatform.addPlatform("test", testPlatform,
366: defaultHarnessLocation, "Test");
367: assertEquals("6.1 harness detected",
368: NbPlatform.HARNESS_VERSION_61, p.getHarnessVersion());
369: PropertyEvaluator eval = PropertyUtils
370: .sequentialPropertyEvaluator(null, PropertyUtils
371: .globalPropertyProvider());
372: assertEquals(defaultHarnessLocation, FileUtil
373: .normalizeFile(new File(eval
374: .getProperty("nbplatform.test.harness.dir"))));
375: NbPlatform.reset();
376: p = NbPlatform.getPlatformByID("test");
377: assertNotNull(p);
378: assertEquals(testPlatform, p.getDestDir());
379: assertEquals(defaultHarnessLocation, p.getHarnessLocation());
380: assertEquals(NbPlatform.HARNESS_VERSION_61, p
381: .getHarnessVersion());
382: }
383:
384: public void testSourceRootChangeFiring() throws Exception {
385: NbPlatform p = NbPlatform.getDefaultPlatform();
386: SourceRootsPCL pcl = new SourceRootsPCL();
387: p.addPropertyChangeListener(pcl);
388: assertFalse("source roots has not changed yet (sanity check)",
389: pcl.sourcesChanged);
390: doAddSourceRoot(p, ARTIFICIAL_DIR);
391: assertTrue("source roots has changed", pcl.sourcesChanged);
392: }
393:
394: private static final class SourceRootsPCL implements
395: PropertyChangeListener {
396:
397: boolean sourcesChanged;
398:
399: public void propertyChange(PropertyChangeEvent evt) {
400: if (NbPlatform.PROP_SOURCE_ROOTS.equals(evt
401: .getPropertyName())) {
402: sourcesChanged = true;
403: }
404: }
405:
406: }
407:
408: // XXX testHarnessSelection
409:
410: }
|