KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > options > SystemOptionTest


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.openide.options;
21
22 import org.netbeans.junit.*;
23 import junit.textui.TestRunner;
24 import org.openide.util.SharedClassObject;
25 import java.io.*;
26 import java.net.URL JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28
29 /** Test system options (their serialization and deserialization specifically).
30  * @author Jesse Glick
31  */

32 public class SystemOptionTest extends NbTestCase {
33
34     public SystemOptionTest(String JavaDoc name) {
35         super(name);
36     }
37
38     public static void main(String JavaDoc[] args) {
39         TestRunner.run(new NbTestSuite(SystemOptionTest.class));
40     }
41     
42     /** Test manipulation of a SystemOption in memory.
43      */

44     public void testBasicUsage() throws Exception JavaDoc {
45         assertNull(SharedClassObject.findObject(SimpleOption.class, false));
46         SimpleOption o = (SimpleOption)SharedClassObject.findObject(SimpleOption.class, true);
47         assertEquals(3, o.getX());
48         assertEquals("hello", o.getY());
49         o.setX(5);
50         o.setY("nue");
51         assertEquals(5, o.getX());
52         assertEquals("nue", o.getY());
53     }
54     
55     /** Test deserializability of a simple option.
56      * Uses both SCO properties and static vars for storage; SystemOption
57      * should treat them the same because the vars match the property types.
58      */

59     public void testDeserialization() throws Exception JavaDoc {
60         InputStream is = getClass().getResourceAsStream("simpleOption2.ser");
61         assertNotNull("simpleOption2.ser exists", is);
62         try {
63             ObjectInputStream ois = new ObjectInputStream(is);
64             SimpleOption2 o = (SimpleOption2)ois.readObject();
65             assertEquals(4, o.getX());
66             assertEquals(4, o.getX2());
67             assertEquals("four", o.getY());
68             assertEquals("four", o.getY2());
69         } finally {
70             is.close();
71         }
72     }
73     
74     /** Test deserializability of an option which stores props in a special way.
75      * Note that it keeps properties but they are not assignable to the property type.
76      * I.e. the property type is for client use only, not for storage.
77      * The Cell's are actually stored; we keep track of the "natural state"; the .ser
78      * was stored with saveNatural turned on, so we check that it is really deserializing
79      * the Cell's and not just calling the getters with the public values.
80      */

81     public void testMixedTypeDeserialization() throws Exception JavaDoc {
82         InputStream is = getClass().getResourceAsStream("mixedTypeOption.ser");
83         assertNotNull("mixedTypeOption.ser exists", is);
84         try {
85             ObjectInputStream ois = new ObjectInputStream(is);
86             MixedTypeOption o = (MixedTypeOption)ois.readObject();
87             assertEquals("25", o.getX());
88             assertEquals(25, o.getY());
89             // Makes a network connection: assertEquals(new URL("http://openide.netbeans.org/"), o.getZ());
90
assertEquals("openide.netbeans.org", o.getZ().getHost());
91             assertTrue(o.isNatural("x"));
92             assertTrue(o.isNatural("y"));
93             assertTrue(o.isNatural("z"));
94             o.setY(26);
95             assertEquals(26, o.getY());
96             assertFalse(o.isNatural("y"));
97         } finally {
98             is.close();
99         }
100     }
101     
102     //
103
// Implements reset to default values
104
//
105

106     public void testSimpleResetToOldValuesWhenTheyWereInitializedInInitialize () throws Exception JavaDoc {
107         SimpleOption s = (SimpleOption)SimpleOption.findObject (SimpleOption.class, true);
108         
109         s.setX (-10);
110         s.setY ("-10");
111         
112         s.reset ();
113         
114         assertEquals ("Was 3 in initialize", 3, s.getX ());
115         assertEquals ("Was hello", "hello", s.getY ());
116     }
117     
118     public void testSimpleResetEvenWhenWeHaveStaticInitialValues () throws Exception JavaDoc {
119         SimpleOption2 s = (SimpleOption2)SimpleOption.findObject (SimpleOption2.class, true);
120         
121         class PL implements java.beans.PropertyChangeListener JavaDoc {
122             public int cnt;
123             public String JavaDoc name;
124             
125             public void propertyChange (java.beans.PropertyChangeEvent JavaDoc ev) {
126                 cnt++;
127                 name = ev.getPropertyName ();
128             }
129             
130             public void assertChange (String JavaDoc name, int cnt) {
131                 if (name != null) {
132                     assertEquals ("The this property had to change", name, this.name);
133                     this.name = null;
134                 }
135                 if (cnt != -1) {
136                     assertEquals ("This number of times", cnt, this.cnt);
137                     this.cnt = 0;
138                 }
139             }
140         }
141         
142         PL pl = new PL ();
143         s.addPropertyChangeListener (pl);
144         
145         s.setX (-10);
146         pl.assertChange ("x", 1);
147         s.setX (-9);
148         pl.assertChange ("x", 1);
149         s.setY ("-10");
150         pl.assertChange ("y", 1);
151         s.setX2 (7777);
152         s.setY2 ("-4444");
153         
154         s.reset ();
155         
156         assertEquals ("Was 3 in initialize", 3, s.getX ());
157         assertEquals ("Was hello", "hello", s.getY ());
158         assertEquals ("2 Was 3 in initialize", 3, s.getX2 ());
159         assertEquals ("2 Was hello", "hello", s.getY2 ());
160         
161         
162     }
163     
164     public void testTheProblemWithI18NOptionsAsDiscoveredRecentlyWithIssue20962 () throws Exception JavaDoc {
165         I18NLikeOption s = (I18NLikeOption)I18NLikeOption.findObject (I18NLikeOption.class, true);
166
167         assertFalse ("Not set by default", s.isAdvancedWizard ());
168         s.setAdvancedWizard (true);
169         assertTrue ("Changes to true", s.isAdvancedWizard ());
170         s.reset ();
171         
172         assertFalse ("Is cleared", s.isAdvancedWizard ());
173         
174         s.setAdvancedWizard (true);
175         assertTrue ("yes again", s.isAdvancedWizard ());
176         s.setAdvancedWizard (false);
177         assertFalse ("no again", s.isAdvancedWizard ());
178         s.reset ();
179         assertFalse ("still no", s.isAdvancedWizard ());
180     }
181     
182     // XXX test that serialization works and matches deserialization
183
// (hint: use MaskingURLClassLoader from SharedClassObjectTest)
184

185     // XXX test that SharedClassObject.find(optionClass, true) asks Lookup for the singleton
186

187     // XXX test that the BeanInfo property descriptors are really used to determine
188
// property names and writability (and that these can override getter/setter name munging)
189

190     // XXX test that isReadExternal/isWriteExternal work
191

192     // XXX test that read-only properties are not stored
193

194     // XXX test that deser failure of one property does not break others
195

196     // XXX ContextSystemOptionTest: test that child options are stored correctly, bean context
197
// works, serialization stores all of them together
198

199     // XXX VetoSystemOptionTest: test that you can veto some property changes
200

201     public static final class SimpleOption extends SystemOption {
202         public String JavaDoc displayName() {
203             return "SimpleOption";
204         }
205         protected void initialize() {
206             super.initialize();
207             setX(3);
208             setY("hello");
209         }
210         public int getX() {
211             return ((Integer JavaDoc)getProperty("x")).intValue();
212         }
213         public void setX(int x) {
214             putProperty("x", new Integer JavaDoc(x), true);
215         }
216         public String JavaDoc getY() {
217             return (String JavaDoc)getProperty("y");
218         }
219         public void setY(String JavaDoc y) {
220             putProperty("y", y, true);
221         }
222         
223         public void reset () {
224             super.reset ();
225         }
226     }
227     
228     public static final class SimpleOption2 extends SystemOption {
229         private static final long serialVersionUID = 2456964509644026223L;
230         public String JavaDoc displayName() {
231             return "SimpleOption2";
232         }
233         private static int x2 = 3;
234         private static String JavaDoc y2 = "hello";
235         protected void initialize() {
236             super.initialize();
237             setX(3);
238             setY("hello");
239         }
240         public int getX() {
241             return ((Integer JavaDoc)getProperty("x")).intValue();
242         }
243         public void setX(int x) {
244             putProperty("x", new Integer JavaDoc(x), true);
245         }
246         public String JavaDoc getY() {
247             return (String JavaDoc)getProperty("y");
248         }
249         public void setY(String JavaDoc y) {
250             putProperty("y", y, true);
251         }
252         public int getX2() {
253             return x2;
254         }
255         public void setX2(int nue) {
256             int old = x2;
257             x2 = nue;
258             firePropertyChange("x2", new Integer JavaDoc(old), new Integer JavaDoc(nue));
259         }
260         public String JavaDoc getY2() {
261             return y2;
262         }
263         public void setY2(String JavaDoc nue) {
264             String JavaDoc old = y2;
265             y2 = nue;
266             firePropertyChange("y2", old, nue);
267         }
268         public void reset () {
269             super.reset ();
270         }
271     }
272     
273     public static class MixedTypeOption extends SystemOption {
274         private static final class Cell implements Serializable {
275             private static final long serialVersionUID = -2882494319143608556L;
276             public final Object JavaDoc o;
277             public final boolean natural;
278             public Cell(Object JavaDoc o, boolean natural) {
279                 this.o = o;
280                 this.natural = natural;
281             }
282         }
283         private static final long serialVersionUID = 262688904041933263L;
284         public static boolean saveNatural = false;
285         public boolean isNatural(String JavaDoc prop) {
286             return ((Cell)getProperty(prop)).natural;
287         }
288         public String JavaDoc displayName() {
289             return "MixedTypeOption";
290         }
291         protected void initialize() {
292             super.initialize();
293             putProperty("x", new Cell(new Integer JavaDoc(12), true));
294             putProperty("y", new Cell("12", true));
295             putProperty("z", new Cell("http://www.netbeans.org/", true));
296         }
297         public String JavaDoc getX() {
298             return ((Integer JavaDoc)((Cell)getProperty("x")).o).toString();
299         }
300         public void setX(String JavaDoc x) {
301             putProperty("x", new Cell(new Integer JavaDoc(x), saveNatural));
302         }
303         public int getY() {
304             return Integer.parseInt((String JavaDoc)((Cell)getProperty("y")).o);
305         }
306         public void setY(int i) {
307             putProperty("y", new Cell(String.valueOf(i), saveNatural));
308         }
309         public URL JavaDoc getZ() {
310             try {
311                 return new URL JavaDoc((String JavaDoc)((Cell)getProperty("z")).o);
312             } catch (MalformedURLException JavaDoc mfue) {
313                 throw new IllegalStateException JavaDoc(mfue.toString());
314             }
315         }
316         public void setZ(URL JavaDoc z) {
317             putProperty("z", new Cell(z.toString(), saveNatural));
318         }
319     }
320
321     public static class I18NLikeOption extends SystemOption {
322         public static final String JavaDoc PROP_ADVANCED_WIZARD = "advancedWizard";
323         
324         /** Implements superclass abstract method. */
325         public String JavaDoc displayName() {
326             return "I18NLikeOption";
327         }
328         
329         /** Getter for init advanced wizard property. */
330         public boolean isAdvancedWizard() {
331             // Lazy init.
332
if(getProperty(PROP_ADVANCED_WIZARD) == null)
333                 return false;
334
335             return ((Boolean JavaDoc)getProperty(PROP_ADVANCED_WIZARD)).booleanValue();
336         }
337
338         /** Setter for init advanced wizard property. */
339         public void setAdvancedWizard(boolean generateField) {
340             // Stores in class-wide state and fires property changes if needed:
341
putProperty(PROP_ADVANCED_WIZARD, generateField ? Boolean.TRUE : Boolean.FALSE, true);
342         }
343         
344     }
345 }
346
Popular Tags