Search in sources :

Example 11 with AbstractPreferences

use of java.util.prefs.AbstractPreferences in project robovm by robovm.

the class OldAbstractPreferencesTest method testClear.

public void testClear() throws BackingStoreException {
    AbstractPreferences ap = (AbstractPreferences) pref.node("New node");
    pref.putInt("IntValue", 33);
    pref.putBoolean("BoolValue", true);
    pref.flush();
    assertTrue(pref.getBoolean("BoolValue", false));
    assertEquals(33, pref.getInt("IntValue", 22));
    assertEquals(1, pref.childrenNames().length);
    pref.clear();
    assertFalse(pref.getBoolean("BoolValue", false));
    assertEquals(22, pref.getInt("IntValue", 22));
    assertEquals(1, pref.childrenNames().length);
    pref.removeNode();
    try {
        pref.clear();
        fail("IllegalStateException expected");
    } catch (IllegalStateException e) {
    //expected
    }
    try {
        ap.clear();
        fail("IllegalStateException expected");
    } catch (IllegalStateException e) {
    //expected
    }
}
Also used : AbstractPreferences(java.util.prefs.AbstractPreferences)

Example 12 with AbstractPreferences

use of java.util.prefs.AbstractPreferences in project robovm by robovm.

the class OldAbstractPreferencesTest method testExportNode.

public void testExportNode() throws BackingStoreException, IOException, InvalidPreferencesFormatException {
    AbstractPreferences ap = (AbstractPreferences) pref.node("New node");
    pref.putInt("IntValue", 33);
    pref.putBoolean("BoolValue", true);
    pref.flush();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    pref.exportNode(baos);
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    assertTrue(pref.getBoolean("BoolValue", false));
    assertEquals(33, pref.getInt("IntValue", 22));
    assertEquals(1, pref.childrenNames().length);
    String xmlData = new String(baos.toByteArray());
    assertTrue(xmlData.contains("IntValue"));
    assertTrue(xmlData.contains("BoolValue"));
    assertTrue(xmlData.contains("33"));
    assertTrue(xmlData.contains("true"));
    pref.removeNode();
    try {
        pref.exportNode(new ByteArrayOutputStream());
        fail("IllegalStateException expected");
    } catch (IllegalStateException e) {
    //expected
    }
    try {
        pref.getBoolean("BoolValue", false);
        fail("IllegalStateException expected");
    } catch (IllegalStateException e) {
    //expected
    }
    pref = (AbstractPreferences) parent.node(nodeName);
    pref.importPreferences(bais);
    assertTrue(pref.getBoolean("BoolValue", false));
    assertEquals(33, pref.getInt("IntValue", 22));
    assertEquals(0, pref.childrenNames().length);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AbstractPreferences(java.util.prefs.AbstractPreferences) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 13 with AbstractPreferences

use of java.util.prefs.AbstractPreferences in project robovm by robovm.

the class OldNodeChangeEventTest method testGetChild.

public void testGetChild() throws BackingStoreException {
    AbstractPreferences parent = (AbstractPreferences) Preferences.userNodeForPackage(Preferences.class);
    AbstractPreferences pref = (AbstractPreferences) parent.node("mock");
    MockNodeChangeListener nl = new MockNodeChangeListener() {

        public synchronized void childAdded(NodeChangeEvent e) {
            Preferences child = e.getChild();
            if (child == null) {
                addResult = false;
            } else {
                if (child.name() == "mock1") {
                    addResult = true;
                }
            }
            super.childAdded(e);
        }

        public synchronized void childRemoved(NodeChangeEvent e) {
            Preferences child = e.getChild();
            if (child == null) {
                removeResult = false;
            } else {
                if (child.name() == "mock1") {
                    removeResult = true;
                }
            }
            super.childRemoved(e);
        }
    };
    try {
        pref.addNodeChangeListener(nl);
        Preferences child1 = pref.node("mock1");
        nl.waitForEvent();
        assertEquals(1, nl.getAdded());
        assertTrue(nl.getAddResult());
        nl.reset();
        child1.removeNode();
        nl.waitForEvent();
        assertEquals(1, nl.getRemoved());
        assertTrue(nl.getRemoveResult());
        nl.reset();
    } finally {
        pref.removeNodeChangeListener(nl);
    }
}
Also used : NodeChangeEvent(java.util.prefs.NodeChangeEvent) AbstractPreferences(java.util.prefs.AbstractPreferences) AbstractPreferences(java.util.prefs.AbstractPreferences) Preferences(java.util.prefs.Preferences)

Example 14 with AbstractPreferences

use of java.util.prefs.AbstractPreferences in project robovm by robovm.

the class OldNodeChangeEventTest method testGetParent.

public void testGetParent() throws BackingStoreException {
    AbstractPreferences parent = (AbstractPreferences) Preferences.userNodeForPackage(Preferences.class);
    AbstractPreferences pref = (AbstractPreferences) parent.node("mock");
    MockNodeChangeListener nl = new MockNodeChangeListener() {

        public synchronized void childAdded(NodeChangeEvent e) {
            Preferences parent = e.getParent();
            if (parent == null) {
                addResult = false;
            } else {
                if (parent.name() == "mock") {
                    addResult = true;
                }
            }
            super.childAdded(e);
        }

        public synchronized void childRemoved(NodeChangeEvent e) {
            Preferences parent = e.getParent();
            if (parent == null) {
                removeResult = false;
            } else {
                if (parent.name() == "mock") {
                    removeResult = true;
                }
            }
            super.childRemoved(e);
        }
    };
    try {
        pref.addNodeChangeListener(nl);
        Preferences child1 = pref.node("mock1");
        nl.waitForEvent();
        assertEquals(1, nl.getAdded());
        assertTrue(nl.getAddResult());
        nl.reset();
        child1.removeNode();
        nl.waitForEvent();
        assertEquals(1, nl.getRemoved());
        assertTrue(nl.getRemoveResult());
        nl.reset();
    } finally {
        pref.removeNodeChangeListener(nl);
    }
}
Also used : NodeChangeEvent(java.util.prefs.NodeChangeEvent) AbstractPreferences(java.util.prefs.AbstractPreferences) AbstractPreferences(java.util.prefs.AbstractPreferences) Preferences(java.util.prefs.Preferences)

Example 15 with AbstractPreferences

use of java.util.prefs.AbstractPreferences in project robovm by robovm.

the class OldPreferenceChangeEventTest method testGetKey.

public void testGetKey() {
    AbstractPreferences parent = (AbstractPreferences) Preferences.userNodeForPackage(Preferences.class);
    AbstractPreferences pref = (AbstractPreferences) parent.node("mock");
    MockPreferenceChangeListener pl = new MockPreferenceChangeListener() {

        public void preferenceChange(PreferenceChangeEvent pce) {
            if (pce != null && pce.getKey().equals("key_int")) {
                result = true;
            }
            super.preferenceChange(pce);
        }
    };
    pref.addPreferenceChangeListener(pl);
    try {
        pref.putInt("key_int", Integer.MAX_VALUE);
        assertEquals(1, pl.getChanged());
        assertTrue(pl.getResult());
        pl.reset();
    } finally {
        pref.removePreferenceChangeListener(pl);
    }
}
Also used : PreferenceChangeEvent(java.util.prefs.PreferenceChangeEvent) AbstractPreferences(java.util.prefs.AbstractPreferences) AbstractPreferences(java.util.prefs.AbstractPreferences) Preferences(java.util.prefs.Preferences)

Aggregations

AbstractPreferences (java.util.prefs.AbstractPreferences)15 Preferences (java.util.prefs.Preferences)6 PreferenceChangeEvent (java.util.prefs.PreferenceChangeEvent)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 NodeChangeEvent (java.util.prefs.NodeChangeEvent)2