View Javadoc
1   package org.apache.fulcrum.localization;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.fail;
24  
25  import java.util.Locale;
26  import java.util.MissingResourceException;
27  
28  import org.apache.fulcrum.testcontainer.BaseUnit5Test;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests the API of the
34   * {@link org.apache.fulcrum.localization.LocalizationService}.
35   *
36   * @author <a href="mailto:mcconnell@apache.org">Stephen McConnell</a>
37   * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
38   * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
39   */
40  public class LocalizationTest extends BaseUnit5Test
41  {
42  
43      private LocalizationService localizationService = null;
44  
45      @BeforeEach
46      public void setUp() throws Exception
47      {
48      	localizationService = (LocalizationService) lookup(LocalizationService.ROLE);
49      }
50      
51      
52      /**
53       * Test localization
54       * @throws Exception generic exception
55       */
56      @Test
57      public void testLocalization() throws Exception
58      {
59          // Test retrieval of text using multiple default bundles
60          String s = localizationService.getString(null, null, "key1");
61          assertEquals("value1", s, "Unable to retrieve localized text for locale: default");
62          
63          s = localizationService.getString(null, new Locale("en", "US"), "key2");
64          assertEquals("value2", s, "Unable to retrieve localized text for locale: en-US");
65          
66          s = localizationService.getString("org.apache.fulcrum.localization.BarBundle", new Locale("ko", "KR"), "key3");
67          assertEquals(s, "[ko] value3", "Unable to retrieve localized text for locale: ko-KR");
68          
69          try
70          {
71              localizationService.getString("DoesNotExist", new Locale("ko", ""), "key1");
72              fail();
73          }
74          catch (MissingResourceException expectedFailure)
75          {
76              // Asked for resource bundle which does not exist.
77          }
78          catch( Throwable e )
79          {
80              // should not happen
81              fail();
82          }
83          
84          // When a locale is used which cannot be produced for a given
85          // bundle, fall back to the default locale.
86          s = localizationService.getString(null, new Locale("ko", "KR"), "key4");
87          assertEquals(s, "value4", "Unable to retrieve localized text for locale: default");
88          
89          try
90          {
91              localizationService.getString(null, null, "NoSuchKey");
92              fail();
93          }
94          catch (MissingResourceException expectedFailure)
95          {
96              // Asked for key from default bundle which does not exist,
97          }
98      }
99      
100     @Test
101     public void testGetString()
102     {
103         String key1 = "key1";
104         String value1 = "value1";
105         String key2 = "key2";
106         String value2 = "value2";
107         String key3 = "key3";
108         String value3 = "value3";
109         String key4 = "key4";
110         String value4 = "value4";
111 
112         assertEquals(value1, localizationService.getString(key1));
113         assertEquals(value2, localizationService.getString(key2));
114         assertEquals(value3, localizationService.getString(key3));
115         assertEquals(value4, localizationService.getString(key4));
116 
117     }
118     
119     /**
120      * Putting this in a separate test case because it fails..  Why?  I don't know.  I have never
121      * used localization, so I leave it to brains better then mine. -dep
122      * 
123      * @throws Exception generic exception
124      */
125     /*
126     public void OFFtestRetrievingOddLocale() throws Exception
127     {
128     	// TODO Figure out why this test fails!
129         String s = localizationService.getString(null, new Locale("fr", "US"), "key3");
130         assertEquals("[fr] value3", s, "Unable to retrieve localized text for locale: fr");
131     }
132     */
133 }