PreferencesTest.java revision e34fbfb66a724284a9722892fa8f51f7c663ed86
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.io.FileOutputStream; 25import java.net.MalformedURLException; 26import java.nio.charset.StandardCharsets; 27import java.util.prefs.AbstractPreferences; 28import java.util.prefs.BackingStoreException; 29import java.util.prefs.InvalidPreferencesFormatException; 30import java.util.prefs.NodeChangeListener; 31import java.util.prefs.PreferenceChangeListener; 32import java.util.prefs.Preferences; 33import java.util.prefs.PreferencesFactory; 34import junit.framework.TestCase; 35import libcore.io.IoUtils; 36 37/** 38 * 39 */ 40public class PreferencesTest extends TestCase { 41 42 private static final String PREFS = 43 "<!DOCTYPE preferences SYSTEM \"http://java.sun.com/dtd/preferences.dtd\">" + 44 "<preferences>" + 45 "<root type=\"user\">" + 46 "<map></map>" + 47 "</root>" + 48 "</preferences>"; 49 50 51 private MockInputStream stream ; 52 private InputStream in; 53 private PreferencesFactory defaultFactory; 54 private File backendDir; 55 56 @Override 57 protected void setUp() throws Exception { 58 backendDir = IoUtils.createTemporaryDirectory("OldAbstractPreferencesTest"); 59 defaultFactory = Preferences.setPreferencesFactory( 60 new AbstractPreferencesTest.TestPreferencesFactory(backendDir.getAbsolutePath())); 61 62 in = new ByteArrayInputStream(PREFS.getBytes(StandardCharsets.US_ASCII)); 63 stream = new MockInputStream(in); 64 } 65 66 @Override 67 protected void tearDown() throws Exception { 68 Preferences.setPreferencesFactory(defaultFactory); 69 stream.close(); 70 } 71 72 public void testSystemNodeForPackage() throws Exception { 73 Preferences p = Preferences.systemNodeForPackage(Object.class); 74 75 assertEquals("/java/lang", p.absolutePath()); 76 assertTrue(p instanceof AbstractPreferences); 77 Preferences root = Preferences.systemRoot(); 78 Preferences parent = root.node("java"); 79 assertSame(parent, p.parent()); 80 assertFalse(p.isUserNode()); 81 assertEquals("lang", p.name()); 82 assertEquals("System Preference Node: " + p.absolutePath(), p 83 .toString()); 84 85 assertEquals(0, p.childrenNames().length); 86 assertEquals(0, p.keys().length); 87 parent.removeNode(); 88 try { 89 Preferences.userNodeForPackage(null); 90 fail("should throw NullPointerException"); 91 } catch (NullPointerException e) { 92 // Expected 93 } 94 } 95 96 public void testSystemRoot() throws BackingStoreException { 97 Preferences p = Preferences.systemRoot(); 98 assertTrue(p instanceof AbstractPreferences); 99 assertEquals("/", p.absolutePath()); 100 assertSame(null, p.parent()); 101 assertFalse(p.isUserNode()); 102 assertEquals("", p.name()); 103 assertEquals("System Preference Node: " + p.absolutePath(), p 104 .toString()); 105 } 106 107 public void testConsts() { 108 assertEquals(80, Preferences.MAX_KEY_LENGTH); 109 assertEquals(80, Preferences.MAX_NAME_LENGTH); 110 assertEquals(8192, Preferences.MAX_VALUE_LENGTH); 111 } 112 113 public void testUserNodeForPackage() throws BackingStoreException { 114 Preferences p = Preferences.userNodeForPackage(Object.class); 115 assertEquals("/java/lang", p.absolutePath()); 116 assertTrue(p instanceof AbstractPreferences); 117 Preferences root = Preferences.userRoot(); 118 Preferences parent = root.node("java"); 119 assertSame(parent, p.parent()); 120 assertTrue(p.isUserNode()); 121 assertEquals("lang", p.name()); 122 assertEquals("User Preference Node: " + p.absolutePath(), p.toString()); 123 assertEquals(0, p.childrenNames().length); 124 assertEquals(0, p.keys().length); 125 126 try { 127 Preferences.userNodeForPackage(null); 128 fail("should throw NullPointerException"); 129 } catch (NullPointerException e) { 130 // Expected 131 } 132 } 133 134 public void testUserRoot() throws BackingStoreException { 135 Preferences p = Preferences.userRoot(); 136 assertTrue(p instanceof AbstractPreferences); 137 assertEquals("/", p.absolutePath()); 138 assertSame(null, p.parent()); 139 assertTrue(p.isUserNode()); 140 assertEquals("", p.name()); 141 assertEquals("User Preference Node: " + p.absolutePath(), p.toString()); 142 } 143 144 public void testImportPreferences() throws Exception { 145 Preferences prefs = null; 146 try { 147 prefs = Preferences.userNodeForPackage(PreferencesTest.class); 148 149 prefs.put("prefskey", "oldvalue"); 150 prefs.put("prefskey2", "oldvalue2"); 151 in = PreferencesTest.class 152 .getResourceAsStream("/resources/prefs/java/util/prefs/userprefs.xml"); 153 Preferences.importPreferences(in); 154 155 prefs = Preferences.userNodeForPackage(PreferencesTest.class); 156 assertEquals(1, prefs.childrenNames().length); 157 assertTrue(prefs.nodeExists("mock/child/grandson")); 158 assertEquals("newvalue", prefs.get("prefskey", null)); 159 assertEquals("oldvalue2", prefs.get("prefskey2", null)); 160 assertEquals("newvalue3", prefs.get("prefskey3", null)); 161 162 in = PreferencesTest.class.getResourceAsStream( 163 "/prefs/java/util/prefs/userprefs-badform.xml"); 164 try { 165 Preferences.importPreferences(in); 166 fail("should throw InvalidPreferencesFormatException"); 167 } catch (InvalidPreferencesFormatException expected) { 168 } 169 } finally { 170 try { 171 prefs = Preferences.userNodeForPackage(PreferencesTest.class); 172 prefs.removeNode(); 173 } catch (Exception e) { 174 } 175 } 176 } 177 178 public void testImportPreferencesException() throws Exception { 179 try { 180 Preferences.importPreferences(null); 181 fail("should throw InvalidPreferencesFormatException"); 182 } catch (InvalidPreferencesFormatException expected) { 183 } 184 185 byte[] source = new byte[0]; 186 InputStream in = new ByteArrayInputStream(source); 187 try { 188 Preferences.importPreferences(in); 189 fail("should throw InvalidPreferencesFormatException"); 190 } catch (InvalidPreferencesFormatException expected) { 191 } 192 193 stream.setResult(MockInputStream.exception); 194 try { 195 Preferences.importPreferences(stream); 196 fail("should throw IOException"); 197 } catch (IOException expected) { 198 } 199 200 stream.setResult(MockInputStream.runtimeException); 201 try { 202 Preferences.importPreferences(stream); 203 fail("should throw RuntimeException"); 204 } catch (RuntimeException expected) { 205 } 206 } 207 208 static class MockInputStream extends InputStream { 209 210 static final int normal = 0; 211 212 static final int exception = 1; 213 214 static final int runtimeException = 2; 215 216 int result = normal; 217 218 InputStream wrapper; 219 220 public void setResult(int i) { 221 result = i; 222 } 223 224 private void checkException() throws IOException { 225 switch (result) { 226 case normal: 227 return; 228 case exception: 229 throw new IOException("test"); 230 case runtimeException: 231 throw new RuntimeException("test"); 232 } 233 } 234 235 public MockInputStream(InputStream in) { 236 wrapper = in; 237 } 238 239 @Override 240 public int read() throws IOException { 241 checkException(); 242 return wrapper.read(); 243 } 244 } 245 246 static class MockPreferences extends Preferences { 247 248 public MockPreferences() { 249 super(); 250 } 251 252 @Override 253 public String absolutePath() { 254 return null; 255 } 256 257 @Override 258 public String[] childrenNames() throws BackingStoreException { 259 return null; 260 } 261 262 @Override 263 public void clear() throws BackingStoreException { 264 } 265 266 @Override 267 public void exportNode(OutputStream ostream) throws IOException, 268 BackingStoreException { 269 } 270 271 @Override 272 public void exportSubtree(OutputStream ostream) throws IOException, 273 BackingStoreException { 274 } 275 276 @Override 277 public void flush() throws BackingStoreException { 278 } 279 280 @Override 281 public String get(String key, String deflt) { 282 return null; 283 } 284 285 @Override 286 public boolean getBoolean(String key, boolean deflt) { 287 return false; 288 } 289 290 @Override 291 public byte[] getByteArray(String key, byte[] deflt) { 292 return null; 293 } 294 295 @Override 296 public double getDouble(String key, double deflt) { 297 return 0; 298 } 299 300 @Override 301 public float getFloat(String key, float deflt) { 302 return 0; 303 } 304 305 @Override 306 public int getInt(String key, int deflt) { 307 return 0; 308 } 309 310 @Override 311 public long getLong(String key, long deflt) { 312 return 0; 313 } 314 315 @Override 316 public boolean isUserNode() { 317 return false; 318 } 319 320 @Override 321 public String[] keys() throws BackingStoreException { 322 return null; 323 } 324 325 @Override 326 public String name() { 327 return null; 328 } 329 330 @Override 331 public Preferences node(String name) { 332 return null; 333 } 334 335 @Override 336 public boolean nodeExists(String name) throws BackingStoreException { 337 return false; 338 } 339 340 @Override 341 public Preferences parent() { 342 return null; 343 } 344 345 @Override 346 public void put(String key, String value) { 347 348 } 349 350 @Override 351 public void putBoolean(String key, boolean value) { 352 353 } 354 355 @Override 356 public void putByteArray(String key, byte[] value) { 357 358 } 359 360 @Override 361 public void putDouble(String key, double value) { 362 363 } 364 365 @Override 366 public void putFloat(String key, float value) { 367 368 } 369 370 @Override 371 public void putInt(String key, int value) { 372 373 } 374 375 @Override 376 public void putLong(String key, long value) { 377 378 } 379 380 @Override 381 public void remove(String key) { 382 383 } 384 385 @Override 386 public void removeNode() throws BackingStoreException { 387 388 } 389 390 @Override 391 public void addNodeChangeListener(NodeChangeListener ncl) { 392 393 } 394 395 @Override 396 public void addPreferenceChangeListener(PreferenceChangeListener pcl) { 397 398 } 399 400 @Override 401 public void removeNodeChangeListener(NodeChangeListener ncl) { 402 403 } 404 405 @Override 406 public void removePreferenceChangeListener(PreferenceChangeListener pcl) { 407 408 } 409 410 @Override 411 public void sync() throws BackingStoreException { 412 413 } 414 415 @Override 416 public String toString() { 417 return null; 418 } 419 420 } 421 422 // b/27645233 423 public void testReadingFromBackendCache() throws Exception { 424 // Create a preferences filesystem backend storage file. 425 // It emulates a situation where we saved some data in the past, 426 // restarted the application and we're trying to read it. 427 // 428 // We need a package without the existing backing file (so a package that 429 // wasn't used in this test case), java.io is a good candidate. 430 File backendFile = new File(backendDir, "/user/java/io/prefs.xml"); 431 InputStream inputData = PreferencesTest.class 432 .getResourceAsStream("/resources/prefs/java/util/prefs/backendread.xml"); 433 assertTrue(new File(backendDir, "/user/java/io").mkdirs()); 434 assertTrue(backendFile.createNewFile()); 435 436 // Copy the example content (one test=test1 entry) to a storage file 437 FileOutputStream fos = new FileOutputStream(backendFile); 438 try { 439 byte[] buffer = new byte[1024]; 440 int length; 441 while ((length = inputData.read(buffer)) > 0) { 442 fos.write(buffer, 0, length); 443 } 444 } finally { 445 fos.close(); 446 inputData.close(); 447 } 448 449 Preferences prefs = Preferences.userNodeForPackage(OutputStream.class); 450 // Any exception from reading the file will be swallowed and ignored, 451 // only result we will see is a lack of requested key-value pair. 452 assertEquals("test1", prefs.get("test","")); 453 } 454 455 456} 457