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.io.IOException;
046: import java.util.Arrays;
047: import java.util.Set;
048: import javax.swing.JList;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.api.project.ProjectManager;
051: import org.netbeans.modules.apisupport.project.NbModuleProject;
052: import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
053: import org.netbeans.modules.apisupport.project.SuiteProvider;
054: import org.netbeans.modules.apisupport.project.TestBase;
055: import org.netbeans.modules.apisupport.project.Util;
056: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
057: import org.netbeans.modules.apisupport.project.ui.customizer.CustomizerComponentFactory.SuiteSubModulesListModel;
058: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
059: import org.netbeans.spi.project.SubprojectProvider;
060: import org.netbeans.spi.project.support.ant.AntProjectHelper;
061: import org.netbeans.spi.project.support.ant.EditableProperties;
062: import org.openide.util.Mutex;
063: import org.openide.util.MutexException;
064: import org.openide.util.NbCollections;
065:
066: /**
067: * Tests {@link SuiteProperties}. Actually also for some classes which
068: * SuiteProperties utilizes - which doesn't mean they shouldn't be tested
069: * individually :)
070: *
071: * @author Martin Krauskopf
072: */
073: public class SuitePropertiesTest extends TestBase {
074:
075: public SuitePropertiesTest(String name) {
076: super (name);
077: }
078:
079: protected void setUp() throws Exception {
080: clearWorkDir();
081: super .setUp();
082: ModuleProperties.runFromTests = true;
083: }
084:
085: public static SuiteProperties getSuiteProperties(SuiteProject suite)
086: throws IOException {
087: SubprojectProvider spp = getSubProjectProvider(suite);
088: Set<NbModuleProject> subModules = NbCollections
089: .checkedSetByCopy(spp.getSubprojects(),
090: NbModuleProject.class, true);
091: SuiteProperties suiteProps = new SuiteProperties(suite, suite
092: .getHelper(), suite.getEvaluator(), subModules);
093: return suiteProps;
094: }
095:
096: static SubprojectProvider getSubProjectProvider(Project project)
097: throws IOException {
098: return project.getLookup().lookup(SubprojectProvider.class);
099: }
100:
101: public void testPropertiesAreLoaded() throws Exception {
102: SuiteProject suite1 = generateSuite("suite1");
103: assert suite1 != null;
104: NbModuleProject module1a = TestBase.generateSuiteComponent(
105: suite1, "module1a");
106: assert module1a != null;
107: NbModuleProject module1b = TestBase.generateSuiteComponent(
108: suite1, "module1b");
109: assert module1b != null;
110: SuiteProperties suite1Props = getSuiteProperties(suite1);
111:
112: assertNotNull(suite1Props.getActivePlatform());
113: assertEquals("platform id: ", NbPlatform.PLATFORM_ID_DEFAULT,
114: suite1Props.getActivePlatform().getID());
115: SuiteSubModulesListModel model = suite1Props
116: .getModulesListModel();
117: assertNotNull(model);
118: assertEquals("number of sub-modules: ", 2, model.getSize());
119: // assert order by display name ("module1a", "module1b")
120: NbModuleProject p = (NbModuleProject) model.getElementAt(0);
121: assertEquals("module1a project first", p.getCodeNameBase(),
122: "org.example.module1a");
123: p = (NbModuleProject) model.getElementAt(1);
124: assertEquals("module1b project first", p.getCodeNameBase(),
125: "org.example.module1b");
126: }
127:
128: public void testRemoveAllSubModules() throws Exception {
129: SuiteProject suite1 = generateSuite("suite1");
130: TestBase.generateSuiteComponent(suite1, "module1a");
131: TestBase.generateSuiteComponent(suite1, "module1b");
132: SuiteProperties suite1Props = getSuiteProperties(suite1);
133:
134: SuiteSubModulesListModel model = suite1Props
135: .getModulesListModel();
136: assertNotNull(model);
137:
138: // simulate removing all items from the list
139: JList moduleList = new JList(model);
140: moduleList.setSelectedIndices(new int[] { 0,
141: model.getSize() - 1 });
142: model.removeModules(Arrays.asList(moduleList
143: .getSelectedValues()));
144: assertEquals("no subModule should be left", 0, model.getSize());
145:
146: saveProperties(suite1Props);
147:
148: SubprojectProvider spp = getSubProjectProvider(suite1);
149: assertEquals("no module should be left", 0, spp
150: .getSubprojects().size());
151: }
152:
153: public void testAddModule() throws Exception {
154: SuiteProject suite1 = generateSuite("suite1");
155: TestBase.generateSuiteComponent(suite1, "module1");
156:
157: SubprojectProvider spp = getSubProjectProvider(suite1);
158: SuiteProperties suiteProps = getSuiteProperties(suite1);
159:
160: SuiteSubModulesListModel model = suiteProps
161: .getModulesListModel();
162: assertEquals("one module suite component", 1, model.getSize());
163:
164: NbModuleProject module2ToAdd = generateStandaloneModule("module2");
165: NbModuleProject module3ToAdd = generateStandaloneModule("module3");
166: SuiteProvider suiteProvider = module2ToAdd.getLookup().lookup(
167: SuiteProvider.class);
168: assertNull(
169: "module2ToAdd is standalone module - doesn't have valid SuiteProvider",
170: suiteProvider.getSuiteDirectory());
171: model.addModule(module2ToAdd);
172: model.addModule(module3ToAdd);
173:
174: saveProperties(suiteProps);
175:
176: assertNotNull(
177: "module2ToAdd is standalone module - has valid SuiteProvider",
178: suiteProvider.getSuiteDirectory());
179:
180: suiteProps.refresh(NbCollections.checkedSetByFilter(spp
181: .getSubprojects(), NbModuleProject.class, true));
182: assertNotNull(
183: "module2ToAdd is standalone module - has valid SuiteProvider",
184: suiteProvider.getSuiteDirectory());
185: assertEquals("three module suite components", 3, suiteProps
186: .getModulesListModel().getSize());
187: }
188:
189: public void testRemoveOneSubModule() throws Exception {
190: SuiteProject suite1 = generateSuite("suite1");
191: NbModuleProject module1a = TestBase.generateSuiteComponent(
192: suite1, "module1a");
193: TestBase.generateSuiteComponent(suite1, "module1b");
194: SuiteProperties suite1Props = getSuiteProperties(suite1);
195:
196: SuiteSubModulesListModel model = suite1Props
197: .getModulesListModel();
198: assertNotNull(model);
199:
200: // simulate removing first item from the list
201: JList moduleList = new JList(model);
202: moduleList.setSelectedIndex(0);
203: model.removeModules(Arrays.asList(moduleList
204: .getSelectedValues()));
205: assertEquals("one subModule should left", 1, model.getSize());
206:
207: saveProperties(suite1Props);
208:
209: SubprojectProvider spp = getSubProjectProvider(suite1);
210: assertEquals("one module should be left", 1, spp
211: .getSubprojects().size());
212: NbModuleProject project = (NbModuleProject) spp
213: .getSubprojects().toArray()[0];
214: assertEquals("module1b should be the one",
215: "org.example.module1b", project.getCodeNameBase());
216: NbModuleProvider libProjectNmtp = project.getLookup().lookup(
217: NbModuleProvider.class);
218: assertSame("module1b module is still suite component module",
219: NbModuleProvider.SUITE_COMPONENT, libProjectNmtp
220: .getModuleType());
221:
222: // assert that the remove module (module1a) is standalone
223: NbModuleProvider module1aNmtp = module1a.getLookup().lookup(
224: NbModuleProvider.class);
225: assertNotNull(module1aNmtp);
226: assertSame("module1a module is standalone module now",
227: NbModuleProvider.STANDALONE, module1aNmtp
228: .getModuleType());
229: }
230:
231: public void testMoveSubModuleBetweenSuites() throws Exception {
232: SuiteProject suite1 = generateSuite("suite1");
233: TestBase.generateSuiteComponent(suite1, "module1a");
234: TestBase.generateSuiteComponent(suite1, "module1b");
235: TestBase.generateSuiteComponent(suite1, "module1c");
236:
237: SuiteProject suite2 = generateSuite("suite2");
238: NbModuleProject module2a = TestBase.generateSuiteComponent(
239: suite2, "module2a");
240:
241: // simulate addition of module2a to the suite1
242: SuiteProperties suite1Props = getSuiteProperties(suite1);
243: SuiteSubModulesListModel suite1model = suite1Props
244: .getModulesListModel();
245: suite1model.addModule(module2a);
246:
247: saveProperties(suite1Props); // saves all changes
248:
249: // assert module2a is part of suite1 (has moved from suite2)....
250: SubprojectProvider suite1spp = getSubProjectProvider(suite1);
251: assertEquals("four module", 4, suite1spp.getSubprojects()
252: .size());
253: assertTrue("module2a has moved to suite1", suite1spp
254: .getSubprojects().contains(module2a));
255:
256: // ....and as such has correctly set suite provider
257: SuiteProvider sp = module2a.getLookup().lookup(
258: SuiteProvider.class);
259: assertNotNull(sp);
260: assertNotNull(sp.getSuiteDirectory());
261: assertEquals("module2a has suite1 as a SuiteProvider", suite1
262: .getProjectDirectoryFile(), sp.getSuiteDirectory());
263:
264: // assert module2a is not part of suite2 (has moved to suite1)....
265: SubprojectProvider suite2spp = getSubProjectProvider(suite2);
266: assertEquals("no module", 0, suite2spp.getSubprojects().size());
267: assertFalse("module2a is not part of suite2", suite2spp
268: .getSubprojects().contains(module2a));
269: }
270:
271: public void testRemovingSecondModuleFromThree_63307()
272: throws Exception {
273: SuiteProject suite1 = generateSuite("suite1");
274: TestBase.generateSuiteComponent(suite1, "module1");
275: NbModuleProject module2 = TestBase.generateSuiteComponent(
276: suite1, "module2");
277: TestBase.generateSuiteComponent(suite1, "module3");
278:
279: SubprojectProvider spp = getSubProjectProvider(suite1);
280: SuiteProperties suiteProps = getSuiteProperties(suite1);
281:
282: SuiteSubModulesListModel model = suiteProps
283: .getModulesListModel();
284: assertEquals("three module suite components", 3, model
285: .getSize());
286: model.removeModules(Arrays.asList(new Object[] { module2 }));
287:
288: saveProperties(suiteProps);
289:
290: suiteProps.refresh(NbCollections.checkedSetByFilter(spp
291: .getSubprojects(), NbModuleProject.class, true));
292: assertEquals("two module suite components", 2, suiteProps
293: .getModulesListModel().getSize());
294: }
295:
296: public void testRefreshing_63307() throws Exception {
297: SuiteProject suite1 = generateSuite("suite1");
298: TestBase.generateSuiteComponent(suite1, "module1");
299: SubprojectProvider spp = getSubProjectProvider(suite1);
300: SuiteProperties suiteProps = getSuiteProperties(suite1);
301: assertEquals("one module", "${project.org.example.module1}",
302: suiteProps.getProperty(SuiteUtils.MODULES_PROPERTY));
303: TestBase.generateSuiteComponent(suite1, "module2");
304: suiteProps.refresh(NbCollections.checkedSetByFilter(spp
305: .getSubprojects(), NbModuleProject.class, true));
306: assertEquals(
307: "two modules",
308: "${project.org.example.module1}:${project.org.example.module2}",
309: suiteProps.getProperty(SuiteUtils.MODULES_PROPERTY));
310: assertEquals("two module suite component", 2, suiteProps
311: .getModulesListModel().getSize());
312: }
313:
314: public void testRefreshingWithRemovedPlatformDoesNotThrowNPE()
315: throws Exception {
316: SuiteProject suite1 = TestBase.generateSuite(getWorkDir(),
317: "suite1", "custom");
318: SubprojectProvider spp = getSubProjectProvider(suite1);
319: SuiteProperties suiteProps = getSuiteProperties(suite1);
320: NbPlatform.removePlatform(NbPlatform.getPlatformByID("custom"));
321: suiteProps.refresh(NbCollections.checkedSetByFilter(spp
322: .getSubprojects(), NbModuleProject.class, true));
323: assertNotNull(suiteProps.getActivePlatform());
324: assertEquals("default platform",
325: suiteProps.getActivePlatform(), NbPlatform
326: .getDefaultPlatform());
327: }
328:
329: public void testCustomPropertiesReferences_61318() throws Exception {
330: final SuiteProject suite1 = generateSuite("suite1");
331: TestBase.generateSuiteComponent(suite1, "module1");
332: NbModuleProject module2 = TestBase.generateSuiteComponent(
333: suite1, "module2");
334: TestBase.generateSuiteComponent(suite1, "module3");
335:
336: SubprojectProvider spp = getSubProjectProvider(suite1);
337: final SuiteProperties suiteProps = getSuiteProperties(suite1);
338:
339: SuiteSubModulesListModel model = suiteProps
340: .getModulesListModel();
341: assertEquals("three module suite components", 3, model
342: .getSize());
343:
344: ProjectManager.mutex().writeAccess(
345: new Mutex.ExceptionAction<Void>() {
346: public Void run() throws Exception {
347: // choose another way to store submodules
348: EditableProperties edProps = suiteProps
349: .getProjectProperties();
350: edProps.setProperty("moddir", ".");
351: edProps
352: .setProperty("modules",
353: "${moddir}/module1:${moddir}/module2:${moddir}/module3");
354: Util
355: .storeProperties(
356: suite1
357: .getProjectDirectory()
358: .getFileObject(
359: AntProjectHelper.PROJECT_PROPERTIES_PATH),
360: edProps);
361: return null;
362: }
363: });
364:
365: suiteProps.refresh(NbCollections.checkedSetByFilter(spp
366: .getSubprojects(), NbModuleProject.class, true));
367: model = suiteProps.getModulesListModel(); // reload
368: assertEquals("three module suite components", 3, model
369: .getSize());
370: model.removeModules(Arrays.asList(new Object[] { module2 }));
371: saveProperties(suiteProps);
372:
373: suiteProps.refresh(NbCollections.checkedSetByFilter(spp
374: .getSubprojects(), NbModuleProject.class, true));
375: model = suiteProps.getModulesListModel(); // reload
376: assertEquals("two module suite components", 2, model.getSize());
377: }
378:
379: public void testSuitePropertiesWithAnonymousPlatform()
380: throws Exception { // #73795
381: SuiteProject suite1 = TestBase.generateSuite(getWorkDir(),
382: "suite1", "custom");
383: SuiteProperties suiteProps = getSuiteProperties(suite1);
384: File destDir = NbPlatform.getPlatformByID("custom")
385: .getDestDir();
386: NbPlatform.removePlatform(NbPlatform.getPlatformByID("custom"));
387: suiteProps.setActivePlatform(NbPlatform
388: .getPlatformByDestDir(destDir));
389: saveProperties(suiteProps);
390: }
391:
392: private void saveProperties(final SuiteProperties props)
393: throws IOException {
394: try {
395: // Store properties
396: ProjectManager.mutex().writeAccess(
397: new Mutex.ExceptionAction<Void>() {
398: public Void run() throws IOException {
399: props.storeProperties();
400: return null;
401: }
402: });
403: ProjectManager.getDefault().saveProject(props.getProject());
404: } catch (MutexException e) {
405: throw (IOException) e.getException();
406: }
407: }
408:
409: }
|