1   /*
2   *
3   * ====================================================================
4   *
5   * Licensed to the Apache Software Foundation (ASF) under one or more
6   * contributor license agreements.  See the NOTICE file distributed with
7   * this work for additional information regarding copyright ownership.
8   * The ASF licenses this file to You under the Apache License, Version 2.0
9   * (the "License"); you may not use this file except in compliance with
10  * 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, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21  package org.apache.commons.i18n;
22  
23  import java.util.Locale;
24  import java.util.Map;
25  
26  import org.apache.commons.i18n.bundles.MessageBundle;
27  
28  /**
29   * @author Daniel Florey
30   *
31   */
32  public class ResourceBundleMessageProviderTest extends MessageProviderTestBase {
33      public ResourceBundleMessageProviderTest(String testName) {
34          super(testName);
35      }
36  
37      public void testInstallResourceBundle() {
38          MessageBundle testMessage = new MessageBundle("helloWorld");
39  
40          try {
41              testMessage.getTitle(Locale.GERMAN);
42              fail("ResourceBundle not installed, should throw exception");
43          }
44          catch(MessageNotFoundException mnfex) {
45              assertEquals("No MessageProvider registered", mnfex.getMessage());
46          }
47  
48          MessageManager.addMessageProvider("messageBundle", new ResourceBundleMessageProvider("messageBundle"));
49  
50          assertEquals("Hallo Welt", testMessage.getTitle(Locale.GERMAN));
51  
52          MessageManager.removeMessageProvider("messageBundle");
53  
54          try {
55              testMessage.getTitle(Locale.GERMAN);
56              fail("ResourceBundle uinstalled, should throw exception");
57          }
58          catch(MessageNotFoundException mnfex) {
59              assertEquals("No MessageProvider registered", mnfex.getMessage());
60          }
61      }
62  
63      public void testGetText() {
64          ResourceBundleMessageProvider rbmp = new ResourceBundleMessageProvider("messageBundle");
65  
66          super.testGetText(rbmp);
67  
68          // TODO: Wait for Daniels reply on whether this is intended
69          assertEquals("Fallback when only in default", "This entry is not translated to any other languages",
70                  rbmp.getText("helloWorld", "notTranslated", Locale.GERMAN));
71  
72          // Test with list resource bundle
73          ResourceBundleMessageProvider listResourceBundleProvider =
74                  new ResourceBundleMessageProvider("org.apache.commons.i18n.MyListResourceBundle"); // Install ListResourceBundle
75          assertEquals("Value from ListResourceBundle", "listResourceValue", listResourceBundleProvider.getText("helloWorld", "title", Locale.US));
76          assertEquals("Value from ListResourceBundle", "1", listResourceBundleProvider.getText("helloWorld", "text", Locale.US));
77  
78          try {
79              new ResourceBundleMessageProvider("nonExistentBundle");
80              fail("Bundle does not exist and should cause error");
81          }
82          catch(MessageNotFoundException mnfex) {
83              assertEquals("Could not find resource bundle with base name nonExistentBundle, uninstalling it", mnfex.getMessage());
84          }
85      }
86  
87      public void testGetEntries() {
88  //        ResourceBundleMessageProvider.install("messageBundle");
89          Map usEntries = new ResourceBundleMessageProvider("messageBundle").getEntries("helloWorld", Locale.US);
90          assertEquals("Default locale, no of entries", 3, usEntries.size());
91          assertEquals("Default locale, titel", "Hello World", usEntries.get("title"));
92          assertEquals("Default locale, text", "Hello World, we are in {0}.", usEntries.get("text"));
93          assertEquals("This entry is not translated to any other languages", usEntries.get("notTranslated"));
94  
95          Map germanEntries = new ResourceBundleMessageProvider("messageBundle").getEntries("helloWorld", Locale.GERMAN);
96          assertEquals("No of entries", 3, germanEntries.size());
97          assertEquals("Hallo Welt", germanEntries.get("title"));
98          assertEquals("Hallo Welt, wir sind in {0}.", germanEntries.get("text"));
99          assertEquals("This entry is not translated to any other languages", germanEntries.get("notTranslated"));
100 
101         Map frenchEntries = new ResourceBundleMessageProvider("messageBundle").getEntries("helloWorld", Locale.FRENCH);
102         assertEquals("Fallback locale, no of entries", 3, frenchEntries.size());
103         assertEquals("Fallback locale, titel", "Hello World", frenchEntries.get("title"));
104         assertEquals("Fallback locale, text", "Hello World, we are in {0}.", frenchEntries.get("text"));
105         assertEquals("This entry is not translated to any other languages", frenchEntries.get("notTranslated"));
106 
107         try {
108             ResourceBundleMessageProvider provider = new ResourceBundleMessageProvider("messageBundle");
109             provider.getEntries("fooBar", Locale.GERMAN);
110             fail("Bundle does not exist and should cause error");
111         }
112         catch(MessageNotFoundException mnfex) {
113             assertEquals("No message entries found for bundle with key fooBar", mnfex.getMessage());
114         }
115 
116         try {
117             new ResourceBundleMessageProvider("nonExistentBundle");
118             fail("Bundle does not exist and should cause error");
119         }
120         catch(MessageNotFoundException mnfex) {
121             assertEquals("Could not find resource bundle with base name nonExistentBundle, uninstalling it", mnfex.getMessage());
122         }
123     }
124 }