View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.cli2.resource;
20  
21  import java.util.Locale;
22  import java.util.MissingResourceException;
23  import java.util.ResourceBundle;
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * A utility class used to provide internationalisation support.
29   *
30   * @author John Keyes
31   */
32  public class ResourceHelperTest extends TestCase {
33      /** system property */
34      private static final String PROP_LOCALE = "org.apache.commons.cli2.resource.bundle";
35  
36      private static ResourceHelper helper;
37  
38      /** resource bundle */
39      private ResourceBundle bundle;
40  
41      public void setUp() {
42      	System.setProperty(PROP_LOCALE, "org.apache.commons.cli2.resource.TestBundle");
43      	helper = ResourceHelper.getResourceHelper();
44      }
45      
46      public void tearDown() {
47      	System.setProperty(PROP_LOCALE, "org.apache.commons.cli2.resource.CLIMessageBundle_en_US.properties");
48      }
49      
50      /**
51       * Create a new ResourceHelper for the specified class.
52       */
53      public ResourceHelperTest() {
54      	super("ResourceHelperTest");
55      }
56      
57      public void testOverridden() {
58      	assertEquals("wrong message", "The class name \"ResourceHelper\" is invalid.", helper.getMessage("ClassValidator.bad.classname", "ResourceHelper"));
59      }
60      
61      public void testNewMessage1Param() {
62      	assertEquals("wrong message", "Some might say we will find a brighter day.", helper.getMessage("test.message"));
63      }
64  
65      public void testNewMessage2Params() {
66      	assertEquals("wrong message", "Some might say we will find a brighter day.", helper.getMessage("test.message", "Some"));
67      }
68  
69      public void testNewMessage3Params() {
70      	assertEquals("wrong message", "Some might say we will find a brighter day.", helper.getMessage("test.message", "Some", "might"));
71      }
72  
73      public void testNewMessage4Params() {
74      	assertEquals("wrong message", "Some might say we will find a brighter day.", helper.getMessage("test.message", "Some", "might", "say"));
75      }
76  
77      public void testDefaultBundle() {
78      	System.setProperty(PROP_LOCALE, "madeupname.properties");
79      	helper = ResourceHelper.getResourceHelper();
80      	assertEquals("wrong message", "The class name \"ResourceHelper\" is invalid.", helper.getMessage("ClassValidator.bad.classname", "ResourceHelper"));
81      }
82  }