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.ui.customizer;
043:
044: import java.io.File;
045: import java.util.Arrays;
046: import java.util.Collections;
047: import java.util.HashMap;
048: import java.util.HashSet;
049: import java.util.Map;
050: import java.util.Set;
051: import java.util.jar.Manifest;
052: import org.netbeans.api.project.ProjectManager;
053: import org.netbeans.junit.NbTestCase;
054: import org.netbeans.modules.apisupport.project.EditableManifest;
055: import org.netbeans.modules.apisupport.project.ManifestManager;
056: import org.netbeans.modules.apisupport.project.NbModuleProject;
057: import org.netbeans.modules.apisupport.project.TestBase;
058: import org.netbeans.modules.apisupport.project.Util;
059: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
060: import org.netbeans.modules.apisupport.project.universe.LocalizedBundleInfo;
061: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
062: import org.openide.modules.Dependency;
063: import org.openide.modules.SpecificationVersion;
064:
065: /**
066: * Tests module dependencies in a suite.
067: * @author Jesse Glick
068: */
069: public class SuiteCustomizerLibrariesTest extends NbTestCase {
070:
071: public SuiteCustomizerLibrariesTest(String name) {
072: super (name);
073: }
074:
075: private NbPlatform platform;
076: private SuiteProject suite;
077:
078: protected void setUp() throws Exception {
079: super .setUp();
080: clearWorkDir();
081: // PLATFORM SETUP
082: TestBase.initializeBuildProperties(getWorkDir(), getDataDir());
083: File install = new File(getWorkDir(), "install");
084: TestBase.makePlatform(install);
085: // MODULE foo
086: Manifest mani = new Manifest();
087: mani.getMainAttributes().putValue(
088: ManifestManager.OPENIDE_MODULE, "foo/1");
089: mani.getMainAttributes().putValue(
090: ManifestManager.OPENIDE_MODULE_SPECIFICATION_VERSION,
091: "1.0");
092: mani.getMainAttributes().putValue(
093: ManifestManager.OPENIDE_MODULE_IMPLEMENTATION_VERSION,
094: "foo-1");
095: mani.getMainAttributes().putValue(
096: ManifestManager.OPENIDE_MODULE_LOCALIZING_BUNDLE,
097: "foo/Bundle.properties");
098: mani.getMainAttributes().putValue(
099: ManifestManager.OPENIDE_MODULE_PROVIDES, "tok1, tok1a");
100: Map<String, String> contents = new HashMap<String, String>();
101: contents.put("foo/Bundle.properties",
102: "OpenIDE-Module-Name=Foo Module");
103: TestBase.createJar(new File(new File(new File(install,
104: "somecluster"), "modules"), "foo.jar"), contents, mani);
105: // MODULE bar
106: mani = new Manifest();
107: mani.getMainAttributes().putValue(
108: ManifestManager.OPENIDE_MODULE, "bar");
109: mani.getMainAttributes().putValue(
110: ManifestManager.OPENIDE_MODULE_REQUIRES, "tok1");
111: TestBase.createJar(new File(new File(new File(install,
112: "somecluster"), "modules"), "bar.jar"),
113: Collections.EMPTY_MAP, mani);
114: // MODULE foo2
115: mani = new Manifest();
116: mani.getMainAttributes().putValue(
117: ManifestManager.OPENIDE_MODULE, "foo2");
118: mani.getMainAttributes().putValue(
119: ManifestManager.OPENIDE_MODULE_PROVIDES, "tok1b");
120: contents = new HashMap<String, String>();
121: TestBase
122: .createJar(new File(new File(new File(install,
123: "somecluster"), "modules"), "foo2.jar"),
124: contents, mani);
125: // MODULE bar2
126: mani = new Manifest();
127: mani.getMainAttributes().putValue(
128: ManifestManager.OPENIDE_MODULE, "bar2");
129: mani.getMainAttributes().putValue(
130: ManifestManager.OPENIDE_MODULE_NEEDS, "tok1b");
131: TestBase.createJar(new File(new File(new File(install,
132: "somecluster"), "modules"), "bar2.jar"),
133: Collections.EMPTY_MAP, mani);
134: // MODULE baz
135: mani = new Manifest();
136: mani.getMainAttributes().putValue(
137: ManifestManager.OPENIDE_MODULE, "baz");
138: mani.getMainAttributes().putValue(
139: "OpenIDE-Module-Module-Dependencies", "foo/1 > 1.0");
140: mani
141: .getMainAttributes()
142: .putValue("OpenIDE-Module-Requires",
143: "org.openide.modules.ModuleFormat1, org.openide.modules.os.Windows");
144: TestBase.createJar(new File(new File(new File(install,
145: "anothercluster"), "modules"), "baz.jar"),
146: Collections.EMPTY_MAP, mani);
147: platform = NbPlatform.addPlatform("custom", install, "custom");
148: // SUITE setup
149: suite = TestBase.generateSuite(getWorkDir(), "suite", "custom");
150: // MODULE org.example.module1
151: NbModuleProject module = TestBase.generateSuiteComponent(suite,
152: "module1");
153: EditableManifest em = Util.loadManifest(module
154: .getManifestFile());
155: em.setAttribute(ManifestManager.OPENIDE_MODULE,
156: "org.example.module1/2", null);
157: em.setAttribute(
158: ManifestManager.OPENIDE_MODULE_SPECIFICATION_VERSION,
159: "2.0", null);
160: em.setAttribute(ManifestManager.OPENIDE_MODULE_PROVIDES,
161: "tok2", null);
162: Util.storeManifest(module.getManifestFile(), em);
163: LocalizedBundleInfo lbinfo = module.getLookup().lookup(
164: LocalizedBundleInfo.Provider.class)
165: .getLocalizedBundleInfo();
166: lbinfo.setDisplayName("Module One");
167: lbinfo.store();
168: // MODULE org.example.module2
169: module = TestBase.generateSuiteComponent(suite, "module2");
170: em = Util.loadManifest(module.getManifestFile());
171: em.removeAttribute(
172: ManifestManager.OPENIDE_MODULE_SPECIFICATION_VERSION,
173: null);
174: em.setAttribute(ManifestManager.OPENIDE_MODULE_REQUIRES,
175: "tok2", null);
176: Util.storeManifest(module.getManifestFile(), em);
177: lbinfo = module.getLookup().lookup(
178: LocalizedBundleInfo.Provider.class)
179: .getLocalizedBundleInfo();
180: lbinfo.setDisplayName("Module Two");
181: lbinfo.store();
182: // MODULE org.example.module3
183: module = TestBase.generateSuiteComponent(suite, "module3");
184: Util.addDependency(module, "org.example.module2");
185: Util.addDependency(module, "bar");
186: lbinfo = module.getLookup().lookup(
187: LocalizedBundleInfo.Provider.class)
188: .getLocalizedBundleInfo();
189: lbinfo.setDisplayName("Module Three");
190: lbinfo.store();
191: ProjectManager.getDefault().saveAllProjects();
192: }
193:
194: public void testUniverseModules() throws Exception { // #65924
195: Map<String, SuiteCustomizerLibraries.UniverseModule> modulesByName = new HashMap<String, SuiteCustomizerLibraries.UniverseModule>();
196: Set<SuiteCustomizerLibraries.UniverseModule> modules = SuiteCustomizerLibraries
197: .loadUniverseModules(platform.getModules(), SuiteUtils
198: .getSubProjects(suite));
199: for (SuiteCustomizerLibraries.UniverseModule m : modules) {
200: modulesByName.put(m.getCodeNameBase(), m);
201: }
202: assertEquals(modules.size(), modulesByName.size());
203: SuiteCustomizerLibraries.UniverseModule m = modulesByName
204: .get("core");
205: assertNotNull(m);
206: // core.jar is just a dummy JAR, nothing interesting to test
207: m = modulesByName.get("foo");
208: assertNotNull(m);
209: assertEquals("somecluster", m.getCluster());
210: assertEquals("Foo Module", m.getDisplayName());
211: assertEquals(1, m.getReleaseVersion());
212: assertEquals(new SpecificationVersion("1.0"), m
213: .getSpecificationVersion());
214: assertEquals("foo-1", m.getImplementationVersion());
215: assertEquals(
216: new HashSet<String>(Arrays.asList("tok1", "tok1a")), m
217: .getProvidedTokens());
218: assertEquals(Collections.EMPTY_SET, m.getRequiredTokens());
219: assertEquals(Collections.EMPTY_SET, m.getModuleDependencies());
220: m = modulesByName.get("bar");
221: assertNotNull(m);
222: assertEquals(Collections.EMPTY_SET, m.getProvidedTokens());
223: assertEquals(Collections.singleton("tok1"), m
224: .getRequiredTokens());
225: m = modulesByName.get("baz");
226: assertNotNull(m);
227: assertEquals(Dependency.create(Dependency.TYPE_MODULE,
228: "foo/1 > 1.0"), m.getModuleDependencies());
229: m = modulesByName.get("org.example.module1");
230: assertNotNull(m);
231: assertNull(m.getCluster());
232: assertEquals("Module One", m.getDisplayName());
233: assertEquals(2, m.getReleaseVersion());
234: assertEquals(new SpecificationVersion("2.0"), m
235: .getSpecificationVersion());
236: assertNull(m.getImplementationVersion());
237: assertEquals(Collections.singleton("tok2"), m
238: .getProvidedTokens());
239: assertEquals(Collections.EMPTY_SET, m.getRequiredTokens());
240: assertEquals(Collections.EMPTY_SET, m.getModuleDependencies());
241: m = modulesByName.get("org.example.module2");
242: assertNotNull(m);
243: assertEquals(-1, m.getReleaseVersion());
244: assertNull(m.getSpecificationVersion());
245: assertNull(m.getImplementationVersion());
246: assertEquals(Collections.EMPTY_SET, m.getProvidedTokens());
247: assertEquals(Collections.singleton("tok2"), m
248: .getRequiredTokens());
249: m = modulesByName.get("org.example.module3");
250: assertNotNull(m);
251: assertEquals(Dependency.create(Dependency.TYPE_MODULE,
252: "org.example.module2, bar"), m.getModuleDependencies());
253: }
254:
255: public void testDependencyWarnings() throws Exception { // #65924
256: Set<SuiteCustomizerLibraries.UniverseModule> modules = SuiteCustomizerLibraries
257: .loadUniverseModules(platform.getModules(), SuiteUtils
258: .getSubProjects(suite));
259: Set<String> bothClusters = new HashSet<String>(Arrays.asList(
260: "somecluster", "anothercluster"));
261: assertEquals(null,
262: join(SuiteCustomizerLibraries.findWarning(modules,
263: bothClusters, Collections.<String> emptySet())));
264: assertEquals(
265: "[ERR_platform_excluded_dep, baz, anothercluster, Foo Module, somecluster]",
266: join(SuiteCustomizerLibraries.findWarning(modules,
267: Collections.singleton("anothercluster"),
268: Collections.<String> emptySet())));
269: assertNull(join(SuiteCustomizerLibraries.findWarning(modules,
270: Collections.singleton("somecluster"), Collections
271: .<String> emptySet())));
272: assertEquals(
273: "[ERR_suite_excluded_dep, Module Three, bar, somecluster]",
274: join(SuiteCustomizerLibraries.findWarning(modules,
275: Collections.<String> emptySet(), Collections
276: .<String> emptySet())));
277: assertEquals(
278: "[ERR_platform_only_excluded_providers, tok1, bar, somecluster, Foo Module, somecluster]",
279: join(SuiteCustomizerLibraries.findWarning(modules,
280: bothClusters, Collections.singleton("foo"))));
281: assertEquals(
282: "[ERR_platform_only_excluded_providers, tok1b, bar2, somecluster, foo2, somecluster]",
283: join(SuiteCustomizerLibraries.findWarning(modules,
284: bothClusters, Collections.singleton("foo2"))));
285: // XXX much more could be tested; check coverage results
286: }
287:
288: private static String join(String[] elements) {
289: if (elements != null) {
290: return Arrays.asList(elements).toString();
291: } else {
292: return null;
293: }
294: }
295:
296: }
|