KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > editor > EditorOptionsTest


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.options.editor;
21
22 import java.net.URL JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.spi.options.OptionsCategory;
29 import org.netbeans.spi.options.OptionsPanelController;
30 import org.openide.filesystems.FileObject;
31 import org.openide.filesystems.Repository;
32 import org.openide.loaders.DataFolder;
33 import org.openide.loaders.FolderLookup;
34 import org.openide.util.Lookup;
35
36 import org.netbeans.modules.options.macros.MacrosPanelController;
37 import org.openide.util.lookup.Lookups;
38 import org.openide.util.lookup.ProxyLookup;
39
40
41 /**
42  *
43  * @author Jan Jancura
44  */

45 public class EditorOptionsTest extends NbTestCase {
46     
47     
48     static {
49         IDEInitializer.setup (
50             new String JavaDoc[] {
51                 "org/netbeans/modules/options/editor/mf-layer.xml",
52                 "org/netbeans/modules/defaults/mf-layer.xml"
53             },
54             new Object JavaDoc[] {}
55         );
56     }
57     
58     public EditorOptionsTest (String JavaDoc testName) {
59         super (testName);
60     }
61     
62     public void testOptions () {
63         assertEquals (4, getCategories ().size ());
64     }
65     
66     public void testOptionsCategories () {
67         Iterator JavaDoc it = getCategories ().iterator ();
68         while (it.hasNext ()) {
69             OptionsCategory oc = (OptionsCategory) it.next ();
70             assertNotNull (oc.getCategoryName ());
71             assertNotNull (oc.getIconBase ());
72             assertNotNull (oc.getTitle ());
73         }
74     }
75     
76     public void testUpdateOk () {
77         List JavaDoc controllers = new ArrayList JavaDoc ();
78         List JavaDoc lookups = new ArrayList JavaDoc ();
79         Iterator JavaDoc it = getCategories ().iterator ();
80         while (it.hasNext ()) {
81             OptionsCategory oc = (OptionsCategory) it.next ();
82             OptionsPanelController pc = oc.create ();
83             controllers.add (pc);
84             lookups.add (pc.getLookup ());
85         }
86         Lookup masterLookup = new ProxyLookup
87             ((Lookup[]) lookups.toArray (new Lookup [lookups.size ()]));
88         it = controllers.iterator ();
89         while (it.hasNext ()) {
90             OptionsPanelController pc = (OptionsPanelController) it.next ();
91             JComponent JavaDoc c = pc.getComponent (masterLookup);
92             pc.update ();
93             pc.applyChanges ();
94         }
95     }
96     
97     public void testUpdateCancel () {
98         List JavaDoc controllers = new ArrayList JavaDoc ();
99         List JavaDoc lookups = new ArrayList JavaDoc ();
100         Iterator JavaDoc it = getCategories ().iterator ();
101         while (it.hasNext ()) {
102             OptionsCategory oc = (OptionsCategory) it.next ();
103             OptionsPanelController pc = oc.create ();
104             controllers.add (pc);
105             lookups.add (pc.getLookup ());
106         }
107         Lookup masterLookup = new ProxyLookup
108             ((Lookup[]) lookups.toArray (new Lookup [lookups.size ()]));
109         it = controllers.iterator ();
110         while (it.hasNext ()) {
111             OptionsPanelController pc = (OptionsPanelController) it.next ();
112             JComponent JavaDoc c = pc.getComponent (masterLookup);
113             pc.update ();
114             pc.cancel ();
115         }
116     }
117     
118     public void testOk () {
119         List JavaDoc controllers = new ArrayList JavaDoc ();
120         List JavaDoc lookups = new ArrayList JavaDoc ();
121         Iterator JavaDoc it = getCategories ().iterator ();
122         while (it.hasNext ()) {
123             OptionsCategory oc = (OptionsCategory) it.next ();
124             OptionsPanelController pc = oc.create ();
125             controllers.add (pc);
126             lookups.add (pc.getLookup ());
127         }
128         Lookup masterLookup = new ProxyLookup
129             ((Lookup[]) lookups.toArray (new Lookup [lookups.size ()]));
130         it = controllers.iterator ();
131         while (it.hasNext ()) {
132             OptionsPanelController pc = (OptionsPanelController) it.next ();
133             JComponent JavaDoc c = pc.getComponent (masterLookup);
134             pc.applyChanges ();
135         }
136     }
137     
138     public void testCancel () {
139         
140         // 1) load PanelControllers and init master lookup
141
List JavaDoc controllers = new ArrayList JavaDoc ();
142         List JavaDoc lookups = new ArrayList JavaDoc ();
143         Iterator JavaDoc it = getCategories ().iterator ();
144         while (it.hasNext ()) {
145             OptionsCategory oc = (OptionsCategory) it.next ();
146             OptionsPanelController pc = oc.create ();
147             controllers.add (pc);
148             lookups.add (pc.getLookup ());
149         }
150         Lookup masterLookup = new ProxyLookup
151             ((Lookup[]) lookups.toArray (new Lookup [lookups.size ()]));
152         
153         // 2) create panels & call cancel on all PanelControllers
154
it = controllers.iterator ();
155         while (it.hasNext ()) {
156             OptionsPanelController pc = (OptionsPanelController) it.next ();
157             JComponent JavaDoc c = pc.getComponent (masterLookup);
158             pc.cancel ();
159         }
160     }
161     
162     public void testChangedAndValid () {
163         
164         // 1) load PanelControllers and init master lookup
165
List JavaDoc controllers = new ArrayList JavaDoc ();
166         List JavaDoc lookups = new ArrayList JavaDoc ();
167         Iterator JavaDoc it = getCategories ().iterator ();
168         while (it.hasNext ()) {
169             OptionsCategory oc = (OptionsCategory) it.next ();
170             OptionsPanelController pc = oc.create ();
171             controllers.add (pc);
172             lookups.add (pc.getLookup ());
173         }
174         Lookup masterLookup = new ProxyLookup
175             ((Lookup[]) lookups.toArray (new Lookup [lookups.size ()]));
176         
177         // 2) create panels & call cancel on all PanelControllers
178
it = controllers.iterator ();
179         while (it.hasNext ()) {
180             OptionsPanelController pc = (OptionsPanelController) it.next ();
181             assertFalse ("isChanged should be false if there is no change! (controller = " + pc + ")", pc.isChanged ());
182             assertTrue ("isvalid should be true if there is no change! (controller = " + pc + ")", pc.isValid ());
183             JComponent JavaDoc c = pc.getComponent (masterLookup);
184             assertFalse ("isChanged should be false if there is no change! (controller = " + pc + ")", pc.isChanged ());
185             assertTrue ("isvalid should be true if there is no change! (controller = " + pc + ")", pc.isValid ());
186             pc.update ();
187             assertFalse ("isChanged should be false if there is no change! (controller = " + pc + ")", pc.isChanged ());
188             assertTrue ("isvalid should be true if there is no change! (controller = " + pc + ")", pc.isValid ());
189             pc.update ();
190             assertFalse ("isChanged should be false if there is no change! (controller = " + pc + ")", pc.isChanged ());
191             assertTrue ("isvalid should be true if there is no change! (controller = " + pc + ")", pc.isValid ());
192             pc.cancel ();
193             assertFalse ("isChanged should be false if there is no change! (controller = " + pc + ")", pc.isChanged ());
194             assertTrue ("isvalid should be true if there is no change! (controller = " + pc + ")", pc.isValid ());
195             pc.update ();
196             assertFalse ("isChanged should be false if there is no change! (controller = " + pc + ")", pc.isChanged ());
197             assertTrue ("isvalid should be true if there is no change! (controller = " + pc + ")", pc.isValid ());
198             pc.applyChanges ();
199             assertFalse ("isChanged should be false if there is no change! (controller = " + pc + ")", pc.isChanged ());
200             assertTrue ("isvalid should be true if there is no change! (controller = " + pc + ")", pc.isValid ());
201         }
202         
203         it = controllers.iterator ();
204         while (it.hasNext ()) {
205             OptionsPanelController pc = (OptionsPanelController) it.next ();
206             JComponent JavaDoc c = pc.getComponent (masterLookup);
207             pc.update ();
208             assertFalse ("isChanged should be false if there is no change! (controller = " + pc + ")", pc.isChanged ());
209             assertTrue ("isvalid should be true if there is no change! (controller = " + pc + ")", pc.isValid ());
210         }
211     }
212
213     private List JavaDoc getCategories () {
214         FileObject fo = Repository.getDefault ().getDefaultFileSystem ().
215             findResource ("OptionsDialog");
216         Lookup lookup = new FolderLookup (DataFolder.findFolder (fo)).
217             getLookup ();
218         return new ArrayList JavaDoc (lookup.lookup (
219             new Lookup.Template (OptionsCategory.class)
220         ).allInstances ());
221     }
222 }
223
224
225
Popular Tags