KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > customizer > SuiteCustomizerLibrariesTest


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.ui.customizer;
21
22 import java.io.File JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.jar.Manifest JavaDoc;
31 import org.netbeans.api.project.ProjectManager;
32 import org.netbeans.junit.NbTestCase;
33 import org.netbeans.modules.apisupport.project.EditableManifest;
34 import org.netbeans.modules.apisupport.project.ManifestManager;
35 import org.netbeans.modules.apisupport.project.NbModuleProject;
36 import org.netbeans.modules.apisupport.project.TestBase;
37 import org.netbeans.modules.apisupport.project.Util;
38 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
39 import org.netbeans.modules.apisupport.project.universe.LocalizedBundleInfo;
40 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
41 import org.openide.modules.Dependency;
42 import org.openide.modules.SpecificationVersion;
43
44 /**
45  * Tests module dependencies in a suite.
46  * @author Jesse Glick
47  */

48 public class SuiteCustomizerLibrariesTest extends NbTestCase {
49     
50     public SuiteCustomizerLibrariesTest(String JavaDoc name) {
51         super(name);
52     }
53     
54     private NbPlatform platform;
55     private SuiteProject suite;
56
57     protected void setUp() throws Exception JavaDoc {
58         super.setUp();
59         clearWorkDir();
60         // PLATFORM SETUP
61
TestBase.initializeBuildProperties(getWorkDir(), getDataDir());
62         File JavaDoc install = new File JavaDoc(getWorkDir(), "install");
63         TestBase.makePlatform(install);
64         // MODULE foo
65
Manifest JavaDoc mani = new Manifest JavaDoc();
66         mani.getMainAttributes().putValue(ManifestManager.OPENIDE_MODULE, "foo/1");
67         mani.getMainAttributes().putValue(ManifestManager.OPENIDE_MODULE_SPECIFICATION_VERSION, "1.0");
68         mani.getMainAttributes().putValue(ManifestManager.OPENIDE_MODULE_IMPLEMENTATION_VERSION, "foo-1");
69         mani.getMainAttributes().putValue(ManifestManager.OPENIDE_MODULE_LOCALIZING_BUNDLE, "foo/Bundle.properties");
70         mani.getMainAttributes().putValue(ManifestManager.OPENIDE_MODULE_PROVIDES, "tok1, tok1a");
71         Map JavaDoc/*<String,String>*/ contents = new HashMap JavaDoc();
72         contents.put("foo/Bundle.properties", "OpenIDE-Module-Name=Foo Module");
73         TestBase.createJar(new File JavaDoc(new File JavaDoc(new File JavaDoc(install, "somecluster"), "modules"), "foo.jar"), contents, mani);
74         // MODULE bar
75
mani = new Manifest JavaDoc();
76         mani.getMainAttributes().putValue(ManifestManager.OPENIDE_MODULE, "bar");
77         mani.getMainAttributes().putValue(ManifestManager.OPENIDE_MODULE_REQUIRES, "tok1");
78         TestBase.createJar(new File JavaDoc(new File JavaDoc(new File JavaDoc(install, "somecluster"), "modules"), "bar.jar"), Collections.EMPTY_MAP, mani);
79         // MODULE foo2
80
mani = new Manifest JavaDoc();
81         mani.getMainAttributes().putValue(ManifestManager.OPENIDE_MODULE, "foo2");
82         mani.getMainAttributes().putValue(ManifestManager.OPENIDE_MODULE_PROVIDES, "tok1b");
83         contents = new HashMap JavaDoc();
84         TestBase.createJar(new File JavaDoc(new File JavaDoc(new File JavaDoc(install, "somecluster"), "modules"), "foo2.jar"), contents, mani);
85         // MODULE bar2
86
mani = new Manifest JavaDoc();
87         mani.getMainAttributes().putValue(ManifestManager.OPENIDE_MODULE, "bar2");
88         mani.getMainAttributes().putValue(ManifestManager.OPENIDE_MODULE_NEEDS, "tok1b");
89         TestBase.createJar(new File JavaDoc(new File JavaDoc(new File JavaDoc(install, "somecluster"), "modules"), "bar2.jar"), Collections.EMPTY_MAP, mani);
90         // MODULE baz
91
mani = new Manifest JavaDoc();
92         mani.getMainAttributes().putValue(ManifestManager.OPENIDE_MODULE, "baz");
93         mani.getMainAttributes().putValue("OpenIDE-Module-Module-Dependencies", "foo/1 > 1.0");
94         mani.getMainAttributes().putValue("OpenIDE-Module-Requires", "org.openide.modules.ModuleFormat1, org.openide.modules.os.Windows");
95         TestBase.createJar(new File JavaDoc(new File JavaDoc(new File JavaDoc(install, "anothercluster"), "modules"), "baz.jar"), Collections.EMPTY_MAP, mani);
96         platform = NbPlatform.addPlatform("custom", install, "custom");
97         // SUITE setup
98
suite = TestBase.generateSuite(getWorkDir(), "suite", "custom");
99         // MODULE org.example.module1
100
NbModuleProject module = TestBase.generateSuiteComponent(suite, "module1");
101         EditableManifest em = Util.loadManifest(module.getManifestFile());
102         em.setAttribute(ManifestManager.OPENIDE_MODULE, "org.example.module1/2", null);
103         em.setAttribute(ManifestManager.OPENIDE_MODULE_SPECIFICATION_VERSION, "2.0", null);
104         em.setAttribute(ManifestManager.OPENIDE_MODULE_PROVIDES, "tok2", null);
105         Util.storeManifest(module.getManifestFile(), em);
106         LocalizedBundleInfo lbinfo = ((LocalizedBundleInfo.Provider) module.getLookup().lookup(LocalizedBundleInfo.Provider.class)).getLocalizedBundleInfo();
107         lbinfo.setDisplayName("Module One");
108         lbinfo.store();
109         // MODULE org.example.module2
110
module = TestBase.generateSuiteComponent(suite, "module2");
111         em = Util.loadManifest(module.getManifestFile());
112         em.removeAttribute(ManifestManager.OPENIDE_MODULE_SPECIFICATION_VERSION, null);
113         em.setAttribute(ManifestManager.OPENIDE_MODULE_REQUIRES, "tok2", null);
114         Util.storeManifest(module.getManifestFile(), em);
115         lbinfo = ((LocalizedBundleInfo.Provider) module.getLookup().lookup(LocalizedBundleInfo.Provider.class)).getLocalizedBundleInfo();
116         lbinfo.setDisplayName("Module Two");
117         lbinfo.store();
118         // MODULE org.example.module3
119
module = TestBase.generateSuiteComponent(suite, "module3");
120         Util.addDependency(module, "org.example.module2");
121         Util.addDependency(module, "bar");
122         lbinfo = ((LocalizedBundleInfo.Provider) module.getLookup().lookup(LocalizedBundleInfo.Provider.class)).getLocalizedBundleInfo();
123         lbinfo.setDisplayName("Module Three");
124         lbinfo.store();
125         ProjectManager.getDefault().saveAllProjects();
126     }
127     
128     public void testUniverseModules() throws Exception JavaDoc { // #65924
129
Set JavaDoc/*<UniverseModule>*/ modules = SuiteCustomizerLibraries.loadUniverseModules(platform.getModules(), SuiteUtils.getSubProjects(suite));
130         Map JavaDoc/*<String,UniverseModule>*/ modulesByName = new HashMap JavaDoc();
131         for (Iterator JavaDoc it = modules.iterator(); it.hasNext(); ) {
132             SuiteCustomizerLibraries.UniverseModule m = (SuiteCustomizerLibraries.UniverseModule) it.next();
133             modulesByName.put(m.getCodeNameBase(), m);
134         }
135         assertEquals(modules.size(), modulesByName.size());
136         SuiteCustomizerLibraries.UniverseModule m = (SuiteCustomizerLibraries.UniverseModule) modulesByName.get("core");
137         assertNotNull(m);
138         // core.jar is just a dummy JAR, nothing interesting to test
139
m = (SuiteCustomizerLibraries.UniverseModule) modulesByName.get("foo");
140         assertNotNull(m);
141         assertEquals("somecluster", m.getCluster());
142         assertEquals("Foo Module", m.getDisplayName());
143         assertEquals(1, m.getReleaseVersion());
144         assertEquals(new SpecificationVersion("1.0"), m.getSpecificationVersion());
145         assertEquals("foo-1", m.getImplementationVersion());
146         assertEquals(new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] {"tok1", "tok1a"})), m.getProvidedTokens());
147         assertEquals(Collections.EMPTY_SET, m.getRequiredTokens());
148         assertEquals(Collections.EMPTY_SET, m.getModuleDependencies());
149         m = (SuiteCustomizerLibraries.UniverseModule) modulesByName.get("bar");
150         assertNotNull(m);
151         assertEquals(Collections.EMPTY_SET, m.getProvidedTokens());
152         assertEquals(Collections.singleton("tok1"), m.getRequiredTokens());
153         m = (SuiteCustomizerLibraries.UniverseModule) modulesByName.get("baz");
154         assertNotNull(m);
155         assertEquals(Dependency.create(Dependency.TYPE_MODULE, "foo/1 > 1.0"), m.getModuleDependencies());
156         m = (SuiteCustomizerLibraries.UniverseModule) modulesByName.get("org.example.module1");
157         assertNotNull(m);
158         assertNull(m.getCluster());
159         assertEquals("Module One", m.getDisplayName());
160         assertEquals(2, m.getReleaseVersion());
161         assertEquals(new SpecificationVersion("2.0"), m.getSpecificationVersion());
162         assertNull(m.getImplementationVersion());
163         assertEquals(Collections.singleton("tok2"), m.getProvidedTokens());
164         assertEquals(Collections.EMPTY_SET, m.getRequiredTokens());
165         assertEquals(Collections.EMPTY_SET, m.getModuleDependencies());
166         m = (SuiteCustomizerLibraries.UniverseModule) modulesByName.get("org.example.module2");
167         assertNotNull(m);
168         assertEquals(-1, m.getReleaseVersion());
169         assertNull(m.getSpecificationVersion());
170         assertNull(m.getImplementationVersion());
171         assertEquals(Collections.EMPTY_SET, m.getProvidedTokens());
172         assertEquals(Collections.singleton("tok2"), m.getRequiredTokens());
173         m = (SuiteCustomizerLibraries.UniverseModule) modulesByName.get("org.example.module3");
174         assertNotNull(m);
175         assertEquals(Dependency.create(Dependency.TYPE_MODULE, "org.example.module2, bar"), m.getModuleDependencies());
176     }
177     
178     public void testDependencyWarnings() throws Exception JavaDoc { // #65924
179
Set JavaDoc/*<UniverseModule>*/ modules = SuiteCustomizerLibraries.loadUniverseModules(platform.getModules(), SuiteUtils.getSubProjects(suite));
180         Set JavaDoc/*<String>*/ bothClusters = new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] {"somecluster", "anothercluster"}));
181         assertEquals(null, join(SuiteCustomizerLibraries.findWarning(modules, bothClusters, Collections.EMPTY_SET)));
182         assertEquals("[ERR_platform_excluded_dep, baz, anothercluster, Foo Module, somecluster]",
183                 join(SuiteCustomizerLibraries.findWarning(modules, Collections.singleton("anothercluster"), Collections.EMPTY_SET)));
184         assertNull(join(SuiteCustomizerLibraries.findWarning(modules, Collections.singleton("somecluster"), Collections.EMPTY_SET)));
185         assertEquals("[ERR_suite_excluded_dep, Module Three, bar, somecluster]",
186                 join(SuiteCustomizerLibraries.findWarning(modules, Collections.EMPTY_SET, Collections.EMPTY_SET)));
187         assertEquals("[ERR_platform_only_excluded_providers, tok1, bar, somecluster, Foo Module, somecluster]",
188                 join(SuiteCustomizerLibraries.findWarning(modules, bothClusters, Collections.singleton("foo"))));
189         assertEquals("[ERR_platform_only_excluded_providers, tok1b, bar2, somecluster, foo2, somecluster]",
190                 join(SuiteCustomizerLibraries.findWarning(modules, bothClusters, Collections.singleton("foo2"))));
191         // XXX much more could be tested; check coverage results
192
}
193     
194     private static String JavaDoc join(String JavaDoc[] elements) {
195         if (elements != null) {
196             return Arrays.asList(elements).toString();
197         } else {
198             return null;
199         }
200     }
201     
202 }
203
Popular Tags