KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Arrays JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import org.netbeans.api.project.ProjectManager;
25 import org.netbeans.modules.apisupport.project.TestBase;
26 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
27 import org.netbeans.spi.project.ui.support.ProjectCustomizer;
28 import org.openide.filesystems.FileObject;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.nodes.Node;
31
32 /** Checks the behaviour of enabled module list.
33  *
34  * @author Jaroslav Tulach
35  */

36 public class SuiteCustomizerModuleListTest extends TestBase {
37
38     private FileObject suiteRepoFO;
39     private SuiteProject suite1Prj;
40     private SuiteProperties suite1Props;
41     private FileObject suite1FO;
42     
43     private SuiteCustomizerLibraries customizer;
44     
45     public SuiteCustomizerModuleListTest(String JavaDoc testName) {
46         super(testName);
47     }
48
49     protected void setUp() throws Exception JavaDoc {
50         super.setUp();
51         suiteRepoFO = FileUtil.toFileObject(copyFolder(resolveEEPFile(".")));
52         suite1FO = suiteRepoFO.getFileObject("suite1");
53         suite1Prj = (SuiteProject) ProjectManager.getDefault().findProject(suite1FO);
54         this.suite1Props = new SuiteProperties(suite1Prj, suite1Prj.getHelper(),
55                 suite1Prj.getEvaluator(), SuiteUtils.getSubProjects(suite1Prj));
56         
57         customizer = new SuiteCustomizerLibraries(this.suite1Props, ProjectCustomizer.Category.create("x", "xx", null));
58     }
59
60     public void testDisableCluster() throws Exception JavaDoc {
61         enableAllClusters(false);
62         doDisableCluster(0, true);
63     }
64     
65     public void testDisableCluster2() throws Exception JavaDoc {
66         enableAllClusters(false);
67         doDisableCluster(1, true);
68     }
69     
70     public void testDisableTwoClusters() throws Exception JavaDoc {
71         enableAllClusters(false);
72         
73         String JavaDoc c1 = doDisableCluster(1, true);
74         String JavaDoc c2 = doDisableCluster(2, false);
75         HashSet JavaDoc c = new HashSet JavaDoc();
76         c.add(c1);
77         c.add(c2);
78         
79         String JavaDoc[] xyz = suite1Props.getEnabledClusters();
80         //assertEquals("Two clusters disabled", ???, xyz.length);
81

82         HashSet JavaDoc real = new HashSet JavaDoc(Arrays.asList(xyz));
83         assertFalse(real.containsAll(c));
84     }
85     
86     private String JavaDoc doDisableCluster(int index, boolean doCheck) throws Exception JavaDoc {
87         Node n = customizer.getExplorerManager().getRootContext();
88         Node[] clusters = n.getChildren().getNodes();
89         if (clusters.length <= index) {
90             fail ("Wrong, there should be some clusters. at least: " + index + " and was: " + clusters.length);
91         }
92         Node[] modules = clusters[index].getChildren().getNodes();
93         if (modules.length == 0) {
94             fail("Expected more modules for cluster: " + clusters[index]);
95         }
96
97         setNodeEnabled(clusters[index], false);
98         assertEquals("No modules in disabled clusters",
99                 clusters[index].getChildren().getNodes().length, modules.length);
100         
101         customizer.store();
102         suite1Props.storeProperties();
103         
104         if (doCheck) {
105             String JavaDoc[] xyz = suite1Props.getEnabledClusters();
106             //assertEquals("One cluster is disabled", ???, xyz.length);
107
assertFalse("It's name is name of the node", Arrays.asList(xyz).contains(clusters[index].getName()));
108         }
109         
110         return clusters[index].getName();
111     }
112     
113     public void testDisableModule() throws Exception JavaDoc {
114         enableAllClusters(true);
115         
116         Node n = customizer.getExplorerManager().getRootContext();
117         Node[] clusters = n.getChildren().getNodes();
118         if (clusters.length == 0) {
119             fail("Should be at least one cluster");
120         }
121         Node[] modules = clusters[0].getChildren().getNodes();
122         if (modules.length == 0) {
123             fail("Expected at least one module in cluster: " + clusters[0]);
124         }
125
126         setNodeEnabled(modules[0], false);
127         assertNodeEnabled(modules[0], Boolean.FALSE);
128         
129         customizer.store();
130         suite1Props.storeProperties();
131                 
132         String JavaDoc[] xyz = suite1Props.getDisabledModules();
133         assertEquals("One module is disabled", 1, xyz.length);
134         assertEquals("It's name is name of the node", modules[0].getName(), xyz[0]);
135     }
136     
137     private static void assertNodeEnabled(Node n, Boolean JavaDoc value) throws Exception JavaDoc {
138         org.openide.nodes.Node.PropertySet[] arr = n.getPropertySets();
139         for (int i = 0; i < arr.length; i++) {
140             org.openide.nodes.Node.Property[] x = arr[i].getProperties();
141             for (int j = 0; j < x.length; j++) {
142                 if (x[j].getName().equals("enabled")) {
143                     Object JavaDoc o = x[j].getValue();
144                     assertEquals("Node is correctly enabled/disabled: " + n, value, o);
145                     return;
146                 }
147             }
148         }
149         fail("No enabled property found: " + n);
150     }
151     private static void setNodeEnabled(Node n, boolean value) throws Exception JavaDoc {
152         org.openide.nodes.Node.PropertySet[] arr = n.getPropertySets();
153         for (int i = 0; i < arr.length; i++) {
154             org.openide.nodes.Node.Property[] x = arr[i].getProperties();
155             for (int j = 0; j < x.length; j++) {
156                 if (x[j].getName().equals("enabled")) {
157                     x[j].setValue(Boolean.valueOf(value));
158                     return;
159                 }
160             }
161         }
162         fail("No enabled property found: " + n);
163     }
164
165     private void enableAllClusters(boolean enableModulesAsWell) throws Exception JavaDoc {
166         Node n = customizer.getExplorerManager().getRootContext();
167         Node[] clusters = n.getChildren().getNodes();
168         
169         for (int i = 0; i < clusters.length; i++) {
170             setNodeEnabled(clusters[i], true);
171             if (enableModulesAsWell) {
172                 Node[] modules = clusters[i].getChildren().getNodes();
173                 for (int j = 0; j < modules.length; j++) {
174                     setNodeEnabled(modules[j], true);
175                 }
176             }
177         }
178     }
179 }
180
Popular Tags