PreferencesTest.java revision b0b81dfb681cf401b3fc108b9abe432ee6736164
1/* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.apache.harmony.tests.java.util.prefs; 18 19import java.io.ByteArrayInputStream; 20import java.io.File; 21import java.io.IOException; 22import java.io.InputStream; 23import java.io.OutputStream; 24import java.net.MalformedURLException; 25import java.nio.charset.StandardCharsets; 26import java.util.prefs.AbstractPreferences; 27import java.util.prefs.BackingStoreException; 28import java.util.prefs.InvalidPreferencesFormatException; 29import java.util.prefs.NodeChangeListener; 30import java.util.prefs.PreferenceChangeListener; 31import java.util.prefs.Preferences; 32import java.util.prefs.PreferencesFactory; 33import junit.framework.TestCase; 34import libcore.io.IoUtils; 35 36/** 37 * 38 */ 39public class PreferencesTest extends TestCase { 40 41 private static final String PREFS = 42 "<!DOCTYPE preferences SYSTEM \"http://java.sun.com/dtd/preferences.dtd\">" + 43 "<preferences>" + 44 "<root type=\"user\">" + 45 "<map></map>" + 46 "</root>" + 47 "</preferences>"; 48 49 50 private MockInputStream stream ; 51 private InputStream in; 52 private PreferencesFactory defaultFactory; 53 54 @Override 55 protected void setUp() throws Exception { 56 File tmpDir = IoUtils.createTemporaryDirectory("OldAbstractPreferencesTest"); 57 defaultFactory = Preferences.setPreferencesFactory( 58 new AbstractPreferencesTest.TestPreferencesFactory(tmpDir.getAbsolutePath())); 59 60 in = new ByteArrayInputStream(PREFS.getBytes(StandardCharsets.US_ASCII)); 61 stream = new MockInputStream(in); 62 } 63 64 @Override 65 protected void tearDown() throws Exception { 66 Preferences.setPreferencesFactory(defaultFactory); 67 stream.close(); 68 } 69 70 public void testSystemNodeForPackage() throws Exception { 71 Preferences p = Preferences.systemNodeForPackage(Object.class); 72 73 assertEquals("/java/lang", p.absolutePath()); 74 assertTrue(p instanceof AbstractPreferences); 75 Preferences root = Preferences.systemRoot(); 76 Preferences parent = root.node("java"); 77 assertSame(parent, p.parent()); 78 assertFalse(p.isUserNode()); 79 assertEquals("lang", p.name()); 80 assertEquals("System Preference Node: " + p.absolutePath(), p 81 .toString()); 82 83 assertEquals(0, p.childrenNames().length); 84 assertEquals(0, p.keys().length); 85 parent.removeNode(); 86 try { 87 Preferences.userNodeForPackage(null); 88 fail("should throw NullPointerException"); 89 } catch (NullPointerException e) { 90 // Expected 91 } 92 } 93 94 public void testSystemRoot() throws BackingStoreException { 95 Preferences p = Preferences.systemRoot(); 96 assertTrue(p instanceof AbstractPreferences); 97 assertEquals("/", p.absolutePath()); 98 assertSame(null, p.parent()); 99 assertFalse(p.isUserNode()); 100 assertEquals("", p.name()); 101 assertEquals("System Preference Node: " + p.absolutePath(), p 102 .toString()); 103 } 104 105 public void testConsts() { 106 assertEquals(80, Preferences.MAX_KEY_LENGTH); 107 assertEquals(80, Preferences.MAX_NAME_LENGTH); 108 assertEquals(8192, Preferences.MAX_VALUE_LENGTH); 109 } 110 111 public void testUserNodeForPackage() throws BackingStoreException { 112 Preferences p = Preferences.userNodeForPackage(Object.class); 113 assertEquals("/java/lang", p.absolutePath()); 114 assertTrue(p instanceof AbstractPreferences); 115 Preferences root = Preferences.userRoot(); 116 Preferences parent = root.node("java"); 117 assertSame(parent, p.parent()); 118 assertTrue(p.isUserNode()); 119 assertEquals("lang", p.name()); 120 assertEquals("User Preference Node: " + p.absolutePath(), p.toString()); 121 assertEquals(0, p.childrenNames().length); 122 assertEquals(0, p.keys().length); 123 124 try { 125 Preferences.userNodeForPackage(null); 126 fail("should throw NullPointerException"); 127 } catch (NullPointerException e) { 128 // Expected 129 } 130 } 131 132 public void testUserRoot() throws BackingStoreException { 133 Preferences p = Preferences.userRoot(); 134 assertTrue(p instanceof AbstractPreferences); 135 assertEquals("/", p.absolutePath()); 136 assertSame(null, p.parent()); 137 assertTrue(p.isUserNode()); 138 assertEquals("", p.name()); 139 assertEquals("User Preference Node: " + p.absolutePath(), p.toString()); 140 } 141 142 public void testImportPreferences() throws Exception { 143 Preferences prefs = null; 144 try { 145 prefs = Preferences.userNodeForPackage(PreferencesTest.class); 146 147 prefs.put("prefskey", "oldvalue"); 148 prefs.put("prefskey2", "oldvalue2"); 149 in = PreferencesTest.class 150 .getResourceAsStream("/resources/prefs/java/util/prefs/userprefs.xml"); 151 Preferences.importPreferences(in); 152 153 prefs = Preferences.userNodeForPackage(PreferencesTest.class); 154 assertEquals(1, prefs.childrenNames().length); 155 assertTrue(prefs.nodeExists("mock/child/grandson")); 156 assertEquals("newvalue", prefs.get("prefskey", null)); 157 assertEquals("oldvalue2", prefs.get("prefskey2", null)); 158 assertEquals("newvalue3", prefs.get("prefskey3", null)); 159 160 in = PreferencesTest.class.getResourceAsStream( 161 "/prefs/java/util/prefs/userprefs-badform.xml"); 162 try { 163 Preferences.importPreferences(in); 164 fail("should throw InvalidPreferencesFormatException"); 165 } catch (InvalidPreferencesFormatException expected) { 166 } 167 } finally { 168 try { 169 prefs = Preferences.userNodeForPackage(PreferencesTest.class); 170 prefs.removeNode(); 171 } catch (Exception e) { 172 } 173 } 174 } 175 176 public void testImportPreferencesException() throws Exception { 177 try { 178 Preferences.importPreferences(null); 179 fail("should throw MalformedURLException"); 180 } catch (MalformedURLException expected) { 181 } 182 183 byte[] source = new byte[0]; 184 InputStream in = new ByteArrayInputStream(source); 185 try { 186 Preferences.importPreferences(in); 187 fail("should throw InvalidPreferencesFormatException"); 188 } catch (InvalidPreferencesFormatException expected) { 189 } 190 191 stream.setResult(MockInputStream.exception); 192 try { 193 Preferences.importPreferences(stream); 194 fail("should throw IOException"); 195 } catch (IOException expected) { 196 } 197 198 stream.setResult(MockInputStream.runtimeException); 199 try { 200 Preferences.importPreferences(stream); 201 fail("should throw RuntimeException"); 202 } catch (RuntimeException expected) { 203 } 204 } 205 206 static class MockInputStream extends InputStream { 207 208 static final int normal = 0; 209 210 static final int exception = 1; 211 212 static final int runtimeException = 2; 213 214 int result = normal; 215 216 InputStream wrapper; 217 218 public void setResult(int i) { 219 result = i; 220 } 221 222 private void checkException() throws IOException { 223 switch (result) { 224 case normal: 225 return; 226 case exception: 227 throw new IOException("test"); 228 case runtimeException: 229 throw new RuntimeException("test"); 230 } 231 } 232 233 public MockInputStream(InputStream in) { 234 wrapper = in; 235 } 236 237 @Override 238 public int read() throws IOException { 239 checkException(); 240 return wrapper.read(); 241 } 242 } 243 244 static class MockPreferences extends Preferences { 245 246 public MockPreferences() { 247 super(); 248 } 249 250 @Override 251 public String absolutePath() { 252 return null; 253 } 254 255 @Override 256 public String[] childrenNames() throws BackingStoreException { 257 return null; 258 } 259 260 @Override 261 public void clear() throws BackingStoreException { 262 } 263 264 @Override 265 public void exportNode(OutputStream ostream) throws IOException, 266 BackingStoreException { 267 } 268 269 @Override 270 public void exportSubtree(OutputStream ostream) throws IOException, 271 BackingStoreException { 272 } 273 274 @Override 275 public void flush() throws BackingStoreException { 276 } 277 278 @Override 279 public String get(String key, String deflt) { 280 return null; 281 } 282 283 @Override 284 public boolean getBoolean(String key, boolean deflt) { 285 return false; 286 } 287 288 @Override 289 public byte[] getByteArray(String key, byte[] deflt) { 290 return null; 291 } 292 293 @Override 294 public double getDouble(String key, double deflt) { 295 return 0; 296 } 297 298 @Override 299 public float getFloat(String key, float deflt) { 300 return 0; 301 } 302 303 @Override 304 public int getInt(String key, int deflt) { 305 return 0; 306 } 307 308 @Override 309 public long getLong(String key, long deflt) { 310 return 0; 311 } 312 313 @Override 314 public boolean isUserNode() { 315 return false; 316 } 317 318 @Override 319 public String[] keys() throws BackingStoreException { 320 return null; 321 } 322 323 @Override 324 public String name() { 325 return null; 326 } 327 328 @Override 329 public Preferences node(String name) { 330 return null; 331 } 332 333 @Override 334 public boolean nodeExists(String name) throws BackingStoreException { 335 return false; 336 } 337 338 @Override 339 public Preferences parent() { 340 return null; 341 } 342 343 @Override 344 public void put(String key, String value) { 345 346 } 347 348 @Override 349 public void putBoolean(String key, boolean value) { 350 351 } 352 353 @Override 354 public void putByteArray(String key, byte[] value) { 355 356 } 357 358 @Override 359 public void putDouble(String key, double value) { 360 361 } 362 363 @Override 364 public void putFloat(String key, float value) { 365 366 } 367 368 @Override 369 public void putInt(String key, int value) { 370 371 } 372 373 @Override 374 public void putLong(String key, long value) { 375 376 } 377 378 @Override 379 public void remove(String key) { 380 381 } 382 383 @Override 384 public void removeNode() throws BackingStoreException { 385 386 } 387 388 @Override 389 public void addNodeChangeListener(NodeChangeListener ncl) { 390 391 } 392 393 @Override 394 public void addPreferenceChangeListener(PreferenceChangeListener pcl) { 395 396 } 397 398 @Override 399 public void removeNodeChangeListener(NodeChangeListener ncl) { 400 401 } 402 403 @Override 404 public void removePreferenceChangeListener(PreferenceChangeListener pcl) { 405 406 } 407 408 @Override 409 public void sync() throws BackingStoreException { 410 411 } 412 413 @Override 414 public String toString() { 415 return null; 416 } 417 418 } 419 420} 421