commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nia...@apache.org
Subject svn commit: r354318 - /jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/impl/ResourcesBaseMethodTestCase.java
Date Tue, 06 Dec 2005 05:15:02 GMT
Author: niallp
Date: Mon Dec  5 21:14:51 2005
New Revision: 354318

URL: http://svn.apache.org/viewcvs?rev=354318&view=rev
Log:
Add new ResourcesBase JUnit test (forgotten when committing changes for Bug 37642)

Added:
    jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/impl/ResourcesBaseMethodTestCase.java
  (with props)

Added: jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/impl/ResourcesBaseMethodTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/impl/ResourcesBaseMethodTestCase.java?rev=354318&view=auto
==============================================================================
--- jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/impl/ResourcesBaseMethodTestCase.java
(added)
+++ jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/impl/ResourcesBaseMethodTestCase.java
Mon Dec  5 21:14:51 2005
@@ -0,0 +1,393 @@
+/*
+ * $Id$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ *  Copyright 2005 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.commons.resources.impl;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.TimeZone;
+import java.util.Map;
+import java.util.HashMap;
+import java.io.Reader;
+import java.io.InputStream;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.framework.TestCase;
+
+import org.apache.commons.resources.Resources;
+import org.apache.commons.resources.ResourcesException;
+import org.apache.commons.resources.ResourcesKeyException;
+
+/**
+ * <p>Unit tests for ResourceBase which checks that the standard
+ *    content retrieval methods all work inter-changeably.
+ * </p>
+ * <p>For example if the getObject() method retrieves an InputStream
+ *    the getReader() method converts the InputStream to a Reader.
+ * </p>
+ */
+public class ResourcesBaseMethodTestCase extends TestCase {
+
+
+    // ----------------------------------------------------- Instance Variables
+
+
+    // The Resources instance to be tested
+    protected Resources resources = null;
+
+    private static Map testMap;
+    private String[] keys = null;
+    private static int BUFFER_SIZE = 20;
+
+
+    // ----------------------------------------------------------- Constructors
+
+
+    public ResourcesBaseMethodTestCase(String name) {
+        super(name);
+    }
+
+
+    // --------------------------------------------------- Overall Test Methods
+
+
+    // Set up instance variables required by this test case
+    public void setUp() throws Exception {
+        super.setUp();
+        testMap = new HashMap();
+        testMap.put("buffer-0", "");
+        testMap.put("buffer-1",     makeString(1));
+        testMap.put("buffer-2",     makeString(2));
+        testMap.put("buffer-less1", makeString(BUFFER_SIZE - 1));
+        testMap.put("buffer-size",  makeString(BUFFER_SIZE));
+        testMap.put("buffer-plus1", makeString(BUFFER_SIZE + 1));
+        testMap.put("buffer-large", makeString((BUFFER_SIZE * 3) + 1));
+
+        Iterator iterator = testMap.keySet().iterator();
+        List list = new ArrayList();
+        while (iterator.hasNext()) {
+            Object key = iterator.next();
+            list.add(key);
+//            System.out.println("TEST MAP Key=[" + key + "] value=[" + testMap.get(key)
+ "]");
+        }
+        keys = new String[list.size()];
+        keys = (String[])list.toArray(keys);
+
+    }
+
+    /**
+     * Create a String value of a specified length
+     * containing repeated digits of 0 - 9.
+     */
+    private String makeString(int size) {
+        StringBuffer buffer = new StringBuffer();
+        int count = 0;
+        for (int i = 0; i < size; i++) {
+           if (count > 9) {
+               count = 0;
+           }
+           buffer.append(count);
+           count++;
+        }
+        return buffer.toString();
+    }
+
+    // Return the tests included in this test suite
+    public static Test suite() {
+        return (new TestSuite(ResourcesBaseMethodTestCase.class));
+    }
+
+    // Tear down the instance variables required by this test case
+    public void tearDown() {
+        resources = null;
+    }
+
+
+    // ------------------------------------------------ Individual Test Methods
+
+
+    /**
+     * Test a Resources implementation whose getObject() method
+     * returns a Reader.
+     */
+    public void testReader() throws Exception {
+        Resources resources = new ReaderResources();
+        checkBytes(resources);
+        checkString(resources);
+        checkReader(resources);
+        checkStream(resources);
+    }
+
+    /**
+     * Test a Resources implementation whose getObject() method
+     * returns an InputStream.
+     */
+    public void testStream() throws Exception {
+        Resources resources = new StreamResources();
+        checkBytes(resources);
+        checkString(resources);
+        checkReader(resources);
+        checkStream(resources);
+    }
+
+    /**
+     * Test a Resources implementation whose getObject() method
+     * returns a String.
+     */
+    public void testString() throws Exception {
+        Resources resources = new StringResources();
+        checkBytes(resources);
+        checkString(resources);
+        checkReader(resources);
+        checkStream(resources);
+    }
+
+    /**
+     * Test a Resources implementation whose getObject() method
+     * returns a byte array.
+     */
+    public void testByteArray() throws Exception {
+        Resources resources = new ByteArrayResources();
+        checkBytes(resources);
+        checkString(resources);
+        checkReader(resources);
+        checkStream(resources);
+    }
+
+    /**
+     * Test a Resources implementation whose getObject() method
+     * returns an Object that is not a String/Reader/InputStream/byte[].
+     */
+    public void testObject() throws Exception {
+        Resources resources = new ObjectResources();
+        checkBytes(resources);
+        checkString(resources);
+        checkReader(resources);
+        checkStream(resources);
+    }
+
+    /**
+     * Check the Resources.getBytes() method.
+     */
+    private void checkBytes(Resources resources) {
+        for (int i = 0; i < keys.length; i++) {
+            String mapValue = (String)testMap.get(keys[i]);
+            byte[] resValue = null;
+            try {
+                resValue = resources.getBytes(keys[i], null, null);
+            } catch(Exception e) {
+                fail("Error checking Bytes for key " + keys[i] + " " + e);
+            }
+            if (mapValue.length() == 0 && resValue == null) {
+                continue;
+            }
+
+            for (int j = 0; j < mapValue.length(); j++) {
+                if (mapValue.charAt(j) != resValue[j]) {
+                    fail("Bytes don't match for key " + keys[i] + " at " + j + " map=" +
mapValue.charAt(j) + " resource=" + resValue[j]);
+                }
+            }
+            assertEquals("Bytes lengths don't match for key " + keys[i], mapValue.length(),
resValue.length); 
+        }
+    }
+
+    /**
+     * Check the Resources.getString() method.
+     */
+    private void checkString(Resources resources) {
+        for (int i = 0; i < keys.length; i++) {
+            String mapValue = (String)testMap.get(keys[i]);
+            String resValue = null;
+            try {
+                resValue = resources.getString(keys[i], null, null);
+            } catch(Exception e) {
+                fail("Error checking String for key " + keys[i] + " " + e);
+            }
+            if (mapValue.length() == 0 && resValue == null) {
+                continue;
+            }
+            assertEquals("Strings don't match for key " + keys[i], mapValue, resValue); 
+        }
+    }
+
+    /**
+     * Check the Resources.getReader() method.
+     */
+    private void checkReader(Resources resources) {
+        for (int i = 0; i < keys.length; i++) {
+            String mapValue = (String)testMap.get(keys[i]);
+            Reader reader = null;
+            try {
+                reader = resources.getReader(keys[i], null, null);
+                if (reader == null && mapValue.length() == 0) {
+                    continue;
+                }
+                char[] resValue = new char[mapValue.length()];
+                int reslength  = reader.read(resValue);
+                if ((mapValue.length() == 0 && reslength > 0) || 
+                    (mapValue.length() >  0 && reslength != mapValue.length()))
{
+                    fail("Error checking Reader for key " + keys[i] + 
+                         " mapLength="+mapValue.length()+ " resLength="+reslength);
+                }
+                if (reader.read(new char[1]) != -1) {
+                    fail("Error checking Reader for key " + keys[i] + " - Too long");
+                }
+                if (mapValue.length() == 0 && resValue == null) {
+                    continue;
+                }
+                for (int j = 0; j < mapValue.length(); j++) {
+                    if (mapValue.charAt(j) != resValue[j]) {
+                        fail("Readers don't match for key " + keys[i] + " at " + j + 
+                             " map=" + mapValue.charAt(j) + " resource=" + resValue[j]);
+                    }
+                }
+            } catch(Exception e) {
+                fail("Error checking Reader for key " + keys[i] + " " + e);
+            }
+        }
+    }
+
+    /**
+     * Check the Resources.getInputStream() method.
+     */
+    private void checkStream(Resources resources) {
+        for (int i = 0; i < keys.length; i++) {
+            String mapValue = (String)testMap.get(keys[i]);
+            InputStream stream = null;
+            try {
+                stream = resources.getInputStream(keys[i], null, null);
+                if (stream == null && mapValue.length() == 0) {
+                    continue;
+                }
+                byte[] resValue = new byte[mapValue.length()];
+                int reslength  = stream.read(resValue);
+                if ((mapValue.length() == 0 && reslength > 0) || 
+                    (mapValue.length() >  0 && reslength != mapValue.length()))
{
+                    fail("Error checking Stream for key " + keys[i] + 
+                         " mapLength="+mapValue.length()+ " resLength="+reslength);
+                }
+                if (stream.read(new byte[1]) != -1) {
+                    fail("Error checking Stream for key " + keys[i] + " - Too long");
+                }
+                if (mapValue.length() == 0 && resValue == null) {
+                    continue;
+                }
+                for (int j = 0; j < mapValue.length(); j++) {
+                    if (mapValue.charAt(j) != resValue[j]) {
+                        fail("Streams don't match for key " + keys[i] + " at " + j + 
+                             " map=" + mapValue.charAt(j) + " resource=" + resValue[j]);
+                    }
+                }
+            } catch(Exception e) {
+                fail("Error checking Stream for key " + keys[i] + " " + e);
+            }
+        }
+    }
+
+    /**
+     * Base Test Resources implementation.
+     */
+    private static class TestResourcesBase extends ResourcesBase {
+        protected Map map;
+        public TestResourcesBase() {
+            setBufferSize(BUFFER_SIZE);
+            this.map = testMap;
+        }
+        public Iterator getKeys() {
+            return map.keySet().iterator();
+        }
+        public Object getObject(String key, Locale locale, TimeZone timeZone) {
+            return map.get(key);
+        }
+    }
+
+    /**
+     * Resources implementation whose getObject() method
+     * returns a Reader.
+     */
+    private static class ReaderResources extends TestResourcesBase {
+        public ReaderResources() {
+            super();
+        }
+        public Object getObject(String key, Locale locale, TimeZone timeZone) {
+            return new java.io.StringReader((String)map.get(key));
+        }
+    }
+
+    /**
+     * Resources implementation whose getObject() method
+     * returns a InputStream.
+     */
+    private static class StreamResources extends TestResourcesBase {
+        public StreamResources() {
+            super();
+        }
+        public Object getObject(String key, Locale locale, TimeZone timeZone) {
+            return new java.io.ByteArrayInputStream(((String)map.get(key)).getBytes());
+        }
+    }
+
+    /**
+     * Resources implementation whose getObject() method
+     * returns a byte array.
+     */
+    private static class ByteArrayResources extends TestResourcesBase {
+        public ByteArrayResources() {
+            super();
+        }
+        public Object getObject(String key, Locale locale, TimeZone timeZone) {
+            return ((String)map.get(key)).getBytes();
+        }
+    }
+
+    /**
+     * Resources implementation whose getObject() method
+     * returns a String.
+     */
+    private static class StringResources extends TestResourcesBase {
+        public StringResources() {
+            super();
+        }
+        public Object getObject(String key, Locale locale, TimeZone timeZone) {
+            return (String)map.get(key);
+        }
+    }
+
+    /**
+     * Resources implementation whose getObject() method
+     * returns an Object.
+     */
+    private static class ObjectResources extends TestResourcesBase {
+        public ObjectResources() {
+            super();
+        }
+        public Object getObject(String key, Locale locale, TimeZone timeZone) {
+            return new StringBuffer((String)map.get(key));
+        }
+    }
+
+
+}

Propchange: jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/impl/ResourcesBaseMethodTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/proper/resources/trunk/src/test/org/apache/commons/resources/impl/ResourcesBaseMethodTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message