KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.swing.JList JavaDoc;
27 import org.netbeans.api.project.Project;
28 import org.netbeans.api.project.ProjectManager;
29 import org.netbeans.modules.apisupport.project.NbModuleProject;
30 import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
31 import org.netbeans.modules.apisupport.project.SuiteProvider;
32 import org.netbeans.modules.apisupport.project.TestBase;
33 import org.netbeans.modules.apisupport.project.Util;
34 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
35 import org.netbeans.modules.apisupport.project.ui.customizer.CustomizerComponentFactory.SuiteSubModulesListModel;
36 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
37 import org.netbeans.spi.project.SubprojectProvider;
38 import org.netbeans.spi.project.support.ant.AntProjectHelper;
39 import org.netbeans.spi.project.support.ant.EditableProperties;
40 import org.openide.util.Mutex;
41 import org.openide.util.MutexException;
42 import org.openide.util.NbCollections;
43
44 /**
45  * Tests {@link SuiteProperties}. Actually also for some classes which
46  * SuiteProperties utilizes - which doesn't mean they shouldn't be tested
47  * individually :)
48  *
49  * @author Martin Krauskopf
50  */

51 public class SuitePropertiesTest extends TestBase {
52     
53     public SuitePropertiesTest(String JavaDoc name) {
54         super(name);
55     }
56     
57     protected void setUp() throws Exception JavaDoc {
58         clearWorkDir();
59         super.setUp();
60         ModuleProperties.runFromTests = true;
61     }
62     
63     public static SuiteProperties getSuiteProperties(SuiteProject suite) throws IOException JavaDoc {
64         SubprojectProvider spp = getSubProjectProvider(suite);
65         Set JavaDoc/*<Project>*/ subModules = spp.getSubprojects();
66         SuiteProperties suiteProps = new SuiteProperties(suite, suite.getHelper(),
67                 suite.getEvaluator(), subModules);
68         return suiteProps;
69     }
70     
71     static SubprojectProvider getSubProjectProvider(Project project) throws IOException JavaDoc {
72         return (SubprojectProvider) project.getLookup().lookup(SubprojectProvider.class);
73     }
74     
75     public void testPropertiesAreLoaded() throws Exception JavaDoc {
76         SuiteProject suite1 = generateSuite("suite1");
77         assert suite1 != null;
78         NbModuleProject module1a = TestBase.generateSuiteComponent(suite1, "module1a");
79         assert module1a != null;
80         NbModuleProject module1b = TestBase.generateSuiteComponent(suite1, "module1b");
81         assert module1b != null;
82         SuiteProperties suite1Props = getSuiteProperties(suite1);
83         
84         assertNotNull(suite1Props.getActivePlatform());
85         assertEquals("platform id: ", NbPlatform.PLATFORM_ID_DEFAULT, suite1Props.getActivePlatform().getID());
86         SuiteSubModulesListModel model = suite1Props.getModulesListModel();
87         assertNotNull(model);
88         assertEquals("number of sub-modules: ", 2, model.getSize());
89         // assert order by display name ("module1a", "module1b")
90
NbModuleProject p = (NbModuleProject) model.getElementAt(0);
91         assertEquals("module1a project first", p.getCodeNameBase(), "org.example.module1a");
92         p = (NbModuleProject) model.getElementAt(1);
93         assertEquals("module1b project first", p.getCodeNameBase(), "org.example.module1b");
94     }
95     
96     public void testRemoveAllSubModules() throws Exception JavaDoc {
97         SuiteProject suite1 = generateSuite("suite1");
98         TestBase.generateSuiteComponent(suite1, "module1a");
99         TestBase.generateSuiteComponent(suite1, "module1b");
100         SuiteProperties suite1Props = getSuiteProperties(suite1);
101         
102         SuiteSubModulesListModel model = suite1Props.getModulesListModel();
103         assertNotNull(model);
104         
105         // simulate removing all items from the list
106
JList JavaDoc moduleList = new JList JavaDoc(model);
107         moduleList.setSelectedIndices(new int[] {0, model.getSize() - 1});
108         model.removeModules(Arrays.asList(moduleList.getSelectedValues()));
109         assertEquals("no subModule should be left", 0, model.getSize());
110         
111         saveProperties(suite1Props);
112         
113         SubprojectProvider spp = getSubProjectProvider(suite1);
114         assertEquals("no module should be left", 0, spp.getSubprojects().size());
115     }
116     
117     public void testAddModule() throws Exception JavaDoc {
118         SuiteProject suite1 = generateSuite("suite1");
119         TestBase.generateSuiteComponent(suite1, "module1");
120         
121         SubprojectProvider spp = getSubProjectProvider(suite1);
122         SuiteProperties suiteProps = getSuiteProperties(suite1);
123         
124         SuiteSubModulesListModel model = suiteProps.getModulesListModel();
125         assertEquals("one module suite component", 1, model.getSize());
126         
127         NbModuleProject module2ToAdd = generateStandaloneModule("module2");
128         NbModuleProject module3ToAdd = generateStandaloneModule("module3");
129         SuiteProvider suiteProvider = (SuiteProvider) module2ToAdd.getLookup().lookup(SuiteProvider.class);
130         assertNull("module2ToAdd is standalone module - doesn't have valid SuiteProvider", suiteProvider.getSuiteDirectory());
131         model.addModule(module2ToAdd);
132         model.addModule(module3ToAdd);
133         
134         saveProperties(suiteProps);
135         
136         assertNotNull("module2ToAdd is standalone module - has valid SuiteProvider", suiteProvider.getSuiteDirectory());
137         
138         suiteProps.refresh(NbCollections.checkedSetByFilter(spp.getSubprojects(), NbModuleProject.class, true));
139         assertNotNull("module2ToAdd is standalone module - has valid SuiteProvider", suiteProvider.getSuiteDirectory());
140         assertEquals("three module suite components", 3, suiteProps.getModulesListModel().getSize());
141     }
142     
143     public void testRemoveOneSubModule() throws Exception JavaDoc {
144         SuiteProject suite1 = generateSuite("suite1");
145         NbModuleProject module1a = TestBase.generateSuiteComponent(suite1, "module1a");
146         TestBase.generateSuiteComponent(suite1, "module1b");
147         SuiteProperties suite1Props = getSuiteProperties(suite1);
148         
149         SuiteSubModulesListModel model = suite1Props.getModulesListModel();
150         assertNotNull(model);
151         
152         // simulate removing first item from the list
153
JList JavaDoc moduleList = new JList JavaDoc(model);
154         moduleList.setSelectedIndex(0);
155         model.removeModules(Arrays.asList(moduleList.getSelectedValues()));
156         assertEquals("one subModule should left", 1, model.getSize());
157         
158         saveProperties(suite1Props);
159         
160         SubprojectProvider spp = getSubProjectProvider(suite1);
161         assertEquals("one module should be left", 1, spp.getSubprojects().size());
162         NbModuleProject project = (NbModuleProject) spp.getSubprojects().toArray()[0];
163         assertEquals("module1b should be the one", "org.example.module1b", project.getCodeNameBase());
164         NbModuleProvider libProjectNmtp = (NbModuleProvider) project.
165                 getLookup().lookup(NbModuleProvider.class);
166         assertSame("module1b module is still suite component module", NbModuleProvider.SUITE_COMPONENT,
167                 libProjectNmtp.getModuleType());
168         
169         // assert that the remove module (module1a) is standalone
170
NbModuleProvider module1aNmtp = (NbModuleProvider) module1a.
171                 getLookup().lookup(NbModuleProvider.class);
172         assertNotNull(module1aNmtp);
173         assertSame("module1a module is standalone module now", NbModuleProvider.STANDALONE,
174                 module1aNmtp.getModuleType());
175     }
176     
177     public void testMoveSubModuleBetweenSuites() throws Exception JavaDoc {
178         SuiteProject suite1 = generateSuite("suite1");
179         TestBase.generateSuiteComponent(suite1, "module1a");
180         TestBase.generateSuiteComponent(suite1, "module1b");
181         TestBase.generateSuiteComponent(suite1, "module1c");
182         
183         SuiteProject suite2 = generateSuite("suite2");
184         NbModuleProject module2a = TestBase.generateSuiteComponent(suite2, "module2a");
185         
186         // simulate addition of module2a to the suite1
187
SuiteProperties suite1Props = getSuiteProperties(suite1);
188         SuiteSubModulesListModel suite1model = suite1Props.getModulesListModel();
189         suite1model.addModule(module2a);
190         
191         saveProperties(suite1Props); // saves all changes
192

193         // assert module2a is part of suite1 (has moved from suite2)....
194
SubprojectProvider suite1spp = getSubProjectProvider(suite1);
195         assertEquals("four module", 4, suite1spp.getSubprojects().size());
196         assertTrue("module2a has moved to suite1", suite1spp.getSubprojects().contains(module2a));
197         
198         // ....and as such has correctly set suite provider
199
SuiteProvider sp = (SuiteProvider) module2a.getLookup().lookup(SuiteProvider.class);
200         assertNotNull(sp);
201         assertNotNull(sp.getSuiteDirectory());
202         assertEquals("module2a has suite1 as a SuiteProvider", suite1.getProjectDirectoryFile(), sp.getSuiteDirectory());
203         
204         // assert module2a is not part of suite2 (has moved to suite1)....
205
SubprojectProvider suite2spp = getSubProjectProvider(suite2);
206         assertEquals("no module", 0, suite2spp.getSubprojects().size());
207         assertFalse("module2a is not part of suite2", suite2spp.getSubprojects().contains(module2a));
208     }
209     
210     public void testRemovingSecondModuleFromThree_63307() throws Exception JavaDoc {
211         SuiteProject suite1 = generateSuite("suite1");
212         TestBase.generateSuiteComponent(suite1, "module1");
213         NbModuleProject module2 = TestBase.generateSuiteComponent(suite1, "module2");
214         TestBase.generateSuiteComponent(suite1, "module3");
215         
216         SubprojectProvider spp = getSubProjectProvider(suite1);
217         SuiteProperties suiteProps = getSuiteProperties(suite1);
218         
219         SuiteSubModulesListModel model = suiteProps.getModulesListModel();
220         assertEquals("three module suite components", 3, model.getSize());
221         model.removeModules(Arrays.asList(new Object JavaDoc[] { module2 }));
222         
223         saveProperties(suiteProps);
224         
225         suiteProps.refresh(NbCollections.checkedSetByFilter(spp.getSubprojects(), NbModuleProject.class, true));
226         assertEquals("two module suite components", 2, suiteProps.getModulesListModel().getSize());
227     }
228     
229     public void testRefreshing_63307() throws Exception JavaDoc {
230         SuiteProject suite1 = generateSuite("suite1");
231         TestBase.generateSuiteComponent(suite1, "module1");
232         SubprojectProvider spp = getSubProjectProvider(suite1);
233         SuiteProperties suiteProps = getSuiteProperties(suite1);
234         assertEquals("one module", "${project.org.example.module1}", suiteProps.getProperty(SuiteUtils.MODULES_PROPERTY));
235         TestBase.generateSuiteComponent(suite1, "module2");
236         suiteProps.refresh(NbCollections.checkedSetByFilter(spp.getSubprojects(), NbModuleProject.class, true));
237         assertEquals("two modules", "${project.org.example.module1}:${project.org.example.module2}", suiteProps.getProperty(SuiteUtils.MODULES_PROPERTY));
238         assertEquals("two module suite component", 2, suiteProps.getModulesListModel().getSize());
239     }
240     
241     public void testRefreshingWithRemovedPlatformDoesNotThrowNPE() throws Exception JavaDoc {
242         SuiteProject suite1 = TestBase.generateSuite(getWorkDir(), "suite1", "custom");
243         SubprojectProvider spp = getSubProjectProvider(suite1);
244         SuiteProperties suiteProps = getSuiteProperties(suite1);
245         NbPlatform.removePlatform(NbPlatform.getPlatformByID("custom"));
246         suiteProps.refresh(NbCollections.checkedSetByFilter(spp.getSubprojects(), NbModuleProject.class, true));
247         assertNotNull(suiteProps.getActivePlatform());
248         assertEquals("default platform", suiteProps.getActivePlatform(), NbPlatform.getDefaultPlatform());
249     }
250     
251     public void testCustomPropertiesReferences_61318() throws Exception JavaDoc {
252         final SuiteProject suite1 = generateSuite("suite1");
253         TestBase.generateSuiteComponent(suite1, "module1");
254         NbModuleProject module2 = TestBase.generateSuiteComponent(suite1, "module2");
255         TestBase.generateSuiteComponent(suite1, "module3");
256         
257         SubprojectProvider spp = getSubProjectProvider(suite1);
258         final SuiteProperties suiteProps = getSuiteProperties(suite1);
259         
260         SuiteSubModulesListModel model = suiteProps.getModulesListModel();
261         assertEquals("three module suite components", 3, model.getSize());
262         
263         ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() {
264             public Object JavaDoc run() throws Exception JavaDoc {
265                 // choose another way to store submodules
266
EditableProperties edProps = suiteProps.getProjectProperties();
267                 edProps.setProperty("moddir", ".");
268                 edProps.setProperty("modules", "${moddir}/module1:${moddir}/module2:${moddir}/module3");
269                 Util.storeProperties(
270                         suite1.getProjectDirectory().getFileObject(AntProjectHelper.PROJECT_PROPERTIES_PATH),
271                         edProps);
272                 return null;
273             }
274         });
275         
276         suiteProps.refresh(NbCollections.checkedSetByFilter(spp.getSubprojects(), NbModuleProject.class, true));
277         model = suiteProps.getModulesListModel(); // reload
278
assertEquals("three module suite components", 3, model.getSize());
279         model.removeModules(Arrays.asList(new Object JavaDoc[] { module2 }));
280         saveProperties(suiteProps);
281         
282         suiteProps.refresh(NbCollections.checkedSetByFilter(spp.getSubprojects(), NbModuleProject.class, true));
283         model = suiteProps.getModulesListModel(); // reload
284
assertEquals("two module suite components", 2, model.getSize());
285     }
286     
287     public void testSuitePropertiesWithAnonymousPlatform() throws Exception JavaDoc { // #73795
288
SuiteProject suite1 = TestBase.generateSuite(getWorkDir(), "suite1", "custom");
289         SuiteProperties suiteProps = getSuiteProperties(suite1);
290         File JavaDoc destDir = NbPlatform.getPlatformByID("custom").getDestDir();
291         NbPlatform.removePlatform(NbPlatform.getPlatformByID("custom"));
292         suiteProps.setActivePlatform(NbPlatform.getPlatformByDestDir(destDir));
293         saveProperties(suiteProps);
294     }
295     
296     private void saveProperties(final SuiteProperties props) throws IOException JavaDoc {
297         try {
298             // Store properties
299
ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction() {
300                 public Object JavaDoc run() throws IOException JavaDoc {
301                     props.storeProperties();
302                     return null;
303                 }
304             });
305             ProjectManager.getDefault().saveProject(props.getProject());
306         } catch (MutexException e) {
307             throw (IOException JavaDoc) e.getException();
308         }
309     }
310     
311 }
312
313
Popular Tags