1   /*
2    * $Id: ResourceBundleResourcesTestCase.java 354330 2005-12-06 06:05:19Z niallp $
3    * $Revision: 354330 $
4    * $Date: 2005-12-06 01:05:19 -0500 (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.Locale;
27  
28  import junit.framework.Test;
29  import junit.framework.TestSuite;
30  
31  import org.apache.commons.resources.Resources;
32  import org.apache.commons.resources.ResourcesException;
33  import org.apache.commons.resources.ResourcesKeyException;
34  
35  /***
36   * <p>Unit tests for
37   * <code>org.apache.commons.resources.impl.ResourceBundleResources</code>.
38   * </p>
39   */
40  public class ResourceBundleResourcesTestCase
41      extends ResourceBundleResourcesFactoryTestCase {
42  
43  
44      // ----------------------------------------------------- Instance Variables
45  
46  
47      // The Resources instance to be tested
48      protected Resources resources = null;
49  
50  
51      // ----------------------------------------------------------- Constructors
52  
53  
54      public ResourceBundleResourcesTestCase(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          super.setUp();
65          resources = factory.getResources(NAME, CONFIG);
66      }
67  
68      // Return the tests included in this test suite
69      public static Test suite() {
70          return (new TestSuite(ResourceBundleResourcesTestCase.class));
71      }
72  
73      // Tear down the instance variables required by this test case
74      public void tearDown() {
75          resources = null;
76          super.tearDown();
77      }
78  
79  
80      // ------------------------------------------------ Individual Test Methods
81  
82  
83      // Test retrieving Strings that should be inherited from parent Locales
84      public void testInherit() throws Exception {
85  
86          Locale locale = null;
87  
88          locale = new Locale("en", "US");
89          assertEquals("en_US inherit value",
90                       "[en] INHERIT",
91                       resources.getString("test.inherit", locale));
92  
93          locale = new Locale("fr", "FR");
94          assertEquals("fr_FR inherit value",
95                       "[fr] INHERIT",
96                       resources.getString("test.inherit", locale));
97  
98      }
99  
100 
101     // Test retrieving missing resource keys
102     public void testMissing() throws Exception {
103 
104         Locale locale = new Locale("en","");
105         try {
106             resources.setReturnNull(false);
107             resources.getString("test.missing", locale);
108             fail("Should have thrown ResourcesException");
109         } catch (ResourcesKeyException e) {
110             // Expected result
111         }
112         try {
113             resources.setReturnNull(true);
114             String value = resources.getString("test.missing", locale);
115             assertTrue("Should have returned null", value == null);
116         } catch (ResourcesException e) {
117             fail("Should have returned null");
118         }
119 
120     }
121 
122 
123     // Test the characteristics of a newly created instance
124     public void testPristine() {
125         assertNotNull(resources);
126         assertEquals("Correct name", NAME, resources.getName());
127         assertEquals("Correct base", CONFIG,
128                      ((ResourceBundleResources) resources).getBase());
129     }
130 
131 
132     // Test retrieving Locale-specific Strings explicitly
133     public void testSpecific() throws Exception {
134 
135         Locale locale = null;
136 
137         locale = new Locale("en","");
138         assertEquals("en specific value",
139                      "[en] SPECIFIC",
140                      resources.getString("test.specific", locale));
141 
142         locale = new Locale("en", "US");
143         assertEquals("en_US specific value",
144                      "[en_US] SPECIFIC",
145                      resources.getString("test.specific", locale));
146 
147         locale = new Locale("fr","");
148         assertEquals("fr specific value",
149                      "[fr] SPECIFIC",
150                      resources.getString("test.specific", locale));
151 
152     }
153 
154 
155 }