1 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 ; 27 import java.net.MalformedURLException ; 28 29 32 public class SystemOptionTest extends NbTestCase { 33 34 public SystemOptionTest(String name) { 35 super(name); 36 } 37 38 public static void main(String [] args) { 39 TestRunner.run(new NbTestSuite(SystemOptionTest.class)); 40 } 41 42 44 public void testBasicUsage() throws Exception { 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 59 public void testDeserialization() throws Exception { 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 81 public void testMixedTypeDeserialization() throws Exception { 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 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 106 public void testSimpleResetToOldValuesWhenTheyWereInitializedInInitialize () throws Exception { 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 { 119 SimpleOption2 s = (SimpleOption2)SimpleOption.findObject (SimpleOption2.class, true); 120 121 class PL implements java.beans.PropertyChangeListener { 122 public int cnt; 123 public String name; 124 125 public void propertyChange (java.beans.PropertyChangeEvent ev) { 126 cnt++; 127 name = ev.getPropertyName (); 128 } 129 130 public void assertChange (String 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 { 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 185 187 190 192 194 196 199 201 public static final class SimpleOption extends SystemOption { 202 public String 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 )getProperty("x")).intValue(); 212 } 213 public void setX(int x) { 214 putProperty("x", new Integer (x), true); 215 } 216 public String getY() { 217 return (String )getProperty("y"); 218 } 219 public void setY(String 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 displayName() { 231 return "SimpleOption2"; 232 } 233 private static int x2 = 3; 234 private static String y2 = "hello"; 235 protected void initialize() { 236 super.initialize(); 237 setX(3); 238 setY("hello"); 239 } 240 public int getX() { 241 return ((Integer )getProperty("x")).intValue(); 242 } 243 public void setX(int x) { 244 putProperty("x", new Integer (x), true); 245 } 246 public String getY() { 247 return (String )getProperty("y"); 248 } 249 public void setY(String 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 (old), new Integer (nue)); 259 } 260 public String getY2() { 261 return y2; 262 } 263 public void setY2(String nue) { 264 String 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 o; 277 public final boolean natural; 278 public Cell(Object 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 prop) { 286 return ((Cell)getProperty(prop)).natural; 287 } 288 public String displayName() { 289 return "MixedTypeOption"; 290 } 291 protected void initialize() { 292 super.initialize(); 293 putProperty("x", new Cell(new Integer (12), true)); 294 putProperty("y", new Cell("12", true)); 295 putProperty("z", new Cell("http://www.netbeans.org/", true)); 296 } 297 public String getX() { 298 return ((Integer )((Cell)getProperty("x")).o).toString(); 299 } 300 public void setX(String x) { 301 putProperty("x", new Cell(new Integer (x), saveNatural)); 302 } 303 public int getY() { 304 return Integer.parseInt((String )((Cell)getProperty("y")).o); 305 } 306 public void setY(int i) { 307 putProperty("y", new Cell(String.valueOf(i), saveNatural)); 308 } 309 public URL getZ() { 310 try { 311 return new URL ((String )((Cell)getProperty("z")).o); 312 } catch (MalformedURLException mfue) { 313 throw new IllegalStateException (mfue.toString()); 314 } 315 } 316 public void setZ(URL z) { 317 putProperty("z", new Cell(z.toString(), saveNatural)); 318 } 319 } 320 321 public static class I18NLikeOption extends SystemOption { 322 public static final String PROP_ADVANCED_WIZARD = "advancedWizard"; 323 324 325 public String displayName() { 326 return "I18NLikeOption"; 327 } 328 329 330 public boolean isAdvancedWizard() { 331 if(getProperty(PROP_ADVANCED_WIZARD) == null) 333 return false; 334 335 return ((Boolean )getProperty(PROP_ADVANCED_WIZARD)).booleanValue(); 336 } 337 338 339 public void setAdvancedWizard(boolean generateField) { 340 putProperty(PROP_ADVANCED_WIZARD, generateField ? Boolean.TRUE : Boolean.FALSE, true); 342 } 343 344 } 345 } 346 | Popular Tags |