[jsword-svn] r2052 - in trunk/jsword/src: main/java/org/crosswire/common/util test/java/org/crosswire/common/util
dmsmith at crosswire.org
dmsmith at crosswire.org
Fri Dec 10 12:23:46 MST 2010
Author: dmsmith
Date: 2010-12-10 12:23:46 -0700 (Fri, 10 Dec 2010)
New Revision: 2052
Added:
trunk/jsword/src/main/java/org/crosswire/common/util/PropertyMap.java
trunk/jsword/src/test/java/org/crosswire/common/util/PropertyMap.properties
trunk/jsword/src/test/java/org/crosswire/common/util/PropertyMapTest.java
Modified:
trunk/jsword/src/test/java/org/crosswire/common/util/AllTests.java
Log:
JS-145 added PropertyMap and a test case for it.
Added: trunk/jsword/src/main/java/org/crosswire/common/util/PropertyMap.java
===================================================================
--- trunk/jsword/src/main/java/org/crosswire/common/util/PropertyMap.java (rev 0)
+++ trunk/jsword/src/main/java/org/crosswire/common/util/PropertyMap.java 2010-12-10 19:23:46 UTC (rev 2052)
@@ -0,0 +1,209 @@
+/**
+ * Distribution License:
+ * JSword is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License, version 2.1 as published by
+ * the Free Software Foundation. This program is distributed in the hope
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * The License is available on the internet at:
+ * http://www.gnu.org/copyleft/lgpl.html
+ * or by writing to:
+ * Free Software Foundation, Inc.
+ * 59 Temple Place - Suite 330
+ * Boston, MA 02111-1307, USA
+ *
+ * Copyright: 2008
+ * The copyright to this program is held by it's authors.
+ *
+ * ID: $Id$
+ */
+package org.crosswire.common.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Enumeration;
+import java.util.LinkedHashMap;
+import java.util.Properties;
+
+
+/**
+ * A PropertyMap is a Map<String,String> sitting over top a Property file.
+ * As such it must be defined in the same way as a java.lang.Properties expects.
+ *
+ * @see java.lang.Properties
+ * @see gnu.lgpl.License for license details.<br>
+ * The copyright to this program is held by it's authors.
+ * @author DM Smith [dmsmith555 at yahoo dot com]
+ */
+/**
+ *
+ *
+ * @see gnu.lgpl.License for license details.<br>
+ * The copyright to this program is held by it's authors.
+ * @author DM Smith [dmsmith555 at yahoo dot com]
+ */
+public class PropertyMap extends LinkedHashMap<String,String> {
+ /**
+ * Creates an empty property list with no default values.
+ */
+ public PropertyMap() {
+ this(null);
+ }
+
+ /**
+ * Creates an empty property map with the specified defaults.
+ *
+ * @param defaults the defaults.
+ */
+ public PropertyMap(PropertyMap defaults) {
+ this.defaults = defaults;
+ }
+
+ /**
+ * Searches for the property with the specified key in this property list.
+ * If the key is not found in this property list, the default property list,
+ * and its defaults, recursively, are then checked. The method returns
+ * <code>null</code> if the property is not found.
+ *
+ * @param key the lookup key.
+ * @return the value in this property list with the specified key value.
+ * @see java.lang.Properties#setProperty
+ * @see #defaults
+ */
+ public String get(String key) {
+ String value = super.get(key);
+ return ((value == null) && (defaults != null)) ? defaults.get(key) : value;
+ }
+
+ /**
+ * Searches for the property with the specified key in this property list.
+ * If the key is not found in this property list, the default property list,
+ * and its defaults, recursively, are then checked. The method returns the
+ * default value argument if the property is not found.
+ *
+ * @param key the lookup key.
+ * @param defaultValue a default value.
+ *
+ * @return the value in this property list with the specified key value.
+ * @see java.lang.Properties#setProperty
+ * @see #defaults
+ */
+ public String get(String key, String defaultValue) {
+ String value = get(key);
+ return value == null ? defaultValue : value;
+ }
+
+ /**
+ * Merely a call to {@link #put(String,String)}, provided as
+ * a simple way to migrate from {@link java.lang.Properties}.
+ *
+ * @param key the key to be placed into this property list.
+ * @param value the value corresponding to <tt>key</tt>.
+ * @return the previous value of the specified key in this property
+ * list, or <code>null</code> if it did not have one.
+ * @deprecated use {@link #put(String,String)} instead
+ */
+ @Deprecated
+ public String setProperty(String key, String value) {
+ return put(key, value);
+ }
+
+ /**
+ * Merely a call to {@link #get(String)}, provided as
+ * a simple way to migrate from {@link java.lang.Properties}.
+ *
+ * @param key the lookup key
+ * @return the value in this property list with the specified key value.
+ * @deprecated use {@link @get(String)} instead
+ */
+ @Deprecated
+ public String getProperty(String key) {
+ return get(key);
+ }
+
+ /**
+ * Merely a call to {@link #get(String, String)}, provided as
+ * a simple way to migrate from {@link java.lang.Properties}.
+ * @param key the lookup key.
+ * @param defaultValue a default value.
+ * @return the value in this property list with the specified key value.
+ * @deprecated use {@link #get(String, String)} instead
+ */
+ @Deprecated
+ public String getProperty(String key, String defaultValue) {
+ return get(key, defaultValue);
+ }
+
+ /**
+ * Reads a property list (key and element pairs) from the input
+ * byte stream. The input stream is in a simple line-oriented
+ * format as specified in
+ * {@link java.lang.Properties#load(java.io.InputStream) load(InputStream)} and is assumed to use
+ * the ISO 8859-1 character encoding; that is each byte is one Latin1
+ * character. Characters not in Latin1, and certain special characters,
+ * are represented in keys and elements using
+ * <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.3">Unicode escapes</a>.
+ * <p>
+ * The specified stream remains open after this method returns.
+ *
+ * @param inStream the input stream.
+ * @exception IOException if an error occurred when reading from the
+ * input stream.
+ * @throws IllegalArgumentException if the input stream contains a
+ * malformed Unicode escape sequence.
+ * @since 1.2
+ */
+ public void load(InputStream inStream) throws IOException {
+ Properties prop = new Properties();
+ prop.load(inStream);
+ for (Enumeration<Object> e = prop.keys() ; e.hasMoreElements() ;) {
+ Object k = e.nextElement();
+ Object v = prop.get(k);
+ if (k instanceof String && v instanceof String) {
+ put((String) k, (String) v);
+ }
+ }
+ }
+
+ /**
+ * Writes this property list (key and element pairs) in this
+ * <code>PropertyMap</code> table to the output stream in a format suitable
+ * for loading into a <code>PropertyMap</code> table using the
+ * {@link #load(InputStream) load(InputStream)} method.
+ * <p>
+ * Properties from the defaults table of this <code>PropertyMap</code>
+ * table (if any) are <i>not</i> written out by this method.
+ * <p>
+ * This method outputs the comments, properties keys and values in
+ * the same format as specified in
+ * {@link java.lang.Properties#store(java.io.OutputStream, java.lang.String) store(Writer)},
+ * <p>
+ * After the entries have been written, the output stream is flushed.
+ * The output stream remains open after this method returns.
+ * <p>
+ * @param out an output stream.
+ * @param comments a description of the property list.
+ * @exception IOException if writing this property list to the specified
+ * output stream throws an <tt>IOException</tt>.
+ * @exception NullPointerException if <code>out</code> is null.
+ * @since 1.2
+ */
+ public void store(OutputStream out, String comments) throws IOException {
+ Properties temp = new Properties();
+ temp.putAll(this);
+ temp.store(out, comments);
+ }
+
+ /**
+ * Default values for any keys not found in this property map.
+ */
+ private PropertyMap defaults;
+
+ /**
+ * The serialization id
+ */
+ private static final long serialVersionUID = 2821277155924802795L;
+}
Property changes on: trunk/jsword/src/main/java/org/crosswire/common/util/PropertyMap.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
Modified: trunk/jsword/src/test/java/org/crosswire/common/util/AllTests.java
===================================================================
--- trunk/jsword/src/test/java/org/crosswire/common/util/AllTests.java 2010-12-09 19:09:05 UTC (rev 2051)
+++ trunk/jsword/src/test/java/org/crosswire/common/util/AllTests.java 2010-12-10 19:23:46 UTC (rev 2052)
@@ -37,6 +37,7 @@
// $JUnit-BEGIN$
suite.addTest(new TestSuite(HelpDeskTest.class));
suite.addTest(new TestSuite(StringUtilTest.class));
+ suite.addTest(new TestSuite(PropertyMapTest.class));
// $JUnit-END$
return suite;
}
Added: trunk/jsword/src/test/java/org/crosswire/common/util/PropertyMap.properties
===================================================================
--- trunk/jsword/src/test/java/org/crosswire/common/util/PropertyMap.properties (rev 0)
+++ trunk/jsword/src/test/java/org/crosswire/common/util/PropertyMap.properties 2010-12-10 19:23:46 UTC (rev 2052)
@@ -0,0 +1 @@
+Here=I am
\ No newline at end of file
Added: trunk/jsword/src/test/java/org/crosswire/common/util/PropertyMapTest.java
===================================================================
--- trunk/jsword/src/test/java/org/crosswire/common/util/PropertyMapTest.java (rev 0)
+++ trunk/jsword/src/test/java/org/crosswire/common/util/PropertyMapTest.java 2010-12-10 19:23:46 UTC (rev 2052)
@@ -0,0 +1,130 @@
+/**
+ * Distribution License:
+ * JSword is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License, version 2.1 as published by
+ * the Free Software Foundation. This program is distributed in the hope
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ * See the GNU Lesser General Public License for more details.
+ *
+ * The License is available on the internet at:
+ * http://www.gnu.org/copyleft/lgpl.html
+ * or by writing to:
+ * Free Software Foundation, Inc.
+ * 59 Temple Place - Suite 330
+ * Boston, MA 02111-1307, USA
+ *
+ * Copyright: 2007
+ * The copyright to this program is held by it's authors.
+ *
+ * ID: $Id$
+ */
+package org.crosswire.common.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URI;
+import java.util.MissingResourceException;
+
+import junit.framework.TestCase;
+
+/**
+ *
+ *
+ * @see gnu.lgpl.License for license details.<br>
+ * The copyright to this program is held by it's authors.
+ * @author DM Smith [dmsmith555 at yahoo dot com]
+ */
+public class PropertyMapTest extends TestCase {
+
+ /**
+ * @param name
+ */
+ public PropertyMapTest(String name) {
+ super(name);
+ }
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception {
+ super.setUp();
+ m = new PropertyMap();
+ }
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ public void testBasic() {
+ assertEquals("Test for an element not present", null, m.get("diddly"));
+ m.put("diddly", "squat");
+ assertEquals("Test for a present element", "squat", m.get("diddly"));
+ }
+
+ public void testLoad() {
+ InputStream in = null;
+ try {
+ in = ResourceUtil.getResourceAsStream(this.getClass(), "PropertyMap.properties");
+ m.load(in);
+ } catch (MissingResourceException e) {
+ fail("Unable to find PropertyMap.properties");
+ } catch (IOException e) {
+ fail("Unable to read PropertyMap.properties");
+ } finally {
+ IOUtil.close(in);
+ }
+ }
+
+ public void testRead() {
+ InputStream in = null;
+ try {
+ in = ResourceUtil.getResourceAsStream(this.getClass(), "PropertyMap.properties");
+ m.load(in);
+ } catch (MissingResourceException e) {
+ fail("Unable to find PropertyMap.properties");
+ } catch (IOException e) {
+ fail("Unable to read PropertyMap.properties");
+ } finally {
+ IOUtil.close(in);
+ }
+ assertEquals("Only one element was in the file", 1, m.size());
+ assertEquals("Test that the load worked", "I am", m.get("Here"));
+ }
+ public void testSave() {
+ m.put("Here", "Am I");
+ URI uri = CWProject.instance().getWritableURI("test", FileUtil.EXTENSION_PROPERTIES);
+ OutputStream out = null;
+ try {
+ out = NetUtil.getOutputStream(uri);
+ m.store(out, "Test data can be deleted at any time");
+ } catch (IOException e) {
+ fail("Unable to save test.properties");
+ } finally {
+ IOUtil.close(out);
+ }
+ }
+
+ public void testReload() {
+ assertEquals("The map is empty", 0, m.size());
+
+ InputStream is = null;
+ URI uri = CWProject.instance().getWritableURI("test", FileUtil.EXTENSION_PROPERTIES);
+ try {
+ is = NetUtil.getInputStream(uri);
+ m.load(is);
+ } catch (IOException e) {
+ fail("Unable to reload test.properties");
+ } finally {
+ IOUtil.close(is);
+ }
+ assertEquals("Only one element was in the file", 1, m.size());
+ assertEquals("Test that the save and reload worked", "Am I", m.get("Here"));
+ }
+
+ private PropertyMap m;
+}
Property changes on: trunk/jsword/src/test/java/org/crosswire/common/util/PropertyMapTest.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: svn:keywords
+ Id
Added: svn:eol-style
+ native
More information about the jsword-svn
mailing list