geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From djen...@apache.org
Subject svn commit: r405651 - in /geronimo/branches/1.1/modules/common/src: java/org/apache/geronimo/common/propertyeditor/ test/org/apache/geronimo/common/propertyeditor/
Date Wed, 10 May 2006 06:25:31 GMT
Author: djencks
Date: Tue May  9 23:25:29 2006
New Revision: 405651

URL: http://svn.apache.org/viewcvs?rev=405651&view=rev
Log:
GERONIMO-1782 Fix PropertiesEditor, with a test

Added:
    geronimo/branches/1.1/modules/common/src/test/org/apache/geronimo/common/propertyeditor/AbstractMapEditorTest.java
    geronimo/branches/1.1/modules/common/src/test/org/apache/geronimo/common/propertyeditor/PropertiesEditorTest.java
Modified:
    geronimo/branches/1.1/modules/common/src/java/org/apache/geronimo/common/propertyeditor/PropertiesEditor.java

Modified: geronimo/branches/1.1/modules/common/src/java/org/apache/geronimo/common/propertyeditor/PropertiesEditor.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/common/src/java/org/apache/geronimo/common/propertyeditor/PropertiesEditor.java?rev=405651&r1=405650&r2=405651&view=diff
==============================================================================
--- geronimo/branches/1.1/modules/common/src/java/org/apache/geronimo/common/propertyeditor/PropertiesEditor.java
(original)
+++ geronimo/branches/1.1/modules/common/src/java/org/apache/geronimo/common/propertyeditor/PropertiesEditor.java
Tue May  9 23:25:29 2006
@@ -17,6 +17,7 @@
 package org.apache.geronimo.common.propertyeditor;
 
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.util.Properties;
 
@@ -35,18 +36,47 @@
      * @throws PropertyEditorException An error occurred creating the Properties object.
      */
     public Object getValue() {
-        try {
+        Object currentValue = super.getValue();
+        if (currentValue instanceof Properties) {
+            return (Properties) currentValue;
+        } else {
             // convert the text value into an in-memory input stream we can used for
             // property loading.
-            ByteArrayInputStream stream = new ByteArrayInputStream(getAsText().getBytes());
+            ByteArrayInputStream stream = new ByteArrayInputStream(currentValue.toString().getBytes());
             // load this into a properties instance.
             Properties bundle = new Properties();
-            bundle.load(stream);
-
+            try {
+                bundle.load(stream);
+            } catch (IOException e) {
+                // any errors here are just a property exception
+                throw new PropertyEditorException(e);
+            }
             return bundle;
-        } catch (IOException e) {
-            // any errors here are just a property exception
-            throw new PropertyEditorException(e);
         }
     }
+
+    /**
+     * Provides a String version of a Properties object suitable
+     * for loading into a Properties table using the load method.
+     *
+     * @return The String value of the Properties object as created
+     *         by the store method.
+     * @throws PropertyEditorException An error occurred converting the Properties object
+     * @see Properties#store(java.io.OutputStream, String)
+     */
+    public String getAsText() {
+        Object value = getValue();
+        if (value instanceof Properties) {
+            ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            try {
+                ((Properties) value).store(baos, null);
+            } catch (IOException e) {
+                // any errors here are just a property exception
+                throw new PropertyEditorException(e);
+            }
+            return baos.toString();
+        }
+        return ("" + value);
+    }
+
 }

Added: geronimo/branches/1.1/modules/common/src/test/org/apache/geronimo/common/propertyeditor/AbstractMapEditorTest.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/common/src/test/org/apache/geronimo/common/propertyeditor/AbstractMapEditorTest.java?rev=405651&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/common/src/test/org/apache/geronimo/common/propertyeditor/AbstractMapEditorTest.java
(added)
+++ geronimo/branches/1.1/modules/common/src/test/org/apache/geronimo/common/propertyeditor/AbstractMapEditorTest.java
Tue May  9 23:25:29 2006
@@ -0,0 +1,49 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.geronimo.common.propertyeditor;
+
+import java.beans.PropertyEditor;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+/**
+ * Abstract superclass for map editor tests.  Implement the two abstract methods and initialize
+ * editor in the setUp() method.
+ *
+ * @version $Rev: 356022 $ $Date: 2005-12-11 15:58:34 -0500 (Sun, 11 Dec 2005) $
+ */
+public abstract class AbstractMapEditorTest extends TestCase {
+    PropertyEditor editor;
+
+    public void testRoundTrip() {
+        Map map = createMap();
+        map.put("key1","value1");
+        map.put("key2","value2");
+        editor.setValue(map);
+        String text = editor.getAsText();
+        editor.setAsText(text);
+        Map result = (Map) editor.getValue();
+        assertEquals(2, result.size());
+        assertEquals("value1", result.get("key1"));
+        assertEquals("value2", result.get("key2"));
+    }
+
+    protected abstract void checkType(Object output);
+
+    protected abstract Map createMap();
+}

Added: geronimo/branches/1.1/modules/common/src/test/org/apache/geronimo/common/propertyeditor/PropertiesEditorTest.java
URL: http://svn.apache.org/viewcvs/geronimo/branches/1.1/modules/common/src/test/org/apache/geronimo/common/propertyeditor/PropertiesEditorTest.java?rev=405651&view=auto
==============================================================================
--- geronimo/branches/1.1/modules/common/src/test/org/apache/geronimo/common/propertyeditor/PropertiesEditorTest.java
(added)
+++ geronimo/branches/1.1/modules/common/src/test/org/apache/geronimo/common/propertyeditor/PropertiesEditorTest.java
Tue May  9 23:25:29 2006
@@ -0,0 +1,74 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.geronimo.common.propertyeditor;
+
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Unit test for {@link PropertiesEditor} class.
+ *
+ * @version $Rev: 356022 $ $Date: 2005-12-11 15:58:34 -0500 (Sun, 11 Dec 2005) $
+ */
+public class PropertiesEditorTest extends AbstractMapEditorTest {
+
+    protected void setUp() {
+        editor = PropertyEditors.findEditor(Properties.class);
+    }
+
+    public void testEditorClass() throws Exception {
+        assertEquals(PropertiesEditor.class, editor.getClass());
+    }
+
+    protected void checkType(Object output) {
+        assertTrue("editor returned a: " + output.getClass(), output instanceof Properties);
+    }
+
+    public void testGetValue_1Item() {
+        String input = "key1=value1";
+
+        editor.setAsText(input);
+        Object output = editor.getValue();
+
+        assertNotNull(output);
+        checkType(output);
+
+        Map map = (Map) output;
+        assertEquals(1, map.size());
+        assertEquals("value1", map.get("key1"));
+    }
+
+    public void testGetValue_2Items() {
+        String input = "key1=value1\nkey2=value2";
+
+        editor.setAsText(input);
+        Object output = editor.getValue();
+
+        assertNotNull(output);
+        checkType(output);
+
+        Map map = (Map) output;
+        assertEquals(2, map.size());
+        assertEquals("value1", map.get("key1"));
+        assertEquals("value2", map.get("key2"));
+    }
+    
+    protected Map createMap() {
+        return new Properties();
+    }
+}



Mime
View raw message