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.util.Arrays;
045: import java.util.HashSet;
046: import java.util.Set;
047: import org.netbeans.api.project.ProjectManager;
048: import org.netbeans.modules.apisupport.project.TestBase;
049: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
050: import org.netbeans.spi.project.ui.support.ProjectCustomizer;
051: import org.openide.filesystems.FileObject;
052: import org.openide.filesystems.FileUtil;
053: import org.openide.nodes.Node;
054: import org.openide.util.Utilities;
055: import org.openide.util.test.RestrictThreadCreation;
056:
057: /** Checks the behaviour of enabled module list.
058: *
059: * @author Jaroslav Tulach
060: */
061: public class SuiteCustomizerModuleListTest extends TestBase {
062:
063: static {
064: SuiteCustomizerLibraries.TEST = true;
065: RestrictThreadCreation.permitStandard();
066: RestrictThreadCreation.forbidNewThreads(true);
067: }
068:
069: private FileObject suiteRepoFO;
070: private SuiteProject suite1Prj;
071: private SuiteProperties suite1Props;
072: private FileObject suite1FO;
073:
074: private SuiteCustomizerLibraries customizer;
075:
076: public SuiteCustomizerModuleListTest(String testName) {
077: super (testName);
078: }
079:
080: @Override
081: public boolean canRun() {
082: // On Windows XP, under JDK 6 (but, oddly, not 5), get apparent path length limit violations:
083: // java.lang.AssertionError: E:\space\test4u\builds\bindist_netbeans_Dev_daily_latest\ unit\apisupport1\org-netbeans-modules-apisupport-project\work\sys\data\example-external-projects\.\suite1\support\lib-project\test\ unit\src\org\netbeans\examples\modules\lib\LibClassTest.java
084: // at org.netbeans.modules.apisupport.project.TestBase.doCopy(TestBase.java:272)
085: // at org.netbeans.modules.apisupport.project.TestBase.doCopy(TestBase.java:269)
086: // [....]
087: // See e.g.: http://beetle.czech.sun.com/automatedtests/xtest/netbeans_dev/200704301800/qa-unit_stable/qa-t4u-xp2_1/testrun_070501-125959/testbag_111/htmlresults/suites/TEST-org.netbeans.modules.apisupport.project.ui.customizer.SuiteCustomizerModuleListTest.html
088: return super .canRun() && !Utilities.isWindows();
089: }
090:
091: protected void setUp() throws Exception {
092: super .setUp();
093: suiteRepoFO = FileUtil
094: .toFileObject(copyFolder(resolveEEPFile(".")));
095: suite1FO = suiteRepoFO.getFileObject("suite1");
096: suite1Prj = (SuiteProject) ProjectManager.getDefault()
097: .findProject(suite1FO);
098: this .suite1Props = new SuiteProperties(suite1Prj, suite1Prj
099: .getHelper(), suite1Prj.getEvaluator(), SuiteUtils
100: .getSubProjects(suite1Prj));
101:
102: customizer = new SuiteCustomizerLibraries(this .suite1Props,
103: ProjectCustomizer.Category.create("x", "xx", null));
104: }
105:
106: public void testDisableCluster() throws Exception {
107: enableAllClusters(false);
108: doDisableCluster(0, true);
109: }
110:
111: public void testDisableCluster2() throws Exception {
112: enableAllClusters(false);
113: doDisableCluster(1, true);
114: }
115:
116: public void testDisableTwoClusters() throws Exception {
117: enableAllClusters(false);
118:
119: String c1 = doDisableCluster(1, true);
120: String c2 = doDisableCluster(2, false);
121: Set<String> c = new HashSet<String>();
122: c.add(c1);
123: c.add(c2);
124:
125: String[] xyz = suite1Props.getEnabledClusters();
126: //assertEquals("Two clusters disabled", ???, xyz.length);
127:
128: Set<String> real = new HashSet<String>(Arrays.asList(xyz));
129: assertFalse(real.containsAll(c));
130: }
131:
132: private String doDisableCluster(int index, boolean doCheck)
133: throws Exception {
134: Node n = customizer.getExplorerManager().getRootContext();
135: Node[] clusters = n.getChildren().getNodes();
136: if (clusters.length <= index) {
137: fail("Wrong, there should be some clusters. at least: "
138: + index + " and was: " + clusters.length);
139: }
140: Node[] modules = clusters[index].getChildren().getNodes();
141: if (modules.length == 0) {
142: fail("Expected more modules for cluster: "
143: + clusters[index]);
144: }
145:
146: setNodeEnabled(clusters[index], false);
147: assertEquals("No modules in disabled clusters", clusters[index]
148: .getChildren().getNodes().length, modules.length);
149:
150: customizer.store();
151: suite1Props.storeProperties();
152:
153: if (doCheck) {
154: String[] xyz = suite1Props.getEnabledClusters();
155: //assertEquals("One cluster is disabled", ???, xyz.length);
156: assertFalse("It's name is name of the node", Arrays.asList(
157: xyz).contains(clusters[index].getName()));
158: }
159:
160: return clusters[index].getName();
161: }
162:
163: public void testDisableModule() throws Exception {
164: enableAllClusters(true);
165:
166: Node n = customizer.getExplorerManager().getRootContext();
167: Node[] clusters = n.getChildren().getNodes();
168: if (clusters.length == 0) {
169: fail("Should be at least one cluster");
170: }
171: Node[] modules = clusters[0].getChildren().getNodes();
172: if (modules.length == 0) {
173: fail("Expected at least one module in cluster: "
174: + clusters[0]);
175: }
176:
177: setNodeEnabled(modules[0], false);
178: assertNodeEnabled(modules[0], Boolean.FALSE);
179:
180: customizer.store();
181: suite1Props.storeProperties();
182:
183: String[] xyz = suite1Props.getDisabledModules();
184: assertEquals("One module is disabled", 1, xyz.length);
185: assertEquals("It's name is name of the node", modules[0]
186: .getName(), xyz[0]);
187: }
188:
189: private static void assertNodeEnabled(Node n, Boolean value)
190: throws Exception {
191: for (Node.PropertySet ps : n.getPropertySets()) {
192: for (Node.Property<?> prop : ps.getProperties()) {
193: if (prop.getName().equals("enabled")) {
194: Object o = prop.getValue();
195: assertEquals("Node is correctly enabled/disabled: "
196: + n, value, o);
197: return;
198: }
199: }
200: }
201: fail("No enabled property found: " + n);
202: }
203:
204: private static void setNodeEnabled(Node n, boolean value)
205: throws Exception {
206: for (Node.PropertySet ps : n.getPropertySets()) {
207: for (Node.Property<?> prop : ps.getProperties()) {
208: if (prop.getName().equals("enabled")) {
209: @SuppressWarnings("unchecked")
210: // value type is Boolean.TYPE, not Boolean.class, so Class.<T>cast will not help
211: Node.Property<Boolean> _prop = (Node.Property<Boolean>) prop;
212: _prop.setValue(value);
213: return;
214: }
215: }
216: }
217: fail("No enabled property found: " + n);
218: }
219:
220: private void enableAllClusters(boolean enableModulesAsWell)
221: throws Exception {
222: Node n = customizer.getExplorerManager().getRootContext();
223: for (Node cluster : n.getChildren().getNodes()) {
224: setNodeEnabled(cluster, true);
225: if (enableModulesAsWell) {
226: for (Node module : cluster.getChildren().getNodes()) {
227: setNodeEnabled(module, true);
228: }
229: }
230: }
231: }
232: }
|