1   /*
2    * $Id: CollectionResourcesBaseTestCase.java 354761 2005-12-07 15:11:58Z niallp $
3    * $Revision: 354761 $
4    * $Date: 2005-12-07 15:11:58 +0000 (Wed, 07 Dec 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2003-2005 The Apache Software Foundation
9    * 
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   *
22   */
23  
24  package org.apache.commons.resources.impl;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Locale;
29  
30  import junit.framework.Test;
31  import junit.framework.TestSuite;
32  
33  import org.apache.commons.resources.ResourcesException;
34  import org.apache.commons.resources.ResourcesKeyException;
35  
36  /**
37   * <p>Unit tests for
38   * <code>org.apache.commons.resources.impl.CollectionResourcesBase</code>.
39   * </p>
40   */
41  public class CollectionResourcesBaseTestCase extends ResourcesBaseTestCase {
42  
43  
44      // ----------------------------------------------------- Instance Variables
45  
46  
47      // Base URL of the resource files for the Resources instance to be created
48      private static final String BASE = "fixme";
49  
50  
51      // ----------------------------------------------------------- Constructors
52  
53  
54      public CollectionResourcesBaseTestCase(String name) {
55          super(name);
56      }
57  
58  
59      // --------------------------------------------------- Overall Test Methods
60  
61  
62      // Set up instance variables required by this test case
63      public void setUp() throws Exception {
64          factory = new CollResourcesFactory();
65          resources = factory.getResources(NAME, getBase());
66      }
67  
68      // Return the tests included in this test suite
69      public static Test suite() {
70          return (new TestSuite(CollectionResourcesBaseTestCase.class));
71      }
72  
73      // Tear down the instance variables required by this test case
74      public void tearDown() {
75          resources = null;
76          factory = null;
77      }
78  
79      protected String getBase() throws Exception {
80          return BASE;
81      }
82  
83      // ------------------------------------------------ Individual Test Methods
84  
85  
86      // Test retrieving Strings that should be inherited from parent Locales
87      public void testInherit() throws Exception {
88  
89          Locale locale = null;
90  
91          locale = new Locale("en", "US");
92          assertEquals("en_US inherit value",
93                       "[en] INHERIT",
94                       resources.getString("test.inherit", locale));
95  
96          locale = new Locale("fr", "FR");
97          assertEquals("fr_FR inherit value",
98                       "[fr] INHERIT",
99                       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 }