1   /*
2    * $Id: ResourcesBaseTestCase.java 354330 2005-12-06 06:05:19Z niallp $
3    * $Revision: 354330 $
4    * $Date: 2005-12-06 06:05:19 +0000 (Tue, 06 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.Iterator;
28  import java.util.List;
29  import java.util.Locale;
30  
31  import junit.framework.Test;
32  import junit.framework.TestSuite;
33  
34  import org.apache.commons.resources.Resources;
35  import org.apache.commons.resources.ResourcesException;
36  import org.apache.commons.resources.ResourcesKeyException;
37  
38  /**
39   * <p>Unit tests for
40   * <code>org.apache.commons.resources.impl.ResourcesBase</code>.
41   * </p>
42   */
43  public class ResourcesBaseTestCase extends ResourcesFactoryBaseTestCase {
44  
45  
46      // ----------------------------------------------------- Instance Variables
47  
48  
49      // The Resources instance to be tested
50      protected Resources resources = null;
51  
52  
53      // ----------------------------------------------------------- Constructors
54  
55  
56      public ResourcesBaseTestCase(String name) {
57          super(name);
58      }
59  
60  
61      // --------------------------------------------------- Overall Test Methods
62  
63  
64      // Set up instance variables required by this test case
65      public void setUp() throws Exception {
66          super.setUp();
67          resources = factory.getResources(NAME);
68      }
69  
70      // Return the tests included in this test suite
71      public static Test suite() {
72          return (new TestSuite(ResourcesBaseTestCase.class));
73      }
74  
75      // Tear down the instance variables required by this test case
76      public void tearDown() {
77          resources = null;
78          super.tearDown();
79      }
80  
81  
82      // ------------------------------------------------ Individual Test Methods
83  
84  
85      // Test retrieving Strings that should be inherited from parent Locales
86      public void testInherit() throws Exception {
87  
88          Locale locale = null;
89  
90          locale = new Locale("en", "US");
91          assertEquals("en_US inherit value",
92                       "[test] INHERIT",
93                       resources.getString("test.inherit", locale));
94  
95          locale = new Locale("fr", "FR");
96          assertEquals("fr_FR inherit value",
97                       "[test] INHERIT",
98                       resources.getString("test.inherit", locale));
99  
100     }
101 
102 
103     // Test retrieving all of the defined keys
104     public void testKeys() throws Exception {
105 
106         // Pre-seed all of the keys we expect to find
107         Locale locale = new Locale("en", "US");
108         resources.getString("test.base", locale);
109         resources.getString("test.specific", locale);
110         resources.getString("test.inherit", locale);
111         resources.getString("test.message", locale);
112 
113         // Accumulate all of the keys that are actually returned
114         List results = new ArrayList();
115         Iterator keys = resources.getKeys();
116         while (keys.hasNext()) {
117             results.add(keys.next());
118         }
119 
120         // Validate the keys that were actually returned
121         assertEquals(5, results.size());
122         assertTrue(results.contains("test.base"));
123         assertTrue(results.contains("test.specific"));
124         assertTrue(results.contains("test.inherit"));
125         assertTrue(results.contains("test.message"));
126 
127     }
128 
129 
130     // Test retrieving missing resource keys
131     public void testMissing() throws Exception {
132 
133         Locale locale = new Locale("en","");
134         try {
135             resources.setReturnNull(false);
136             resources.getString("test.missing", locale);
137             fail("Should have thrown ResourcesException");
138         } catch (ResourcesKeyException e) {
139             // Expected result
140         }
141         try {
142             resources.setReturnNull(true);
143             String value = resources.getString("test.missing", locale);
144             assertTrue("Should have returned null", value == null);
145         } catch (ResourcesException e) {
146             fail("Should have returned null");
147         }
148 
149     }
150 
151 
152     // Test the characteristics of a newly created instance
153     public void testPristine() {
154         assertNotNull(resources);
155         assertEquals("Correct name", NAME, resources.getName());
156         if (resources instanceof TestResources) {
157             assertTrue("Resources initialized",
158                        ((TestResources) resources).isInitialized());
159         }
160     }
161 
162 
163     // Test that releasing should destroy instances
164     public void testRelease() throws Exception {
165         factory.release();
166         if (resources instanceof TestResources) {
167             assertTrue("Resources destroyed",
168                        !((TestResources) resources).isInitialized());
169         }
170     }
171 
172 
173     // Test retrieving Locale-specific Strings explicitly
174     public void testSpecific() throws Exception {
175 
176         Locale locale = null;
177 
178         locale = new Locale("en","");
179         assertEquals("en specific value",
180                      "[test] SPECIFIC",
181                      resources.getString("test.specific", locale));
182 
183         locale = new Locale("en", "US");
184         assertEquals("en_US specific value",
185                      "[test] SPECIFIC",
186                      resources.getString("test.specific", locale));
187 
188         locale = new Locale("fr","");
189         assertEquals("fr specific value",
190                      "[test] SPECIFIC",
191                      resources.getString("test.specific", locale));
192 
193     }
194 
195 
196 }