001    /*
002     * $Id: ResourcesBaseMethodTestCase.java 354330 2005-12-06 06:05:19Z niallp $
003     * $Revision: 354330 $
004     * $Date: 2005-12-06 06:05:19 +0000 (Tue, 06 Dec 2005) $
005     *
006     * ====================================================================
007     *
008     *  Copyright 2005 The Apache Software Foundation
009     * 
010     *  Licensed under the Apache License, Version 2.0 (the "License");
011     *  you may not use this file except in compliance with the License.
012     *  You may obtain a copy of the License at
013     *
014     *      http://www.apache.org/licenses/LICENSE-2.0
015     *
016     *  Unless required by applicable law or agreed to in writing, software
017     *  distributed under the License is distributed on an "AS IS" BASIS,
018     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019     *  See the License for the specific language governing permissions and
020     *  limitations under the License.
021     *
022     */
023    
024    package org.apache.commons.resources.impl;
025    
026    import java.util.ArrayList;
027    import java.util.Iterator;
028    import java.util.List;
029    import java.util.Locale;
030    import java.util.Map;
031    import java.util.HashMap;
032    import java.io.Reader;
033    import java.io.InputStream;
034    
035    import junit.framework.Test;
036    import junit.framework.TestSuite;
037    import junit.framework.TestCase;
038    
039    import org.apache.commons.resources.Resources;
040    import org.apache.commons.resources.ResourcesException;
041    import org.apache.commons.resources.ResourcesKeyException;
042    
043    /**
044     * <p>Unit tests for ResourceBase which checks that the standard
045     *    content retrieval methods all work inter-changeably.
046     * </p>
047     * <p>For example if the getObject() method retrieves an InputStream
048     *    the getReader() method converts the InputStream to a Reader.
049     * </p>
050     */
051    public class ResourcesBaseMethodTestCase extends TestCase {
052    
053    
054        // ----------------------------------------------------- Instance Variables
055    
056    
057        // The Resources instance to be tested
058        protected Resources resources = null;
059    
060        private static Map testMap;
061        private String[] keys = null;
062        private static int BUFFER_SIZE = 20;
063    
064    
065        // ----------------------------------------------------------- Constructors
066    
067    
068        public ResourcesBaseMethodTestCase(String name) {
069            super(name);
070        }
071    
072    
073        // --------------------------------------------------- Overall Test Methods
074    
075    
076        // Set up instance variables required by this test case
077        public void setUp() throws Exception {
078            super.setUp();
079            testMap = new HashMap();
080            testMap.put("buffer-0", "");
081            testMap.put("buffer-1",     makeString(1));
082            testMap.put("buffer-2",     makeString(2));
083            testMap.put("buffer-less1", makeString(BUFFER_SIZE - 1));
084            testMap.put("buffer-size",  makeString(BUFFER_SIZE));
085            testMap.put("buffer-plus1", makeString(BUFFER_SIZE + 1));
086            testMap.put("buffer-large", makeString((BUFFER_SIZE * 3) + 1));
087    
088            Iterator iterator = testMap.keySet().iterator();
089            List list = new ArrayList();
090            while (iterator.hasNext()) {
091                Object key = iterator.next();
092                list.add(key);
093    //            System.out.println("TEST MAP Key=[" + key + "] value=[" + testMap.get(key) + "]");
094            }
095            keys = new String[list.size()];
096            keys = (String[])list.toArray(keys);
097    
098        }
099    
100        /**
101         * Create a String value of a specified length
102         * containing repeated digits of 0 - 9.
103         */
104        private String makeString(int size) {
105            StringBuffer buffer = new StringBuffer();
106            int count = 0;
107            for (int i = 0; i < size; i++) {
108               if (count > 9) {
109                   count = 0;
110               }
111               buffer.append(count);
112               count++;
113            }
114            return buffer.toString();
115        }
116    
117        // Return the tests included in this test suite
118        public static Test suite() {
119            return (new TestSuite(ResourcesBaseMethodTestCase.class));
120        }
121    
122        // Tear down the instance variables required by this test case
123        public void tearDown() {
124            resources = null;
125        }
126    
127    
128        // ------------------------------------------------ Individual Test Methods
129    
130    
131        /**
132         * Test a Resources implementation whose getObject() method
133         * returns a Reader.
134         */
135        public void testReader() throws Exception {
136            Resources resources = new ReaderResources();
137            checkBytes(resources);
138            checkString(resources);
139            checkReader(resources);
140            checkStream(resources);
141        }
142    
143        /**
144         * Test a Resources implementation whose getObject() method
145         * returns an InputStream.
146         */
147        public void testStream() throws Exception {
148            Resources resources = new StreamResources();
149            checkBytes(resources);
150            checkString(resources);
151            checkReader(resources);
152            checkStream(resources);
153        }
154    
155        /**
156         * Test a Resources implementation whose getObject() method
157         * returns a String.
158         */
159        public void testString() throws Exception {
160            Resources resources = new StringResources();
161            checkBytes(resources);
162            checkString(resources);
163            checkReader(resources);
164            checkStream(resources);
165        }
166    
167        /**
168         * Test a Resources implementation whose getObject() method
169         * returns a byte array.
170         */
171        public void testByteArray() throws Exception {
172            Resources resources = new ByteArrayResources();
173            checkBytes(resources);
174            checkString(resources);
175            checkReader(resources);
176            checkStream(resources);
177        }
178    
179        /**
180         * Test a Resources implementation whose getObject() method
181         * returns an Object that is not a String/Reader/InputStream/byte[].
182         */
183        public void testObject() throws Exception {
184            Resources resources = new ObjectResources();
185            checkBytes(resources);
186            checkString(resources);
187            checkReader(resources);
188            checkStream(resources);
189        }
190    
191        /**
192         * Check the Resources.getBytes() method.
193         */
194        private void checkBytes(Resources resources) {
195            for (int i = 0; i < keys.length; i++) {
196                String mapValue = (String)testMap.get(keys[i]);
197                byte[] resValue = null;
198                try {
199                    resValue = resources.getBytes(keys[i], null);
200                } catch(Exception e) {
201                    fail("Error checking Bytes for key " + keys[i] + " " + e);
202                }
203                if (mapValue.length() == 0 && resValue == null) {
204                    continue;
205                }
206    
207                for (int j = 0; j < mapValue.length(); j++) {
208                    if (mapValue.charAt(j) != resValue[j]) {
209                        fail("Bytes don't match for key " + keys[i] + " at " + j + " map=" + mapValue.charAt(j) + " resource=" + resValue[j]);
210                    }
211                }
212                assertEquals("Bytes lengths don't match for key " + keys[i], mapValue.length(), resValue.length); 
213            }
214        }
215    
216        /**
217         * Check the Resources.getString() method.
218         */
219        private void checkString(Resources resources) {
220            for (int i = 0; i < keys.length; i++) {
221                String mapValue = (String)testMap.get(keys[i]);
222                String resValue = null;
223                try {
224                    resValue = resources.getString(keys[i], null);
225                } catch(Exception e) {
226                    fail("Error checking String for key " + keys[i] + " " + e);
227                }
228                if (mapValue.length() == 0 && resValue == null) {
229                    continue;
230                }
231                assertEquals("Strings don't match for key " + keys[i], mapValue, resValue); 
232            }
233        }
234    
235        /**
236         * Check the Resources.getReader() method.
237         */
238        private void checkReader(Resources resources) {
239            for (int i = 0; i < keys.length; i++) {
240                String mapValue = (String)testMap.get(keys[i]);
241                Reader reader = null;
242                try {
243                    reader = resources.getReader(keys[i], null);
244                    if (reader == null && mapValue.length() == 0) {
245                        continue;
246                    }
247                    char[] resValue = new char[mapValue.length()];
248                    int reslength  = reader.read(resValue);
249                    if ((mapValue.length() == 0 && reslength > 0) || 
250                        (mapValue.length() >  0 && reslength != mapValue.length())) {
251                        fail("Error checking Reader for key " + keys[i] + 
252                             " mapLength="+mapValue.length()+ " resLength="+reslength);
253                    }
254                    if (reader.read(new char[1]) != -1) {
255                        fail("Error checking Reader for key " + keys[i] + " - Too long");
256                    }
257                    if (mapValue.length() == 0 && resValue == null) {
258                        continue;
259                    }
260                    for (int j = 0; j < mapValue.length(); j++) {
261                        if (mapValue.charAt(j) != resValue[j]) {
262                            fail("Readers don't match for key " + keys[i] + " at " + j + 
263                                 " map=" + mapValue.charAt(j) + " resource=" + resValue[j]);
264                        }
265                    }
266                } catch(Exception e) {
267                    fail("Error checking Reader for key " + keys[i] + " " + e);
268                }
269            }
270        }
271    
272        /**
273         * Check the Resources.getInputStream() method.
274         */
275        private void checkStream(Resources resources) {
276            for (int i = 0; i < keys.length; i++) {
277                String mapValue = (String)testMap.get(keys[i]);
278                InputStream stream = null;
279                try {
280                    stream = resources.getInputStream(keys[i], null);
281                    if (stream == null && mapValue.length() == 0) {
282                        continue;
283                    }
284                    byte[] resValue = new byte[mapValue.length()];
285                    int reslength  = stream.read(resValue);
286                    if ((mapValue.length() == 0 && reslength > 0) || 
287                        (mapValue.length() >  0 && reslength != mapValue.length())) {
288                        fail("Error checking Stream for key " + keys[i] + 
289                             " mapLength="+mapValue.length()+ " resLength="+reslength);
290                    }
291                    if (stream.read(new byte[1]) != -1) {
292                        fail("Error checking Stream for key " + keys[i] + " - Too long");
293                    }
294                    if (mapValue.length() == 0 && resValue == null) {
295                        continue;
296                    }
297                    for (int j = 0; j < mapValue.length(); j++) {
298                        if (mapValue.charAt(j) != resValue[j]) {
299                            fail("Streams don't match for key " + keys[i] + " at " + j + 
300                                 " map=" + mapValue.charAt(j) + " resource=" + resValue[j]);
301                        }
302                    }
303                } catch(Exception e) {
304                    fail("Error checking Stream for key " + keys[i] + " " + e);
305                }
306            }
307        }
308    
309        /**
310         * Base Test Resources implementation.
311         */
312        private static class TestResourcesBase extends ResourcesBase {
313            protected Map map;
314            public TestResourcesBase() {
315                setBufferSize(BUFFER_SIZE);
316                this.map = testMap;
317            }
318            public Iterator getKeys() {
319                return map.keySet().iterator();
320            }
321            public Object getObject(String key, Locale locale) {
322                return map.get(key);
323            }
324        }
325    
326        /**
327         * Resources implementation whose getObject() method
328         * returns a Reader.
329         */
330        private static class ReaderResources extends TestResourcesBase {
331            public ReaderResources() {
332                super();
333            }
334            public Object getObject(String key, Locale locale) {
335                return new java.io.StringReader((String)map.get(key));
336            }
337        }
338    
339        /**
340         * Resources implementation whose getObject() method
341         * returns a InputStream.
342         */
343        private static class StreamResources extends TestResourcesBase {
344            public StreamResources() {
345                super();
346            }
347            public Object getObject(String key, Locale locale) {
348                return new java.io.ByteArrayInputStream(((String)map.get(key)).getBytes());
349            }
350        }
351    
352        /**
353         * Resources implementation whose getObject() method
354         * returns a byte array.
355         */
356        private static class ByteArrayResources extends TestResourcesBase {
357            public ByteArrayResources() {
358                super();
359            }
360            public Object getObject(String key, Locale locale) {
361                return ((String)map.get(key)).getBytes();
362            }
363        }
364    
365        /**
366         * Resources implementation whose getObject() method
367         * returns a String.
368         */
369        private static class StringResources extends TestResourcesBase {
370            public StringResources() {
371                super();
372            }
373            public Object getObject(String key, Locale locale) {
374                return (String)map.get(key);
375            }
376        }
377    
378        /**
379         * Resources implementation whose getObject() method
380         * returns an Object.
381         */
382        private static class ObjectResources extends TestResourcesBase {
383            public ObjectResources() {
384                super();
385            }
386            public Object getObject(String key, Locale locale) {
387                return new StringBuffer((String)map.get(key));
388            }
389        }
390    
391    
392    }