From 2c23efcdb5bf7e055caa166e56cfdd50011f02df Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Mon, 31 Aug 2009 10:52:23 -0700 Subject: [PATCH] Update prefs to Harmony r772995. Notable changes: - exception handling has changed to cleanup more reliably - calls to Collection.toArray() size the array properly - lots of style tweaks (rewrapping Javadoc, reintending wrapped code, whitespace) - new PrefsTester class ensures tests store prefs in the tmp directory Squashed commit of the following: commit 2157269d8ed39ccb8a71d735c12bbabcfd548243 Merge: d287282 97818bf Author: Jesse Wilson Date: Wed Aug 5 11:14:26 2009 -0700 Merge branch 'prefs_772995' into prefs_dalvik Conflicts: libcore/prefs/.classpath libcore/prefs/.settings/org.eclipse.jdt.core.prefs libcore/prefs/build.xml libcore/prefs/make/exclude.linux.x86_64.drl libcore/prefs/make/exclude.windows.x86.drl libcore/prefs/make/exclude.windows.x86_64.drl libcore/prefs/src/main/java/java/util/prefs/AbstractPreferences.java libcore/prefs/src/main/java/java/util/prefs/BackingStoreException.java libcore/prefs/src/main/java/java/util/prefs/FilePreferencesFactoryImpl.java libcore/prefs/src/main/java/java/util/prefs/FilePreferencesImpl.java libcore/prefs/src/main/java/java/util/prefs/InvalidPreferencesFormatException.java libcore/prefs/src/main/java/java/util/prefs/NodeChangeEvent.java libcore/prefs/src/main/java/java/util/prefs/NodeChangeListener.java libcore/prefs/src/main/java/java/util/prefs/PreferenceChangeEvent.java libcore/prefs/src/main/java/java/util/prefs/PreferenceChangeListener.java libcore/prefs/src/main/java/java/util/prefs/Preferences.java libcore/prefs/src/main/java/java/util/prefs/PreferencesFactory.java libcore/prefs/src/main/java/java/util/prefs/RegistryPreferencesFactoryImpl.java libcore/prefs/src/main/java/java/util/prefs/RegistryPreferencesImpl.java libcore/prefs/src/main/java/java/util/prefs/XMLParser.java libcore/prefs/src/main/java/org/apache/harmony/prefs/internal/nls/Messages.java libcore/prefs/src/main/native/prefs/windows/PreferencesImpl.c libcore/prefs/src/main/native/prefs/windows/hyprefs.rc libcore/prefs/src/main/native/prefs/windows/makefile libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/AbstractPreferencesTest.java libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/AllTests.java libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/FilePreferencesImplTest.java libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/MockAbstractPreferences.java libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/MockPreferencesFactory.java libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/MockSecurityManager.java libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeListenerTest.java libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferenceChangeListenerTest.java libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesFactoryTest.java libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesTest.java libcore/prefs/src/test/java/tests/prefs/AllTests.java commit d287282b550d4a5d262f1d1703344ed61bdc6d15 Author: Jesse Wilson Date: Tue Aug 4 14:36:36 2009 -0700 Dalvik Prefs commit 97818bf21cfde744eeb5fbf1f9c31d9bd66f5a2e Author: Jesse Wilson Date: Tue Aug 4 14:35:52 2009 -0700 Prefs 772995 commit 9a506f93947938dad3b41e1393f53b766ba0319f Author: Jesse Wilson Date: Tue Aug 4 14:22:40 2009 -0700 Prefs 527399 --- .../java/java/util/prefs/AbstractPreferences.java | 199 ++++---- .../java/util/prefs/BackingStoreException.java | 23 +- .../util/prefs/FilePreferencesFactoryImpl.java | 6 +- .../java/java/util/prefs/FilePreferencesImpl.java | 41 +- .../prefs/InvalidPreferencesFormatException.java | 26 +- .../main/java/java/util/prefs/NodeChangeEvent.java | 37 +- .../java/java/util/prefs/NodeChangeListener.java | 17 +- .../java/util/prefs/PreferenceChangeEvent.java | 28 +- .../java/util/prefs/PreferenceChangeListener.java | 16 +- .../src/main/java/java/util/prefs/Preferences.java | 499 ++++++++++----------- .../java/java/util/prefs/PreferencesFactory.java | 18 +- .../src/main/java/java/util/prefs/XMLParser.java | 147 +++--- .../harmony/prefs/internal/nls/Messages.java | 14 +- .../java/util/prefs/AbstractPreferencesTest.java | 42 +- .../java/util/prefs/FilePreferencesImplTest.java | 20 +- .../InvalidPreferencesFormatExceptionTest.java | 4 +- .../java/util/prefs/MockAbstractPreferences.java | 9 + .../tests/java/util/prefs/MockSecurityManager.java | 2 + .../tests/java/util/prefs/NodeChangeEventTest.java | 18 +- .../java/util/prefs/NodeChangeListenerTest.java | 7 + .../java/util/prefs/PreferenceChangeEventTest.java | 4 +- .../util/prefs/PreferenceChangeListenerTest.java | 1 + .../java/util/prefs/PreferencesFactoryTest.java | 5 +- .../tests/java/util/prefs/PreferencesTest.java | 69 +-- .../prefs/src/test/java/tests/prefs/AllTests.java | 3 +- .../prefs/java/util/prefs/preferences.dtd | 56 --- .../src/test/java/tests/util/PrefsTester.java | 73 +++ 27 files changed, 678 insertions(+), 706 deletions(-) delete mode 100644 libcore/prefs/src/test/resources/prefs/java/util/prefs/preferences.dtd create mode 100644 libcore/support/src/test/java/tests/util/PrefsTester.java diff --git a/libcore/prefs/src/main/java/java/util/prefs/AbstractPreferences.java b/libcore/prefs/src/main/java/java/util/prefs/AbstractPreferences.java index 711cc0118..326456935 100644 --- a/libcore/prefs/src/main/java/java/util/prefs/AbstractPreferences.java +++ b/libcore/prefs/src/main/java/java/util/prefs/AbstractPreferences.java @@ -20,6 +20,7 @@ package java.util.prefs; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; +import java.util.Collection; import java.util.EventListener; import java.util.EventObject; import java.util.HashMap; @@ -27,7 +28,6 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.StringTokenizer; import java.util.TreeSet; import org.apache.harmony.luni.util.Base64; @@ -38,8 +38,9 @@ import org.apache.harmony.prefs.internal.nls.Messages; * Preferences, which can be used to simplify {@code Preferences} provider's * implementation. This class defines nine abstract SPI methods, which must be * implemented by a preference provider. - * - * @since Android 1.0 + * + * @since 1.4 + * @see Preferences */ public abstract class AbstractPreferences extends Preferences { /* @@ -47,14 +48,9 @@ public abstract class AbstractPreferences extends Preferences { * Class fields * ----------------------------------------------------------- */ - /** - * The unhandled events collection. - */ + /** the unhandled events collection */ private static final List events = new LinkedList(); - - /** - * The event dispatcher thread. - */ + /** the event dispatcher thread */ private static final EventDispatcher dispatcher = new EventDispatcher("Preference Event Dispatcher"); //$NON-NLS-1$ /* @@ -72,11 +68,13 @@ public abstract class AbstractPreferences extends Preferences { Preferences sroot = Preferences.systemRoot(); try { uroot.flush(); - } catch (BackingStoreException e) {//ignore + } catch (BackingStoreException e) { + // ignore } try { sroot.flush(); - } catch (BackingStoreException e) {//ignore + } catch (BackingStoreException e) { + // ignore } } }); @@ -87,9 +85,7 @@ public abstract class AbstractPreferences extends Preferences { * Instance fields (package-private) * ----------------------------------------------------------- */ - /** - * True, if this node is in user preference hierarchy. - */ + /** true if this node is in user preference hierarchy */ boolean userNode; /* @@ -97,16 +93,11 @@ public abstract class AbstractPreferences extends Preferences { * Instance fields (private) * ----------------------------------------------------------- */ - /** - * Marker class for 'lock' field. - */ - private static class Lock { - } + /** Marker class for 'lock' field. */ + private static class Lock {} /** * The object used to lock this node. - * - * @since Android 1.0 */ protected final Object lock; @@ -115,14 +106,10 @@ public abstract class AbstractPreferences extends Preferences { * backing store. This field's default value is false, and it is checked * when the node creation is completed, and if it is true, the node change * event will be fired for this node's parent. - * - * @since Android 1.0 */ protected boolean newNode; - /** - * Cached child nodes - */ + /** cached child nodes */ private Map cachedNode; //the collections of listeners @@ -147,9 +134,9 @@ public abstract class AbstractPreferences extends Preferences { * ----------------------------------------------------------- */ /** - * Constructs a new {@code AbstractPreferences} instance using the given parent node - * and node name. - * + * Constructs a new {@code AbstractPreferences} instance using the given + * parent node and node name. + * * @param parent * the parent node of the new node or {@code null} to indicate * that the new node is a root node. @@ -159,7 +146,6 @@ public abstract class AbstractPreferences extends Preferences { * @throws IllegalArgumentException * if the name contains a slash character or is empty if {@code * parent} is not {@code null}. - * @since Android 1.0 */ protected AbstractPreferences(AbstractPreferences parent, String name) { if ((null == parent ^ name.length() == 0) || name.indexOf("/") >= 0) { //$NON-NLS-1$ @@ -185,7 +171,6 @@ public abstract class AbstractPreferences extends Preferences { * Returns an array of all cached child nodes. * * @return the array of cached child nodes. - * @since Android 1.0 */ protected final AbstractPreferences[] cachedChildren() { return cachedNode.values().toArray(new AbstractPreferences[cachedNode.size()]); @@ -193,9 +178,10 @@ public abstract class AbstractPreferences extends Preferences { /** * Returns the child node with the specified name or {@code null} if it - * doesn't exist. Implementers can assume that the name supplied to this method - * will be a valid node name string (conforming to the node naming format) and - * will not correspond to a node that has been cached or removed. + * doesn't exist. Implementers can assume that the name supplied to this + * method will be a valid node name string (conforming to the node naming + * format) and will not correspond to a node that has been cached or + * removed. * * @param name * the name of the desired child node. @@ -204,7 +190,6 @@ public abstract class AbstractPreferences extends Preferences { * @throws BackingStoreException * if the backing store is unavailable or causes an operation * failure. - * @since Android 1.0 */ protected AbstractPreferences getChild(String name) throws BackingStoreException { @@ -226,10 +211,9 @@ public abstract class AbstractPreferences extends Preferences { /** * Returns whether this node has been removed by invoking the method {@code * removeNode()}. - * + * * @return {@code true}, if this node has been removed, {@code false} * otherwise. - * @since Android 1.0 */ protected boolean isRemoved() { synchronized (lock) { @@ -240,33 +224,31 @@ public abstract class AbstractPreferences extends Preferences { /** * Flushes changes of this node to the backing store. This method should * only flush this node and should not include the descendant nodes. Any - * implementation that wants to provide functionality to flush all nodes + * implementation that wants to provide functionality to flush all nodes * at once should override the method {@link #flush() flush()}. - * + * * @throws BackingStoreException * if the backing store is unavailable or causes an operation * failure. - * @since Android 1.0 */ protected abstract void flushSpi() throws BackingStoreException; /** - * Returns the names of all of the child nodes of this node or an empty array if - * this node has no children. The names of cached children are not required to be - * returned. - * + * Returns the names of all of the child nodes of this node or an empty + * array if this node has no children. The names of cached children are not + * required to be returned. + * * @return the names of this node's children. * @throws BackingStoreException * if the backing store is unavailable or causes an operation * failure. - * @since Android 1.0 */ protected abstract String[] childrenNamesSpi() throws BackingStoreException; /** * Returns the child preference node with the given name, creating it * if it does not exist. The caller of this method should ensure that the - * given name is valid and that this node has not been removed or cached. + * given name is valid and that this node has not been removed or cached. * If the named node has just been removed, the implementation * of this method must create a new one instead of reactivating the removed * one. @@ -278,7 +260,6 @@ public abstract class AbstractPreferences extends Preferences { * @param name * the name of the child preference to be returned. * @return the child preference node. - * @since Android 1.0 */ protected abstract AbstractPreferences childSpi(String name); @@ -287,39 +268,37 @@ public abstract class AbstractPreferences extends Preferences { * Puts the given key-value pair into this node. Caller of this method * should ensure that both of the given values are valid and that this * node has not been removed. - * + * * @param name * the given preference key. * @param value * the given preference value. - * @since Android 1.0 */ protected abstract void putSpi(String name, String value); /** - * Gets the preference value mapped to the given key. The caller of this method - * should ensure that the given key is valid and that this node has not been - * removed. This method should not throw any exceptions but if it does, the - * caller will ignore the exception, regarding it as a {@code null} return value. - * + * Gets the preference value mapped to the given key. The caller of this + * method should ensure that the given key is valid and that this node has + * not been removed. This method should not throw any exceptions but if it + * does, the caller will ignore the exception, regarding it as a {@code + * null} return value. + * * @param key * the given key to be searched for. * @return the preference value mapped to the given key. - * @since Android 1.0 */ protected abstract String getSpi(String key); /** * Returns an array of all preference keys of this node or an empty array if - * no preferences have been found. The caller of this method should ensure that - * this node has not been removed. - * + * no preferences have been found. The caller of this method should ensure + * that this node has not been removed. + * * @return the array of all preference keys. * @throws BackingStoreException * if the backing store is unavailable or causes an operation * failure. - * @since Android 1.0 */ protected abstract String[] keysSpi() throws BackingStoreException; @@ -329,11 +308,10 @@ public abstract class AbstractPreferences extends Preferences { * method {@link Preferences#removeNode() Preferences.removeNode()} should * invoke this method multiple-times in bottom-up pattern. The removal is * not required to be persisted until after it is flushed. - * + * * @throws BackingStoreException * if the backing store is unavailable or causes an operation * failure. - * @since Android 1.0 */ protected abstract void removeNodeSpi() throws BackingStoreException; @@ -344,20 +322,18 @@ public abstract class AbstractPreferences extends Preferences { * * @param key * the key of the preference that is to be removed. - * @since Android 1.0 */ protected abstract void removeSpi(String key); /** * Synchronizes this node with the backing store. This method should only * synchronize this node and should not include the descendant nodes. An - * implementation that wants to provide functionality to synchronize all nodes at once should - * override the method {@link #sync() sync()}. + * implementation that wants to provide functionality to synchronize all + * nodes at once should override the method {@link #sync() sync()}. * * @throws BackingStoreException * if the backing store is unavailable or causes an operation * failure. - * @since Android 1.0 */ protected abstract void syncSpi() throws BackingStoreException; @@ -385,7 +361,7 @@ public abstract class AbstractPreferences extends Preferences { for (int i = 0; i < names.length; i++) { result.add(names[i]); } - return result.toArray(new String[0]); + return result.toArray(new String[result.size()]); } } @@ -439,13 +415,13 @@ public abstract class AbstractPreferences extends Preferences { if (key == null) { throw new NullPointerException(); } - String result; + String result = null; synchronized (lock) { checkState(); try { result = getSpi(key); } catch (Exception e) { - result = null; + // ignored } } return (result == null ? deflt : result); @@ -456,9 +432,10 @@ public abstract class AbstractPreferences extends Preferences { String result = get(key, null); if (result == null) { return deflt; - } else if (result.equalsIgnoreCase("true")) { //$NON-NLS-1$ + } + if ("true".equalsIgnoreCase(result)) { //$NON-NLS-1$ return true; - } else if (result.equalsIgnoreCase("false")) { //$NON-NLS-1$ + } else if ("false".equalsIgnoreCase(result)) { //$NON-NLS-1$ return false; } else { return deflt; @@ -474,17 +451,15 @@ public abstract class AbstractPreferences extends Preferences { if (svalue.length() == 0) { return new byte[0]; } - byte[] dres; try { byte[] bavalue = svalue.getBytes("US-ASCII"); //$NON-NLS-1$ if (bavalue.length % 4 != 0) { return deflt; } - dres = Base64.decode(bavalue); + return Base64.decode(bavalue); } catch (Exception e) { - dres = deflt; + return deflt; } - return dres; } @Override @@ -493,13 +468,11 @@ public abstract class AbstractPreferences extends Preferences { if (result == null) { return deflt; } - double dres; try { - dres = Double.parseDouble(result); + return Double.parseDouble(result); } catch (NumberFormatException e) { - dres = deflt; + return deflt; } - return dres; } @Override @@ -508,13 +481,11 @@ public abstract class AbstractPreferences extends Preferences { if (result == null) { return deflt; } - float fres; try { - fres = Float.parseFloat(result); + return Float.parseFloat(result); } catch (NumberFormatException e) { - fres = deflt; + return deflt; } - return fres; } @Override @@ -523,13 +494,11 @@ public abstract class AbstractPreferences extends Preferences { if (result == null) { return deflt; } - int ires; try { - ires = Integer.parseInt(result); + return Integer.parseInt(result); } catch (NumberFormatException e) { - ires = deflt; + return deflt; } - return ires; } @Override @@ -538,13 +507,11 @@ public abstract class AbstractPreferences extends Preferences { if (result == null) { return deflt; } - long lres; try { - lres = Long.parseLong(result); + return Long.parseLong(result); } catch (NumberFormatException e) { - lres = deflt; + return deflt; } - return lres; } @Override @@ -583,42 +550,44 @@ public abstract class AbstractPreferences extends Preferences { startNode = this; } } - Preferences result = null; try { - result = startNode.nodeImpl(name, true); + return startNode.nodeImpl(name, true); } catch (BackingStoreException e) { - //should not happen + // should not happen + return null; } - return result; } private void validateName(String name) { if (name.endsWith("/") && name.length() > 1) { //$NON-NLS-1$ // prefs.6=Name cannot end with '/'\! - throw new IllegalArgumentException(Messages.getString("prefs.6")); //$NON-NLS-1$ + throw new IllegalArgumentException(Messages.getString("prefs.6")); //$NON-NLS-1$ } if (name.indexOf("//") >= 0) { //$NON-NLS-1$ // prefs.7=Name cannot contains consecutive '/'\! - throw new IllegalArgumentException( - Messages.getString("prefs.7")); //$NON-NLS-1$ + throw new IllegalArgumentException(Messages.getString("prefs.7")); //$NON-NLS-1$ } } private AbstractPreferences nodeImpl(String path, boolean createNew) throws BackingStoreException { - StringTokenizer st = new StringTokenizer(path, "/"); //$NON-NLS-1$ + String[] names = path.split("/");//$NON-NLS-1$ AbstractPreferences currentNode = this; AbstractPreferences temp = null; - while (st.hasMoreTokens() && null != currentNode) { - String name = st.nextToken(); - synchronized (currentNode.lock) { - temp = currentNode.cachedNode.get(name); - if (temp == null) { - temp = getNodeFromBackend(createNew, currentNode, name); + if (null != currentNode) { + for (int i = 0; i < names.length; i++) { + String name = names[i]; + synchronized (currentNode.lock) { + temp = currentNode.cachedNode.get(name); + if (temp == null) { + temp = getNodeFromBackend(createNew, currentNode, name); + } + } + currentNode = temp; + if (null == currentNode) { + break; } } - - currentNode = temp; } return currentNode; } @@ -626,12 +595,12 @@ public abstract class AbstractPreferences extends Preferences { private AbstractPreferences getNodeFromBackend(boolean createNew, AbstractPreferences currentNode, String name) throws BackingStoreException { - AbstractPreferences temp; if (name.length() > MAX_NAME_LENGTH) { // prefs.8=Name length is too long: {0} - throw new IllegalArgumentException(Messages.getString("prefs.8", //$NON-NLS-1$ + throw new IllegalArgumentException(Messages.getString("prefs.8", //$NON-NLS-1$ name)); } + AbstractPreferences temp; if (createNew) { temp = currentNode.childSpi(name); currentNode.cachedNode.put(name, temp); @@ -646,6 +615,9 @@ public abstract class AbstractPreferences extends Preferences { @Override public boolean nodeExists(String name) throws BackingStoreException { + if (null == name) { + throw new NullPointerException(); + } AbstractPreferences startNode = null; synchronized (lock) { if (isRemoved()) { @@ -771,10 +743,11 @@ public abstract class AbstractPreferences extends Preferences { cachedNode.put(childrenNames[i], child); } } - AbstractPreferences[] children = cachedNode - .values().toArray(new AbstractPreferences[0]); - for (int i = 0; i < children.length; i++) { - children[i].removeNodeImpl(); + + final Collection values = cachedNode.values(); + final AbstractPreferences[] children = values.toArray(new AbstractPreferences[values.size()]); + for (AbstractPreferences child : children) { + child.removeNodeImpl(); } removeNodeSpi(); isRemoved = true; diff --git a/libcore/prefs/src/main/java/java/util/prefs/BackingStoreException.java b/libcore/prefs/src/main/java/java/util/prefs/BackingStoreException.java index e8a805c2d..553d7ab4b 100644 --- a/libcore/prefs/src/main/java/java/util/prefs/BackingStoreException.java +++ b/libcore/prefs/src/main/java/java/util/prefs/BackingStoreException.java @@ -17,40 +17,35 @@ package java.util.prefs; - /** * An exception to indicate that an error was encountered while accessing the * backing store. - * - * @since Android 1.0 + * + * @since 1.4 */ public class BackingStoreException extends Exception { - + private static final long serialVersionUID = 859796500401108469L; - + /** - * Constructs a new {@code BackingStoreException} instance with a detailed exception - * message. + * Constructs a new {@code BackingStoreException} instance with a detailed + * exception message. * * @param s * the detailed exception message. - * @since Android 1.0 */ public BackingStoreException (String s) { super(s); } /** - * Constructs a new {@code BackingStoreException} instance with a nested {@code Throwable}. - * + * Constructs a new {@code BackingStoreException} instance with a nested + * {@code Throwable}. + * * @param t * the nested {@code Throwable}. - * @since Android 1.0 */ public BackingStoreException (Throwable t) { super(t); } } - - - diff --git a/libcore/prefs/src/main/java/java/util/prefs/FilePreferencesFactoryImpl.java b/libcore/prefs/src/main/java/java/util/prefs/FilePreferencesFactoryImpl.java index cc68e62e9..69eaa0151 100644 --- a/libcore/prefs/src/main/java/java/util/prefs/FilePreferencesFactoryImpl.java +++ b/libcore/prefs/src/main/java/java/util/prefs/FilePreferencesFactoryImpl.java @@ -17,10 +17,10 @@ package java.util.prefs; /** - * The default implementation of PreferencesFactory for the Linux + * The default implementation of PreferencesFactory for the Linux * platform, using the file system as its back end. - * - * @since Android 1.0 + * + * @since 1.4 */ class FilePreferencesFactoryImpl implements PreferencesFactory { // user root preferences diff --git a/libcore/prefs/src/main/java/java/util/prefs/FilePreferencesImpl.java b/libcore/prefs/src/main/java/java/util/prefs/FilePreferencesImpl.java index cf85fa081..f6e5e8f73 100644 --- a/libcore/prefs/src/main/java/java/util/prefs/FilePreferencesImpl.java +++ b/libcore/prefs/src/main/java/java/util/prefs/FilePreferencesImpl.java @@ -28,12 +28,12 @@ import java.util.Set; import org.apache.harmony.prefs.internal.nls.Messages; /** - * The default implementation of AbstractPreferences for the Linux platform, - * using the file system as its back end. - * + * The default implementation of AbstractPreferences for the Linux + * platform, using the file system as its back end. + * * TODO some sync mechanism with backend, Performance - check file edit date - * - * @since Android 1.0 + * + * @since 1.4 */ class FilePreferencesImpl extends AbstractPreferences { @@ -64,7 +64,6 @@ class FilePreferencesImpl extends AbstractPreferences { SYSTEM_HOME = System.getProperty("java.home") + "/.systemPrefs";//$NON-NLS-1$//$NON-NLS-2$ return null; } - }); } @@ -97,9 +96,9 @@ class FilePreferencesImpl extends AbstractPreferences { * Constructors * -------------------------------------------------------------- */ - + /** - * Construct root FilePreferencesImpl instance, construct + * Construct root FilePreferencesImpl instance, construct * user root if userNode is true, system root otherwise */ FilePreferencesImpl(boolean userNode) { @@ -108,9 +107,9 @@ class FilePreferencesImpl extends AbstractPreferences { path = userNode ? USER_HOME : SYSTEM_HOME; initPrefs(); } - + /** - * Construct a prefs using given parent and given name + * Construct a prefs using given parent and given name */ private FilePreferencesImpl(AbstractPreferences parent, String name) { super(parent, name); @@ -132,16 +131,16 @@ class FilePreferencesImpl extends AbstractPreferences { @Override protected String[] childrenNamesSpi() throws BackingStoreException { String[] names = AccessController - .doPrivileged(new PrivilegedAction() { - public String[] run() { - return dir.list(new FilenameFilter() { - public boolean accept(File parent, String name) { - return new File(path + File.separator + name).isDirectory(); - } - }); - + .doPrivileged(new PrivilegedAction() { + public String[] run() { + return dir.list(new FilenameFilter() { + public boolean accept(File parent, String name) { + return new File(path + File.separator + name).isDirectory(); } }); + + } + }); if (null == names) {// file is not a directory, exception case // prefs.3=Cannot get children names for {0}! throw new BackingStoreException( @@ -192,14 +191,16 @@ class FilePreferencesImpl extends AbstractPreferences { prefs = XMLParser.loadFilePrefs(prefsFile); } return prefs.getProperty(key); - } catch (Exception e) {// if Exception happened, return null + } catch (Exception e) { + // if Exception happened, return null return null; } } @Override protected String[] keysSpi() throws BackingStoreException { - return prefs.keySet().toArray(new String[0]); + final Set ks = prefs.keySet(); + return ks.toArray(new String[ks.size()]); } @Override diff --git a/libcore/prefs/src/main/java/java/util/prefs/InvalidPreferencesFormatException.java b/libcore/prefs/src/main/java/java/util/prefs/InvalidPreferencesFormatException.java index b31b3a15f..ba8940bbd 100644 --- a/libcore/prefs/src/main/java/java/util/prefs/InvalidPreferencesFormatException.java +++ b/libcore/prefs/src/main/java/java/util/prefs/InvalidPreferencesFormatException.java @@ -21,51 +21,43 @@ package java.util.prefs; * An exception to indicate that the input XML file is not well-formed or could * not be validated against the appropriate document type (specified by * in the {@code Preferences}). - * - * @since Android 1.0 */ public class InvalidPreferencesFormatException extends Exception { - + private static final long serialVersionUID = -791715184232119669L; - + /** - * Constructs a new {@code InvalidPreferencesFormatException} instance with a - * detailed exception message. + * Constructs a new {@code InvalidPreferencesFormatException} instance with + * a detailed exception message. * * @param s * the detailed exception message. - * @since Android 1.0 */ public InvalidPreferencesFormatException (String s) { super(s); } /** - * Constructs a new {@code InvalidPreferencesFormatException} instance with a - * detailed exception message and a nested {@code Throwable}. + * Constructs a new {@code InvalidPreferencesFormatException} instance with + * a detailed exception message and a nested {@code Throwable}. * * @param s * the detailed exception message. * @param t * the nested {@code Throwable}. - * @since Android 1.0 */ public InvalidPreferencesFormatException (String s, Throwable t) { super(s,t); } /** - * Constructs a new {@code InvalidPreferencesFormatException} instance with a nested - * {@code Throwable}. - * + * Constructs a new {@code InvalidPreferencesFormatException} instance with + * a nested {@code Throwable}. + * * @param t * the nested {@code Throwable}. - * @since Android 1.0 */ public InvalidPreferencesFormatException (Throwable t) { super(t); } } - - - diff --git a/libcore/prefs/src/main/java/java/util/prefs/NodeChangeEvent.java b/libcore/prefs/src/main/java/java/util/prefs/NodeChangeEvent.java index e9824bce8..3e23f5a18 100644 --- a/libcore/prefs/src/main/java/java/util/prefs/NodeChangeEvent.java +++ b/libcore/prefs/src/main/java/java/util/prefs/NodeChangeEvent.java @@ -14,7 +14,6 @@ * limitations under the License. */ - package java.util.prefs; import java.io.Serializable; @@ -28,19 +27,22 @@ import java.io.IOException; * This is the event class to indicate that one child of the preference node has * been added or deleted. *

- * Please note that the serialization functionality has not yet been implemented, so - * the serialization methods do nothing but throw a {@code NotSerializableException}. - *

+ * Please note that although the class is marked as {@code Serializable} by + * inheritance from {@code EventObject}, this type is not intended to be serialized + * so the serialization methods do nothing but throw a {@code NotSerializableException}. + * + * @see java.util.prefs.Preferences + * @see java.util.prefs.NodeChangeListener * - * @since Android 1.0 + * @since 1.4 */ public class NodeChangeEvent extends EventObject implements Serializable { - + private static final long serialVersionUID = 8068949086596572957L; - + private final Preferences parent; private final Preferences child; - + /** * Constructs a new {@code NodeChangeEvent} instance. * @@ -49,43 +51,40 @@ public class NodeChangeEvent extends EventObject implements Serializable { * considered as the event source. * @param c * the child {@code Preferences} instance that was added or deleted. - * @since Android 1.0 */ public NodeChangeEvent (Preferences p, Preferences c) { super(p); parent = p; child = c; } - + /** * Gets the {@code Preferences} instance that fired this event. * * @return the {@code Preferences} instance that fired this event. - * @since Android 1.0 */ public Preferences getParent() { return parent; } - + /** * Gets the child {@code Preferences} node that was added or removed. * * @return the added or removed child {@code Preferences} node. - * @since Android 1.0 */ public Preferences getChild() { return child; } - - /* + + /** * This method always throws a NotSerializableException, * because this object cannot be serialized, */ private void writeObject (ObjectOutputStream out) throws IOException { throw new NotSerializableException(); } - - /* + + /** * This method always throws a NotSerializableException, * because this object cannot be serialized, */ @@ -93,7 +92,3 @@ public class NodeChangeEvent extends EventObject implements Serializable { throw new NotSerializableException(); } } - - - - diff --git a/libcore/prefs/src/main/java/java/util/prefs/NodeChangeListener.java b/libcore/prefs/src/main/java/java/util/prefs/NodeChangeListener.java index f16b2061b..41da23e5a 100644 --- a/libcore/prefs/src/main/java/java/util/prefs/NodeChangeListener.java +++ b/libcore/prefs/src/main/java/java/util/prefs/NodeChangeListener.java @@ -14,41 +14,36 @@ * limitations under the License. */ - package java.util.prefs; import java.util.EventListener; import java.util.prefs.NodeChangeEvent; /** - * This interface is used to handle preference node change events. - * The implementation of this interface can be installed by the {@code Preferences} instance. + * This interface is used to handle preference node change events. The + * implementation of this interface can be installed by the {@code Preferences} + * instance. * + * @see Preferences * @see NodeChangeEvent * - * @since Android 1.0 + * @since 1.4 */ public interface NodeChangeListener extends EventListener { - /** * This method gets called whenever a child node is added to another node. * * @param e * the node change event. - * @since Android 1.0 */ public void childAdded (NodeChangeEvent e); - + /** * This method gets called whenever a child node is removed from another * node. * * @param e * the node change event. - * @since Android 1.0 */ public void childRemoved (NodeChangeEvent e); } - - - diff --git a/libcore/prefs/src/main/java/java/util/prefs/PreferenceChangeEvent.java b/libcore/prefs/src/main/java/java/util/prefs/PreferenceChangeEvent.java index f0f0787a3..d355f4e03 100644 --- a/libcore/prefs/src/main/java/java/util/prefs/PreferenceChangeEvent.java +++ b/libcore/prefs/src/main/java/java/util/prefs/PreferenceChangeEvent.java @@ -14,7 +14,6 @@ * limitations under the License. */ - package java.util.prefs; import java.io.IOException; @@ -28,16 +27,19 @@ import java.util.EventObject; * This is the event class to indicate that a preference has been added, deleted * or updated. *

- * Please note that the serialization functionality has not yet been implemented, so - * the serialization methods do nothing but throw a {@code NotSerializableException}. - *

+ * Please note that although the class is marked as {@code Serializable} by + * inheritance from {@code EventObject}, this type is not intended to be serialized + * so the serialization methods do nothing but throw a {@code NotSerializableException}. + * + * @see java.util.prefs.Preferences + * @see java.util.prefs.PreferenceChangeListener * - * @since Android 1.0 + * @since 1.4 */ public class PreferenceChangeEvent extends EventObject implements Serializable { private static final long serialVersionUID = 793724513368024975L; - + private final Preferences node; private final String key; @@ -55,7 +57,6 @@ public class PreferenceChangeEvent extends EventObject implements Serializable { * @param v * the new value of the changed preference, this value can be * {@code null}, which means the preference has been removed. - * @since Android 1.0 */ public PreferenceChangeEvent(Preferences p, String k, String v) { super(p); @@ -68,7 +69,6 @@ public class PreferenceChangeEvent extends EventObject implements Serializable { * Gets the key of the changed preference. * * @return the changed preference's key. - * @since Android 1.0 */ public String getKey() { return key; @@ -78,9 +78,8 @@ public class PreferenceChangeEvent extends EventObject implements Serializable { * Gets the new value of the changed preference or {@code null} if the * preference has been removed. * - * @return the new value of the changed preference or null if the preference - * has been removed. - * @since Android 1.0 + * @return the new value of the changed preference or {@code null} if the + * preference has been removed. */ public String getNewValue() { return value; @@ -90,13 +89,12 @@ public class PreferenceChangeEvent extends EventObject implements Serializable { * Gets the {@code Preferences} instance that fired this event. * * @return the {@code Preferences} instance that fired this event. - * @since Android 1.0 */ public Preferences getNode() { return node; } - /* + /** * This method always throws a NotSerializableException, * because this object cannot be serialized, */ @@ -104,7 +102,7 @@ public class PreferenceChangeEvent extends EventObject implements Serializable { throw new NotSerializableException(); } - /* + /** * This method always throws a NotSerializableException, * because this object cannot be serialized, */ @@ -112,5 +110,3 @@ public class PreferenceChangeEvent extends EventObject implements Serializable { throw new NotSerializableException(); } } - - diff --git a/libcore/prefs/src/main/java/java/util/prefs/PreferenceChangeListener.java b/libcore/prefs/src/main/java/java/util/prefs/PreferenceChangeListener.java index 28bb763e5..97aecedba 100644 --- a/libcore/prefs/src/main/java/java/util/prefs/PreferenceChangeListener.java +++ b/libcore/prefs/src/main/java/java/util/prefs/PreferenceChangeListener.java @@ -14,21 +14,23 @@ * limitations under the License. */ - package java.util.prefs; import java.util.EventListener; /** - * This interface is used to handle preferences change events. The implementation of - * this interface can be installed by the {@code Preferences} instance. + * This interface is used to handle preferences change events. The + * implementation of this interface can be installed by the {@code Preferences} + * instance. * + * @see Preferences * @see PreferenceChangeEvent + * * - * @since Android 1.0 + * @since 1.4 */ public interface PreferenceChangeListener extends EventListener { - + /** * This method gets invoked whenever a preference is added, deleted or * updated. @@ -36,10 +38,6 @@ public interface PreferenceChangeListener extends EventListener { * @param pce * the event instance which describes the changed {@code Preferences} * instance and the preference value. - * @since Android 1.0 */ void preferenceChange (PreferenceChangeEvent pce); } - - - diff --git a/libcore/prefs/src/main/java/java/util/prefs/Preferences.java b/libcore/prefs/src/main/java/java/util/prefs/Preferences.java index 719c89ad7..8b961e4e2 100644 --- a/libcore/prefs/src/main/java/java/util/prefs/Preferences.java +++ b/libcore/prefs/src/main/java/java/util/prefs/Preferences.java @@ -30,89 +30,83 @@ import java.io.OutputStream; import java.net.MalformedURLException; import java.security.AccessController; import java.security.PrivilegedAction; +import java.util.Locale; import org.apache.harmony.prefs.internal.nls.Messages; /** - * An instance of the class {@code Preferences} represents one node in a preference tree, - * which provides a mechanism to store and access configuration data in a - * hierarchical way. Two hierarchy trees are maintained, one for system - * preferences shared by all users and the other for user preferences + * An instance of the class {@code Preferences} represents one node in a + * preference tree, which provides a mechanism to store and access configuration + * data in a hierarchical way. Two hierarchy trees are maintained, one for + * system preferences shared by all users and the other for user preferences * specific to the user. {@code Preferences} hierarchy trees and data are stored * in an implementation-dependent back-end. *

- * Every node has one name and one unique absolute path following the same - * notational conventions as directories in a file system. The root node's - * name is "", and other node name strings cannot contain the slash character - * and cannot be empty. The root node's absolute path is "/", and all other - * nodes' absolute paths are constructed in the standard way: <parent's absolute - * path> + "/" + <node's name>. Since the set of nodes forms a tree with - * the root node at its base, all absolute paths start with the slash character. - * Every node has one relative path to each of its ancestors. The relative path - * doesn't start with slash: it equals the node's absolute path with leading - * substring removed corresponding to the ancestor's absolute path and a slash. - *

+ * Every node has one name and one unique absolute path following the same + * notational conventions as directories in a file system. The root node's + * name is "", and other node name strings cannot contain the slash character + * and cannot be empty. The root node's absolute path is "/", and all other + * nodes' absolute paths are constructed in the standard way: <parent's + * absolute path> + "/" + <node's name>. Since the set of nodes forms a + * tree with the root node at its base, all absolute paths start with the slash + * character. Every node has one relative path to each of its ancestors. The + * relative path doesn't start with slash: it equals the node's absolute path + * with leading substring removed corresponding to the ancestor's absolute path + * and a slash. *

- * Modification to preferences data may be asynchronous, which means that - * preference update method calls may return immediately instead of blocking. - * The {@code flush()} and {@code sync()} methods force the back-end to - * synchronously perform all pending updates, but the implementation is - * permitted to perform the modifications on the underlying back-end data - * at any time between the moment the request is made and the moment the - * {@code flush()} or {@code sync()} method returns. - * Please note that if JVM exit normally, the implementation must assure all - * modifications are persisted implicitly. - *

+ * Modification to preferences data may be asynchronous, which means that + * preference update method calls may return immediately instead of blocking. + * The {@code flush()} and {@code sync()} methods force the back-end to + * synchronously perform all pending updates, but the implementation is + * permitted to perform the modifications on the underlying back-end data + * at any time between the moment the request is made and the moment the + * {@code flush()} or {@code sync()} method returns. Please note that if the JVM + * exits normally, the implementation must assure all modifications are + * persisted implicitly. *

- * When invoking a method that retrieves preferences, the user must provide - * a default value. The default value is returned when the preferences cannot - * be found or the back-end is unavailable. Some other methods will throw + * When invoking a method that retrieves preferences, the user must provide + * a default value. The default value is returned when the preferences cannot + * be found or the back-end is unavailable. Some other methods will throw * {@code BackingStoreException} when the back-end is unavailable. *

*

- * Preferences can be exported to and imported from an XML files. + * Preferences can be exported to and imported from an XML files. These + * documents must have an XML DOCTYPE declaration: + *

{@code
+ * 
+ * }
+ * This system URI is not really accessed by network, it is only a + * identification string. Visit the DTD location to see the actual format + * permitted. *

* There must be a concrete {@code PreferencesFactory} type for every concrete - * {@code Preferences} type developed. Every J2SE implementation must provide a default - * implementation for every supported platform, and must also provide a means of - * replacing the default implementation. This implementation uses the system property - * {@code java.util.prefs.PreferencesFactory} to detemine which preferences - * implementation to use. - *

+ * {@code Preferences} type developed. Every J2SE implementation must provide a + * default implementation for every supported platform, and must also provide a + * means of replacing the default implementation. This implementation uses the + * system property {@code java.util.prefs.PreferencesFactory} to detemine which + * preferences implementation to use. *

- * The methods of this class are thread-safe. If multiple JVMs are using the same - * back-end concurrently, the back-end won't be corrupted, but no other + * The methods of this class are thread-safe. If multiple JVMs are using the + * same back-end concurrently, the back-end won't be corrupted, but no other * behavior guarantees are made. - *

+ * + * @see PreferencesFactory * - * @since Android 1.0 + * @since 1.4 */ public abstract class Preferences { - - /* - * --------------------------------------------------------- - * Class fields - * --------------------------------------------------------- - */ - /** * Maximum size in characters allowed for a preferences key. - * - * @since Android 1.0 */ public static final int MAX_KEY_LENGTH = 80; - + /** * Maximum size in characters allowed for a preferences name. - * - * @since Android 1.0 */ public static final int MAX_NAME_LENGTH = 80; - + /** * Maximum size in characters allowed for a preferences value. - * - * @since Android 1.0 */ public static final int MAX_VALUE_LENGTH = 8192; @@ -137,36 +131,53 @@ public abstract class Preferences { //permission private static final RuntimePermission PREFS_PERM = new RuntimePermission("preferences"); //$NON-NLS-1$ - + //factory used to get user/system prefs root private static final PreferencesFactory factory; - - /** - * --------------------------------------------------------- - * Class initializer - * --------------------------------------------------------- - */ - static{ + + // BEGIN android-removed + // // default provider factory name for Windows + // private static final String DEFAULT_FACTORY_NAME_WIN = "java.util.prefs.RegistryPreferencesFactoryImpl"; //$NON-NLS-1$ + // + // // default provider factory name for Unix + // private static final String DEFAULT_FACTORY_NAME_UNIX = "java.util.prefs.FilePreferencesFactoryImpl"; //$NON-NLS-1$ + // END android-removed + + static { String factoryClassName = AccessController.doPrivileged(new PrivilegedAction() { public String run() { return System.getProperty("java.util.prefs.PreferencesFactory"); //$NON-NLS-1$ } }); // BEGIN android-removed - // if(factoryClassName != null) { - // try { - // ClassLoader loader = Thread.currentThread().getContextClassLoader(); - // if(loader == null){ - // loader = ClassLoader.getSystemClassLoader(); + // // set default provider + // if (factoryClassName == null) { + // String osName = AccessController.doPrivileged(new PrivilegedAction() { + // public String run() { + // return System.getProperty("os.name"); //$NON-NLS-1$ + // } + // }); + // + // // only comparing ASCII, so assume english locale + // osName = (osName == null ? null : osName.toLowerCase(Locale.ENGLISH)); + // + // if (osName != null && osName.startsWith("windows")) { + // factoryClassName = DEFAULT_FACTORY_NAME_WIN; + // } else { + // factoryClassName = DEFAULT_FACTORY_NAME_UNIX; + // } // } - // Class factoryClass = loader.loadClass(factoryClassName); - // factory = (PreferencesFactory) factoryClass.newInstance(); + // try { + // ClassLoader loader = Thread.currentThread().getContextClassLoader(); + // if(loader == null){ + // loader = ClassLoader.getSystemClassLoader(); + // } + // Class factoryClass = loader.loadClass(factoryClassName); + // factory = (PreferencesFactory) factoryClass.newInstance(); // } catch (Exception e) { - // // prefs.10=Cannot initiate PreferencesFactory: {0}. Caused by {1} - // throw new InternalError(Messages.getString("prefs.10", factoryClassName, e)); //$NON-NLS-1$ + // // prefs.10=Cannot initiate PreferencesFactory: {0}. Caused by {1} + // throw new InternalError(Messages.getString("prefs.10", factoryClassName, e)); //$NON-NLS-1$ // } - // } - // END android-removed // BEGIN android-added ClassLoader loader = Thread.currentThread().getContextClassLoader(); if (loader == null) { @@ -182,10 +193,8 @@ public abstract class Preferences { try { InputStream is = en.nextElement().openStream(); // Read each line for charset provider class names - // BEGIN android-modified reader = new BufferedReader(new InputStreamReader(is, CONFIGURATION_FILE_ENCODING), 8192); - // END android-modified factoryClassName = reader.readLine(); commentIndex = factoryClassName.indexOf(CONFIGURATION_FILE_COMMENT); if (commentIndex > 0) { @@ -215,42 +224,27 @@ public abstract class Preferences { factory = (PreferencesFactory)c.newInstance(); } catch (Exception e) { // prefs.10=Cannot initiate PreferencesFactory: {0}. Caused by {1} - throw new InternalError(Messages.getString("prefs.10", factoryClassName, e)); //$NON-NLS-1$ + throw new InternalError(Messages.getString("prefs.10", factoryClassName, e)); //$NON-NLS-1$ } // END android-added } - - /* - * --------------------------------------------------------- - * Constructors - * --------------------------------------------------------- - */ - + /** * Default constructor, for use by subclasses only. - * - * @since Android 1.0 */ protected Preferences() { super(); } - - /* - * --------------------------------------------------------- - * Methods - * --------------------------------------------------------- - */ - + /** * Gets the absolute path string of this preference node. * * @return the preference node's absolute path string. - * @since Android 1.0 */ public abstract String absolutePath(); - + /** - * Returns the names of all children of this node or an empty string if this + * Returns the names of all children of this node or an empty array if this * node has no children. * * @return the names of all children of this node. @@ -259,10 +253,9 @@ public abstract class Preferences { * failure. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ public abstract String[] childrenNames() throws BackingStoreException; - + /** * Removes all preferences of this node. * @@ -271,13 +264,12 @@ public abstract class Preferences { * failure. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ public abstract void clear() throws BackingStoreException; - + /** - * Exports all of the preferences of this node to a XML document using the given - * output stream. + * Exports all of the preferences of this node to a XML document using the + * given output stream. *

* This XML document uses the UTF-8 encoding and is written according to the * DTD in its DOCTYPE declaration, which is the following: @@ -286,7 +278,8 @@ public abstract class Preferences { * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> * * - * Please note that (unlike the methods of this class that don't concern serialization), this call is not thread-safe. + * Please note that (unlike the methods of this class that don't concern + * serialization), this call is not thread-safe. *

* * @param ostream @@ -298,13 +291,12 @@ public abstract class Preferences { * failure. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ - public abstract void exportNode (OutputStream ostream) throws IOException, BackingStoreException; - + public abstract void exportNode(OutputStream ostream) throws IOException, BackingStoreException; + /** - * Exports all of the preferences of this node and all its descendants to a XML - * document using the given output stream. + * Exports all of the preferences of this node and all its descendants to a + * XML document using the given output stream. *

* This XML document uses the UTF-8 encoding and is written according to the * DTD in its DOCTYPE declaration, which is the following: @@ -313,7 +305,8 @@ public abstract class Preferences { * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd"> * * - * Please note that (unlike the methods of this class that don't concern serialization), this call is not thread-safe. + * Please note that (unlike the methods of this class that don't concern + * serialization), this call is not thread-safe. *

* * @param ostream @@ -325,12 +318,12 @@ public abstract class Preferences { * failure. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ - public abstract void exportSubtree (OutputStream ostream) throws IOException, BackingStoreException; - + public abstract void exportSubtree(OutputStream ostream) throws IOException, + BackingStoreException; + /** - * Forces all pending updates to this node and its descendants to be + * Forces all pending updates to this node and its descendants to be * persisted in the backing store. *

* If this node has been removed, the invocation of this method only flushes @@ -340,13 +333,12 @@ public abstract class Preferences { * @throws BackingStoreException * if the backing store is unavailable or causes an operation * failure. - * @since Android 1.0 */ public abstract void flush() throws BackingStoreException; - + /** - * Gets the {@code String} value mapped to the given key or its default value if no - * value is mapped or no backing store is available. + * Gets the {@code String} value mapped to the given key or its default + * value if no value is mapped or no backing store is available. *

* Some implementations may store default values in backing stores. In this * case, if there is no value mapped to the given key, the stored default @@ -363,16 +355,16 @@ public abstract class Preferences { * if this node has been removed. * @throws NullPointerException * if the parameter {@code key} is {@code null}. - * @since Android 1.0 */ - public abstract String get (String key, String deflt); - + public abstract String get(String key, String deflt); + /** - * Gets the {@code boolean} value mapped to the given key or its default value if no - * value is mapped, if the backing store is unavailable, or if the value is invalid. + * Gets the {@code boolean} value mapped to the given key or its default + * value if no value is mapped, if the backing store is unavailable, or if + * the value is invalid. *

- * The only valid values are the {@code String} "true", which represents {@code true} and - * "false", which represents {@code false}, ignoring case. + * The only valid values are the {@code String} "true", which represents + * {@code true} and "false", which represents {@code false}, ignoring case. *

*

* Some implementations may store default values in backing stores. In this @@ -384,25 +376,24 @@ public abstract class Preferences { * the preference key. * @param deflt * the default value, which will be returned if no value is - * mapped to the given key, if the backing store is unavailable, or if the - * value is invalid. + * mapped to the given key, if the backing store is unavailable, + * or if the value is invalid. * @return the boolean value mapped to the given key. * @throws IllegalStateException * if this node has been removed. * @throws NullPointerException * if the parameter {@code key} is {@code null}. - * @since Android 1.0 */ - public abstract boolean getBoolean (String key, boolean deflt); - + public abstract boolean getBoolean(String key, boolean deflt); + /** - * Gets the {@code byte} array value mapped to the given key or its default value if - * no value is mapped, if the backing store is unavailable, or if the value is an - * invalid string. + * Gets the {@code byte} array value mapped to the given key or its default + * value if no value is mapped, if the backing store is unavailable, or if + * the value is an invalid string. *

- * To be valid, the value string must be Base64-encoded binary data. The Base64 encoding - * is as defined in RFC - * 2045, section 6.8. + * To be valid, the value string must be Base64-encoded binary data. The + * Base64 encoding is as defined in RFC 2045, section 6.8. *

*

* Some implementations may store default values in backing stores. In this @@ -414,25 +405,24 @@ public abstract class Preferences { * the preference key. * @param deflt * the default value, which will be returned if no value is - * mapped to the given key, if the backing store is unavailable, or if the - * value is invalid. + * mapped to the given key, if the backing store is unavailable, + * or if the value is invalid. * @return the byte array value mapped to the given key. * @throws IllegalStateException * if this node has been removed. * @throws NullPointerException * if the parameter {@code key} is {@code null}. - * @since Android 1.0 */ - public abstract byte[] getByteArray (String key, byte[] deflt); - + public abstract byte[] getByteArray(String key, byte[] deflt); + /** - * Gets the {@code double} value mapped to the given key or its default value if no - * value is mapped, if the backing store is unavailable, or if the value is an invalid - * string. + * Gets the {@code double} value mapped to the given key or its default + * value if no value is mapped, if the backing store is unavailable, or if + * the value is an invalid string. *

- * To be valid, the value string must be a string that can be converted to a {@code double} by - * {@link Double#parseDouble(String) Double.parseDouble(String)}. - *

+ * To be valid, the value string must be a string that can be converted to a + * {@code double} by {@link Double#parseDouble(String) + * Double.parseDouble(String)}. *

* Some implementations may store default values in backing stores. In this * case, if there is no value mapped to the given key, the stored default @@ -450,17 +440,17 @@ public abstract class Preferences { * if this node has been removed. * @throws NullPointerException * if the parameter {@code key} is {@code null}. - * @since Android 1.0 */ - public abstract double getDouble (String key, double deflt); - + public abstract double getDouble(String key, double deflt); + /** - * Gets the {@code float} value mapped to the given key or its default value if no - * value is mapped, if the backing store is unavailable, or if the value is an invalid - * string. + * Gets the {@code float} value mapped to the given key or its default value + * if no value is mapped, if the backing store is unavailable, or if the + * value is an invalid string. *

- * To be valid, the value string must be a string that can be converted to a {@code float} by - * {@link Float#parseFloat(String) Float.parseFloat(String)}. + * To be valid, the value string must be a string that can be converted to a + * {@code float} by {@link Float#parseFloat(String) + * Float.parseFloat(String)}. *

*

* Some implementations may store default values in backing stores. In this @@ -479,17 +469,17 @@ public abstract class Preferences { * if this node has been removed. * @throws NullPointerException * if the parameter {@code key} is {@code null}. - * @since Android 1.0 */ - public abstract float getFloat (String key, float deflt); - + public abstract float getFloat(String key, float deflt); + /** - * Gets the {@code int} value mapped to the given key or its default value if no - * value is mapped, if the backing store is unavailable, or if the value is an invalid - * string. + * Gets the {@code int} value mapped to the given key or its default value + * if no value is mapped, if the backing store is unavailable, or if the + * value is an invalid string. *

- * To be valid, the value string must be a string that can be converted to an {@code int} by - * {@link Integer#parseInt(String) Integer.parseInt(String)}. + * To be valid, the value string must be a string that can be converted to + * an {@code int} by {@link Integer#parseInt(String) + * Integer.parseInt(String)}. *

*

* Some implementations may store default values in backing stores. In this @@ -501,24 +491,23 @@ public abstract class Preferences { * the preference key. * @param deflt * the default value, which will be returned if no value is - * mapped to the given key, if the backing store is unavailable, or if the - * value is invalid. + * mapped to the given key, if the backing store is unavailable, + * or if the value is invalid. * @return the integer value mapped to the given key. * @throws IllegalStateException * if this node has been removed. * @throws NullPointerException * if the parameter {@code key} is {@code null}. - * @since Android 1.0 */ - public abstract int getInt (String key, int deflt); - + public abstract int getInt(String key, int deflt); + /** - * Gets the {@code long} value mapped to the given key or its default value if no - * value is mapped, if the backing store is unavailable, or if the value is an invalid - * string. + * Gets the {@code long} value mapped to the given key or its default value + * if no value is mapped, if the backing store is unavailable, or if the + * value is an invalid string. *

- * To be valid, the value string must be a string that can be converted to a {@code long} by - * {@link Long#parseLong(String) Long.parseLong(String)}. + * To be valid, the value string must be a string that can be converted to a + * {@code long} by {@link Long#parseLong(String) Long.parseLong(String)}. *

*

* Some implementations may store default values in backing stores. In this @@ -530,29 +519,29 @@ public abstract class Preferences { * the preference key. * @param deflt * the default value, which will be returned if no value is - * mapped to the given key, if the backing store is unavailable, or if the - * value is invalid. + * mapped to the given key, if the backing store is unavailable, + * or if the value is invalid. * @return the long value mapped to the given key. * @throws IllegalStateException * if this node has been removed. * @throws NullPointerException * if the parameter {@code key} is {@code null}. - * @since Android 1.0 */ - public abstract long getLong (String key, long deflt); - + public abstract long getLong(String key, long deflt); + /** * Imports all the preferences from an XML document using the given input * stream. *

- * This XML document uses the UTF-8 encoding and must be written according to the - * DTD in its DOCTYPE declaration, which must be the following: + * This XML document uses the UTF-8 encoding and must be written according + * to the DTD in its DOCTYPE declaration, which must be the following: * *

      * <!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
      * 
* - * Please note that (unlike the methods of this class that don't concern serialization), this call is not thread-safe. + * Please note that (unlike the methods of this class that don't concern + * serialization), this call is not thread-safe. *

* * @param istream @@ -565,7 +554,6 @@ public abstract class Preferences { * @throws SecurityException * if {@code RuntimePermission("preferences")} is denied by a * SecurityManager. - * @since Android 1.0 */ public static void importPreferences (InputStream istream) throws InvalidPreferencesFormatException, IOException { checkSecurity(); @@ -575,16 +563,15 @@ public abstract class Preferences { } XMLParser.importPrefs(istream); } - + /** * Returns whether this is a user preference node. * * @return {@code true}, if this is a user preference node, {@code false} if * this is a system preference node. - * @since Android 1.0 */ public abstract boolean isUserNode(); - + /** * Returns all preference keys stored in this node or an empty array if no * key was found. @@ -595,18 +582,16 @@ public abstract class Preferences { * failure. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ public abstract String[] keys() throws BackingStoreException; - + /** * Returns the name of this node. * * @return the name of this node. - * @since Android 1.0 */ public abstract String name(); - + /** * Returns the preference node with the given path name. The path name can * be relative or absolute. The requested node and its ancestors will @@ -625,10 +610,9 @@ public abstract class Preferences { * if the path name is invalid. * @throws NullPointerException * if the given path is {@code null}. - * @since Android 1.0 */ - public abstract Preferences node (String path); - + public abstract Preferences node(String path); + /** * Returns whether the preference node with the given path name exists. The * path is treated as relative to this node if it doesn't start with a slash, @@ -653,10 +637,9 @@ public abstract class Preferences { * @throws BackingStoreException * if the backing store is unavailable or causes an operation * failure. - * @since Android 1.0 */ - public abstract boolean nodeExists (String path) throws BackingStoreException; - + public abstract boolean nodeExists(String path) throws BackingStoreException; + /** * Returns the parent preference node of this node or {@code null} if this * node is the root node. @@ -664,10 +647,9 @@ public abstract class Preferences { * @return the parent preference node of this node. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ public abstract Preferences parent(); - + /** * Adds a new preference to this node using the given key and value or * updates the value if a preference with the given key already exists. @@ -684,14 +666,13 @@ public abstract class Preferences { * MAX_VALUE_LENGTH}. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ - public abstract void put (String key, String value); - + public abstract void put(String key, String value); + /** - * Adds a new preference with a {@code boolean} value to this node using the given - * key and value or updates the value if a preference with the given key - * already exists. + * Adds a new preference with a {@code boolean} value to this node using the + * given key and value or updates the value if a preference with the given + * key already exists. * * @param key * the preference key to be added or updated. @@ -704,10 +685,9 @@ public abstract class Preferences { * MAX_KEY_LENGTH}. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ - public abstract void putBoolean (String key, boolean value); - + public abstract void putBoolean(String key, boolean value); + /** * Adds a new preference to this node using the given key and the string * form of the given value or updates the value if a preference with the @@ -730,10 +710,9 @@ public abstract class Preferences { * quarters of {@code MAX_KEY_LENGTH}. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ - public abstract void putByteArray (String key, byte[] value); - + public abstract void putByteArray(String key, byte[] value); + /** * Adds a new preference to this node using the given key and {@code double} * value or updates the value if a preference with the @@ -754,12 +733,11 @@ public abstract class Preferences { * MAX_KEY_LENGTH}. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ - public abstract void putDouble (String key, double value); - + public abstract void putDouble(String key, double value); + /** - * Adds a new preference to this node using the given key and {@code float} + * Adds a new preference to this node using the given key and {@code float} * value or updates the value if a preference with the * given key already exists. *

@@ -778,12 +756,11 @@ public abstract class Preferences { * MAX_KEY_LENGTH}. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ - public abstract void putFloat (String key, float value); - + public abstract void putFloat(String key, float value); + /** - * Adds a new preference to this node using the given key and {@code int} + * Adds a new preference to this node using the given key and {@code int} * value or updates the value if a preference with the * given key already exists. *

@@ -802,12 +779,11 @@ public abstract class Preferences { * MAX_KEY_LENGTH}. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ - public abstract void putInt (String key, int value); - + public abstract void putInt(String key, int value); + /** - * Adds a new preference to this node using the given key and {@code long} + * Adds a new preference to this node using the given key and {@code long} * value or updates the value if a preference with the * given key already exists. *

@@ -826,9 +802,8 @@ public abstract class Preferences { * MAX_KEY_LENGTH}. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ - public abstract void putLong (String key, long value); + public abstract void putLong(String key, long value); /** * Removes the preference mapped to the given key from this node. @@ -839,13 +814,12 @@ public abstract class Preferences { * if the given key is {@code null}. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ - public abstract void remove (String key); - + public abstract void remove(String key); + /** - * Removes this preference node with all its descendants. The removal - * won't necessarily be persisted until the method {@code flush()} is invoked. + * Removes this preference node with all its descendants. The removal won't + * necessarily be persisted until the method {@code flush()} is invoked. * * @throws BackingStoreException * if the backing store is unavailable or causes an operation @@ -854,14 +828,13 @@ public abstract class Preferences { * if this node has been removed. * @throws UnsupportedOperationException * if this is a root node. - * @since Android 1.0 */ public abstract void removeNode() throws BackingStoreException; - + /** - * Registers a {@code NodeChangeListener} instance for this node, which will handle - * {@code NodeChangeEvent}s. {@code NodeChangeEvent}s will be fired when a child node has - * been added to or removed from this node. + * Registers a {@code NodeChangeListener} instance for this node, which will + * handle {@code NodeChangeEvent}s. {@code NodeChangeEvent}s will be fired + * when a child node has been added to or removed from this node. * * @param ncl * the listener to be registered. @@ -869,25 +842,24 @@ public abstract class Preferences { * if the given listener is {@code null}. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ - public abstract void addNodeChangeListener (NodeChangeListener ncl); - + public abstract void addNodeChangeListener(NodeChangeListener ncl); + /** - * Registers a {@code PreferenceChangeListener} instance for this node, which will - * handle {@code PreferenceChangeEvent}s. {@code PreferenceChangeEvent}s will be fired when - * a preference has been added to, removed from, or updated for this node. - * + * Registers a {@code PreferenceChangeListener} instance for this node, + * which will handle {@code PreferenceChangeEvent}s. {@code + * PreferenceChangeEvent}s will be fired when a preference has been added + * to, removed from, or updated for this node. + * * @param pcl * the listener to be registered. * @throws NullPointerException * if the given listener is {@code null}. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ public abstract void addPreferenceChangeListener (PreferenceChangeListener pcl); - + /** * Removes the given {@code NodeChangeListener} instance from this node. * @@ -897,12 +869,12 @@ public abstract class Preferences { * if the given listener is {@code null}. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ public abstract void removeNodeChangeListener (NodeChangeListener ncl); - + /** - * Removes the given {@code PreferenceChangeListener} instance from this node. + * Removes the given {@code PreferenceChangeListener} instance from this + * node. * * @param pcl * the listener to be removed. @@ -910,36 +882,34 @@ public abstract class Preferences { * if the given listener is {@code null}. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ public abstract void removePreferenceChangeListener (PreferenceChangeListener pcl); - + /** * Synchronizes the data of this preference node and its descendants with - * the back-end preference store. Any changes found in the back-end data should be reflected - * in this node and its descendants, and at the same time any local changes to this node and - * descendants should be persisted. + * the back-end preference store. Any changes found in the back-end data + * should be reflected in this node and its descendants, and at the same + * time any local changes to this node and descendants should be persisted. * * @throws BackingStoreException * if the backing store is unavailable or causes an operation * failure. * @throws IllegalStateException * if this node has been removed. - * @since Android 1.0 */ public abstract void sync() throws BackingStoreException; - + /** * Returns the system preference node for the package of the given class. * The absolute path of the returned node is one slash followed by the given * class's full package name, replacing each period character ('.') with - * a slash. For example, the absolute path of the preference associated with + * a slash. For example, the absolute path of the preference associated with * the class Object would be "/java/lang". As a special case, the unnamed * package is associated with a preference node "/<unnamed>". This * method will create the node and its ancestors as needed. Any nodes created - * by this method won't necessarily be persisted until the method {@code flush()} is - * invoked. - * + * by this method won't necessarily be persisted until the method {@code + * flush()} is invoked. + * * @param c * the given class. * @return the system preference node for the package of the given class. @@ -948,13 +918,12 @@ public abstract class Preferences { * @throws SecurityException * if the {@code RuntimePermission("preferences")} is denied by * a SecurityManager. - * @since Android 1.0 */ public static Preferences systemNodeForPackage (Class c) { checkSecurity(); return factory.systemRoot().node(getNodeName(c)); } - + /** * Returns the root node of the system preference hierarchy. * @@ -962,33 +931,32 @@ public abstract class Preferences { * @throws SecurityException * if the {@code RuntimePermission("preferences")} is denied by * a SecurityManager. - * @since Android 1.0 */ public static Preferences systemRoot() { checkSecurity(); return factory.systemRoot(); } - + //check the RuntimePermission("preferences") private static void checkSecurity() { SecurityManager manager = System.getSecurityManager(); if(null != manager){ manager.checkPermission(PREFS_PERM); } - + } /** * Returns the user preference node for the package of the given class. * The absolute path of the returned node is one slash followed by the given * class's full package name, replacing each period character ('.') with - * a slash. For example, the absolute path of the preference associated with + * a slash. For example, the absolute path of the preference associated with * the class Object would be "/java/lang". As a special case, the unnamed * package is associated with a preference node "/<unnamed>". This * method will create the node and its ancestors as needed. Any nodes created - * by this method won't necessarily be persisted until the method {@code flush()} is - * invoked. - * + * by this method won't necessarily be persisted until the method {@code + * flush()} is invoked. + * * @param c * the given class. * @return the user preference node for the package of the given class. @@ -997,13 +965,12 @@ public abstract class Preferences { * @throws SecurityException * if the {@code RuntimePermission("preferences")} is denied by * a SecurityManager. - * @since Android 1.0 */ public static Preferences userNodeForPackage (Class c) { checkSecurity(); return factory.userRoot().node(getNodeName(c)); } - + //parse node's absolute path from class instance private static String getNodeName(Class c){ Package p = c.getPackage(); @@ -1020,7 +987,6 @@ public abstract class Preferences { * @throws SecurityException * if the {@code RuntimePermission("preferences")} is denied by * a SecurityManager. - * @since Android 1.0 */ public static Preferences userRoot() { checkSecurity(); @@ -1032,7 +998,6 @@ public abstract class Preferences { * Preference Node: " followed by this node's absolute path. * * @return the string representation of this node. - * @since Android 1.0 */ @Override public abstract String toString(); diff --git a/libcore/prefs/src/main/java/java/util/prefs/PreferencesFactory.java b/libcore/prefs/src/main/java/java/util/prefs/PreferencesFactory.java index e56dd95d2..3ac13e415 100644 --- a/libcore/prefs/src/main/java/java/util/prefs/PreferencesFactory.java +++ b/libcore/prefs/src/main/java/java/util/prefs/PreferencesFactory.java @@ -14,36 +14,30 @@ * limitations under the License. */ - package java.util.prefs; /** * This interface is used by the {@link Preferences} class as factory class to - * create {@code Preferences} instances. This interface can be implemented and installed - * to replace the default preferences implementation. + * create {@code Preferences} instances. This interface can be implemented and + * installed to replace the default preferences implementation. + * + * @see java.util.prefs.Preferences * - * @since Android 1.0 + * @since 1.4 */ public interface PreferencesFactory { - /** * Returns the root node of the preferences hierarchy for the calling user * context. * * @return the user preferences hierarchy root node. - * @since Android 1.0 */ Preferences userRoot(); - + /** * Returns the root node of the system preferences hierarchy. * * @return the system preferences hierarchy root node. - * @since Android 1.0 */ Preferences systemRoot(); } - - - - diff --git a/libcore/prefs/src/main/java/java/util/prefs/XMLParser.java b/libcore/prefs/src/main/java/java/util/prefs/XMLParser.java index 2edfc7126..c5a234c51 100644 --- a/libcore/prefs/src/main/java/java/util/prefs/XMLParser.java +++ b/libcore/prefs/src/main/java/java/util/prefs/XMLParser.java @@ -14,7 +14,6 @@ * limitations under the License. */ - package java.util.prefs; import java.io.BufferedInputStream; @@ -27,6 +26,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.StringReader; +import java.io.Writer; import java.nio.channels.FileChannel; import java.nio.channels.FileLock; import java.security.AccessController; @@ -64,9 +64,7 @@ import org.w3c.dom.Node; // END android-added /** - * Utility class for importing and exporting {@code Preferences} data from an XML file. - * - * @since Android 1.0 + * Utility class for the Preferences import/export from XML file. */ class XMLParser { @@ -79,15 +77,15 @@ class XMLParser { * Constant - the DTD string */ static final String PREFS_DTD = "" //$NON-NLS-1$ - + " " //$NON-NLS-1$ - + " " //$NON-NLS-1$ - + " " //$NON-NLS-1$ - + " " //$NON-NLS-1$ - + " " //$NON-NLS-1$ - + " " //$NON-NLS-1$ - + " " //$NON-NLS-1$ - + " " //$NON-NLS-1$ - + " "; //$NON-NLS-1$ + + " " //$NON-NLS-1$ + + " " //$NON-NLS-1$ + + " " //$NON-NLS-1$ + + " " //$NON-NLS-1$ + + " " //$NON-NLS-1$ + + " " //$NON-NLS-1$ + + " " //$NON-NLS-1$ + + " " //$NON-NLS-1$ + + " "; //$NON-NLS-1$ /* * Constant - the specified header @@ -103,24 +101,24 @@ class XMLParser { * empty string array constant */ private static final String[] EMPTY_SARRAY = new String[0]; - + /* - * Constant - used by FilePreferencesImpl, which is default implementation of Linux platform + * Constant - used by FilePreferencesImpl, which is default implementation of Linux platform */ private static final String FILE_PREFS = ""; //$NON-NLS-1$ /* * Constant - specify the DTD version */ - private static final float XML_VERSION = 1.0f; - + private static final float XML_VERSION = 1.0f; + /* * DOM builder */ private static final DocumentBuilder builder; /* - * specify the indent level + * specify the indent level */ private static int indent = -1; @@ -139,7 +137,7 @@ class XMLParser { } builder.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) - throws SAXException, IOException { + throws SAXException, IOException { if (systemId.equals(PREFS_DTD_NAME)) { InputSource result = new InputSource(new StringReader( PREFS_DTD)); @@ -196,7 +194,7 @@ class XMLParser { flushEmptyElement("map", out); //$NON-NLS-1$ StringTokenizer ancestors = new StringTokenizer(prefs.absolutePath(), - "/"); //$NON-NLS-1$ + "/"); //$NON-NLS-1$ exportNode(ancestors, prefs, withSubTree, out); flushEndTag("root", out); //$NON-NLS-1$ @@ -207,7 +205,7 @@ class XMLParser { private static void exportNode(StringTokenizer ancestors, Preferences prefs, boolean withSubTree, BufferedWriter out) - throws IOException, BackingStoreException { + throws IOException, BackingStoreException { if (ancestors.hasMoreTokens()) { String name = ancestors.nextToken(); flushStartTag( @@ -226,7 +224,7 @@ class XMLParser { } private static void exportSubTree(Preferences prefs, BufferedWriter out) - throws BackingStoreException, IOException { + throws BackingStoreException, IOException { String[] names = prefs.childrenNames(); if (names.length > 0) { for (int i = 0; i < names.length; i++) { @@ -241,7 +239,7 @@ class XMLParser { } private static void exportEntries(Preferences prefs, BufferedWriter out) - throws BackingStoreException, IOException { + throws BackingStoreException, IOException { String[] keys = prefs.keys(); String[] values = new String[keys.length]; for (int i = 0; i < keys.length; i++) { @@ -267,7 +265,7 @@ class XMLParser { } private static void flushEndTag(String tagName, BufferedWriter out) - throws IOException { + throws IOException { flushIndent(indent--, out); out.write(" input = new ArrayList(); - + String[] path = string.split("/"); - + NodeList childNodes = documentElement.getChildNodes(); - + if(path[0].equals("entry") || path[0].equals("node")) { for(int i = 0; i < childNodes.getLength(); i++) { Object next = childNodes.item(i); @@ -483,21 +481,21 @@ class XMLParser { } } } - + result = new NodeSet(input.iterator()); - + return result; } // END android-added - + /*************************************************************************** * 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 + * + * @param file the XML file to be read * @return Properties instance which indicates the preferences key-value pairs */ static Properties loadFilePrefs(final File file) { @@ -506,14 +504,6 @@ class XMLParser { return loadFilePrefsImpl(file); } }); - - // try { - // //FIXME: lines below can be deleted, because it is not required to - // persistent at the very beginning - // flushFilePrefs(file, result); - // } catch (IOException e) { - // e.printStackTrace(); - // } } static Properties loadFilePrefsImpl(final File file) { @@ -524,7 +514,6 @@ class XMLParser { InputStream in = null; FileLock lock = null; try { - FileInputStream istream = new FileInputStream(file); // BEGIN android-modified in = new BufferedInputStream(istream, 8192); @@ -544,17 +533,16 @@ class XMLParser { result.setProperty(key, value); } return result; - } catch (Exception e) { - e.printStackTrace(); + } catch (IOException e) { + } catch (SAXException e) { + // BEGIN android-removed + // } catch (TransformerException e) { + // // transform shouldn't fail for xpath call + // throw new AssertionError(e); + // END android-removed } finally { - try { - lock.release(); - } catch (Exception e) {//ignore - } - try { - in.close(); - } catch (Exception e) {//ignore - } + releaseQuietly(lock); + closeQuietly(in); } } else { file.delete(); @@ -563,7 +551,7 @@ class XMLParser { } /** - * + * * @param file * @param prefs * @throws PrivilegedActionException @@ -576,7 +564,7 @@ class XMLParser { } }); } - + static void flushFilePrefsImpl(File file, Properties prefs) throws IOException { BufferedWriter out = null; FileLock lock = null; @@ -604,18 +592,35 @@ class XMLParser { } out.flush(); } finally { - try { - lock.release(); - } catch (Exception e) {//ignore - } - try { - if (null != out) { - out.close(); - } - } catch (Exception e) {//ignore - } + releaseQuietly(lock); + closeQuietly(out); } } -} + private static void releaseQuietly(FileLock lock) { + if (lock == null) { + return; + } + try { + lock.release(); + } catch (IOException e) {} + } + private static void closeQuietly(Writer out) { + if (out == null) { + return; + } + try { + out.close(); + } catch (IOException e) {} + } + + private static void closeQuietly(InputStream in) { + if (in == null) { + return; + } + try { + in.close(); + } catch (IOException e) {} + } +} diff --git a/libcore/prefs/src/main/java/org/apache/harmony/prefs/internal/nls/Messages.java b/libcore/prefs/src/main/java/org/apache/harmony/prefs/internal/nls/Messages.java index cfc7236c0..aaf5d8da5 100644 --- a/libcore/prefs/src/main/java/org/apache/harmony/prefs/internal/nls/Messages.java +++ b/libcore/prefs/src/main/java/org/apache/harmony/prefs/internal/nls/Messages.java @@ -43,7 +43,7 @@ import org.apache.harmony.luni.util.MsgHelp; * is looked up, or resource bundle support is not available, the key itself * will be returned as the associated message. This means that the KEY * should a reasonable human-readable (english) string. - * + * */ public class Messages { @@ -54,7 +54,7 @@ public class Messages { /** * Retrieves a message which has no arguments. - * + * * @param msg * String the key to look up. * @return String the message for that key in the system message bundle. @@ -67,7 +67,7 @@ public class Messages { /** * Retrieves a message which takes 1 argument. - * + * * @param msg * String the key to look up. * @param arg @@ -80,7 +80,7 @@ public class Messages { /** * Retrieves a message which takes 1 integer argument. - * + * * @param msg * String the key to look up. * @param arg @@ -93,7 +93,7 @@ public class Messages { /** * Retrieves a message which takes 1 character argument. - * + * * @param msg * String the key to look up. * @param arg @@ -106,7 +106,7 @@ public class Messages { /** * Retrieves a message which takes 2 arguments. - * + * * @param msg * String the key to look up. * @param arg1 @@ -121,7 +121,7 @@ public class Messages { /** * Retrieves a message which takes several arguments. - * + * * @param msg * String the key to look up. * @param args diff --git a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/AbstractPreferencesTest.java b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/AbstractPreferencesTest.java index 418e52db9..76ef4e7f4 100644 --- a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/AbstractPreferencesTest.java +++ b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/AbstractPreferencesTest.java @@ -35,9 +35,13 @@ import java.util.prefs.PreferenceChangeEvent; import java.util.prefs.PreferenceChangeListener; import java.util.prefs.Preferences; +import tests.util.PrefsTester; + @TestTargetClass(AbstractPreferences.class) public class AbstractPreferencesTest extends TestCase { + private final PrefsTester prefsTester = new PrefsTester(); + AbstractPreferences pref; static AbstractPreferences root; @@ -51,13 +55,8 @@ public class AbstractPreferencesTest extends TestCase { protected void setUp() throws Exception { super.setUp(); + prefsTester.setUp(); - System.setProperty("user.home", System.getProperty("java.io.tmpdir")); - System.setProperty("java.home", System.getProperty("java.io.tmpdir")); - - Preferences.systemRoot().clear(); - Preferences.userRoot().clear(); - root = (AbstractPreferences) Preferences.userRoot(); parent = (AbstractPreferences) Preferences.userNodeForPackage(this.getClass()); @@ -65,11 +64,7 @@ public class AbstractPreferencesTest extends TestCase { } protected void tearDown() throws Exception { - parent.removeNode(); - Preferences.systemRoot().clear(); - Preferences.userRoot().clear(); - System.setProperty("user.home", oldUserHome); - System.setProperty("java.home", oldJavaHome); + prefsTester.tearDown(); super.tearDown(); } @@ -855,6 +850,31 @@ public class AbstractPreferencesTest extends TestCase { } @TestTargetNew( + level = TestLevel.PARTIAL_COMPLETE, + notes = "", + method = "nodeExists", + args = {String.class} + ) + public void test_nodeExists() throws BackingStoreException { + AbstractPreferences test = (AbstractPreferences) Preferences.userRoot() + .node("test"); + try { + test.nodeExists(null); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // Expected + } + + test.removeNode(); + try { + test.nodeExists(null); + fail("should throw NullPointerException"); + } catch (NullPointerException e) { + // Expected + } + } + + @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "parent", diff --git a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/FilePreferencesImplTest.java b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/FilePreferencesImplTest.java index 0c61e7546..0cb197503 100644 --- a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/FilePreferencesImplTest.java +++ b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/FilePreferencesImplTest.java @@ -28,28 +28,22 @@ import java.util.prefs.BackingStoreException; import java.util.prefs.Preferences; import junit.framework.TestCase; +import tests.util.PrefsTester; @TestTargetClass(java.util.prefs.Preferences.class) public class FilePreferencesImplTest extends TestCase { - String oldUserHome = System.getProperty("user.home"); - String oldJavaHome = System.getProperty("java.home"); + private final PrefsTester prefsTester = new PrefsTester(); + @Override protected void setUp() throws Exception { super.setUp(); - - System.setProperty("user.home", System.getProperty("java.io.tmpdir")); - System.setProperty("java.home", System.getProperty("java.io.tmpdir")); - Preferences.systemRoot().clear(); - Preferences.userRoot().clear(); + prefsTester.setUp(); } + @Override protected void tearDown() throws Exception { - Preferences.systemRoot().clear(); - Preferences.userRoot().clear(); - System.setProperty("user.home", oldUserHome); - System.setProperty("java.home", oldJavaHome); - + prefsTester.tearDown(); super.tearDown(); } @@ -313,6 +307,7 @@ public class FilePreferencesImplTest extends TestCase { System.setSecurityManager(dflt); } + @Override public void checkPermission(Permission perm) { if (perm instanceof FilePermission) { throw new SecurityException(); @@ -321,6 +316,7 @@ public class FilePreferencesImplTest extends TestCase { } } + @Override public void checkPermission(Permission perm, Object ctx) { if (perm instanceof FilePermission) { System.out.println(perm.getActions()); diff --git a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/InvalidPreferencesFormatExceptionTest.java b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/InvalidPreferencesFormatExceptionTest.java index 28c953ef5..c7ff946f4 100644 --- a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/InvalidPreferencesFormatExceptionTest.java +++ b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/InvalidPreferencesFormatExceptionTest.java @@ -44,7 +44,7 @@ public class InvalidPreferencesFormatExceptionTest extends TestCase { ) public void testInvalidPreferencesFormatExceptionString() { InvalidPreferencesFormatException e = new InvalidPreferencesFormatException( - "msg"); + "msg"); assertNull(e.getCause()); assertEquals("msg", e.getMessage()); } @@ -99,7 +99,7 @@ public class InvalidPreferencesFormatExceptionTest extends TestCase { public void testSerializationSelf() throws Exception { SerializationTest.verifySelf(new InvalidPreferencesFormatException( - "msg")); + "msg")); } /** diff --git a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/MockAbstractPreferences.java b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/MockAbstractPreferences.java index 18209542c..b14922586 100644 --- a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/MockAbstractPreferences.java +++ b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/MockAbstractPreferences.java @@ -84,6 +84,7 @@ public class MockAbstractPreferences extends AbstractPreferences { return lock; } + @Override public String[] childrenNamesSpi() throws BackingStoreException { checkException(); if (result == returnNull) @@ -108,6 +109,7 @@ public class MockAbstractPreferences extends AbstractPreferences { return childSpi(name); } + @Override public AbstractPreferences childSpi(String name) { try { checkException(); @@ -123,11 +125,13 @@ public class MockAbstractPreferences extends AbstractPreferences { return r; } + @Override public void flushSpi() throws BackingStoreException { checkException(); flushedTimes++; } + @Override public String getSpi(String key) { try { checkException(); @@ -139,6 +143,7 @@ public class MockAbstractPreferences extends AbstractPreferences { return result == returnNull ? null : attr.getProperty(key); } + @Override public String[] keysSpi() throws BackingStoreException { checkException(); Set keys = attr.keySet(); @@ -147,6 +152,7 @@ public class MockAbstractPreferences extends AbstractPreferences { return result == returnNull ? null : results; } + @Override public void putSpi(String name, String value) { try { checkException(); @@ -158,11 +164,13 @@ public class MockAbstractPreferences extends AbstractPreferences { attr.put(name, value); } + @Override protected void removeNodeSpi() throws BackingStoreException { checkException(); ((MockAbstractPreferences) parent()).childs.remove(name()); } + @Override public void removeSpi(String key) { try { checkException(); @@ -174,6 +182,7 @@ public class MockAbstractPreferences extends AbstractPreferences { attr.remove(key); } + @Override public void syncSpi() throws BackingStoreException { checkException(); syncTimes++; diff --git a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/MockSecurityManager.java b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/MockSecurityManager.java index e5a0bfdcd..d6047fbb3 100644 --- a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/MockSecurityManager.java +++ b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/MockSecurityManager.java @@ -39,6 +39,7 @@ class MockSecurityManager extends SecurityManager { System.setSecurityManager(dflt); } + @Override public void checkPermission(Permission perm) { if (perm instanceof RuntimePermission && perm.getName().equals("preferences")) { @@ -48,6 +49,7 @@ class MockSecurityManager extends SecurityManager { } } + @Override public void checkPermission(Permission perm, Object ctx) { if (perm instanceof RuntimePermission && perm.getName().equals("preferences")) { diff --git a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeEventTest.java b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeEventTest.java index eab3b14f3..1afd7557b 100644 --- a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeEventTest.java +++ b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeEventTest.java @@ -30,6 +30,7 @@ import java.util.prefs.Preferences; import junit.framework.TestCase; import org.apache.harmony.testframework.serialization.SerializationTest; +import tests.util.PrefsTester; /** * @@ -37,26 +38,17 @@ import org.apache.harmony.testframework.serialization.SerializationTest; @TestTargetClass(NodeChangeEvent.class) public class NodeChangeEventTest extends TestCase { - NodeChangeEvent event; + private final PrefsTester prefsTester = new PrefsTester(); - String oldUserHome = System.getProperty("user.home"); - String oldJavaHome = System.getProperty("java.home"); + NodeChangeEvent event; protected void setUp() throws Exception { super.setUp(); - - System.setProperty("user.home", System.getProperty("java.io.tmpdir")); - System.setProperty("java.home", System.getProperty("java.io.tmpdir")); - Preferences.systemRoot().clear(); - Preferences.userRoot().clear(); + prefsTester.setUp(); } protected void tearDown() throws Exception { - Preferences.systemRoot().clear(); - Preferences.userRoot().clear(); - System.setProperty("user.home", oldUserHome); - System.setProperty("java.home", oldJavaHome); - + prefsTester.tearDown(); super.tearDown(); } diff --git a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeListenerTest.java b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeListenerTest.java index c5e3252da..3cdb4d974 100644 --- a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeListenerTest.java +++ b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/NodeChangeListenerTest.java @@ -26,6 +26,7 @@ import java.util.prefs.NodeChangeListener; import java.util.prefs.Preferences; import junit.framework.TestCase; +import tests.util.PrefsTester; /** * @@ -33,20 +34,26 @@ import junit.framework.TestCase; @TestTargetClass(NodeChangeListener.class) public class NodeChangeListenerTest extends TestCase { + private final PrefsTester prefsTester = new PrefsTester(); + NodeChangeListener l; /* * @see TestCase#setUp() */ + @Override protected void setUp() throws Exception { super.setUp(); + prefsTester.setUp(); l = new NodeChangeListenerImpl(); } /* * @see TestCase#tearDown() */ + @Override protected void tearDown() throws Exception { + prefsTester.tearDown(); super.tearDown(); } diff --git a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferenceChangeEventTest.java b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferenceChangeEventTest.java index 4030b8918..dda1f6ccd 100644 --- a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferenceChangeEventTest.java +++ b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferenceChangeEventTest.java @@ -92,7 +92,7 @@ public class PreferenceChangeEventTest extends TestCase { ) public void testConstructor() { event = new PreferenceChangeEvent(Preferences.userRoot(), "key", - "value"); + "value"); assertEquals("key", event.getKey()); assertEquals("value", event.getNewValue()); assertSame(Preferences.userRoot(), event.getNode()); @@ -107,7 +107,7 @@ public class PreferenceChangeEventTest extends TestCase { ) public void testSerialization() throws Exception { event = new PreferenceChangeEvent(Preferences.userRoot(), "key", - "value"); + "value"); try { SerializationTest.copySerializable(event); fail("No expected NotSerializableException"); diff --git a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferenceChangeListenerTest.java b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferenceChangeListenerTest.java index e4df9c4ff..7080aa503 100644 --- a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferenceChangeListenerTest.java +++ b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferenceChangeListenerTest.java @@ -38,6 +38,7 @@ public class PreferenceChangeListenerTest extends TestCase { /* * @see TestCase#setUp() */ + @Override protected void setUp() throws Exception { super.setUp(); l = new PreferenceChangeListenerImpl(); diff --git a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesFactoryTest.java b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesFactoryTest.java index 818d5adb8..729bc05b1 100644 --- a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesFactoryTest.java +++ b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesFactoryTest.java @@ -37,6 +37,7 @@ public class PreferencesFactoryTest extends TestCase { /* * @see TestCase#setUp() */ + @Override protected void setUp() throws Exception { super.setUp(); f = new PreferencesFactoryImpl(); @@ -49,7 +50,7 @@ public class PreferencesFactoryTest extends TestCase { args = {} ) public void testUserRoot() { - f.userRoot(); + assertNull(f.userRoot()); } @TestTargetNew( @@ -59,7 +60,7 @@ public class PreferencesFactoryTest extends TestCase { args = {} ) public void testSystemRoot() { - f.systemRoot(); + assertNull(f.systemRoot()); } public static class PreferencesFactoryImpl implements PreferencesFactory { diff --git a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesTest.java b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesTest.java index 0ebf6bb0c..c9c74fd35 100644 --- a/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesTest.java +++ b/libcore/prefs/src/test/java/org/apache/harmony/prefs/tests/java/util/prefs/PreferencesTest.java @@ -39,14 +39,15 @@ import java.util.prefs.NodeChangeListener; import java.util.prefs.PreferenceChangeListener; import java.util.prefs.Preferences; +import tests.util.PrefsTester; + /** * */ @TestTargetClass(Preferences.class) public class PreferencesTest extends TestCase { - String oldUserHome = System.getProperty("user.home"); - String oldJavaHome = System.getProperty("java.home"); + private final PrefsTester prefsTester = new PrefsTester(); MockSecurityManager manager = new MockSecurityManager(); @@ -74,43 +75,24 @@ public class PreferencesTest extends TestCase { /* * @see TestCase#setUp() */ + @Override protected void setUp() throws Exception { super.setUp(); in = new ByteArrayInputStream( "" .getBytes("UTF-8")); stream = new MockInputStream(in); - - System.setProperty("user.home", System.getProperty("java.io.tmpdir")); - System.setProperty("java.home", System.getProperty("java.io.tmpdir")); - Preferences.systemRoot().clear(); - Preferences.userRoot().clear(); - - Preferences p = Preferences.userNodeForPackage(Preferences.class); - p.clear(); - try { - p.removeNode(); - } catch (BackingStoreException e) { - } + prefsTester.setUp(); } /* * @see TestCase#tearDown() */ + @Override protected void tearDown() throws Exception { - super.tearDown(); stream.close(); - Preferences p = Preferences.userNodeForPackage(Preferences.class); - p.clear(); - try { - p.removeNode(); - } catch (BackingStoreException e) { - } - - Preferences.systemRoot().clear(); - Preferences.userRoot().clear(); - System.setProperty("user.home", oldUserHome); - System.setProperty("java.home", oldJavaHome); + prefsTester.tearDown(); + super.tearDown(); } @TestTargetNew( @@ -1830,6 +1812,7 @@ public class PreferencesTest extends TestCase { wrapper = in; } + @Override public int read() throws IOException { checkException(); return wrapper.read(); @@ -1843,136 +1826,170 @@ public class PreferencesTest extends TestCase { super(); } + @Override public String absolutePath() { return null; } + @Override public String[] childrenNames() throws BackingStoreException { return null; } + @Override public void clear() throws BackingStoreException { } + @Override public void exportNode(OutputStream ostream) throws IOException, BackingStoreException { } + @Override public void exportSubtree(OutputStream ostream) throws IOException, BackingStoreException { } + @Override public void flush() throws BackingStoreException { } + @Override public String get(String key, String deflt) { return null; } + @Override public boolean getBoolean(String key, boolean deflt) { return false; } + @Override public byte[] getByteArray(String key, byte[] deflt) { return null; } + @Override public double getDouble(String key, double deflt) { return 0; } + @Override public float getFloat(String key, float deflt) { return 0; } + @Override public int getInt(String key, int deflt) { return 0; } + @Override public long getLong(String key, long deflt) { return 0; } + @Override public boolean isUserNode() { return false; } + @Override public String[] keys() throws BackingStoreException { return null; } + @Override public String name() { return null; } + @Override public Preferences node(String name) { return null; } + @Override public boolean nodeExists(String name) throws BackingStoreException { return false; } + @Override public Preferences parent() { return null; } + @Override public void put(String key, String value) { } + @Override public void putBoolean(String key, boolean value) { } + @Override public void putByteArray(String key, byte[] value) { } + @Override public void putDouble(String key, double value) { } + @Override public void putFloat(String key, float value) { } + @Override public void putInt(String key, int value) { } + @Override public void putLong(String key, long value) { } + @Override public void remove(String key) { } + @Override public void removeNode() throws BackingStoreException { } + @Override public void addNodeChangeListener(NodeChangeListener ncl) { } + @Override public void addPreferenceChangeListener(PreferenceChangeListener pcl) { } + @Override public void removeNodeChangeListener(NodeChangeListener ncl) { } + @Override public void removePreferenceChangeListener(PreferenceChangeListener pcl) { } + @Override public void sync() throws BackingStoreException { } + @Override public String toString() { return null; } diff --git a/libcore/prefs/src/test/java/tests/prefs/AllTests.java b/libcore/prefs/src/test/java/tests/prefs/AllTests.java index 843d733b2..b3f2ed6ed 100644 --- a/libcore/prefs/src/test/java/tests/prefs/AllTests.java +++ b/libcore/prefs/src/test/java/tests/prefs/AllTests.java @@ -20,7 +20,8 @@ import junit.framework.Test; import junit.framework.TestSuite; /** - * Test suite that includes all tests for the Math project. + * Test suite that includes all tests for the Prefs project. + * */ public class AllTests { diff --git a/libcore/prefs/src/test/resources/prefs/java/util/prefs/preferences.dtd b/libcore/prefs/src/test/resources/prefs/java/util/prefs/preferences.dtd deleted file mode 100644 index a11601528..000000000 --- a/libcore/prefs/src/test/resources/prefs/java/util/prefs/preferences.dtd +++ /dev/null @@ -1,56 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/libcore/support/src/test/java/tests/util/PrefsTester.java b/libcore/support/src/test/java/tests/util/PrefsTester.java new file mode 100644 index 000000000..047b3579f --- /dev/null +++ b/libcore/support/src/test/java/tests/util/PrefsTester.java @@ -0,0 +1,73 @@ +/* Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tests.util; + +import java.util.prefs.BackingStoreException; +import java.util.prefs.Preferences; +import java.util.Arrays; + +/** + * Prepares the shared preferences store for a test by wiping preference data + * before and after the test. Sample usage: + *
+ * public void MyPreferencesTest extends TestCase {
+ *     private final PrefsTester prefsTester = new PrefsTester();
+ *
+ *     public void setUp() throws BackingStoreException {
+ *         super.setUp();
+ *         prefsTester.setUp();
+ *     }
+ *
+ *     public void tearDown() throws BackingStoreException {
+ *         prefsTester.tearDown();
+ *         super.tearDown();
+ *     }
+ *
+ *     ...
+ * }
+ * + *

Once the preferences classes have been initialized, the path where their + * data is stored is fixed. For that reason, every test that reads or writes + * preferences must first prepare preferences for testing by using this class. + */ +public final class PrefsTester { + + static { + String tmp = System.getProperty("java.io.tmpdir"); + System.setProperty("user.home", tmp); + System.setProperty("java.home", tmp); + } + + public void setUp() throws BackingStoreException { + clear(); + } + + public void tearDown() throws BackingStoreException { + clear(); + } + + private void clear() throws BackingStoreException { + for (Preferences root : Arrays .asList( + Preferences.systemRoot(), Preferences.userRoot())) { + for (String child : root.childrenNames()) { + root.node(child).removeNode(); + } + root.clear(); + root.flush(); + } + } +} -- 2.11.0