harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r386087 [31/45] - in /incubator/harmony/enhanced/classlib/trunk: make/ make/patternsets/ modules/jndi/ modules/jndi/META-INF/ modules/jndi/make/ modules/jndi/make/common/ modules/jndi/src/ modules/jndi/src/main/ modules/jndi/src/main/java/ ...
Date Wed, 15 Mar 2006 14:57:17 GMT
Added: incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/XMLParser.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/XMLParser.java?rev=386087&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/XMLParser.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/main/java/java/util/prefs/XMLParser.java Wed Mar 15 06:55:38 2006
@@ -0,0 +1,542 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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 java.util.prefs;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.StringReader;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+import java.util.Properties;
+import java.util.StringTokenizer;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.FactoryConfigurationError;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerException;
+
+import org.apache.xpath.XPathAPI;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.ErrorHandler;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+import org.xml.sax.SAXParseException;
+
+/**
+ * Utility class for the Preferences import/export from xml file.
+ * 
+ */
+class XMLParser {
+
+    /*
+     * const - the specified dtd URL
+     */
+    static final String PREFS_DTD_NAME = "http://java.sun.com/dtd/preferences.dtd"; //$NON-NLS-1$
+
+    /*
+     * const - the dtd string
+     */
+    static final String PREFS_DTD = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" //$NON-NLS-1$
+            + "    <!ELEMENT preferences (root)>" //$NON-NLS-1$
+            + "    <!ATTLIST preferences EXTERNAL_XML_VERSION CDATA \"0.0\" >" //$NON-NLS-1$
+            + "    <!ELEMENT root (map, node*) >" //$NON-NLS-1$
+            + "    <!ATTLIST root type (system|user) #REQUIRED >" //$NON-NLS-1$
+            + "    <!ELEMENT node (map, node*) >" //$NON-NLS-1$
+            + "    <!ATTLIST node name CDATA #REQUIRED >" //$NON-NLS-1$
+            + "    <!ELEMENT map (entry*) >" //$NON-NLS-1$
+            + "    <!ELEMENT entry EMPTY >" //$NON-NLS-1$
+            + "    <!ATTLIST entry key   CDATA #REQUIRED value CDATA #REQUIRED >"; //$NON-NLS-1$
+
+    /*
+     * const - the specified header
+     */
+    static final String HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; //$NON-NLS-1$
+
+    /*
+     * const - the specified doctype
+     */
+    static final String DOCTYPE = "<!DOCTYPE preferences SYSTEM"; //$NON-NLS-1$
+
+    /*
+     * empty string array constant
+     */
+    private static final String[] EMPTY_SARRAY = new String[0];
+    
+    /*
+     * const - used by FilePreferencesImpl, which is default implementation of Linux platform 
+     */
+    private static final String FILE_PREFS = "<!DOCTYPE map SYSTEM 'http://java.sun.com/dtd/preferences.dtd'>"; //$NON-NLS-1$
+
+    /*
+     * const - specify the dtd version
+     */
+    private static final float XML_VERSION = 1.0f;    
+    
+    /*
+     * dom builder
+     */
+    private static final DocumentBuilder builder;
+
+    /*
+     * specify the indent level 
+     */
+    private static int indent = -1;
+
+    /*
+     * init dom builder
+     */
+    static {
+        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+        factory.setValidating(true);
+        try {
+            builder = factory.newDocumentBuilder();
+        } catch (ParserConfigurationException e) {
+            throw new Error(e);
+        }
+        builder.setEntityResolver(new EntityResolver() {
+            public InputSource resolveEntity(String publicId, String systemId)
+                    throws SAXException, IOException {
+                if (systemId.equals(PREFS_DTD_NAME)) {
+                    InputSource result = new InputSource(new StringReader(
+                            PREFS_DTD));
+                    result.setSystemId(PREFS_DTD_NAME);
+                    return result;
+                }
+                throw new SAXException(
+                        "Invalid DOCTYPE declaration: " + systemId); //$NON-NLS-1$
+            }
+        });
+        builder.setErrorHandler(new ErrorHandler() {
+            public void warning(SAXParseException arg0) throws SAXException {
+                throw arg0;
+            }
+
+            public void error(SAXParseException arg0) throws SAXException {
+                throw arg0;
+            }
+
+            public void fatalError(SAXParseException arg0) throws SAXException {
+                throw arg0;
+            }
+        });
+    }
+
+    private XMLParser() {// empty constructor
+    }
+
+    /***************************************************************************
+     * utilities for Preferences export
+     **************************************************************************/
+    static void exportPrefs(Preferences prefs, OutputStream stream,
+            boolean withSubTree) throws IOException, BackingStoreException {
+        indent = -1;
+        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(stream,
+                "utf-8")); //$NON-NLS-1$
+        out.write(HEADER);
+        out.newLine();
+        out.newLine();
+
+        out.write(DOCTYPE);
+        out.write(" '"); //$NON-NLS-1$
+        out.write(PREFS_DTD_NAME);
+        out.write("'>"); //$NON-NLS-1$
+        out.newLine();
+        out.newLine();
+
+        flushStartTag(
+                "preferences", new String[] { "EXTERNAL_XML_VERSION" }, new String[] { String.valueOf(XML_VERSION) }, out); //$NON-NLS-1$ //$NON-NLS-2$
+        flushStartTag(
+                "root", new String[] { "type" }, new String[] { prefs.isUserNode() ? "user" : "system" }, out); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+        flushEmptyElement("map", out); //$NON-NLS-1$
+
+        StringTokenizer ancestors = new StringTokenizer(prefs.absolutePath(),
+                "/"); //$NON-NLS-1$
+        exportNode(ancestors, prefs, withSubTree, out);
+
+        flushEndTag("root", out); //$NON-NLS-1$
+        flushEndTag("preferences", out); //$NON-NLS-1$
+        out.flush();
+        out = null;
+    }
+
+    private static void exportNode(StringTokenizer ancestors,
+            Preferences prefs, boolean withSubTree, BufferedWriter out)
+            throws IOException, BackingStoreException {
+        if (ancestors.hasMoreTokens()) {
+            String name = ancestors.nextToken();
+            flushStartTag(
+                    "node", new String[] { "name" }, new String[] { name }, out); //$NON-NLS-1$ //$NON-NLS-2$
+            if (ancestors.hasMoreTokens()) {
+                flushEmptyElement("map", out); //$NON-NLS-1$
+                exportNode(ancestors, prefs, withSubTree, out);
+            } else {
+                exportEntries(prefs, out);
+                if (withSubTree) {
+                    exportSubTree(prefs, out);
+                }
+            }
+            flushEndTag("node", out); //$NON-NLS-1$
+        }
+    }
+
+    private static void exportSubTree(Preferences prefs, BufferedWriter out)
+            throws BackingStoreException, IOException {
+        String[] names = prefs.childrenNames();
+        if (names.length > 0) {
+            for (int i = 0; i < names.length; i++) {
+                Preferences child = prefs.node(names[i]);
+                flushStartTag(
+                        "node", new String[] { "name" }, new String[] { names[i] }, out); //$NON-NLS-1$ //$NON-NLS-2$
+                exportEntries(child, out);
+                exportSubTree(child, out);
+                flushEndTag("node", out); //$NON-NLS-1$
+            }
+        }
+    }
+
+    private static void exportEntries(Preferences prefs, BufferedWriter out)
+            throws BackingStoreException, IOException {
+        String[] keys = prefs.keys();
+        String[] values = new String[keys.length];
+        for (int i = 0; i < keys.length; i++) {
+            values[i] = prefs.get(keys[i], null);
+        }
+        exportEntries(keys, values, out);
+    }
+
+    private static void exportEntries(String[] keys, String[] values,
+            BufferedWriter out) throws IOException {
+        if (keys.length == 0) {
+            flushEmptyElement("map", out); //$NON-NLS-1$
+            return;
+        }
+        flushStartTag("map", out); //$NON-NLS-1$
+        for (int i = 0; i < keys.length; i++) {
+            if (values[i] != null) {
+                flushEmptyElement(
+                        "entry", new String[] { "key", "value" }, new String[] { keys[i], values[i] }, out); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+            }
+        }
+        flushEndTag("map", out); //$NON-NLS-1$
+    }
+
+    private static void flushEndTag(String tagName, BufferedWriter out)
+            throws IOException {
+        flushIndent(indent--, out);
+        out.write("</"); //$NON-NLS-1$
+        out.write(tagName);
+        out.write(">"); //$NON-NLS-1$
+        out.newLine();
+    }
+
+    private static void flushEmptyElement(String tagName, BufferedWriter out)
+            throws IOException {
+        flushIndent(++indent, out);
+        out.write("<"); //$NON-NLS-1$
+        out.write(tagName);
+        out.write(" />"); //$NON-NLS-1$
+        out.newLine();
+        indent--;
+    }
+
+    private static void flushEmptyElement(String tagName, String[] attrKeys,
+            String[] attrValues, BufferedWriter out) throws IOException {
+        flushIndent(++indent, out);
+        out.write("<"); //$NON-NLS-1$
+        out.write(tagName);
+        flushPairs(attrKeys, attrValues, out);
+        out.write(" />"); //$NON-NLS-1$
+        out.newLine();
+        indent--;
+    }
+
+    private static void flushPairs(String[] attrKeys, String[] attrValues,
+            BufferedWriter out) throws IOException {
+        for (int i = 0; i < attrKeys.length; i++) {
+            out.write(" "); //$NON-NLS-1$
+            out.write(attrKeys[i]);
+            out.write("=\""); //$NON-NLS-1$
+            out.write(htmlEncode(attrValues[i]));
+            out.write("\""); //$NON-NLS-1$
+        }
+    }
+
+    private static void flushIndent(int ind, BufferedWriter out)
+            throws IOException {
+        for (int i = 0; i < ind; i++) {
+            out.write("  "); //$NON-NLS-1$
+        }
+    }
+
+    private static void flushStartTag(String tagName, String[] attrKeys,
+            String[] attrValues, BufferedWriter out) throws IOException {
+        flushIndent(++indent, out);
+        out.write("<"); //$NON-NLS-1$
+        out.write(tagName);
+        flushPairs(attrKeys, attrValues, out);
+        out.write(">"); //$NON-NLS-1$
+        out.newLine();
+    }
+
+    private static void flushStartTag(String tagName, BufferedWriter out)
+            throws IOException {
+        flushIndent(++indent, out);
+        out.write("<"); //$NON-NLS-1$
+        out.write(tagName);
+        out.write(">"); //$NON-NLS-1$
+        out.newLine();
+    }
+
+    private static String htmlEncode(String s) {
+        StringBuffer sb = new StringBuffer();
+        char c;
+        for (int i = 0; i < s.length(); i++) {
+            c = s.charAt(i);
+            switch (c) {
+            case '<':
+                sb.append("&lt;"); //$NON-NLS-1$
+                break;
+            case '>':
+                sb.append("&gt;"); //$NON-NLS-1$
+                break;
+            case '&':
+                sb.append("&amp;"); //$NON-NLS-1$
+                break;
+            case '\\':
+                sb.append("&apos;"); //$NON-NLS-1$
+                break;
+            case '"':
+                sb.append("&quot;"); //$NON-NLS-1$
+                break;
+            default:
+                sb.append(c);
+            }
+        }
+        return sb.toString();
+    }
+
+    /***************************************************************************
+     * utilities for Preferences import
+     **************************************************************************/
+    static void importPrefs(InputStream in) throws IOException,
+            InvalidPreferencesFormatException {
+        try {
+            // load xml document
+            Document doc = builder.parse(new InputSource(in));
+
+            // check preferences' export version
+            Element preferences;
+            preferences = doc.getDocumentElement();
+            String version = preferences.getAttribute("EXTERNAL_XML_VERSION"); //$NON-NLS-1$
+            if (version != null && Float.parseFloat(version) > XML_VERSION) {
+                throw new InvalidPreferencesFormatException(
+                        "This preferences exported version is not supported:" + version); //$NON-NLS-1$
+            }
+
+            // check preferences root's type
+            Element root = (Element) preferences
+                    .getElementsByTagName("root").item(0); //$NON-NLS-1$
+            Preferences prefsRoot = null;
+            String type = root.getAttribute("type"); //$NON-NLS-1$
+            if (type.equals("user")) { //$NON-NLS-1$
+                prefsRoot = Preferences.userRoot();
+            } else {
+                prefsRoot = Preferences.systemRoot();
+            }
+
+            // load node
+            loadNode(prefsRoot, root);
+        } catch (FactoryConfigurationError e) {
+            throw new InvalidPreferencesFormatException(e);
+        } catch (SAXException e) {
+            throw new InvalidPreferencesFormatException(e);
+        } catch (TransformerException e) {
+            throw new InvalidPreferencesFormatException(e);
+        }
+    }
+
+    private static void loadNode(Preferences prefs, Element node)
+            throws TransformerException {
+        // load preferences
+        NodeList children = XPathAPI.selectNodeList(node, "node"); //$NON-NLS-1$
+        NodeList entries = XPathAPI.selectNodeList(node, "map/entry"); //$NON-NLS-1$
+        int childNumber = children.getLength();
+        Preferences[] prefChildren = new Preferences[childNumber];
+        int entryNumber = entries.getLength();
+        synchronized (((AbstractPreferences) prefs).lock) {
+            if (((AbstractPreferences) prefs).isRemoved()) {
+                return;
+            }
+            for (int i = 0; i < entryNumber; i++) {
+                Element entry = (Element) entries.item(i);
+                String key = entry.getAttribute("key"); //$NON-NLS-1$
+                String value = entry.getAttribute("value"); //$NON-NLS-1$
+                prefs.put(key, value);
+            }
+            // get children preferences node
+            for (int i = 0; i < childNumber; i++) {
+                Element child = (Element) children.item(i);
+                String name = child.getAttribute("name"); //$NON-NLS-1$
+                prefChildren[i] = prefs.node(name);
+            }
+        }
+
+        // load children nodes after unlock
+        for (int i = 0; i < childNumber; i++) {
+            loadNode(prefChildren[i], (Element) children.item(i));
+        }
+    }
+
+    /***************************************************************************
+     * utilities for FilePreferencesImpl, which is default implementation of Linux platform
+     **************************************************************************/
+    /**
+     * load preferences from file, if cannot load, create a new one FIXME: need
+     * lock or not?
+     * 
+     * @param file	the xml file to be read
+     * @return Properties instance which indicates the preferences key-value pairs
+     */
+    static Properties loadFilePrefs(final File file) {
+        return (Properties) AccessController
+                .doPrivileged(new PrivilegedAction() {
+                    public Object run() {
+                        return loadFilePrefsImpl(file);
+                    }
+                });
+
+        // try {
+        // //FIXME: lines below can be deleted, because it is not required to
+        // persistent at the very begining
+        // flushFilePrefs(file, result);
+        // } catch (IOException e) {
+        // e.printStackTrace();
+        // }
+    }
+
+    static Object loadFilePrefsImpl(final File file) {
+        Properties result = new Properties();
+        if (!file.exists()) {
+            file.getParentFile().mkdirs();
+        } else if (file.canRead()) {
+            InputStream in = null;
+            FileLock lock = null;
+            try {
+
+                FileInputStream istream = new FileInputStream(file);
+                in = new BufferedInputStream(istream);
+                FileChannel channel = istream.getChannel();
+                lock = channel.lock(0L, Long.MAX_VALUE, true);
+                Document doc = builder.parse(in);
+                NodeList entries = XPathAPI.selectNodeList(doc
+                        .getDocumentElement(), "entry"); //$NON-NLS-1$
+                int length = entries.getLength();
+                for (int i = 0; i < length; i++) {
+                    Element node = (Element) entries.item(i);
+                    String key = node.getAttribute("key"); //$NON-NLS-1$
+                    String value = node.getAttribute("value"); //$NON-NLS-1$
+                    result.setProperty(key, value);
+                }
+                return result;
+            } catch (Exception e) {
+                e.printStackTrace();
+            } finally {
+                try {
+                    lock.release();
+                } catch (Exception e) {//ignore
+                }
+                try {
+                    in.close();
+                } catch (Exception e) {//ignore
+                }
+            }
+        } else {
+            file.delete();
+        }
+        return result;
+    }
+
+    /**
+     * 
+     * @param file
+     * @param prefs
+     * @throws PrivilegedActionException
+     */
+    static void flushFilePrefs(final File file, final Properties prefs) throws PrivilegedActionException {
+        AccessController
+                .doPrivileged(new PrivilegedExceptionAction() {
+                    public Object run() throws IOException {
+                        flushFilePrefsImpl(file, prefs);
+                        return null;
+                    }
+                });
+    }
+    
+    static void flushFilePrefsImpl(File file, Properties prefs) throws IOException {
+        BufferedWriter out = null;
+        FileLock lock = null;
+        try {
+            FileOutputStream ostream = new FileOutputStream(file);
+            out = new BufferedWriter(new OutputStreamWriter(ostream, "utf-8")); //$NON-NLS-1$
+            FileChannel channel = ostream.getChannel();
+            lock = channel.lock();
+            out.write(HEADER);
+            out.newLine();
+            out.write(FILE_PREFS);
+            out.newLine();
+            if (prefs.size() == 0) {
+                exportEntries(EMPTY_SARRAY, EMPTY_SARRAY, out);
+            } else {
+                String[] keys = (String[]) prefs.keySet().toArray(EMPTY_SARRAY);
+                int length = keys.length;
+                String[] values = new String[length];
+                for (int i = 0; i < length; i++) {
+                    values[i] = prefs.getProperty(keys[i]);
+                }
+                exportEntries(keys, values, out);
+            }
+            out.flush();
+        } finally {
+            try {
+                lock.release();
+            } catch (Exception e) {//ignore
+            }
+            try {
+                if (null != out)
+                    out.close();
+            } catch (Exception e) {//ignore
+            }
+        }
+    }
+}
+
+

Added: incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/AbstractPreferencesTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/AbstractPreferencesTest.java?rev=386087&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/AbstractPreferencesTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/AbstractPreferencesTest.java Wed Mar 15 06:55:38 2006
@@ -0,0 +1,1848 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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 tests.api.java.util.prefs;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+import java.util.prefs.AbstractPreferences;
+import java.util.prefs.BackingStoreException;
+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 javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import junit.framework.TestCase;
+
+import org.apache.xpath.XPathAPI;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * TODO: refine this test to adapt all implementations
+ * 
+ */
+public class AbstractPreferencesTest extends TestCase {
+
+	AbstractPreferences pref;
+
+	static AbstractPreferences root = null;
+
+	static AbstractPreferences parent = null;
+
+	final static String longKey;
+
+	final static String longValue;
+
+	final static String longName;
+
+	MockNodeChangeListener nl;
+
+	MockPreferenceChangeListener pl;
+
+	static {
+		StringBuffer key = new StringBuffer(Preferences.MAX_KEY_LENGTH);
+		for (int i = 0; i < Preferences.MAX_KEY_LENGTH; i++) {
+			key.append('a');
+		}
+		longKey = key.toString();
+		StringBuffer value = new StringBuffer(Preferences.MAX_VALUE_LENGTH);
+		for (int i = 0; i < Preferences.MAX_VALUE_LENGTH; i++) {
+			value.append('a');
+		}
+		longValue = value.toString();
+
+		StringBuffer name = new StringBuffer(Preferences.MAX_NAME_LENGTH);
+		for (int i = 0; i < Preferences.MAX_NAME_LENGTH; i++) {
+			name.append('a');
+		}
+		longName = name.toString();
+	}
+
+	protected void setUp() throws Exception {
+		super.setUp();
+		root = (AbstractPreferences) Preferences.userRoot();
+		parent = (AbstractPreferences) Preferences
+				.userNodeForPackage(Preferences.class);
+		// FIXME: change here is dangerous
+		// pref = new MockAbstractPreferences((AbstractPreferences) parent,
+		// "mock");
+
+		pref = (AbstractPreferences) parent.node("mock");
+	}
+
+	protected void tearDown() throws Exception {
+		try {
+			if (pref instanceof MockAbstractPreferences) {
+				((MockAbstractPreferences) pref)
+						.setResult(MockAbstractPreferences.NORMAL);
+			}
+			pref.removeNode();
+		} catch (Exception e) {
+		}
+		super.tearDown();
+	}
+
+	public void testConstructor() throws BackingStoreException {
+		try {
+			pref = new MockAbstractPreferences(
+					(AbstractPreferences) Preferences.userRoot(), "mo/ck");
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			pref = new MockAbstractPreferences(null, "mock");
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			Preferences p3 = new MockAbstractPreferences(null, " ");
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			Preferences p3 = new MockAbstractPreferences(pref, "");
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			Preferences p3 = new MockAbstractPreferences(pref, null);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		if (!(pref instanceof MockAbstractPreferences)) {
+			return;
+		}
+		Preferences p3 = 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(((MockAbstractPreferences) 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");
+		Preferences child2 = pref.node("child2");
+		Preferences child3 = pref.node("child3");
+		Preferences subchild1 = 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();
+		assertEquals(null, pref.get("testClearKey", null));
+		assertEquals(null, 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();
+		} catch (NullPointerException e) {
+		}
+		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();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.get(null, "abc");
+			fail();
+		} catch (NullPointerException e) {
+		}
+	}
+
+	public void testGetBoolean() {
+		try {
+			pref.getBoolean(null, false);
+			fail();
+		} catch (NullPointerException e) {
+		}
+
+		pref.put("testGetBooleanKey", "false");
+		pref.put("testGetBooleanKey2", "value");
+		assertEquals(false, pref.getBoolean("testGetBooleanKey", true));
+		assertEquals(true, pref.getBoolean("testGetBooleanKey2", true));
+	}
+
+	public void testPutByteArray() {
+		try {
+			pref.putByteArray(null, new byte[0]);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			pref.putByteArray("testPutByteArrayKey4", null);
+			fail();
+		} catch (NullPointerException e) {
+		}
+
+		pref.putByteArray(longKey, new byte[0]);
+		try {
+			pref.putByteArray(longKey + "a", new byte[0]);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		byte[] longArray = new byte[(int) (Preferences.MAX_VALUE_LENGTH * 0.75)];
+		byte[] longerArray = new byte[(int) (Preferences.MAX_VALUE_LENGTH * 0.75) + 1];
+		pref.putByteArray(longKey, longArray);
+		try {
+			pref.putByteArray(longKey, longerArray);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+
+		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();
+		} catch (NullPointerException e) {
+		}
+		byte[] b64Array = new byte[] { 0x59, 0x57, 0x4a, 0x6a };// BASE64
+																// encoding for
+																// "abc"
+
+		pref.put("testGetByteArrayKey", "abc=");
+		pref.put("testGetByteArrayKey2", new String(b64Array));
+		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();
+		} catch (NullPointerException e) {
+		}
+
+		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();
+		} catch (NullPointerException e) {
+		}
+		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();
+		} catch (NullPointerException e) {
+		}
+
+		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();
+		} catch (NullPointerException e) {
+		}
+
+		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();
+		} catch (NullPointerException e) {
+		}
+		try {
+			pref.node("/java/util/prefs/");
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			pref.node("/java//util/prefs");
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			pref.node(longName + "a");
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		assertNotNull(pref.node(longName));
+
+		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();
+		} catch (NullPointerException e) {
+		}
+		try {
+			pref.nodeExists("/java/util/prefs/");
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			pref.nodeExists("/java//util/prefs");
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+
+		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 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();
+		} catch (NullPointerException e) {
+		}
+		try {
+			pref.put("key", null);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		pref.put(longKey, longValue);
+		try {
+			pref.put(longKey + 1, longValue);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			pref.put(longKey, longValue + 1);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+
+		pref.removeNode();
+		try {
+			pref.put(longKey, longValue + 1);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+
+		try {
+			pref.put(longKey, longValue);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+	}
+
+	public void testPutBoolean() {
+		try {
+			pref.putBoolean(null, false);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		pref.putBoolean(longKey, false);
+		try {
+			pref.putBoolean(longKey + "a", false);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		pref.putBoolean("testPutBooleanKey", false);
+		assertEquals("false", pref.get("testPutBooleanKey", null));
+		assertEquals(false, pref.getBoolean("testPutBooleanKey", true));
+	}
+
+	public void testPutDouble() {
+		try {
+			pref.putDouble(null, 3);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		pref.putDouble(longKey, 3);
+		try {
+			pref.putDouble(longKey + "a", 3);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		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();
+		} catch (NullPointerException e) {
+		}
+		pref.putFloat(longKey, 3f);
+		try {
+			pref.putFloat(longKey + "a", 3f);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		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();
+		} catch (NullPointerException e) {
+		}
+		pref.putInt(longKey, 3);
+		try {
+			pref.putInt(longKey + "a", 3);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		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();
+		} catch (NullPointerException e) {
+		}
+		pref.putLong(longKey, 3L);
+		try {
+			pref.putLong(longKey + "a", 3L);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		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);
+		} catch (NullPointerException e) {
+		}
+
+		pref.removeNode();
+		try {
+			pref.remove("key");
+			fail();
+		} catch (IllegalStateException e) {
+		}
+	}
+
+	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 testAddNodeChangeListener() throws BackingStoreException {
+	// try {
+	// pref.addNodeChangeListener(null);
+	// fail();
+	// } catch (NullPointerException e) {
+	// }
+	//
+	// Preferences child1 = null;
+	// Preferences child2 = null;
+	// Preferences child3 = null;
+	// //To get existed node doesn't create the change event
+	// try {
+	// nl = new MockNodeChangeListener();
+	// pref.addNodeChangeListener(nl);
+	// child1 = pref.node("mock1");
+	// assertEquals(1, nl.getAdded());
+	// nl.reset();
+	// child2 = pref.node("mock1");
+	// assertEquals(0, nl.getAdded());
+	// nl.reset();
+	// } finally {
+	// pref.removeNodeChangeListener(nl);
+	// child1.removeNode();
+	// }
+	// //same listener can be added twice, and must be removed twice
+	// try {
+	// nl = new MockNodeChangeListener();
+	// pref.addNodeChangeListener(nl);
+	// pref.addNodeChangeListener(nl);
+	// child1 = pref.node("mock2");
+	// assertEquals(2, nl.getAdded());
+	// nl.reset();
+	// } finally {
+	// pref.removeNodeChangeListener(nl);
+	// pref.removeNodeChangeListener(nl);
+	// child1.removeNode();
+	// }
+	// //test remove event
+	// try {
+	// nl = new MockNodeChangeListener();
+	// pref.addNodeChangeListener(nl);
+	// child1 = pref.node("mock3");
+	// child1.removeNode();
+	// assertEquals(1, nl.getRemoved());
+	// nl.reset();
+	// } finally {
+	// pref.removeNodeChangeListener(nl);
+	// }
+	// // test remove event with two listeners
+	// try {
+	// nl = new MockNodeChangeListener();
+	// pref.addNodeChangeListener(nl);
+	// pref.addNodeChangeListener(nl);
+	// child1 = pref.node("mock6");
+	// child1.removeNode();
+	// assertEquals(2, nl.getRemoved());
+	// nl.reset();
+	// } finally {
+	// pref.removeNodeChangeListener(nl);
+	// pref.removeNodeChangeListener(nl);
+	// }
+	// //test add/remove indirect children, or remove several children at the
+	// same time
+	// try {
+	// nl = new MockNodeChangeListener();
+	// child1 = pref.node("mock4");
+	// child1.addNodeChangeListener(nl);
+	// child2 = pref.node("mock4/mock5");
+	// assertEquals(1, nl.getAdded());
+	// nl.reset();
+	// child3 = pref.node("mock4/mock5/mock6");
+	// assertEquals(0, nl.getAdded());
+	// nl.reset();
+	//
+	// child3.removeNode();
+	// assertEquals(0, nl.getRemoved());
+	// nl.reset();
+	//
+	// child3 = pref.node("mock4/mock7");
+	// assertEquals(1, nl.getAdded());
+	// nl.reset();
+	//
+	// child1.removeNode();
+	// assertEquals(2, nl.getRemoved());
+	// nl.reset();
+	// } finally {
+	// try {
+	// child1.removeNode();
+	// } catch (Exception e) {
+	// }
+	// }
+	//
+	// }
+
+	public void testAddPreferenceChangeListener() {
+		// TODO: start from here
+
+	}
+
+	public void testRemoveNodeChangeListener() {
+		try {
+			pref.removeNodeChangeListener(null);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		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();
+		} catch (IllegalArgumentException e) {
+		}
+		try {
+			pref.removeNodeChangeListener(l2);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+	}
+
+	public void testRemovePreferenceChangeListener() {
+		try {
+			pref.removePreferenceChangeListener(null);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		MockPreferenceChangeListener l1 = new MockPreferenceChangeListener();
+		MockPreferenceChangeListener l2 = new MockPreferenceChangeListener();
+		pref.addPreferenceChangeListener(l1);
+		pref.addPreferenceChangeListener(l1);
+		try {
+			pref.removePreferenceChangeListener(l2);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+		pref.removePreferenceChangeListener(l1);
+		pref.removePreferenceChangeListener(l1);
+		try {
+			pref.removePreferenceChangeListener(l1);
+			fail();
+		} catch (IllegalArgumentException e) {
+		}
+
+	}
+
+	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(longName + "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();
+		} catch (NullPointerException e) {
+		}
+
+		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);
+
+		Document doc = parseXmlStream(in, true);
+
+		// only output this node without parent and children
+		String rootpath = "/preferences[@EXTERNAL_XML_VERSION='1.0']/root[@type='user']/node[@name='java']/node[@name='util']/node[@name='prefs']";
+		Node node = XPathAPI
+				.selectSingleNode(
+						doc,
+						rootpath
+								+ "/node[@name='mock']/map/entry[@key='key' and @value='false']");
+		assertNull(node);
+		node = XPathAPI
+				.selectSingleNode(
+						doc,
+						rootpath
+								+ "/node[@name='mock']/node[@name='child<']/map/entry[@key='key2' and @value='value2<']");
+		assertNotNull(node);
+		node = XPathAPI.selectSingleNode(doc, rootpath
+				+ "/node[@name='mock']/node[@name='child']/node");
+		assertNull(node);
+	}
+
+	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();
+		builder.setEntityResolver(new EntityResolver() {
+			public InputSource resolveEntity(String publicId, String systemId)
+					throws SAXException, IOException {
+				if (systemId.equals("http://java.sun.com/dtd/preferences.dtd")) {
+					InputSource result = new InputSource(
+							AbstractPreferencesTest.class
+									.getResourceAsStream("preferences.dtd"));
+					result.setSystemId("preferences.dtd");
+					return result;
+				}
+				throw new SAXException("Invalid DOCTYPE:" + systemId);
+			}
+		});
+		Document doc = builder.parse(input);
+		return doc;
+	}
+
+	public void testExportSubtree() throws Exception {
+		try {
+			pref.exportSubtree(null);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		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");
+		Preferences grandson2 = child.node("grandson2");
+		Preferences grandgrandson = grandson.node("grandgrandson");
+		grandgrandson.put("key4", "value4");
+		child.exportSubtree(out);
+
+		byte[] result = out.toByteArray();
+		// System.out.println(new String(result, "utf-8"));
+		ByteArrayInputStream in = new ByteArrayInputStream(result);
+		Document doc = parseXmlStream(in, true);
+
+		// only output this node and subtree without parent
+		String rootpath = "/preferences[@EXTERNAL_XML_VERSION='1.0']/root[@type='user']/node[@name='java']/node[@name='util']/node[@name='prefs']";
+		Node node = XPathAPI
+				.selectSingleNode(
+						doc,
+						rootpath
+								+ "/node[@name='mock']/map/entry[@key='key' and @value='false']");
+		assertNull(node);
+		node = XPathAPI
+				.selectSingleNode(
+						doc,
+						rootpath
+								+ "/node[@name='mock']/node[@name='child']/map/entry[@key='key2' and @value='value2']");
+		assertNotNull(node);
+		node = XPathAPI
+				.selectSingleNode(
+						doc,
+						rootpath
+								+ "/node[@name='mock']/node[@name='child']/node[@name='grandson']/map/entry[@key='key3' and @value='value3']");
+		assertNotNull(node);
+		node = XPathAPI
+				.selectSingleNode(
+						doc,
+						rootpath
+								+ "/node[@name='mock']/node[@name='child']/node[@name='grandson']/node[@name='grandgrandson']/map/entry[@key='key4' and @value='value4']");
+		assertNotNull(node);
+		node = XPathAPI
+				.selectSingleNode(
+						doc,
+						rootpath
+								+ "/node[@name='mock']/node[@name='child']/node[@name='grandson2']/map");
+		assertNotNull(node);
+	}
+
+	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 Object invokeNonPublicMethod(AbstractPreferences obj, String name,
+			Class[] params, Object[] paramValues) throws SecurityException,
+			NoSuchMethodException, IllegalArgumentException,
+			IllegalAccessException, InvocationTargetException {
+		Method method = obj.getClass().getMethod(name, params);
+		method.setAccessible(true);
+		return method.invoke(obj, paramValues);
+	}
+
+	public void testBackingStoreException() throws IOException,
+			BackingStoreException {
+		if (!(pref instanceof MockAbstractPreferences)) {
+			return;
+		}
+		MockAbstractPreferences p = (MockAbstractPreferences) pref;
+		p.setResult(MockAbstractPreferences.backingException);
+		try {
+			p.childrenNames();
+			fail();
+		} 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();
+		} catch (BackingStoreException e) {
+		}
+		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();
+		} catch (BackingStoreException e) {
+		}
+
+		try {
+			p.keys();
+			fail();
+		} catch (BackingStoreException e) {
+		}
+		try {
+			p.childrenNames();
+			fail();
+		} catch (BackingStoreException e) {
+		}
+		p.parent();
+		p.node("");
+		p.nodeExists("");
+		try {
+			p.removeNode();
+			fail();
+		} catch (BackingStoreException e) {
+		}
+		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();
+		} catch (BackingStoreException e) {
+		}
+		try {
+			p.flush();
+			fail();
+		} catch (BackingStoreException e) {
+		}
+		try {
+			p.exportNode(new ByteArrayOutputStream());
+			fail();
+		} catch (BackingStoreException e) {
+		}
+		try {
+			p.exportSubtree(new ByteArrayOutputStream());
+			fail();
+		} catch (BackingStoreException e) {
+		}
+		p.isRemovedImpl();
+		try {
+			p.getChildImpl(null);
+			fail();
+		} catch (BackingStoreException e) {
+		}
+		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();
+		} catch (MockRuntimeException e) {
+		}
+		try {
+			p.put("exceptionkey", "value");
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		p.absolutePath();
+		p.toString();
+		assertEquals("exception default", p.get("key", "exception default"));
+		try {
+			p.remove("key");
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		try {
+			p.clear();
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		try {
+			p.putInt("key", 3);
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		p.getInt("key", 3);
+		try {
+			p.putLong("key", 3l);
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		p.getLong("key", 3l);
+		try {
+			p.putDouble("key", 3);
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		p.getDouble("key", 3);
+		try {
+			p.putBoolean("key", true);
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		p.getBoolean("key", true);
+		try {
+			p.putFloat("key", 3f);
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		p.getFloat("key", 3f);
+		try {
+			p.putByteArray("key", new byte[0]);
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		p.getByteArray("key", new byte[0]);
+		try {
+			p.keys();
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		try {
+			p.keys();
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		try {
+			p.childrenNames();
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		p.parent();
+		p.node("");
+		p.nodeExists("");
+		try {
+			p.removeNode();
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		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();
+		} catch (MockRuntimeException e) {
+		}
+		try {
+			p.flush();
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		try {
+			p.exportNode(new ByteArrayOutputStream());
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		try {
+			p.exportSubtree(new ByteArrayOutputStream());
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		p.isRemovedImpl();
+		try {
+			p.getChildImpl(null);
+			fail();
+		} catch (MockRuntimeException e) {
+		}
+		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();
+		} catch (NullPointerException e) {
+		}
+		p.absolutePath();
+		p.toString();
+		p.put("nullkey", "value");
+		assertEquals("null default", p.get("key", "null default"));
+		p.remove("key");
+		try {
+			p.clear();
+			fail();
+		} catch (NullPointerException e) {
+		}
+		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();
+		} catch (NullPointerException e) {
+		}
+		p.parent();
+		p.node("");
+		p.nodeExists("");
+		try {
+			p.removeNode();
+			fail();
+		} catch (NullPointerException e) {
+		}
+		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();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.exportSubtree(System.out);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		p.isRemovedImpl();
+		try {
+			p.getChildImpl("");
+			fail();
+		} catch (NullPointerException e) {
+		}
+		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();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.childrenNames();
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.remove(null);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.clear();
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.get("key", "null default");
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.put("nullkey", "value");
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.putInt("key", 3);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.getInt("key", 3);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.putLong("key", 3l);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.getLong("key", 3l);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.putDouble("key", 3);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.getDouble("key", 3);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.putBoolean("key", true);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.getBoolean("key", true);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.putFloat("key", 3f);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.getFloat("key", 3f);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.putByteArray("key", new byte[0]);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.getByteArray("key", new byte[0]);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.keys();
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.keys();
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.childrenNames();
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.parent();
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.node(null);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.removeNode();
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref
+					.addPreferenceChangeListener(new MockPreferenceChangeListener());
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref
+					.removePreferenceChangeListener(new MockPreferenceChangeListener());
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.addNodeChangeListener(new MockNodeChangeListener());
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.removeNodeChangeListener(new MockNodeChangeListener());
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.sync();
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.exportNode(null);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			pref.exportSubtree(null);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		if (!(pref instanceof MockAbstractPreferences)) {
+			return;
+		}
+		MockAbstractPreferences p = (MockAbstractPreferences) pref;
+		p.isRemovedImpl();
+		p.cachedChildrenImpl();
+		try {
+			p.getChildImpl(null);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+	}
+
+	public void testNullAndIllegalStateException() throws Exception {
+		if (!(pref instanceof MockAbstractPreferences)) {
+			return;
+		}
+		MockAbstractPreferences p = (MockAbstractPreferences) pref;
+		p.removeNode();
+		try {
+			p.get(null, "null default");
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.put(null, "value");
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.putInt(null, 3);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.getInt(null, 3);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.putLong(null, 3l);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.getLong(null, 3l);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.putDouble(null, 3);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.getDouble(null, 3);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.putBoolean(null, true);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.getBoolean(null, true);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.putFloat(null, 3f);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.getFloat(null, 3f);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.putByteArray(null, new byte[0]);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.getByteArray(null, new byte[0]);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.addPreferenceChangeListener(null);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.removePreferenceChangeListener(null);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+		try {
+			p.addNodeChangeListener(null);
+			fail();
+		} catch (NullPointerException e) {
+		}
+		try {
+			p.removeNodeChangeListener(null);
+			fail();
+		} catch (IllegalStateException e) {
+		}
+	}
+
+	public static class MockPreferenceChangeListener implements
+			PreferenceChangeListener {
+		private int changed = 0;
+
+		// private Object lock = new Object();
+
+		public void preferenceChange(PreferenceChangeEvent pce) {
+			// synchronized(lock){
+			changed++;
+			// }
+		}
+
+		public int getChanged() {
+			// synchronized(lock){
+			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) {
+						e.printStackTrace();
+					}
+				}
+				addDispatched = false;
+			}
+			return added;
+		}
+
+		public int getRemoved() {
+			synchronized (removeLock) {
+				if (!removeDispatched) {
+					try {
+						removeLock.wait(100);
+					} catch (InterruptedException e) {
+						e.printStackTrace();
+					}
+				}
+				removeDispatched = false;
+			}
+			return removed;
+
+		}
+
+		public void reset() {
+			added = 0;
+			removed = 0;
+		}
+	}
+}
+

Added: incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/AllTests.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/AllTests.java?rev=386087&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/AllTests.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/AllTests.java Wed Mar 15 06:55:38 2006
@@ -0,0 +1,46 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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 tests.api.java.util.prefs;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * TODO Type description
+ * 
+ */
+public class AllTests {
+
+	public static void main(String[] args) {
+		junit.textui.TestRunner.run(AllTests.suite());
+	}
+
+	public static Test suite() {
+		TestSuite suite = new TestSuite("Test for tests.api.java.util.prefs");
+		// $JUnit-BEGIN$
+		suite.addTestSuite(NodeChangeListenerTest.class);
+		suite.addTestSuite(PreferenceChangeListenerTest.class);
+		suite.addTestSuite(PreferencesFactoryTest.class);
+		suite.addTestSuite(BackingStoreExceptionTest.class);
+		suite.addTestSuite(InvalidPreferencesFormatExceptionTest.class);
+		suite.addTestSuite(PreferenceChangeEventTest.class);
+		suite.addTestSuite(NodeChangeEventTest.class);
+		suite.addTestSuite(PreferencesTest.class);
+		suite.addTestSuite(AbstractPreferencesTest.class);
+		// $JUnit-END$
+		return suite;
+	}
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/BackingStoreExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/BackingStoreExceptionTest.java?rev=386087&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/BackingStoreExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/BackingStoreExceptionTest.java Wed Mar 15 06:55:38 2006
@@ -0,0 +1,60 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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 tests.api.java.util.prefs;
+
+import java.io.NotSerializableException;
+import java.util.prefs.BackingStoreException;
+
+import junit.framework.TestCase;
+import tests.util.SerializationTester;
+
+/**
+ * 
+ * 
+ */
+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());
+	}
+
+	public void testSerialization() throws Exception {
+		try {
+			SerializationTester.writeObject(new BackingStoreException("msg"),
+					"test.txt");
+			fail();
+		} catch (NotSerializableException e) {
+		}
+	}
+
+}
+

Added: incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/FilePreferencesImplTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/FilePreferencesImplTest.java?rev=386087&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/FilePreferencesImplTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/FilePreferencesImplTest.java Wed Mar 15 06:55:38 2006
@@ -0,0 +1,222 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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 tests.api.java.util.prefs;
+
+import java.io.FilePermission;
+import java.io.IOException;
+import java.security.Permission;
+import java.util.prefs.BackingStoreException;
+import java.util.prefs.Preferences;
+
+import junit.framework.TestCase;
+
+/**
+ * TODO Type description
+ * 
+ */
+public class FilePreferencesImplTest extends TestCase {
+
+	Preferences uroot = Preferences.userRoot();
+
+	Preferences sroot = Preferences.systemRoot();
+
+	/*
+	 * @see TestCase#setUp()
+	 */
+	protected void setUp() throws Exception {
+		super.setUp();
+	}
+
+	/*
+	 * @see TestCase#tearDown()
+	 */
+	protected void tearDown() throws Exception {
+		super.tearDown();
+	}
+
+	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");
+
+		String[] childNames = uroot.childrenNames();
+		assertEquals(2, childNames.length);
+		for (int i = 0; i < childNames.length; i++) {
+			System.out.println(childNames[i]);
+		}
+
+		childNames = child1.childrenNames();
+		assertEquals(1, childNames.length);
+		for (int i = 0; i < childNames.length; i++) {
+			System.out.println(childNames[i]);
+		}
+
+		childNames = child2.childrenNames();
+		assertEquals(0, childNames.length);
+		for (int i = 0; i < childNames.length; i++) {
+			System.out.println(childNames[i]);
+		}
+
+		child1.removeNode();
+		childNames = uroot.childrenNames();
+		assertEquals(1, childNames.length);
+		for (int i = 0; i < childNames.length; i++) {
+			System.out.println(childNames[i]);
+		}
+		// child2.removeNode();
+		// childNames = uroot.childrenNames();
+		// assertEquals(0, childNames.length);
+
+		child1 = sroot.node("child1");
+		child2 = sroot.node("child2");
+		grandchild = child1.node("grand");
+
+		childNames = sroot.childrenNames();
+
+		for (int i = 0; i < childNames.length; i++) {
+			System.out.println(childNames[i]);
+		}
+		assertEquals(2, childNames.length);
+
+		childNames = child1.childrenNames();
+		assertEquals(1, childNames.length);
+		for (int i = 0; i < childNames.length; i++) {
+			System.out.println(childNames[i]);
+		}
+
+		childNames = child2.childrenNames();
+		assertEquals(0, childNames.length);
+		for (int i = 0; i < childNames.length; i++) {
+			System.out.println(childNames[i]);
+		}
+
+		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);
+		for (int i = 0; i < childNames.length; i++) {
+			System.out.println(childNames[i]);
+		}
+		child2.removeNode();
+		childNames = sroot.childrenNames();
+		assertEquals(0, childNames.length);
+	}
+
+	public void testSecurityException() throws BackingStoreException {
+		Preferences child1 = uroot.node("child1");
+		MockFileSecurityManager manager = new MockFileSecurityManager();
+		manager.install();
+		try {
+			try {
+				uroot.node("securityNode");
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+			try {
+				// need FilePermission(delete);
+				child1.removeNode();
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+			try {
+				uroot.childrenNames();
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			}
+			uroot.keys();
+			uroot.put("securitykey", "value1");
+			uroot.remove("securitykey");
+			try {
+				uroot.flush();
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			} catch (BackingStoreException e) {
+				assertTrue(e.getCause() instanceof SecurityException);
+			}
+			try {
+				uroot.sync();
+				fail("should throw security exception");
+			} catch (SecurityException e) {
+			} catch (BackingStoreException e) {
+				assertTrue(e.getCause() instanceof SecurityException);
+			}
+		} finally {
+			manager.restoreDefault();
+		}
+	}
+
+	static class MockFileSecurityManager extends SecurityManager {
+
+		SecurityManager dflt;
+
+		public MockFileSecurityManager() {
+			super();
+			dflt = System.getSecurityManager();
+		}
+
+		public void install() {
+			System.setSecurityManager(this);
+		}
+
+		public void restoreDefault() {
+			System.setSecurityManager(dflt);
+		}
+
+		public void checkPermission(Permission perm) {
+			if (perm instanceof FilePermission) {
+				throw new SecurityException();
+			} else if (dflt != null) {
+				dflt.checkPermission(perm);
+			}
+		}
+
+		public void checkPermission(Permission perm, Object ctx) {
+			if (perm instanceof FilePermission) {
+				System.out.println(perm.getActions());
+				throw new SecurityException();
+			} else if (dflt != null) {
+				dflt.checkPermission(perm, ctx);
+			}
+		}
+
+	}
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/InvalidPreferencesFormatExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/InvalidPreferencesFormatExceptionTest.java?rev=386087&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/InvalidPreferencesFormatExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/prefs/src/test/java/tests/api/java/util/prefs/InvalidPreferencesFormatExceptionTest.java Wed Mar 15 06:55:38 2006
@@ -0,0 +1,76 @@
+/* Copyright 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed 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 tests.api.java.util.prefs;
+
+import java.io.NotSerializableException;
+import java.util.prefs.InvalidPreferencesFormatException;
+
+import tests.util.SerializationTester;
+
+import junit.framework.TestCase;
+
+/**
+ * 
+ */
+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);
+	}
+
+	public void testSerialization() throws Exception {
+		try {
+			SerializationTester.writeObject(
+					new InvalidPreferencesFormatException("msg"), "test.txt");
+			fail();
+		} catch (NotSerializableException e) {
+		}
+	}
+
+}
+



Mime
View raw message