001    /*
002     * $Id: CollectionResourcesBaseTestCase.java 354761 2005-12-07 15:11:58Z niallp $
003     * $Revision: 354761 $
004     * $Date: 2005-12-07 15:11:58 +0000 (Wed, 07 Dec 2005) $
005     *
006     * ====================================================================
007     *
008     *  Copyright 2003-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.List;
028    import java.util.Locale;
029    
030    import junit.framework.Test;
031    import junit.framework.TestSuite;
032    
033    import org.apache.commons.resources.ResourcesException;
034    import org.apache.commons.resources.ResourcesKeyException;
035    
036    /**
037     * <p>Unit tests for
038     * <code>org.apache.commons.resources.impl.CollectionResourcesBase</code>.
039     * </p>
040     */
041    public class CollectionResourcesBaseTestCase extends ResourcesBaseTestCase {
042    
043    
044        // ----------------------------------------------------- Instance Variables
045    
046    
047        // Base URL of the resource files for the Resources instance to be created
048        private static final String BASE = "fixme";
049    
050    
051        // ----------------------------------------------------------- Constructors
052    
053    
054        public CollectionResourcesBaseTestCase(String name) {
055            super(name);
056        }
057    
058    
059        // --------------------------------------------------- Overall Test Methods
060    
061    
062        // Set up instance variables required by this test case
063        public void setUp() throws Exception {
064            factory = new CollResourcesFactory();
065            resources = factory.getResources(NAME, getBase());
066        }
067    
068        // Return the tests included in this test suite
069        public static Test suite() {
070            return (new TestSuite(CollectionResourcesBaseTestCase.class));
071        }
072    
073        // Tear down the instance variables required by this test case
074        public void tearDown() {
075            resources = null;
076            factory = null;
077        }
078    
079        protected String getBase() throws Exception {
080            return BASE;
081        }
082    
083        // ------------------------------------------------ Individual Test Methods
084    
085    
086        // Test retrieving Strings that should be inherited from parent Locales
087        public void testInherit() throws Exception {
088    
089            Locale locale = null;
090    
091            locale = new Locale("en", "US");
092            assertEquals("en_US inherit value",
093                         "[en] INHERIT",
094                         resources.getString("test.inherit", locale));
095    
096            locale = new Locale("fr", "FR");
097            assertEquals("fr_FR inherit value",
098                         "[fr] INHERIT",
099                         resources.getString("test.inherit", locale));
100    
101        }
102    
103    
104        // Test the getLocaleList() method
105        public void testLocaleList() {
106    
107            Locale test = null;
108            List expected = null;
109            CollectionResourcesBase cresources =
110                (CollectionResourcesBase) resources;
111    
112            test = new Locale("en", "US", "POSIX");
113            expected = new ArrayList();
114            expected.add(test);
115            expected.add(new Locale("en", "US"));
116            expected.add(new Locale("en", ""));
117            expected.add(new Locale("", ""));
118            checkLocaleList(test, expected, cresources.getLocaleList(test));
119    
120            test = new Locale("en", "US");
121            expected = new ArrayList();
122            expected.add(test);
123            expected.add(new Locale("en", ""));
124            expected.add(new Locale("", ""));
125            checkLocaleList(test, expected, cresources.getLocaleList(test));
126    
127            test = new Locale("en", "");
128            expected = new ArrayList();
129            expected.add(test);
130            expected.add(new Locale("",""));
131            checkLocaleList(test, expected, cresources.getLocaleList(test));
132    
133            test = new Locale("", "US", "POSIX");
134            expected = new ArrayList();
135            expected.add(test);
136            expected.add(new Locale("", "US"));
137            expected.add(new Locale("", ""));
138            checkLocaleList(test, expected, cresources.getLocaleList(test));
139    
140            test = new Locale("", "US");
141            expected = new ArrayList();
142            expected.add(test);
143            expected.add(new Locale("", ""));
144            checkLocaleList(test, expected, cresources.getLocaleList(test));
145    
146        }
147    
148    
149        // Test retrieving missing resource keys
150        public void testMissing() throws Exception {
151    
152            Locale locale = new Locale("en","");
153            try {
154                resources.setReturnNull(true);
155                String value = resources.getString("test.missing", locale);
156                assertTrue("Should have returned null", value == null);
157            } catch (ResourcesException e) {
158                fail("Should have returned null");
159            }
160            
161            try {
162                resources.setReturnNull(false);
163                resources.getString("test.missing", locale);
164                fail("Should have thrown ResourcesException");
165            } catch (ResourcesKeyException e) {
166                // Expected result
167            }
168    
169        }
170    
171    
172        // Test retrieving Locale-specific Strings explicitly
173        public void testSpecific() throws Exception {
174    
175            Locale locale = null;
176    
177            locale = new Locale("en", "");
178            assertEquals("en specific value",
179                         "[en] SPECIFIC",
180                         resources.getString("test.specific", locale));
181    
182            locale = new Locale("en", "US");
183            assertEquals("en_US specific value",
184                         "[en_US] SPECIFIC",
185                         resources.getString("test.specific", locale));
186    
187            locale = new Locale("fr", "");
188            assertEquals("fr specific value",
189                         "[fr] SPECIFIC",
190                         resources.getString("test.specific", locale));
191    
192        }
193    
194    
195        // ------------------------------------------------------ Protected Methods
196    
197    
198        // Check the results of a getLocaleList() operation
199        protected void checkLocaleList(Locale test, List expected, List actual) {
200    
201            assertEquals("[" + test + "] list length",
202                         expected.size(),
203                         actual.size());
204            for (int i = 0; i < expected.size(); i++) {
205                assertEquals("[" + test + "] list contents[" + i + "]",
206                             expected.get(i),
207                             actual.get(i));
208            }
209    
210        }
211    
212    
213    }