diff options
Diffstat (limited to 'harmony-tests')
17 files changed, 3228 insertions, 0 deletions
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/AbstractPreferencesTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/AbstractPreferencesTest.java new file mode 100644 index 0000000..98476f5 --- /dev/null +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/AbstractPreferencesTest.java @@ -0,0 +1,1699 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.java.util.prefs; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.prefs.AbstractPreferences; +import java.util.prefs.BackingStoreException; +import java.util.prefs.FilePreferencesImpl; +import java.util.prefs.NodeChangeEvent; +import java.util.prefs.NodeChangeListener; +import java.util.prefs.PreferenceChangeEvent; +import java.util.prefs.PreferenceChangeListener; +import java.util.prefs.Preferences; +import java.util.prefs.PreferencesFactory; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import junit.framework.TestCase; +import libcore.io.IoUtils; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +public class AbstractPreferencesTest extends TestCase { + + public static final class TestPreferencesFactory implements PreferencesFactory { + private final Preferences userPrefs; + private final Preferences systemPrefs; + + public TestPreferencesFactory(String root) { + userPrefs = new FilePreferencesImpl(root + "/user", true); + systemPrefs = new FilePreferencesImpl(root + "/system", false); + } + + public Preferences userRoot() { + return userPrefs; + } + + public Preferences systemRoot() { + return systemPrefs; + } + } + + private AbstractPreferences pref; + private AbstractPreferences root; + private AbstractPreferences parent; + private PreferencesFactory defaultFactory; + + private final static String LONG_KEY; + private final static String LONG_VALUE; + private final static String LONG_NAME; + + static { + final byte[] chars = new byte[Preferences.MAX_VALUE_LENGTH]; + + LONG_VALUE = new String(chars, StandardCharsets.US_ASCII); + LONG_KEY = LONG_VALUE.substring(0, Preferences.MAX_KEY_LENGTH); + LONG_NAME = LONG_VALUE.substring(0, Preferences.MAX_NAME_LENGTH); + } + + @Override + protected void setUp() throws Exception { + File tmpDir = IoUtils.createTemporaryDirectory("OldAbstractPreferencesTest"); + defaultFactory = Preferences.setPreferencesFactory( + new TestPreferencesFactory(tmpDir.getAbsolutePath())); + root = (AbstractPreferences) Preferences.userRoot(); + parent = (AbstractPreferences) Preferences.userNodeForPackage(Preferences.class); + + pref = (AbstractPreferences) parent.node("mock"); + } + + @Override + protected void tearDown() throws Exception { + Preferences.setPreferencesFactory(defaultFactory); + } + + public void testConstructor() throws BackingStoreException { + try { + pref = new MockAbstractPreferences( + (AbstractPreferences) Preferences.userRoot(), "mo/ck"); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + try { + pref = new MockAbstractPreferences(null, "mock"); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + try { + new MockAbstractPreferences(null, " "); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + try { + new MockAbstractPreferences(pref, ""); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + try { + new MockAbstractPreferences(pref, null); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + new MockAbstractPreferences(pref, " "); + + Preferences p2 = new MockAbstractPreferences(null, ""); + assertNotSame(p2, Preferences.systemRoot()); + assertNotSame(p2, Preferences.userRoot()); + assertFalse(p2.isUserNode()); + + p2 = new MockAbstractPreferences((AbstractPreferences) Preferences + .userRoot(), "mock"); + assertNotSame(p2, pref); + p2.removeNode(); + } + + public void testProtectedFields() throws BackingStoreException { + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + MockAbstractPreferences p = new MockAbstractPreferences(pref, "newNode"); + assertFalse(p.getNewNode()); + assertSame(p.getLock().getClass(), Object.class); + + p = (MockAbstractPreferences) pref.node("child"); + assertTrue(p.getNewNode()); + + p = (MockAbstractPreferences) ((MockAbstractPreferences) pref) + .publicChildSpi("child2"); + assertTrue(p.getNewNode()); + } + + public void testToString() { + assertEquals("User Preference Node: " + pref.absolutePath(), pref + .toString()); + + pref = new MockAbstractPreferences((AbstractPreferences) Preferences + .systemRoot(), "mock"); + assertEquals("System Preference Node: " + pref.absolutePath(), pref + .toString()); + } + + public void testAbsolutePath() { + assertEquals("/java/util/prefs/mock", pref.absolutePath()); + + pref = new MockAbstractPreferences(pref, " "); + assertEquals("/java/util/prefs/mock/ ", pref.absolutePath()); + } + + public void testChildrenNames() throws BackingStoreException { + assertEquals(0, pref.childrenNames().length); + + // MockAbstractPreferences child1 = new MockAbstractPreferences(pref, + // "child1"); + // MockAbstractPreferences child2 = new MockAbstractPreferences(pref, + // "child2"); + // MockAbstractPreferences child3 = new MockAbstractPreferences(pref, + // "child3"); + // MockAbstractPreferences subchild1 = new + // MockAbstractPreferences(child1, + // "subchild1"); + Preferences child1 = pref.node("child1"); + + pref.node("child2"); + pref.node("child3"); + child1.node("subchild1"); + + assertSame(pref, child1.parent()); + assertEquals(3, pref.childrenNames().length); + } + + public void testClear() throws BackingStoreException { + pref.put("testClearKey", "testClearValue"); + pref.put("testClearKey1", "testClearValue1"); + assertEquals("testClearValue", pref.get("testClearKey", null)); + assertEquals("testClearValue1", pref.get("testClearKey1", null)); + pref.clear(); + assertNull(pref.get("testClearKey", null)); + assertNull(pref.get("testClearKey1", null)); + } + + public void testGet() throws BackingStoreException { + assertNull(pref.get("", null)); + assertEquals("default", pref.get("key", "default")); + assertNull(pref.get("key", null)); + pref.put("testGetkey", "value"); + assertNull(pref.get("testGetKey", null)); + assertEquals("value", pref.get("testGetkey", null)); + + try { + pref.get(null, "abc"); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.get("", "abc"); + pref.get("key", null); + pref.get("key", ""); + pref.putFloat("floatKey", 1.0f); + assertEquals("1.0", pref.get("floatKey", null)); + + pref.removeNode(); + try { + pref.get("key", "abc"); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.get(null, "abc"); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + } + + public void testGetBoolean() { + try { + pref.getBoolean(null, false); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.put("testGetBooleanKey", "false"); + pref.put("testGetBooleanKey2", "value"); + assertFalse(pref.getBoolean("testGetBooleanKey", true)); + assertTrue(pref.getBoolean("testGetBooleanKey2", true)); + } + + public void testPutByteArray() { + try { + pref.putByteArray(null, new byte[0]); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + pref.putByteArray("testPutByteArrayKey4", null); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.putByteArray(LONG_KEY, new byte[0]); + try { + pref.putByteArray(LONG_KEY + "a", new byte[0]); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + byte[] longArray = new byte[(int) (Preferences.MAX_VALUE_LENGTH * 0.74)]; + byte[] longerArray = new byte[(int) (Preferences.MAX_VALUE_LENGTH * 0.75) + 1]; + pref.putByteArray(LONG_KEY, longArray); + try { + pref.putByteArray(LONG_KEY, longerArray); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + + pref.putByteArray("testPutByteArrayKey", new byte[0]); + assertEquals("", pref.get("testPutByteArrayKey", null)); + assertTrue(Arrays.equals(new byte[0], pref.getByteArray( + "testPutByteArrayKey", null))); + + pref.putByteArray("testPutByteArrayKey3", new byte[] { 'a', 'b', 'c' }); + assertEquals("YWJj", pref.get("testPutByteArrayKey3", null)); + assertTrue(Arrays.equals(new byte[] { 'a', 'b', 'c' }, pref + .getByteArray("testPutByteArrayKey3", null))); + } + + public void testGetByteArray() throws UnsupportedEncodingException { + try { + pref.getByteArray(null, new byte[0]); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + byte[] b64Array = new byte[] { 0x59, 0x57, 0x4a, 0x6a };// BASE64 + // encoding for + // "abc" + + pref.put("testGetByteArrayKey", "abc="); + pref.put("testGetByteArrayKey2", new String(b64Array, "UTF-8")); + pref.put("invalidKey", "<>?"); + // assertTrue(Arrays.equals(new byte[0], p.getByteArray( + // "testGetByteArrayKey", new byte[0]))); + assertTrue(Arrays.equals(new byte[] { 105, -73 }, pref.getByteArray( + "testGetByteArrayKey", new byte[0]))); + assertTrue(Arrays.equals(new byte[] { 'a', 'b', 'c' }, pref + .getByteArray("testGetByteArrayKey2", new byte[0]))); + assertTrue(Arrays.equals(new byte[0], pref.getByteArray("invalidKey", + new byte[0]))); + + pref.putByteArray("testGetByteArrayKey3", b64Array); + pref.putByteArray("testGetByteArrayKey4", "abc".getBytes()); + assertTrue(Arrays.equals(b64Array, pref.getByteArray( + "testGetByteArrayKey3", new byte[0]))); + assertTrue(Arrays.equals("abc".getBytes(), pref.getByteArray( + "testGetByteArrayKey4", new byte[0]))); + } + + public void testGetDouble() { + try { + pref.getDouble(null, 0); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.put("testGetDoubleKey", "1"); + pref.put("testGetDoubleKey2", "value"); + pref.putDouble("testGetDoubleKey3", 1); + pref.putInt("testGetDoubleKey4", 1); + assertEquals(1.0, pref.getDouble("testGetDoubleKey", 0.0), 0); + assertEquals(0.0, pref.getDouble("testGetDoubleKey2", 0.0), 0); + assertEquals(1.0, pref.getDouble("testGetDoubleKey3", 0.0), 0); + assertEquals(1.0, pref.getDouble("testGetDoubleKey4", 0.0), 0); + } + + public void testGetFloat() { + try { + pref.getFloat(null, 0f); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + pref.put("testGetFloatKey", "1"); + pref.put("testGetFloatKey2", "value"); + assertEquals(1f, pref.getFloat("testGetFloatKey", 0f), 0); //$NON-NLS-1$ + assertEquals(0f, pref.getFloat("testGetFloatKey2", 0f), 0); + } + + public void testGetInt() { + try { + pref.getInt(null, 0); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.put("testGetIntKey", "1"); + pref.put("testGetIntKey2", "value"); + assertEquals(1, pref.getInt("testGetIntKey", 0)); + assertEquals(0, pref.getInt("testGetIntKey2", 0)); + } + + public void testGetLong() { + try { + pref.getLong(null, 0); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.put("testGetLongKey", "1"); + pref.put("testGetLongKey2", "value"); + assertEquals(1, pref.getInt("testGetLongKey", 0)); + assertEquals(0, pref.getInt("testGetLongKey2", 0)); + } + + public void testIsUserNode() { + assertTrue(pref.isUserNode()); + + pref = new MockAbstractPreferences((AbstractPreferences) Preferences + .systemRoot(), "mock"); + assertFalse(pref.isUserNode()); + } + + // TODO, how to test the "stored defaults" + // TODO, how to test the multi-thread + public void testKeys() throws BackingStoreException { + assertEquals(0, pref.keys().length); + + pref.put("key0", "value"); + pref.put("key1", "value1"); + pref.put("key2", "value2"); + pref.put("key3", "value3"); + + String[] keys = pref.keys(); + assertEquals(4, keys.length); + for (int i = 0; i < keys.length; i++) { + assertEquals(0, keys[i].indexOf("key")); + assertEquals(4, keys[i].length()); + } + } + + public void testName() { + assertEquals("mock", pref.name()); + + pref = new MockAbstractPreferences(pref, " "); + assertEquals(" ", pref.name()); + } + + public void testCharCase() throws BackingStoreException { + assertSame(pref.node("samechild"), pref.node("samechild")); + assertNotSame(pref.node("sameChild"), pref.node("samechild")); + assertNotSame(pref.node("child"), pref.node("Child")); + assertNotSame(pref.node("child"), pref.node("Child")); + assertNotSame(pref.node("child"), pref.node(" child")); + String[] names = pref.childrenNames(); + assertEquals(5, names.length); + for (int i = 0; i < names.length; i++) { + String name = names[i]; + assertTrue("samechild".equals(name) || "sameChild".equals(name) + || "child".equals(name) || "Child".equals(name) + || " child".equals(name)); + } + + Preferences mock1 = pref.node("mock1"); + mock1.put("key", "1value"); + mock1.put("KEY", "2value"); + mock1.put("/K/E/Y", "7value"); + mock1.put("/K/E\\Y\\abc~@!#$%^&*(\\", "8value"); + + assertEquals("8value", mock1.get("/K/E\\Y\\abc~@!#$%^&*(\\", null)); + assertNull(mock1.get("/k/e/y", null)); + assertEquals("7value", mock1.get("/K/E/Y", null)); + assertEquals("1value", mock1.get("key", null)); + + String[] keys = mock1.keys(); + assertEquals(4, keys.length); + for (int i = 0; i < keys.length; i++) { + String key = keys[i]; + assertTrue("key".equals(key) || "KEY".equals(key) + || "/K/E/Y".equals(key) + || "/K/E\\Y\\abc~@!#$%^&*(\\".equals(key)); + } + } + + public void testNode() throws BackingStoreException { + try { + pref.node(null); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + pref.node("/java/util/prefs/"); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + try { + pref.node("/java//util/prefs"); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + try { + pref.node(LONG_NAME + "a"); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + assertNotNull(pref.node(LONG_NAME)); + + assertSame(root, pref.node("/")); + + Preferences prefs = pref.node("/java/util/prefs"); + assertSame(prefs, parent); + + assertSame(pref, pref.node("")); + + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + MockAbstractPreferences child = (MockAbstractPreferences) ((MockAbstractPreferences) pref) + .publicChildSpi("child"); + assertSame(child, pref.node("child")); + + Preferences child2 = pref.node("child2"); + assertSame(child2, ((MockAbstractPreferences) pref) + .publicChildSpi("child2")); + + Preferences grandchild = pref.node("child/grandchild"); + assertSame(grandchild, child.childSpi("grandchild")); + assertSame(grandchild, child.cachedChildrenImpl()[0]); + grandchild.removeNode(); + assertNotSame(grandchild, pref.node("child/grandchild")); + + grandchild = pref.node("child3/grandchild"); + AbstractPreferences[] childs = ((MockAbstractPreferences) pref) + .cachedChildrenImpl(); + Preferences child3 = child; + for (int i = 0; i < childs.length; i++) { + if (childs[i].name().equals("child3")) { + child3 = childs[i]; + break; + } + } + assertSame(child3, grandchild.parent()); + } + + public void testNodeExists() throws BackingStoreException { + try { + pref.nodeExists(null); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + pref.nodeExists("/java/util/prefs/"); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + try { + pref.nodeExists("/java//util/prefs"); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + + assertTrue(pref.nodeExists("/")); + + assertTrue(pref.nodeExists("/java/util/prefs")); + + assertTrue(pref.nodeExists("")); + + assertFalse(pref.nodeExists("child")); + Preferences grandchild = pref.node("child/grandchild"); + assertTrue(pref.nodeExists("child")); + assertTrue(pref.nodeExists("child/grandchild")); + grandchild.removeNode(); + assertTrue(pref.nodeExists("child")); + assertFalse(pref.nodeExists("child/grandchild")); + assertFalse(grandchild.nodeExists("")); + + assertFalse(pref.nodeExists("child2/grandchild")); + pref.node("child2/grandchild"); + assertTrue(pref.nodeExists("child2/grandchild")); + } + + public void test_nodeExists() throws BackingStoreException { + AbstractPreferences test = (AbstractPreferences) Preferences.userRoot() + .node("test"); + try { + test.nodeExists(null); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + test.removeNode(); + try { + test.nodeExists(null); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + } + + public void testParent() { + assertSame(parent, pref.parent()); + AbstractPreferences child1 = new MockAbstractPreferences(pref, "child1"); + assertSame(pref, child1.parent()); + assertNull(root.parent()); + } + + public void testPut() throws BackingStoreException { + pref.put("", "emptyvalue"); + assertEquals("emptyvalue", pref.get("", null)); + pref.put("testPutkey", "value1"); + assertEquals("value1", pref.get("testPutkey", null)); + pref.put("testPutkey", "value2"); + assertEquals("value2", pref.get("testPutkey", null)); + + pref.put("", "emptyvalue"); + assertEquals("emptyvalue", pref.get("", null)); + + try { + pref.put(null, "value"); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + try { + pref.put("key", null); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.put(LONG_KEY, LONG_VALUE); + try { + pref.put(LONG_KEY + 1, LONG_VALUE); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + + try { + pref.put(LONG_KEY, LONG_VALUE + 1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + + pref.removeNode(); + try { + pref.put(LONG_KEY, LONG_VALUE + 1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + + try { + pref.put(LONG_KEY, LONG_VALUE); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + } + + public void testPutBoolean() { + try { + pref.putBoolean(null, false); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.putBoolean(LONG_KEY, false); + try { + pref.putBoolean(LONG_KEY + "a", false); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + + pref.putBoolean("testPutBooleanKey", false); + assertEquals("false", pref.get("testPutBooleanKey", null)); + assertFalse(pref.getBoolean("testPutBooleanKey", true)); + } + + public void testPutDouble() { + try { + pref.putDouble(null, 3); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.putDouble(LONG_KEY, 3); + try { + pref.putDouble(LONG_KEY + "a", 3); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + + pref.putDouble("testPutDoubleKey", 3); + assertEquals("3.0", pref.get("testPutDoubleKey", null)); + assertEquals(3, pref.getDouble("testPutDoubleKey", 0), 0); + } + + public void testPutFloat() { + try { + pref.putFloat(null, 3f); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.putFloat(LONG_KEY, 3f); + try { + pref.putFloat(LONG_KEY + "a", 3f); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + + pref.putFloat("testPutFloatKey", 3f); + assertEquals("3.0", pref.get("testPutFloatKey", null)); + assertEquals(3f, pref.getFloat("testPutFloatKey", 0), 0); + } + + public void testPutInt() { + try { + pref.putInt(null, 3); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.putInt(LONG_KEY, 3); + try { + pref.putInt(LONG_KEY + "a", 3); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + + pref.putInt("testPutIntKey", 3); + assertEquals("3", pref.get("testPutIntKey", null)); + assertEquals(3, pref.getInt("testPutIntKey", 0)); + } + + public void testPutLong() { + try { + pref.putLong(null, 3L); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.putLong(LONG_KEY, 3L); + try { + pref.putLong(LONG_KEY + "a", 3L); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + + pref.putLong("testPutLongKey", 3L); + assertEquals("3", pref.get("testPutLongKey", null)); + assertEquals(3L, pref.getLong("testPutLongKey", 0)); + } + + public void testRemove() throws BackingStoreException { + pref.remove("key"); + + pref.put("key", "value"); + assertEquals("value", pref.get("key", null)); + pref.remove("key"); + assertNull(pref.get("key", null)); + + pref.remove("key"); + + try { + pref.remove(null); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.removeNode(); + try { + pref.remove("key"); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + } + + public void testRemoveNode() throws BackingStoreException { + Preferences child = pref.node("child"); + Preferences child1 = pref.node("child1"); + Preferences grandchild = child.node("grandchild"); + + pref.removeNode(); + + assertFalse(child.nodeExists("")); + assertFalse(child1.nodeExists("")); + assertFalse(grandchild.nodeExists("")); + assertFalse(pref.nodeExists("")); + } + + public void testRemoveNodeChangeListener() { + try { + pref.removeNodeChangeListener(null); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + + MockNodeChangeListener l1 = new MockNodeChangeListener(); + MockNodeChangeListener l2 = new MockNodeChangeListener(); + pref.addNodeChangeListener(l1); + pref.addNodeChangeListener(l1); + + pref.removeNodeChangeListener(l1); + pref.removeNodeChangeListener(l1); + try { + pref.removeNodeChangeListener(l1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + + try { + pref.removeNodeChangeListener(l2); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + } + + public void testRemovePreferenceChangeListener() { + try { + pref.removePreferenceChangeListener(null); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + MockPreferenceChangeListener l1 = new MockPreferenceChangeListener(); + MockPreferenceChangeListener l2 = new MockPreferenceChangeListener(); + pref.addPreferenceChangeListener(l1); + pref.addPreferenceChangeListener(l1); + try { + pref.removePreferenceChangeListener(l2); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + pref.removePreferenceChangeListener(l1); + pref.removePreferenceChangeListener(l1); + try { + pref.removePreferenceChangeListener(l1); + fail("should throw IllegalArgumentException"); + } catch (IllegalArgumentException expected) { + } + } + + public void testSync() throws BackingStoreException { + pref.sync(); + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + MockAbstractPreferences p = (MockAbstractPreferences) pref; + p.resetSyncTimes(); + p.sync(); + assertEquals(1, p.getSyncTimes()); + + p.resetSyncTimes(); + MockAbstractPreferences child = (MockAbstractPreferences) p + .node("child"); + MockAbstractPreferences child2 = new MockAbstractPreferences(p, + "child2"); + p.childs.put("child2", child2); + assertEquals(1, p.cachedChildrenImpl().length); + assertSame(child, p.cachedChildrenImpl()[0]); + p.sync(); + assertEquals(1, p.getSyncTimes()); + assertEquals(1, child.getSyncTimes()); + assertEquals(0, child2.getSyncTimes()); + + p.resetSyncTimes(); + child.resetSyncTimes(); + child.sync(); + assertEquals(0, p.getSyncTimes()); + assertEquals(1, child.getSyncTimes()); + + p.resetSyncTimes(); + child.resetSyncTimes(); + MockAbstractPreferences grandson = (MockAbstractPreferences) child + .node("grandson"); + child.sync(); + assertEquals(0, p.getSyncTimes()); + assertEquals(1, child.getSyncTimes()); + assertEquals(1, grandson.getSyncTimes()); + } + + public void testFlush() throws BackingStoreException { + pref.flush(); + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + MockAbstractPreferences p = (MockAbstractPreferences) pref; + p.resetFlushedTimes(); + p.flush(); + assertEquals(1, p.getFlushedTimes()); + + p.resetFlushedTimes(); + MockAbstractPreferences child = (MockAbstractPreferences) p + .node("child"); + MockAbstractPreferences child2 = new MockAbstractPreferences(p, + "child2"); + p.childs.put("child2", child2); + assertEquals(1, p.cachedChildrenImpl().length); + assertSame(child, p.cachedChildrenImpl()[0]); + p.flush(); + assertEquals(1, p.getFlushedTimes()); + assertEquals(1, child.getFlushedTimes()); + assertEquals(0, child2.getFlushedTimes()); + + p.resetFlushedTimes(); + child.resetFlushedTimes(); + child.flush(); + assertEquals(0, p.getFlushedTimes()); + assertEquals(1, child.getFlushedTimes()); + + p.resetFlushedTimes(); + child.resetFlushedTimes(); + MockAbstractPreferences grandson = (MockAbstractPreferences) child + .node("grandson"); + child.flush(); + assertEquals(0, p.getFlushedTimes()); + assertEquals(1, child.getFlushedTimes()); + assertEquals(1, grandson.getFlushedTimes()); + + p.resetFlushedTimes(); + child.resetFlushedTimes(); + grandson.resetFlushedTimes(); + child.removeNode(); + child.flush(); + assertEquals(0, p.getFlushedTimes()); + assertEquals(1, child.getFlushedTimes()); + assertEquals(0, grandson.getFlushedTimes()); + } + + public void testGetChild() throws BackingStoreException { + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + MockAbstractPreferences p = (MockAbstractPreferences) pref; + assertNull(p.getChildImpl("child")); + MockAbstractPreferences child = new MockAbstractPreferences(p, "child"); + p.childs.put("child", child); + assertSame(child, p.getChildImpl("child")); + assertNull(p.getChildImpl("child ")); + + assertNull(p.getChildImpl("child/grandson")); + child.childs.put("grandson", new MockAbstractPreferences(child, + "grandson")); + assertNull(p.getChildImpl("child/grandson")); + + assertNull(p.getChildImpl(null)); + assertNull(p.getChildImpl("")); + assertNull(p.getChildImpl(" ")); + assertNull(p.getChildImpl("abc//abc")); + assertNull(p.getChildImpl("child/")); + assertNull(p.getChildImpl(LONG_NAME + "a")); + + child.removeNode(); + assertNull(p.getChildImpl("child")); + } + + public void testIsRemoved() throws BackingStoreException { + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + MockAbstractPreferences p = (MockAbstractPreferences) pref; + assertFalse(p.isRemovedImpl()); + p.removeNode(); + assertTrue(p.isRemovedImpl()); + } + + public void testExportNode() throws Exception { + try { + pref.exportNode(null); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + pref.putBoolean("key", false); + Preferences child = pref.node("child<"); + child.put("key2", "value2<"); + Preferences grandson = child.node("grandson"); + grandson.put("key3", "value3"); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + child.exportNode(out); + + byte[] result = out.toByteArray(); + ByteArrayInputStream in = new ByteArrayInputStream(result); + + parseXmlStream(in, false); + } + + private static Document parseXmlStream(InputStream input, boolean validating) + throws SAXException, IOException, ParserConfigurationException { + // Create a builder factory + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setValidating(validating); + // Create the builder and parse the file + DocumentBuilder builder = factory.newDocumentBuilder(); + return builder.parse(input); + } + + public void testExportSubtree() throws Exception { + try { + pref.exportSubtree(null); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + pref.putBoolean("key", false); + Preferences child = pref.node("child"); + child.put("key2", "value2"); + Preferences grandson = child.node("grandson"); + grandson.put("key3", "value3"); + child.node("grandson2"); + Preferences grandgrandson = grandson.node("grandgrandson"); + grandgrandson.put("key4", "value4"); + child.exportSubtree(out); + + byte[] result = out.toByteArray(); + ByteArrayInputStream in = new ByteArrayInputStream(result); + + // We don't have an xpath API so all we can do is test that the + // generated XML parses correctly. + parseXmlStream(in, false); + } + + public void testCachedChildren() throws Exception { + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + MockAbstractPreferences p = (MockAbstractPreferences) pref; + assertEquals(0, p.cachedChildrenImpl().length); + + MockAbstractPreferences child = (MockAbstractPreferences) p + .getChildImpl("child"); + assertNull(child); + + child = new MockAbstractPreferences(p, "child"); + assertSame(child, p.getChildImpl("child")); + + assertEquals(0, p.cachedChildrenImpl().length); + + p.node("child"); + assertSame(child, p.cachedChildrenImpl()[0]); + + MockAbstractPreferences grandchild = new MockAbstractPreferences(child, + "grandchild"); + assertSame(grandchild, child.getChildImpl("grandchild")); + assertNull(p.getChildImpl("grandchild")); + + assertEquals(1, p.cachedChildrenImpl().length); + assertEquals(0, child.cachedChildrenImpl().length); + + p.node("child/grandchild"); + assertSame(child, p.cachedChildrenImpl()[0]); + assertSame(grandchild, child.cachedChildrenImpl()[0]); + assertEquals(1, p.cachedChildrenImpl().length); + assertEquals(1, child.cachedChildrenImpl().length); + + p.childs.put("child2", new MockAbstractPreferences(p, "child2")); + p.nodeExists("child2/grandchild"); + assertSame(child, p.cachedChildrenImpl()[0]); + assertSame(grandchild, child.cachedChildrenImpl()[0]); + assertEquals(1, p.cachedChildrenImpl().length); + assertEquals(1, child.cachedChildrenImpl().length); + } + + public void testAbstractMethod() { + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + ((MockAbstractPreferences) pref).protectedAbstractMethod(); + } + + public void testBackingStoreException() throws IOException, + BackingStoreException { + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + MockAbstractPreferences p = (MockAbstractPreferences) pref; + p.setResult(MockAbstractPreferences.backingException); + try { + p.childrenNames(); + fail("should throw BackingStoreException"); + } catch (BackingStoreException e) { + } + p.put("exceptionkey", "value"); + p.absolutePath(); + p.toString(); + assertEquals("exception default", p.get("key", "exception default")); + p.remove("key"); + try { + p.clear(); + fail("should throw BackingStoreException"); + } catch (BackingStoreException expected) { + } + + p.putInt("key", 3); + p.getInt("key", 3); + p.putLong("key", 3l); + p.getLong("key", 3l); + p.putDouble("key", 3); + p.getDouble("key", 3); + p.putBoolean("key", true); + p.getBoolean("key", true); + p.putFloat("key", 3f); + p.getFloat("key", 3f); + p.putByteArray("key", new byte[0]); + p.getByteArray("key", new byte[0]); + try { + p.keys(); + fail("should throw BackingStoreException"); + } catch (BackingStoreException expected) { + } + + try { + p.keys(); + fail("should throw BackingStoreException"); + } catch (BackingStoreException expected) { + } + try { + p.childrenNames(); + fail("should throw BackingStoreException"); + } catch (BackingStoreException expected) { + } + p.parent(); + p.node(""); + p.nodeExists(""); + try { + p.removeNode(); + fail("should throw BackingStoreException"); + } catch (BackingStoreException expected) { + } + p.name(); + p.absolutePath(); + p.isUserNode(); + MockPreferenceChangeListener mockPreferenceChangeListener = new MockPreferenceChangeListener(); + p.addPreferenceChangeListener(mockPreferenceChangeListener); + p.removePreferenceChangeListener(mockPreferenceChangeListener); + MockNodeChangeListener mockNodeChangeListener = new MockNodeChangeListener(); + p.addNodeChangeListener(mockNodeChangeListener); + p.removeNodeChangeListener(mockNodeChangeListener); + p.toString(); + try { + p.sync(); + fail("should throw BackingStoreException"); + } catch (BackingStoreException expected) { + } + try { + p.flush(); + fail("should throw BackingStoreException"); + } catch (BackingStoreException expected) { + } + try { + p.exportNode(new ByteArrayOutputStream()); + fail("should throw BackingStoreException"); + } catch (BackingStoreException expected) { + } + try { + p.exportSubtree(new ByteArrayOutputStream()); + fail("should throw BackingStoreException"); + } catch (BackingStoreException expected) { + } + p.isRemovedImpl(); + try { + p.getChildImpl(null); + fail("should throw BackingStoreException"); + } catch (BackingStoreException expected) { + } + p.cachedChildrenImpl(); + } + + public void testRuntimeException() throws IOException, + BackingStoreException { + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + MockAbstractPreferences p = (MockAbstractPreferences) pref; + p.setResult(MockAbstractPreferences.runtimeException); + try { + p.childrenNames(); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + try { + p.put("exceptionkey", "value"); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + p.absolutePath(); + p.toString(); + assertEquals("exception default", p.get("key", "exception default")); + try { + p.remove("key"); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + try { + p.clear(); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + try { + p.putInt("key", 3); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + p.getInt("key", 3); + try { + p.putLong("key", 3l); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + p.getLong("key", 3l); + try { + p.putDouble("key", 3); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + p.getDouble("key", 3); + try { + p.putBoolean("key", true); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + p.getBoolean("key", true); + try { + p.putFloat("key", 3f); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + p.getFloat("key", 3f); + try { + p.putByteArray("key", new byte[0]); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + p.getByteArray("key", new byte[0]); + try { + p.keys(); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + try { + p.keys(); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + try { + p.childrenNames(); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + p.parent(); + p.node(""); + p.nodeExists(""); + try { + p.removeNode(); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + p.name(); + p.absolutePath(); + p.isUserNode(); + MockPreferenceChangeListener pcl = new MockPreferenceChangeListener(); + p.addPreferenceChangeListener(pcl); + p.removePreferenceChangeListener(pcl); + MockNodeChangeListener ncl = new MockNodeChangeListener(); + p.addNodeChangeListener(ncl); + p.removeNodeChangeListener(ncl); + p.toString(); + try { + p.sync(); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + try { + p.flush(); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + try { + p.exportNode(new ByteArrayOutputStream()); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + try { + p.exportSubtree(new ByteArrayOutputStream()); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + p.isRemovedImpl(); + try { + p.getChildImpl(null); + fail("should throw MockRuntimeException"); + } catch (MockRuntimeException expected) { + } + p.cachedChildrenImpl(); + } + + public void testSPIReturnNull() throws IOException, BackingStoreException { + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + MockAbstractPreferences p = (MockAbstractPreferences) pref; + p.setResult(MockAbstractPreferences.returnNull); + try { + p.childrenNames(); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + p.absolutePath(); + p.toString(); + p.put("nullkey", "value"); + assertEquals("null default", p.get("key", "null default")); + p.remove("key"); + try { + p.clear(); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + p.putInt("key", 3); + p.getInt("key", 3); + p.putLong("key", 3l); + p.getLong("key", 3l); + p.putDouble("key", 3); + p.getDouble("key", 3); + p.putBoolean("key", true); + p.getBoolean("key", true); + p.putFloat("key", 3f); + p.getFloat("key", 3f); + p.putByteArray("key", new byte[0]); + p.getByteArray("key", new byte[0]); + p.keys(); + try { + p.childrenNames(); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + p.parent(); + p.node(""); + p.nodeExists(""); + try { + p.removeNode(); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + p.name(); + p.absolutePath(); + p.isUserNode(); + MockPreferenceChangeListener mockPreferenceChangeListener = new MockPreferenceChangeListener(); + p.addPreferenceChangeListener(mockPreferenceChangeListener); + p.removePreferenceChangeListener(mockPreferenceChangeListener); + MockNodeChangeListener mockNodeChangeListener = new MockNodeChangeListener(); + p.addNodeChangeListener(mockNodeChangeListener); + p.removeNodeChangeListener(mockNodeChangeListener); + p.toString(); + p.sync(); + p.flush(); + try { + p.exportNode(System.out); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.exportSubtree(System.out); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + p.isRemovedImpl(); + try { + p.getChildImpl(""); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + p.cachedChildrenImpl(); + } + + public void testIllegalStateException() throws IOException, + BackingStoreException { + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + pref.removeNode(); + // after remove node, every methods, except name(), absolutePath(), + // isUserNode(), flush() or nodeExists(""), + // will throw illegal state exception + pref.nodeExists(""); + pref.name(); + pref.absolutePath(); + pref.isUserNode(); + pref.toString(); + pref.flush(); + try { + pref.nodeExists("child"); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.childrenNames(); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.remove(null); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.clear(); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.get("key", "null default"); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.put("nullkey", "value"); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.putInt("key", 3); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.getInt("key", 3); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.putLong("key", 3l); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.getLong("key", 3l); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.putDouble("key", 3); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.getDouble("key", 3); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.putBoolean("key", true); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.getBoolean("key", true); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.putFloat("key", 3f); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.getFloat("key", 3f); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.putByteArray("key", new byte[0]); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.getByteArray("key", new byte[0]); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.keys(); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.keys(); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.childrenNames(); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.parent(); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.node(null); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.removeNode(); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref + .addPreferenceChangeListener(new MockPreferenceChangeListener()); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref + .removePreferenceChangeListener(new MockPreferenceChangeListener()); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.addNodeChangeListener(new MockNodeChangeListener()); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.removeNodeChangeListener(new MockNodeChangeListener()); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.sync(); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.exportNode(null); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + pref.exportSubtree(null); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + MockAbstractPreferences p = (MockAbstractPreferences) pref; + p.isRemovedImpl(); + p.cachedChildrenImpl(); + try { + p.getChildImpl(null); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + } + + public void testNullAndIllegalStateException() throws Exception { + if (!(pref instanceof MockAbstractPreferences)) { + return; + } + MockAbstractPreferences p = (MockAbstractPreferences) pref; + p.removeNode(); + try { + p.get(null, "null default"); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.put(null, "value"); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.putInt(null, 3); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.getInt(null, 3); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.putLong(null, 3l); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.getLong(null, 3l); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.putDouble(null, 3); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.getDouble(null, 3); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.putBoolean(null, true); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.getBoolean(null, true); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.putFloat(null, 3f); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.getFloat(null, 3f); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.putByteArray(null, new byte[0]); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.getByteArray(null, new byte[0]); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.addPreferenceChangeListener(null); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.removePreferenceChangeListener(null); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + try { + p.addNodeChangeListener(null); + fail("should throw NullPointerException"); + } catch (NullPointerException expected) { + } + try { + p.removeNodeChangeListener(null); + fail("should throw IllegalStateException"); + } catch (IllegalStateException expected) { + } + } + + /** + * Regression for HARMONY-828 + */ + public void testLongPath() throws Exception { + assertFalse(pref.nodeExists("ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd")); + } + + public static class MockPreferenceChangeListener implements + PreferenceChangeListener { + private int changed = 0; + + public void preferenceChange(PreferenceChangeEvent pce) { + changed++; + } + + public int getChanged() { + int result = changed; + changed = 0; + return result; + } + } + + public static class MockNodeChangeListener implements NodeChangeListener { + private boolean addDispatched = false; + + private boolean removeDispatched = false; + + private Object addLock = new Object(); + + private Object removeLock = new Object(); + + private int added = 0; + + private int removed = 0; + + public void childAdded(NodeChangeEvent e) { + synchronized (addLock) { + ++added; + addDispatched = true; + addLock.notifyAll(); + } + } + + public void childRemoved(NodeChangeEvent e) { + synchronized (removeLock) { + removed++; + removeDispatched = true; + removeLock.notifyAll(); + } + } + + public int getAdded() { + synchronized (addLock) { + if (!addDispatched) { + try { + // TODO: don't know why must add limitation + addLock.wait(100); + } catch (InterruptedException e) { + } + } + addDispatched = false; + } + return added; + } + + public int getRemoved() { + synchronized (removeLock) { + if (!removeDispatched) { + try { + removeLock.wait(100); + } catch (InterruptedException e) { + } + } + removeDispatched = false; + } + return removed; + + } + + public void reset() { + added = 0; + removed = 0; + } + } + +} diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/BackingStoreExceptionTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/BackingStoreExceptionTest.java new file mode 100644 index 0000000..4d264e1 --- /dev/null +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/BackingStoreExceptionTest.java @@ -0,0 +1,62 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.java.util.prefs; + +import java.util.prefs.BackingStoreException; +import junit.framework.TestCase; +import org.apache.harmony.testframework.serialization.SerializationTest; + +/** + * + * + */ +public class BackingStoreExceptionTest extends TestCase { + + /* + * Class under test for void BackingStoreException(String) + */ + public void testBackingStoreExceptionString() { + BackingStoreException e = new BackingStoreException("msg"); + assertNull(e.getCause()); + assertEquals("msg", e.getMessage()); + } + + /* + * Class under test for void BackingStoreException(Throwable) + */ + public void testBackingStoreExceptionThrowable() { + Throwable t = new Throwable("msg"); + BackingStoreException e = new BackingStoreException(t); + assertTrue(e.getMessage().indexOf(t.getClass().getName()) >= 0); + assertTrue(e.getMessage().indexOf("msg") >= 0); + assertEquals(t, e.getCause()); + } + + /** + * @tests serialization/deserialization. + */ + public void testSerializationSelf() throws Exception { + SerializationTest.verifySelf(new BackingStoreException("msg")); + } + + /** + * @tests serialization/deserialization compatibility with RI. + */ + public void testSerializationCompatibility() throws Exception { + SerializationTest.verifyGolden(this, new BackingStoreException("msg")); + } +} diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/FilePreferencesImplTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/FilePreferencesImplTest.java new file mode 100644 index 0000000..d2b344e --- /dev/null +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/FilePreferencesImplTest.java @@ -0,0 +1,130 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.java.util.prefs; + +import java.io.File; +import java.io.IOException; +import java.util.prefs.BackingStoreException; +import java.util.prefs.Preferences; +import java.util.prefs.PreferencesFactory; +import junit.framework.TestCase; +import libcore.io.IoUtils; + +public class FilePreferencesImplTest extends TestCase { + + private String prevFactory; + private Preferences uroot; + private Preferences sroot; + private PreferencesFactory defaultFactory; + + @Override + protected void setUp() throws Exception { + prevFactory = System.getProperty("java.util.prefs.PreferencesFactory"); + System.setProperty("java.util.prefs.PreferencesFactory", "java.util.prefs.FilePreferencesFactoryImpl"); + + File tmpDir = IoUtils.createTemporaryDirectory("OldAbstractPreferencesTest"); + defaultFactory = Preferences.setPreferencesFactory( + new AbstractPreferencesTest.TestPreferencesFactory(tmpDir.getAbsolutePath())); + + uroot = Preferences.userRoot().node("harmony_test"); + sroot = Preferences.systemRoot().node("harmony_test"); + } + + @Override + protected void tearDown() throws Exception { + Preferences.setPreferencesFactory(defaultFactory); + if (prevFactory != null) { + System.setProperty("java.util.prefs.PreferencesFactory", prevFactory); + } + + uroot.removeNode(); + sroot.removeNode(); + uroot = null; + sroot = null; + } + + public void testPutGet() throws IOException, BackingStoreException { + uroot.put("ukey1", "value1"); + assertEquals("value1", uroot.get("ukey1", null)); + String[] names = uroot.keys(); + assertEquals(1, names.length); + + uroot.put("ukey2", "value3"); + assertEquals("value3", uroot.get("ukey2", null)); + uroot.put("\u4e2d key1", "\u4e2d value1"); + assertEquals("\u4e2d value1", uroot.get("\u4e2d key1", null)); + names = uroot.keys(); + assertEquals(3, names.length); + + uroot.flush(); + uroot.clear(); + names = uroot.keys(); + assertEquals(0, names.length); + + sroot.put("skey1", "value1"); + assertEquals("value1", sroot.get("skey1", null)); + sroot.put("\u4e2d key1", "\u4e2d value1"); + assertEquals("\u4e2d value1", sroot.get("\u4e2d key1", null)); + } + + public void testChildNodes() throws Exception { + Preferences child1 = uroot.node("child1"); + Preferences child2 = uroot.node("\u4e2d child2"); + Preferences grandchild = child1.node("grand"); + assertNotNull(grandchild); + + String[] childNames = uroot.childrenNames(); + assertEquals(2, childNames.length); + + childNames = child1.childrenNames(); + assertEquals(1, childNames.length); + + childNames = child2.childrenNames(); + assertEquals(0, childNames.length); + + child1.removeNode(); + childNames = uroot.childrenNames(); + assertEquals(1, childNames.length); + + child2.removeNode(); + childNames = uroot.childrenNames(); + assertEquals(0, childNames.length); + + child1 = sroot.node("child1"); + child2 = sroot.node("child2"); + childNames = sroot.childrenNames(); + + Preferences grandchild2 = child1.node("grand"); + assertEquals(2, childNames.length); + + childNames = child1.childrenNames(); + assertEquals(1, childNames.length); + + childNames = child2.childrenNames(); + assertEquals(0, childNames.length); + + child1.removeNode(); + assertNotSame(child1, sroot.node("child1")); + assertSame(sroot.node("child1"), sroot.node("child1")); + sroot.node("child1").removeNode(); + childNames = sroot.childrenNames(); + assertEquals(1, childNames.length); + child2.removeNode(); + childNames = sroot.childrenNames(); + assertEquals(0, childNames.length); + } +} diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/InvalidPreferencesFormatExceptionTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/InvalidPreferencesFormatExceptionTest.java new file mode 100644 index 0000000..7c4891f --- /dev/null +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/InvalidPreferencesFormatExceptionTest.java @@ -0,0 +1,81 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.java.util.prefs; + +import java.util.prefs.InvalidPreferencesFormatException; +import junit.framework.TestCase; +import org.apache.harmony.testframework.serialization.SerializationTest; + +/** + * + */ +public class InvalidPreferencesFormatExceptionTest extends TestCase { + + /* + * Class under test for void InvalidPreferencesFormatException(String) + */ + public void testInvalidPreferencesFormatExceptionString() { + InvalidPreferencesFormatException e = new InvalidPreferencesFormatException( + "msg"); + assertNull(e.getCause()); + assertEquals("msg", e.getMessage()); + } + + /* + * Class under test for void InvalidPreferencesFormatException(String, + * Throwable) + */ + public void testInvalidPreferencesFormatExceptionStringThrowable() { + Throwable t = new Throwable("root"); + InvalidPreferencesFormatException e = new InvalidPreferencesFormatException( + "msg", t); + assertSame(t, e.getCause()); + assertTrue(e.getMessage().indexOf("root") < 0); + assertTrue(e.getMessage().indexOf(t.getClass().getName()) < 0); + assertTrue(e.getMessage().indexOf("msg") >= 0); + } + + /* + * Class under test for void InvalidPreferencesFormatException(Throwable) + */ + public void testInvalidPreferencesFormatExceptionThrowable() { + Throwable t = new Throwable("root"); + InvalidPreferencesFormatException e = new InvalidPreferencesFormatException( + t); + assertSame(t, e.getCause()); + assertTrue(e.getMessage().indexOf("root") >= 0); + assertTrue(e.getMessage().indexOf(t.getClass().getName()) >= 0); + } + + /** + * @tests serialization/deserialization. + */ + public void testSerializationSelf() throws Exception { + + SerializationTest.verifySelf(new InvalidPreferencesFormatException( + "msg")); + } + + /** + * @tests serialization/deserialization compatibility with RI. + */ + public void testSerializationCompatibility() throws Exception { + + SerializationTest.verifyGolden(this, + new InvalidPreferencesFormatException("msg")); + } +} diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/MockAbstractPreferences.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/MockAbstractPreferences.java new file mode 100644 index 0000000..88f184b --- /dev/null +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/MockAbstractPreferences.java @@ -0,0 +1,263 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.java.util.prefs; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.prefs.AbstractPreferences; +import java.util.prefs.BackingStoreException; +import java.util.prefs.Preferences; + +public class MockAbstractPreferences extends AbstractPreferences { + static final int NORMAL = 0; + + static final int backingException = 1; + + static final int runtimeException = 2; + + static final int returnNull = 3; + + int result = NORMAL; + + Properties attr = new Properties(); + + Map<String, MockAbstractPreferences> childs = new HashMap<String, MockAbstractPreferences>(); + + private int flushedTimes; + + private int syncTimes; + + protected MockAbstractPreferences(AbstractPreferences parent, String name) { + this(parent, name, false); + + } + + protected MockAbstractPreferences(AbstractPreferences parent, String name, + boolean newNode) { + super(parent, name); + super.newNode = newNode; + if (parent instanceof MockAbstractPreferences) { + ((MockAbstractPreferences) parent).addChild(this); + } + } + + public int getFlushedTimes() { + return flushedTimes; + } + + public void resetFlushedTimes() { + flushedTimes = 0; + } + + public int getSyncTimes() { + return syncTimes; + } + + public void resetSyncTimes() { + syncTimes = 0; + } + + private void addChild(MockAbstractPreferences c) { + childs.put(c.name(), c); + } + + public void setResult(int r) { + result = r; + } + + public Object lock() { + return lock; + } + + @Override + protected String[] childrenNamesSpi() throws BackingStoreException { + checkException(); + if (result == returnNull) + return null; + String[] r = new String[childs.size()]; + childs.keySet().toArray(r); + return r; + } + + private void checkException() throws BackingStoreException { + switch (result) { + case NORMAL: + return; + case backingException: + throw new BackingStoreException("test"); + case runtimeException: + throw new MockRuntimeException("test"); + } + } + + public AbstractPreferences publicChildSpi(String name) { + return childSpi(name); + } + + @Override + protected AbstractPreferences childSpi(String name) { + try { + checkException(); + } catch (BackingStoreException e) { + } + if (result == returnNull) + return null; + AbstractPreferences r = childs.get(name); + if (r == null) { + r = new MockAbstractPreferences(this, name, true); + + } + return r; + } + + @Override + protected void flushSpi() throws BackingStoreException { + checkException(); + flushedTimes++; + } + + @Override + protected String getSpi(String key) { + try { + checkException(); + } catch (BackingStoreException e) { + } + if (null == key) { + return null; + } + return result == returnNull ? null : attr.getProperty(key); + } + + @Override + protected String[] keysSpi() throws BackingStoreException { + checkException(); + Set<Object> keys = attr.keySet(); + String[] results = new String[keys.size()]; + keys.toArray(results); + return result == returnNull ? null : results; + } + + @Override + protected void putSpi(String name, String value) { + try { + checkException(); + } catch (BackingStoreException e) { + } + if (name == null || value == null) { + return; + } + attr.put(name, value); + } + + @Override + protected void removeNodeSpi() throws BackingStoreException { + checkException(); + Preferences p = parent(); + if (p instanceof MockAbstractPreferences) { + ((MockAbstractPreferences) p).childs.remove(name()); + } else { + String[] children = p.childrenNames(); + for (String child : children) { + p.node(child).removeNode(); + } + } + } + + @Override + protected void removeSpi(String key) { + try { + checkException(); + } catch (BackingStoreException e) { + } + if (null == key) { + return; + } + attr.remove(key); + } + + @Override + protected void syncSpi() throws BackingStoreException { + checkException(); + syncTimes++; + } + + public boolean getNewNode() { + return newNode; + } + + public Object getLock() { + return lock; + } + + public void protectedAbstractMethod() { + try { + childrenNamesSpi(); + } catch (BackingStoreException e) { + } + childSpi("mock"); + try { + flushSpi(); + } catch (BackingStoreException e1) { + } + getSpi(null); + isRemoved(); + try { + keysSpi(); + } catch (BackingStoreException e2) { + } + putSpi(null, null); + try { + removeNodeSpi(); + } catch (BackingStoreException e3) { + } + removeSpi(null); + try { + syncSpi(); + } catch (BackingStoreException e4) { + } + } + + public boolean isRemovedImpl() { + return super.isRemoved(); + } + + public AbstractPreferences getChildImpl(String name) + throws BackingStoreException { + return super.getChild(name); + } + + public AbstractPreferences[] cachedChildrenImpl() { + return super.cachedChildren(); + } + +} + +class MockRuntimeException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + public MockRuntimeException(String s) { + super(s); + } + + public MockRuntimeException() { + super(); + } +} + diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/MockPreferencesFactory.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/MockPreferencesFactory.java new file mode 100644 index 0000000..8bbbed2 --- /dev/null +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/MockPreferencesFactory.java @@ -0,0 +1,42 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.java.util.prefs; + +import java.util.prefs.Preferences; +import java.util.prefs.PreferencesFactory; + +/** + * + */ +public class MockPreferencesFactory implements PreferencesFactory { + static MockAbstractPreferences userRoot = new MockAbstractPreferences(null, + ""); + + static MockAbstractPreferences systemRoot = new MockAbstractPreferences( + null, ""); + + public MockPreferencesFactory() { + } + + public Preferences userRoot() { + return userRoot; + } + + public Preferences systemRoot() { + return systemRoot; + } +} diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/NodeChangeEventTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/NodeChangeEventTest.java new file mode 100644 index 0000000..07fd887 --- /dev/null +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/NodeChangeEventTest.java @@ -0,0 +1,63 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.java.util.prefs; + +import java.io.NotSerializableException; +import java.util.prefs.NodeChangeEvent; +import java.util.prefs.Preferences; +import junit.framework.TestCase; +import org.apache.harmony.testframework.serialization.SerializationTest; + +/** + * + */ +public class NodeChangeEventTest extends TestCase { + + NodeChangeEvent event; + + public void testConstructor() { + event = new NodeChangeEvent(Preferences.systemRoot(), Preferences + .userRoot()); + assertSame(Preferences.systemRoot(), event.getParent()); + assertSame(Preferences.userRoot(), event.getChild()); + assertSame(Preferences.systemRoot(), event.getSource()); + } + + public void testConstructorNullParam() { + try { + event = new NodeChangeEvent(null, Preferences.userRoot()); + fail(); + } catch (IllegalArgumentException e) { + } + + event = new NodeChangeEvent(Preferences.systemRoot(), null); + assertSame(Preferences.systemRoot(), event.getParent()); + assertNull(event.getChild()); + assertSame(Preferences.systemRoot(), event.getSource()); + } + + public void testSerialization() throws Exception { + + event = new NodeChangeEvent(Preferences.systemRoot(), null); + + try { + SerializationTest.copySerializable(event); + fail("No expected NotSerializableException"); + } catch (NotSerializableException e) { + } + } +} diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/NodeChangeListenerTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/NodeChangeListenerTest.java new file mode 100644 index 0000000..cda52ec --- /dev/null +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/NodeChangeListenerTest.java @@ -0,0 +1,68 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.java.util.prefs; + +import java.util.prefs.NodeChangeEvent; +import java.util.prefs.NodeChangeListener; +import java.util.prefs.Preferences; +import junit.framework.TestCase; + +/** + * + */ +public class NodeChangeListenerTest extends TestCase { + + NodeChangeListener l; + + /* + * @see TestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + super.setUp(); + l = new NodeChangeListenerImpl(); + } + + /* + * @see TestCase#tearDown() + */ + @Override + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testChildAdded() { + l.childAdded(new NodeChangeEvent(Preferences.userRoot(), Preferences + .userRoot())); + } + + public void testChildRemoved() { + l.childRemoved(new NodeChangeEvent(Preferences.userRoot(), Preferences + .userRoot())); + } + + public static class NodeChangeListenerImpl implements NodeChangeListener { + + public void childAdded(NodeChangeEvent e) { + } + + public void childRemoved(NodeChangeEvent e) { + } + + } + +} diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/PreferenceChangeEventTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/PreferenceChangeEventTest.java new file mode 100644 index 0000000..7d403c7 --- /dev/null +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/PreferenceChangeEventTest.java @@ -0,0 +1,84 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.java.util.prefs; + +import java.io.NotSerializableException; +import java.util.prefs.PreferenceChangeEvent; +import java.util.prefs.Preferences; +import junit.framework.TestCase; +import org.apache.harmony.testframework.serialization.SerializationTest; + +/** + * + */ +public class PreferenceChangeEventTest extends TestCase { + + PreferenceChangeEvent event; + + public void testPreferenceChangeEventException() { + try { + event = new PreferenceChangeEvent(null, "key", "value"); + fail(); + } catch (IllegalArgumentException e) { + } + } + + public void testConstructorNullValue() { + event = new PreferenceChangeEvent(Preferences.userRoot(), "key", null); + assertEquals("key", event.getKey()); + assertNull(event.getNewValue()); + assertSame(Preferences.userRoot(), event.getNode()); + assertSame(Preferences.userRoot(), event.getSource()); + + event = new PreferenceChangeEvent(Preferences.userRoot(), "", null); + assertEquals("", event.getKey()); + assertNull(event.getNewValue()); + assertSame(Preferences.userRoot(), event.getNode()); + assertSame(Preferences.userRoot(), event.getSource()); + + event = new PreferenceChangeEvent(Preferences.userRoot(), null, "value"); + assertNull(event.getKey()); + assertEquals("value", event.getNewValue()); + assertSame(Preferences.userRoot(), event.getNode()); + assertSame(Preferences.userRoot(), event.getSource()); + + event = new PreferenceChangeEvent(Preferences.userRoot(), null, ""); + assertNull(event.getKey()); + assertEquals("", event.getNewValue()); + assertSame(Preferences.userRoot(), event.getNode()); + assertSame(Preferences.userRoot(), event.getSource()); + } + + public void testConstructor() { + event = new PreferenceChangeEvent(Preferences.userRoot(), "key", + "value"); + assertEquals("key", event.getKey()); + assertEquals("value", event.getNewValue()); + assertSame(Preferences.userRoot(), event.getNode()); + assertSame(Preferences.userRoot(), event.getSource()); + } + + public void testSerialization() throws Exception { + event = new PreferenceChangeEvent(Preferences.userRoot(), "key", + "value"); + try { + SerializationTest.copySerializable(event); + fail("No expected NotSerializableException"); + } catch (NotSerializableException e) { + } + } +} diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/PreferenceChangeListenerTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/PreferenceChangeListenerTest.java new file mode 100644 index 0000000..63170e7 --- /dev/null +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/PreferenceChangeListenerTest.java @@ -0,0 +1,52 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.java.util.prefs; + +import java.util.prefs.PreferenceChangeEvent; +import java.util.prefs.PreferenceChangeListener; +import java.util.prefs.Preferences; +import junit.framework.TestCase; + +/** + * + */ +public class PreferenceChangeListenerTest extends TestCase { + + PreferenceChangeListener l; + + /* + * @see TestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + super.setUp(); + l = new PreferenceChangeListenerImpl(); + } + + public void testPreferenceChange() { + l.preferenceChange(new PreferenceChangeEvent(Preferences.userRoot(), + "", "")); + } + + public static class PreferenceChangeListenerImpl implements + PreferenceChangeListener { + public void preferenceChange(PreferenceChangeEvent pce) { + } + + } + +} diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/PreferencesFactoryTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/PreferencesFactoryTest.java new file mode 100644 index 0000000..e70fc5a --- /dev/null +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/PreferencesFactoryTest.java @@ -0,0 +1,59 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.java.util.prefs; + +import java.util.prefs.Preferences; +import java.util.prefs.PreferencesFactory; +import junit.framework.TestCase; + +/** + * + */ +public class PreferencesFactoryTest extends TestCase { + + PreferencesFactory f; + + /* + * @see TestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + super.setUp(); + f = new PreferencesFactoryImpl(); + } + + public void testUserRoot() { + assertNull(f.userRoot()); + } + + public void testSystemRoot() { + assertNull(f.systemRoot()); + } + + public static class PreferencesFactoryImpl implements PreferencesFactory { + + public Preferences userRoot() { + return null; + } + + public Preferences systemRoot() { + return null; + } + + } + +} diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/PreferencesTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/PreferencesTest.java new file mode 100644 index 0000000..3d194da --- /dev/null +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/prefs/PreferencesTest.java @@ -0,0 +1,420 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.tests.java.util.prefs; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.MalformedURLException; +import java.nio.charset.StandardCharsets; +import java.util.prefs.AbstractPreferences; +import java.util.prefs.BackingStoreException; +import java.util.prefs.InvalidPreferencesFormatException; +import java.util.prefs.NodeChangeListener; +import java.util.prefs.PreferenceChangeListener; +import java.util.prefs.Preferences; +import java.util.prefs.PreferencesFactory; +import junit.framework.TestCase; +import libcore.io.IoUtils; + +/** + * + */ +public class PreferencesTest extends TestCase { + + private static final String PREFS = + "<!DOCTYPE preferences SYSTEM \"http://java.sun.com/dtd/preferences.dtd\">" + + "<preferences>" + + "<root type=\"user\">" + + "<map></map>" + + "</root>" + + "</preferences>"; + + + private MockInputStream stream ; + private InputStream in; + private PreferencesFactory defaultFactory; + + @Override + protected void setUp() throws Exception { + File tmpDir = IoUtils.createTemporaryDirectory("OldAbstractPreferencesTest"); + defaultFactory = Preferences.setPreferencesFactory( + new AbstractPreferencesTest.TestPreferencesFactory(tmpDir.getAbsolutePath())); + + in = new ByteArrayInputStream(PREFS.getBytes(StandardCharsets.US_ASCII)); + stream = new MockInputStream(in); + } + + @Override + protected void tearDown() throws Exception { + Preferences.setPreferencesFactory(defaultFactory); + stream.close(); + } + + public void testSystemNodeForPackage() throws Exception { + Preferences p = Preferences.systemNodeForPackage(Object.class); + + assertEquals("/java/lang", p.absolutePath()); + assertTrue(p instanceof AbstractPreferences); + Preferences root = Preferences.systemRoot(); + Preferences parent = root.node("java"); + assertSame(parent, p.parent()); + assertFalse(p.isUserNode()); + assertEquals("lang", p.name()); + assertEquals("System Preference Node: " + p.absolutePath(), p + .toString()); + + assertEquals(0, p.childrenNames().length); + assertEquals(0, p.keys().length); + parent.removeNode(); + try { + Preferences.userNodeForPackage(null); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // Expected + } + } + + public void testSystemRoot() throws BackingStoreException { + Preferences p = Preferences.systemRoot(); + assertTrue(p instanceof AbstractPreferences); + assertEquals("/", p.absolutePath()); + assertSame(null, p.parent()); + assertFalse(p.isUserNode()); + assertEquals("", p.name()); + assertEquals("System Preference Node: " + p.absolutePath(), p + .toString()); + } + + public void testConsts() { + assertEquals(80, Preferences.MAX_KEY_LENGTH); + assertEquals(80, Preferences.MAX_NAME_LENGTH); + assertEquals(8192, Preferences.MAX_VALUE_LENGTH); + } + + public void testUserNodeForPackage() throws BackingStoreException { + Preferences p = Preferences.userNodeForPackage(Object.class); + assertEquals("/java/lang", p.absolutePath()); + assertTrue(p instanceof AbstractPreferences); + Preferences root = Preferences.userRoot(); + Preferences parent = root.node("java"); + assertSame(parent, p.parent()); + assertTrue(p.isUserNode()); + assertEquals("lang", p.name()); + assertEquals("User Preference Node: " + p.absolutePath(), p.toString()); + assertEquals(0, p.childrenNames().length); + assertEquals(0, p.keys().length); + + try { + Preferences.userNodeForPackage(null); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // Expected + } + } + + public void testUserRoot() throws BackingStoreException { + Preferences p = Preferences.userRoot(); + assertTrue(p instanceof AbstractPreferences); + assertEquals("/", p.absolutePath()); + assertSame(null, p.parent()); + assertTrue(p.isUserNode()); + assertEquals("", p.name()); + assertEquals("User Preference Node: " + p.absolutePath(), p.toString()); + } + + public void testImportPreferences() throws Exception { + Preferences prefs = null; + try { + prefs = Preferences.userNodeForPackage(PreferencesTest.class); + + prefs.put("prefskey", "oldvalue"); + prefs.put("prefskey2", "oldvalue2"); + in = PreferencesTest.class + .getResourceAsStream("/resources/prefs/java/util/prefs/userprefs.xml"); + Preferences.importPreferences(in); + + prefs = Preferences.userNodeForPackage(PreferencesTest.class); + assertEquals(1, prefs.childrenNames().length); + assertTrue(prefs.nodeExists("mock/child/grandson")); + assertEquals("newvalue", prefs.get("prefskey", null)); + assertEquals("oldvalue2", prefs.get("prefskey2", null)); + assertEquals("newvalue3", prefs.get("prefskey3", null)); + + in = PreferencesTest.class.getResourceAsStream( + "/prefs/java/util/prefs/userprefs-badform.xml"); + try { + Preferences.importPreferences(in); + fail("should throw InvalidPreferencesFormatException"); + } catch (InvalidPreferencesFormatException expected) { + } + } finally { + try { + prefs = Preferences.userNodeForPackage(PreferencesTest.class); + prefs.removeNode(); + } catch (Exception e) { + } + } + } + + public void testImportPreferencesException() throws Exception { + try { + Preferences.importPreferences(null); + fail("should throw MalformedURLException"); + } catch (MalformedURLException expected) { + } + + byte[] source = new byte[0]; + InputStream in = new ByteArrayInputStream(source); + try { + Preferences.importPreferences(in); + fail("should throw InvalidPreferencesFormatException"); + } catch (InvalidPreferencesFormatException expected) { + } + + stream.setResult(MockInputStream.exception); + try { + Preferences.importPreferences(stream); + fail("should throw IOException"); + } catch (IOException expected) { + } + + stream.setResult(MockInputStream.runtimeException); + try { + Preferences.importPreferences(stream); + fail("should throw RuntimeException"); + } catch (RuntimeException expected) { + } + } + + static class MockInputStream extends InputStream { + + static final int normal = 0; + + static final int exception = 1; + + static final int runtimeException = 2; + + int result = normal; + + InputStream wrapper; + + public void setResult(int i) { + result = i; + } + + private void checkException() throws IOException { + switch (result) { + case normal: + return; + case exception: + throw new IOException("test"); + case runtimeException: + throw new RuntimeException("test"); + } + } + + public MockInputStream(InputStream in) { + wrapper = in; + } + + @Override + public int read() throws IOException { + checkException(); + return wrapper.read(); + } + } + + static class MockPreferences extends Preferences { + + public MockPreferences() { + super(); + } + + @Override + public String absolutePath() { + return null; + } + + @Override + public String[] childrenNames() throws BackingStoreException { + return null; + } + + @Override + public void clear() throws BackingStoreException { + } + + @Override + public void exportNode(OutputStream ostream) throws IOException, + BackingStoreException { + } + + @Override + public void exportSubtree(OutputStream ostream) throws IOException, + BackingStoreException { + } + + @Override + public void flush() throws BackingStoreException { + } + + @Override + public String get(String key, String deflt) { + return null; + } + + @Override + public boolean getBoolean(String key, boolean deflt) { + return false; + } + + @Override + public byte[] getByteArray(String key, byte[] deflt) { + return null; + } + + @Override + public double getDouble(String key, double deflt) { + return 0; + } + + @Override + public float getFloat(String key, float deflt) { + return 0; + } + + @Override + public int getInt(String key, int deflt) { + return 0; + } + + @Override + public long getLong(String key, long deflt) { + return 0; + } + + @Override + public boolean isUserNode() { + return false; + } + + @Override + public String[] keys() throws BackingStoreException { + return null; + } + + @Override + public String name() { + return null; + } + + @Override + public Preferences node(String name) { + return null; + } + + @Override + public boolean nodeExists(String name) throws BackingStoreException { + return false; + } + + @Override + public Preferences parent() { + return null; + } + + @Override + public void put(String key, String value) { + + } + + @Override + public void putBoolean(String key, boolean value) { + + } + + @Override + public void putByteArray(String key, byte[] value) { + + } + + @Override + public void putDouble(String key, double value) { + + } + + @Override + public void putFloat(String key, float value) { + + } + + @Override + public void putInt(String key, int value) { + + } + + @Override + public void putLong(String key, long value) { + + } + + @Override + public void remove(String key) { + + } + + @Override + public void removeNode() throws BackingStoreException { + + } + + @Override + public void addNodeChangeListener(NodeChangeListener ncl) { + + } + + @Override + public void addPreferenceChangeListener(PreferenceChangeListener pcl) { + + } + + @Override + public void removeNodeChangeListener(NodeChangeListener ncl) { + + } + + @Override + public void removePreferenceChangeListener(PreferenceChangeListener pcl) { + + } + + @Override + public void sync() throws BackingStoreException { + + } + + @Override + public String toString() { + return null; + } + + } + +} diff --git a/harmony-tests/src/test/resources/resources/prefs/java/util/prefs/userprefs-badencoding.xml b/harmony-tests/src/test/resources/resources/prefs/java/util/prefs/userprefs-badencoding.xml new file mode 100644 index 0000000..182edc7 --- /dev/null +++ b/harmony-tests/src/test/resources/resources/prefs/java/util/prefs/userprefs-badencoding.xml @@ -0,0 +1,65 @@ +<?xml version="1.0" encoding="badencoding"?> + +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> + +<!DOCTYPE preferences SYSTEM 'http://java.sun.com/dtd/preferences.dtd'> + +<preferences EXTERNAL_XML_VERSION="1.0"> + <root type="user"> + <map/> + <node name="tests"> + <map/> + <node name="api"> + <map/> + <node name="java"> + <map/> + <node name="util"> + <map/> + <node name="prefs"> + <map> + <entry key="prefskey" value="newvalue"/> + <entry key="prefskey3" value="newvalue3"/> + </map> + <node name="mock"> + <map/> + <node name="child"> + <map> + <entry key="key2" value="value2"/> + </map> + <node name="grandson"> + <map> + <entry key="key3" value="value3"/> + </map> + <node name="grandgrandson"> + <map> + <entry key="key4" + value="value4"/> + </map> + </node> + </node> + </node> + </node> + </node> + </node> + </node> + </node> + </node> + </root> +</preferences> diff --git a/harmony-tests/src/test/resources/resources/prefs/java/util/prefs/userprefs-badform.xml b/harmony-tests/src/test/resources/resources/prefs/java/util/prefs/userprefs-badform.xml new file mode 100644 index 0000000..da3aeb9 --- /dev/null +++ b/harmony-tests/src/test/resources/resources/prefs/java/util/prefs/userprefs-badform.xml @@ -0,0 +1,51 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> + +<!DOCTYPE preferences SYSTEM 'http://java.sun.com/dtd/preferences.dtd'> + +<preferences EXTERNAL_XML_VERSION="1.0"> + <root type="user"> + <map/> + <node name="java"> + <map/> + <node name="util"> + <map/> + <node name="mock"> + <map/> + <node name="child"> + <map> + <entry key="key2" value="value2"/> + </map> + <node name="grandson"> + <map> + <entry key="key3" value="value3"/> + </map> + <node name="grandgrandson"> + <map> + <entry key="key4" value="value4"/> + </map> + </node> + </node> + </node> + </node> + </node> + </node> + </root> diff --git a/harmony-tests/src/test/resources/resources/prefs/java/util/prefs/userprefs.xml b/harmony-tests/src/test/resources/resources/prefs/java/util/prefs/userprefs.xml new file mode 100644 index 0000000..7bee4c7 --- /dev/null +++ b/harmony-tests/src/test/resources/resources/prefs/java/util/prefs/userprefs.xml @@ -0,0 +1,89 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +--> + +<!DOCTYPE preferences SYSTEM 'http://java.sun.com/dtd/preferences.dtd'> + +<preferences EXTERNAL_XML_VERSION="1.0"> + <root type="user"> + <map/> + <node name="org"> + <map/> + <node name="apache"> + <map/> + <node name="harmony"> + <map/> + <node name="tests"> + <map/> + <node name="java"> + <map/> + <node name="util"> + <map/> + + <node name="prefs"> + <map> + <entry key="prefskey" + value="newvalue"/> + <entry key="prefskey3" + value="newvalue3"/> + </map> + + <node name="mock"> + <map/> + <node name="child"> + <map> + <entry key="key2" + value="value2"/> + </map> + <node name="grandson"> + <map> + <entry key="key3" + value="value3"/> + </map> + <node name="grandgrandson"> + <map> + <entry key="key4" + value="value4"/> + </map> + </node> + <!--grandgrandson--> + </node> + <!--grandson--> + </node> + <!--child--> + </node> + <!--mock--> + + </node> + <!--prefs--> + </node> + <!--util--> + </node> + <!--java--> + </node> + <!--tests--> + </node> + <!--harmony--> + </node> + <!--apache--> + </node> + <!--org--> + </root> +</preferences> diff --git a/harmony-tests/src/test/resources/serialization/org/apache/harmony/tests/java/util/prefs/BackingStoreExceptionTest.golden.ser b/harmony-tests/src/test/resources/serialization/org/apache/harmony/tests/java/util/prefs/BackingStoreExceptionTest.golden.ser Binary files differnew file mode 100644 index 0000000..4d26113 --- /dev/null +++ b/harmony-tests/src/test/resources/serialization/org/apache/harmony/tests/java/util/prefs/BackingStoreExceptionTest.golden.ser diff --git a/harmony-tests/src/test/resources/serialization/org/apache/harmony/tests/java/util/prefs/InvalidPreferencesFormatExceptionTest.golden.ser b/harmony-tests/src/test/resources/serialization/org/apache/harmony/tests/java/util/prefs/InvalidPreferencesFormatExceptionTest.golden.ser Binary files differnew file mode 100644 index 0000000..a15948d --- /dev/null +++ b/harmony-tests/src/test/resources/serialization/org/apache/harmony/tests/java/util/prefs/InvalidPreferencesFormatExceptionTest.golden.ser |