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.text.Collator;
045: import java.util.HashSet;
046: import java.util.Iterator;
047: import java.util.Set;
048: import java.util.TreeSet;
049: import org.netbeans.api.project.ProjectManager;
050: import org.netbeans.modules.apisupport.project.EditableManifest;
051: import org.netbeans.modules.apisupport.project.ManifestManager;
052: import org.netbeans.modules.apisupport.project.NbModuleProject;
053: import org.netbeans.modules.apisupport.project.TestBase;
054: import org.netbeans.modules.apisupport.project.Util;
055: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
056: import org.netbeans.modules.apisupport.project.universe.ModuleEntry;
057: import org.netbeans.modules.apisupport.project.universe.ModuleList;
058: import org.netbeans.spi.project.support.ant.AntProjectHelper;
059: import org.netbeans.spi.project.support.ant.EditableProperties;
060:
061: /**
062: * @author Martin Krauskopf
063: */
064: public class ModuleDependencyTest extends TestBase {
065:
066: public ModuleDependencyTest(String testName) {
067: super (testName);
068: }
069:
070: public void testHashCodeAndEqualsAndCompareTo() throws Exception {
071: NbModuleProject module = generateStandaloneModule("module");
072: ModuleList ml = module.getModuleList();
073: ModuleEntry antME = ml.getEntry("org.apache.tools.ant.module");
074: ModuleDependency d1 = new ModuleDependency(antME);
075: ModuleDependency sameAsD1 = new ModuleDependency(antME);
076: ModuleDependency alsoSameAsD1 = new ModuleDependency(antME,
077: antME.getReleaseVersion(), antME
078: .getSpecificationVersion(), true, false);
079: ModuleDependency d2 = new ModuleDependency(antME, "0-1", null,
080: true, false);
081: ModuleDependency d3 = new ModuleDependency(antME, null, null,
082: true, false);
083: ModuleDependency d4 = new ModuleDependency(antME, antME
084: .getReleaseVersion(), null, true, true);
085: ModuleDependency d5 = new ModuleDependency(antME, antME
086: .getReleaseVersion(), null, true, false);
087:
088: // test hash code and equals
089: Set<ModuleDependency> set = new HashSet<ModuleDependency>();
090: Set<ModuleDependency> sorted = new TreeSet<ModuleDependency>();
091: set.add(d1);
092: sorted.add(d1);
093: assertFalse("already there", set.add(sameAsD1));
094: assertFalse("already there", sorted.add(sameAsD1));
095: assertFalse("already there", set.add(alsoSameAsD1));
096: assertFalse("already there", sorted.add(alsoSameAsD1));
097: assertTrue("is not there yet", set.add(d2));
098: assertTrue("is not there yet", sorted.add(d2));
099: assertTrue("is not there yet", set.add(d3));
100: assertTrue("is not there yet", sorted.add(d3));
101: assertTrue("is not there yet", set.add(d4));
102: assertTrue("is not there yet", sorted.add(d4));
103: assertTrue("is not there yet", set.add(d5));
104: assertTrue("is not there yet", sorted.add(d5));
105:
106: ModuleDependency[] expectedOrder = new ModuleDependency[] { d3,
107: d2, d5, d4, d1 };
108: Iterator it = sorted.iterator();
109: for (int i = 0; i < expectedOrder.length; i++) {
110: assertSame("expected order", expectedOrder[i], it.next());
111: }
112: assertFalse("sanity check", it.hasNext());
113: }
114:
115: public void testLocalizedNameComparator() throws Exception {
116: NbModuleProject module = generateStandaloneModule("module");
117: ModuleList ml = module.getModuleList();
118: ModuleDependency[] deps = new ModuleDependency[] {
119: new ModuleDependency(ml
120: .getEntry("org.apache.tools.ant.module")),
121: new ModuleDependency(ml.getEntry("org.openide.loaders")),
122: new ModuleDependency(ml
123: .getEntry("org.apache.tools.ant.module")),
124: new ModuleDependency(ml.getEntry("org.openide.io")),
125: new ModuleDependency(ml.getEntry("org.jdesktop.layout")),
126: new ModuleDependency(ml
127: .getEntry("org.openide.filesystems")),
128: new ModuleDependency(ml
129: .getEntry("org.openide.execution")), };
130:
131: for (int i = 0; i < deps.length; i++) {
132: for (int j = 0; j < deps.length; j++) {
133: int locNameResult = Collator.getInstance().compare(
134: deps[i].getModuleEntry().getLocalizedName(),
135: deps[j].getModuleEntry().getLocalizedName());
136: int realResult = ModuleDependency.LOCALIZED_NAME_COMPARATOR
137: .compare(deps[i], deps[j]);
138: assertTrue("ordering works: " + deps[i] + " <--> "
139: + deps[j], locNameResult > 0 ? realResult > 0
140: : (locNameResult == 0 ? realResult == 0
141: : realResult < 0));
142: // (int) Math.signum(locNameResult), (int) Math.signum(realResult));
143: }
144: }
145: }
146:
147: public void testSpecVersionBaseSourceEntries() throws Exception { // #72463
148: SuiteProject suite = generateSuite("suite");
149: NbModuleProject p = TestBase.generateSuiteComponent(suite,
150: "module");
151: ModuleList ml = ModuleList.getModuleList(p
152: .getProjectDirectoryFile());
153: ModuleEntry e = ml.getEntry("org.example.module");
154: assertNotNull("have entry", e);
155: ModuleDependency dep = new ModuleDependency(e);
156: assertEquals("right initial spec vers from manifest", "1.0",
157: dep.getSpecificationVersion());
158: EditableProperties ep = p.getHelper().getProperties(
159: AntProjectHelper.PROJECT_PROPERTIES_PATH);
160: ep.setProperty(SingleModuleProperties.SPEC_VERSION_BASE,
161: "1.1.0");
162: p.getHelper().putProperties(
163: AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
164: EditableManifest em = Util.loadManifest(p.getManifestFile());
165: em.removeAttribute(
166: ManifestManager.OPENIDE_MODULE_SPECIFICATION_VERSION,
167: null);
168: Util.storeManifest(p.getManifestFile(), em);
169: ProjectManager.getDefault().saveProject(p);
170: dep = new ModuleDependency(e);
171: assertEquals("right spec.version.base", "1.1", dep
172: .getSpecificationVersion());
173: ep.setProperty(SingleModuleProperties.SPEC_VERSION_BASE,
174: "1.2.0");
175: p.getHelper().putProperties(
176: AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
177: ProjectManager.getDefault().saveProject(p);
178: dep = new ModuleDependency(e);
179: assertEquals("right modified spec.version.base", "1.2", dep
180: .getSpecificationVersion());
181: dep = new ModuleDependency(e, null, "1.0", true, false);
182: assertEquals("right explicit spec vers", "1.0", dep
183: .getSpecificationVersion());
184: }
185:
186: public void testAppropriateDefaultCompileDependency()
187: throws Exception { // #73666
188: NbModuleProject p = generateStandaloneModule("module");
189: ModuleList ml = ModuleList.getModuleList(p
190: .getProjectDirectoryFile());
191: ModuleDependency d = new ModuleDependency(ml
192: .getEntry("org.example.module"));
193: assertFalse(
194: "no public packages -> no compile dependency by default",
195: d.hasCompileDependency());
196: }
197:
198: }
|