KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > universe > NbPlatformTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.universe;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.Set JavaDoc;
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 /**
38  * Test functionality of {@link NbPlatform}.
39  * @author Jesse Glick
40  */

41 public class NbPlatformTest extends TestBase {
42     
43     private static final String JavaDoc ARTIFICIAL_DIR = "artificial/";
44     
45     public NbPlatformTest(String JavaDoc name) {
46         super(name);
47     }
48     
49     public void testBasicUsage() throws Exception JavaDoc {
50         Set JavaDoc/*<NbPlatform>*/ 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 JavaDoc(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 JavaDoc(Arrays.asList(new URL JavaDoc[] {
68             Util.urlForDir(nbCVSRootFile()),
69             Util.urlForDir(resolveEEPFile("suite2")),
70         })), new HashSet JavaDoc(Arrays.asList(def.getSourceRoots())));
71         assertEquals("right Javadoc for default platform", new HashSet JavaDoc(Arrays.asList(new URL JavaDoc[] {
72             Util.urlForJar(apisZip),
73         })), new HashSet JavaDoc(Arrays.asList(def.getJavadocRoots())));
74         assertEquals("no sources for custom platform", Collections.EMPTY_SET, new HashSet JavaDoc(Arrays.asList(custom.getSourceRoots())));
75         assertEquals("no Javadoc for custom platform", Collections.EMPTY_SET, new HashSet JavaDoc(Arrays.asList(custom.getJavadocRoots())));
76     }
77     
78     public void testGetSourceLocationOfModule() throws Exception JavaDoc {
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 JavaDoc {
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 JavaDoc {
92         String JavaDoc name = NbPlatform.computeDisplayName(destDirF);
93         //System.out.println("name: " + name);
94
assertTrue("name '" + name + "' mentions 'NetBeans IDE'", name.indexOf("NetBeans IDE") != -1);
95     }
96     
97     public void testAddSourceRoot() throws Exception JavaDoc {
98         NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
99         doAddSourceRoot(def, ARTIFICIAL_DIR);
100     }
101     
102     public void testRemoveRoots() throws Exception JavaDoc {
103         NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
104         URL JavaDoc url = doAddSourceRoot(def, ARTIFICIAL_DIR);
105         assertTrue("adding of new source root", Arrays.asList( // double check
106
def.getSourceRoots()).toString().indexOf(ARTIFICIAL_DIR) != -1);
107         def.removeSourceRoots(new URL JavaDoc[] {url});
108         assertFalse("removing of new source root", Arrays.asList( // double check
109
def.getSourceRoots()).toString().indexOf(ARTIFICIAL_DIR) != -1);
110     }
111     
112     public void testMovingSourceRoots() throws Exception JavaDoc {
113         NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
114         URL JavaDoc url = doAddSourceRoot(def, ARTIFICIAL_DIR);
115         
116         def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
117         URL JavaDoc[] 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 JavaDoc 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 JavaDoc {
130         NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
131         doAddJavadocRoot(def, ARTIFICIAL_DIR);
132     }
133     
134     public void testRemoveJavadocs() throws Exception JavaDoc {
135         NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
136         URL JavaDoc url = doAddJavadocRoot(def, ARTIFICIAL_DIR);
137         assertTrue("adding of new javadoc", Arrays.asList( // double check
138
def.getJavadocRoots()).toString().indexOf(ARTIFICIAL_DIR) != -1);
139         def.removeJavadocRoots(new URL JavaDoc[] {url});
140         assertFalse("removing of new javadoc", Arrays.asList( // double check
141
def.getJavadocRoots()).toString().indexOf(ARTIFICIAL_DIR) != -1);
142     }
143     
144     public void testMovingJavadocRoots() throws Exception JavaDoc {
145         NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
146         URL JavaDoc url = doAddJavadocRoot(def, ARTIFICIAL_DIR);
147         
148         def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
149         URL JavaDoc[] 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 JavaDoc 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     /** Add and tests URL and returns it. */
162     private URL JavaDoc doAddSourceRoot(NbPlatform plaf, String JavaDoc urlInWorkDir) throws Exception JavaDoc {
163         URL JavaDoc urlToAdd = new URL JavaDoc(getWorkDir().toURI().toURL(), urlInWorkDir);
164         plaf.addSourceRoot(urlToAdd);
165         // reload platform
166
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 JavaDoc doAddJavadocRoot(NbPlatform plaf, String JavaDoc urlInWorkDir) throws Exception JavaDoc {
174         URL JavaDoc urlToAdd = new URL JavaDoc(getWorkDir().toURI().toURL(), urlInWorkDir);
175         plaf.addJavadocRoot(urlToAdd);
176         // reload platform
177
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
228         NbPlatform def = NbPlatform.getPlatformByID(NbPlatform.PLATFORM_ID_DEFAULT);
229         assertTrue("platform supported", NbPlatform.isSupportedPlatform(def.getDestDir()));
230         File JavaDoc f1 = new File JavaDoc(getWorkDir(),"f1");
231         File JavaDoc f2 = new File JavaDoc(getWorkDir(),"f2");
232         if (!f1.exists()) {
233             assertTrue(f1.mkdir());
234         }
235         if (!f2.exists()) {
236             assertTrue(f2.mkdir());
237         }
238         URL JavaDoc[] us = new URL JavaDoc[] {f1.toURI().toURL(),f2.toURI().toURL()};
239         String JavaDoc path = def.urlsToAntPath(us);
240         URL JavaDoc[] 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 JavaDoc {
248         NbPlatform p = NbPlatform.getDefaultPlatform();
249         assertEquals("6.0 harness detected", NbPlatform.HARNESS_VERSION_60, p.getHarnessVersion());
250         File JavaDoc testPlatform = new File JavaDoc(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 JavaDoc 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 JavaDoc(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 JavaDoc {
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 JavaDoc {
277         
278         boolean sourcesChanged;
279         
280         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
281             if (evt.getPropertyName() == NbPlatform.PROP_SOURCE_ROOTS) {
282                 sourcesChanged = true;
283             }
284         }
285         
286     }
287     
288     // XXX testHarnessSelection
289

290 }
291
Popular Tags