1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * 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 libcore.java.util.prefs; 18 19import java.io.ByteArrayOutputStream; 20import java.io.File; 21import java.io.FileWriter; 22import java.util.prefs.FilePreferencesImpl; 23import java.util.prefs.Preferences; 24import java.util.prefs.PreferencesFactory; 25import junit.framework.TestCase; 26import libcore.io.IoUtils; 27 28public final class PreferencesTest extends TestCase { 29 30 /** 31 * A preferences factory rooted at a given path. 32 */ 33 public static final class TestPreferencesFactory implements PreferencesFactory { 34 private final Preferences userPrefs; 35 private final Preferences systemPrefs; 36 37 public TestPreferencesFactory(String root) { 38 userPrefs = new FilePreferencesImpl(root + "/user", true); 39 systemPrefs = new FilePreferencesImpl(root + "/system", false); 40 } 41 42 public Preferences userRoot() { 43 return userPrefs; 44 } 45 46 public Preferences systemRoot() { 47 return systemPrefs; 48 } 49 } 50 51 private PreferencesFactory defaultFactory; 52 private File temporaryDirectory; 53 54 @Override 55 public void setUp() throws Exception { 56 temporaryDirectory = IoUtils.createTemporaryDirectory("PreferencesTest"); 57 defaultFactory = Preferences.setPreferencesFactory( 58 new TestPreferencesFactory(temporaryDirectory.getAbsolutePath())); 59 } 60 61 @Override 62 public void tearDown() throws Exception { 63 Preferences.setPreferencesFactory(defaultFactory); 64 } 65 66 /** 67 * The preferences API is designed to be hostile towards files that exist 68 * where it wants to store its XML data. http://b/3431233 69 */ 70 public void testPreferencesClobbersExistingFiles() throws Exception { 71 final File userPrefsDir = new File(temporaryDirectory + "/user"); 72 final File userPrefs = new File(userPrefsDir, "prefs.xml"); 73 assertTrue(userPrefs.createNewFile()); 74 FileWriter writer = new FileWriter(userPrefs); 75 writer.write("lamb"); 76 writer.close(); 77 userPrefs.setReadable(false); 78 userPrefs.setWritable(false); 79 long oldLength = userPrefs.length(); 80 81 Preferences userPreferences = Preferences.userRoot(); 82 userPreferences.sync(); 83 userPreferences.put("a", "lion"); 84 userPreferences.flush(); 85 assertTrue("Expected to exist " + userPrefs, userPrefs.exists()); 86 assertTrue("Expected file to be clobbered", oldLength != userPrefs.length()); 87 } 88 89 public void testHtmlEncoding() throws Exception { 90 Preferences parent = Preferences.userNodeForPackage(this.getClass()); 91 Preferences p = parent.node("testHtmlEncoding"); 92 p.put("key", "a<>&'\"\\b"); 93 p.flush(); 94 95 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 96 p.exportNode(baos); 97 98 String s = new String(baos.toByteArray(), "UTF-8"); 99 assertTrue(s, s.contains("value=\"a<>&'"\\b\"")); 100 } 101} 102