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 org.netbeans.api.project.Project;
046: import org.netbeans.api.project.ProjectManager;
047: import org.netbeans.modules.apisupport.project.NbModuleProject;
048: import org.netbeans.modules.apisupport.project.ProjectXMLManager;
049: import org.netbeans.modules.apisupport.project.SuiteProvider;
050: import org.netbeans.modules.apisupport.project.TestBase;
051: import org.netbeans.modules.apisupport.project.Util;
052: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
053: import org.netbeans.spi.project.SubprojectProvider;
054: import org.netbeans.spi.project.support.ant.EditableProperties;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.loaders.DataFolder;
058:
059: /**
060: * Tests {@link SuiteUtils}
061: *
062: * @author Martin Krauskopf
063: */
064: public class SuiteUtilsTest extends TestBase {
065:
066: public SuiteUtilsTest(String name) {
067: super (name);
068: }
069:
070: public void testAddModule() throws Exception {
071: SuiteProject suite1 = generateSuite("suite1");
072: NbModuleProject module1 = generateStandaloneModule("module1");
073: SuiteProvider suiteProvider = module1.getLookup().lookup(
074: SuiteProvider.class);
075: assertNull(
076: "module1 is standalone module - doesn't have valid SuiteProvider",
077: suiteProvider.getSuiteDirectory());
078:
079: SuiteUtils.addModule(suite1, module1);
080: SubprojectProvider spp = SuitePropertiesTest
081: .getSubProjectProvider(suite1);
082: assertEquals("one module suite component", 1, spp
083: .getSubprojects().size());
084: suiteProvider = module1.getLookup().lookup(SuiteProvider.class);
085: assertNotNull(
086: "module1 became suite component - has valid SuiteProvider",
087: suiteProvider.getSuiteDirectory());
088:
089: NbModuleProject module2 = generateStandaloneModule("module2");
090: NbModuleProject module3 = generateStandaloneModule("module3");
091: SuiteUtils.addModule(suite1, module2);
092: SuiteUtils.addModule(suite1, module3);
093:
094: assertEquals("three module suite components", 3, spp
095: .getSubprojects().size());
096: }
097:
098: public void testRemoveModuleFromSuite() throws Exception {
099: SuiteProject suite1 = generateSuite("suite1");
100: NbModuleProject module1 = TestBase.generateSuiteComponent(
101: suite1, "module1");
102: SubprojectProvider spp = SuitePropertiesTest
103: .getSubProjectProvider(suite1);
104: assertEquals("one module suite component", 1, spp
105: .getSubprojects().size());
106:
107: SuiteProvider suiteProvider = module1.getLookup().lookup(
108: SuiteProvider.class);
109: assertNotNull(
110: "module1 is suite component - has valid SuiteProvider",
111: suiteProvider.getSuiteDirectory());
112:
113: assertNull("user.properites.file property doesn't exist",
114: module1.evaluator().getProperty("user.properties.file"));
115: SuiteUtils.removeModuleFromSuite(module1);
116: assertEquals(
117: "user.properties.file resolved for standalone module",
118: FileUtil.normalizeFile(
119: new File(getWorkDirPath(), "build.properties"))
120: .getAbsolutePath(), module1.evaluator()
121: .getProperty("user.properties.file"));
122: spp = SuitePropertiesTest.getSubProjectProvider(suite1);
123: assertEquals("doesn't have suite component", 0, spp
124: .getSubprojects().size());
125: suiteProvider = module1.getLookup().lookup(SuiteProvider.class);
126: assertNull(
127: "module1 became standalone module - doesn't have valid SuiteProvider",
128: suiteProvider.getSuiteDirectory());
129: }
130:
131: public void testRemoveModuleFromSuiteWithDependencies()
132: throws Exception {
133: SuiteProject suite1 = generateSuite("suite1");
134: NbModuleProject module1 = TestBase.generateSuiteComponent(
135: suite1, "module1");
136: NbModuleProject module2 = TestBase.generateSuiteComponent(
137: suite1, "module2");
138:
139: SubprojectProvider spp = SuitePropertiesTest
140: .getSubProjectProvider(suite1);
141: assertEquals("two suite components", 2, spp.getSubprojects()
142: .size());
143:
144: Util.addDependency(module2, module1);
145: ProjectManager.getDefault().saveProject(module2);
146: ProjectXMLManager pxm2 = new ProjectXMLManager(module2);
147: assertEquals("one dependency", 1, pxm2.getDirectDependencies()
148: .size());
149:
150: SuiteUtils.removeModuleFromSuiteWithDependencies(module1);
151: spp = SuitePropertiesTest.getSubProjectProvider(suite1);
152: assertEquals("one suite component", 1, spp.getSubprojects()
153: .size());
154: SuiteProvider suiteProvider = module1.getLookup().lookup(
155: SuiteProvider.class);
156: assertNull(
157: "module1 became standalone module - doesn't have valid SuiteProvider",
158: suiteProvider.getSuiteDirectory());
159:
160: pxm2 = new ProjectXMLManager(module2);
161: assertEquals("dependency was removed", 0, pxm2
162: .getDirectDependencies().size());
163: }
164:
165: /** Simulates scenario when deadlock occurs when playing with 64582. */
166: public void testPreventDeadLockWhenAddThenRemoveModule_64582()
167: throws Exception {
168: SuiteProject suite1 = generateSuite("suite1");
169: NbModuleProject module1 = TestBase.generateStandaloneModule(
170: getWorkDir(), "module1");
171: SuiteUtils.addModule(suite1, module1);
172: SuiteUtils.removeModuleFromSuite(module1);
173: }
174:
175: public void testAddTwoModulesWithTheSameCNB_62819()
176: throws Exception {
177: SuiteProject suite1 = generateSuite("suite1");
178: NbModuleProject module1a = generateStandaloneModule("module1");
179: File otherDir = new File(getWorkDir(), "otherDir");
180: otherDir.mkdir();
181: NbModuleProject module1b = TestBase.generateStandaloneModule(
182: otherDir, "module1");
183:
184: SuiteUtils.addModule(suite1, module1a);
185: SuiteUtils.addModule(suite1, module1b);
186: SubprojectProvider spp = SuitePropertiesTest
187: .getSubProjectProvider(suite1);
188: assertEquals(
189: "cannot add two suite components with the same cnb", 1,
190: spp.getSubprojects().size());
191:
192: SuiteProvider suiteProvider = module1a.getLookup().lookup(
193: SuiteProvider.class);
194: assertNotNull(
195: "module1a became suite component - has valid SuiteProvider",
196: suiteProvider.getSuiteDirectory());
197: suiteProvider = module1b.getLookup()
198: .lookup(SuiteProvider.class);
199: assertNull(
200: "module1b remains standalone - has not valid SuiteProvider",
201: suiteProvider.getSuiteDirectory());
202: }
203:
204: public void testGeneratingOfUniqAntProperty_62819()
205: throws Exception {
206: SuiteProject suite1 = generateSuite("suite1");
207: NbModuleProject module1 = generateStandaloneModule("module1");
208: NbModuleProject module2 = generateStandaloneModule("module2");
209:
210: SuiteUtils.addModule(suite1, module1);
211: FileObject propsFO = suite1.getProjectDirectory()
212: .getFileObject("nbproject/project.properties");
213: EditableProperties props = Util.loadProperties(propsFO);
214: assertEquals("modules property",
215: "${project.org.example.module1}", props
216: .getProperty("modules"));
217: assertEquals("module1 property", "../module1", props
218: .getProperty("project.org.example.module1"));
219:
220: // user is free to do this, although in more sensible way
221: assertEquals("module1 project removed (sanity check)",
222: "../module1", props
223: .remove("project.org.example.module1"));
224: props.setProperty("modules", "${project.org.example.module2}");
225: props.setProperty("project.org.example.module2", "../module1");
226: Util.storeProperties(propsFO, props);
227:
228: SuiteUtils.addModule(suite1, module2);
229: SubprojectProvider spp = SuitePropertiesTest
230: .getSubProjectProvider(suite1);
231: assertEquals("one module suite component", 2, spp
232: .getSubprojects().size());
233:
234: SuiteProvider suiteProvider = module1.getLookup().lookup(
235: SuiteProvider.class);
236: assertNotNull(
237: "module1 became suite component - has valid SuiteProvider",
238: suiteProvider.getSuiteDirectory());
239: suiteProvider = module2.getLookup().lookup(SuiteProvider.class);
240: assertNotNull(
241: "module2 became suite component - has valid SuiteProvider",
242: suiteProvider.getSuiteDirectory());
243: }
244:
245: public void testIsSuite() throws Exception {
246: SuiteProject suite = generateSuite("suite");
247: generateSuiteComponent(suite, "suiteComponent");
248: generateStandaloneModuleDirectory(getWorkDir(), "module");
249: File suiteF = new File(getWorkDir(), "suite");
250: assertTrue(suite + " is a suite", SuiteUtils.isSuite(suiteF));
251: assertFalse(suite + " is not a suite", SuiteUtils
252: .isSuite(new File(suiteF, "suiteComponent")));
253: assertFalse(suite + " is not a suite", SuiteUtils
254: .isSuite(new File(getWorkDir(), "module")));
255: }
256:
257: public void testFindSuiteNotSuiteProject80786() throws Exception {
258: // Check that SuiteUtils.findSuite gracefully ignores a project which is not a suite project.
259: SuiteProject suite = generateSuite("suite");
260: NbModuleProject module = generateSuiteComponent(suite,
261: "suiteComponent");
262: FileObject copy = suite.getProjectDirectory().getParent()
263: .createFolder("copy");
264: DataFolder.findFolder(module.getProjectDirectory()).copy(
265: DataFolder.findFolder(copy));
266: generateStandaloneModuleDirectory(getWorkDir(), "copy");
267: Project modulecopy = ProjectManager.getDefault().findProject(
268: copy.getFileObject("suiteComponent"));
269: assertNotNull(modulecopy);
270: assertNull(SuiteUtils.findSuite(modulecopy));
271: }
272:
273: }
|