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  
25  import org.apache.commons.i18n.bundles.MessageBundle;
26  
27  /**
28   * @author Daniel Florey
29   *
30   */
31  public class XMLMessageProviderTest extends MessageProviderTestBase {
32  
33      public void testInstallResourceBundle() {
34          MessageBundle testMessage = new MessageBundle("helloWorld");
35  
36          try {
37              testMessage.getTitle(Locale.GERMAN);
38              fail("XML file not installed, should throw exception");
39          }
40          catch(MessageNotFoundException mnfex) {
41              assertEquals("No MessageProvider registered", mnfex.getMessage());
42          }
43  
44          MessageManager.addMessageProvider("org.apache.commons-i18n.test", new XMLMessageProvider(
45                      Thread.currentThread().getContextClassLoader().getResourceAsStream("testMessages.xml")));
46  
47          assertEquals("Hallo Welt", testMessage.getTitle(Locale.GERMAN));
48  
49          MessageManager.removeMessageProvider("org.apache.commons-i18n.test");
50  
51          try {
52              testMessage.getTitle(Locale.GERMAN);
53              fail("XML file uinstalled, should throw exception");
54          }
55          catch(MessageNotFoundException mnfex) {
56              assertEquals("No MessageProvider registered", mnfex.getMessage());
57          }
58  
59          // Try to parse non-XML file
60          try {
61              new XMLMessageProvider(
62                      Thread.currentThread().getContextClassLoader().getResourceAsStream("messageBundle.properties"));
63              fail("Parsing non-XML file should fail");
64          }
65          catch(RuntimeException rtex) {
66              assertEquals("Error while parsing message file", rtex.getMessage());
67          }
68      }
69      
70      public void testGetText() {
71          XMLMessageProvider xmlmp = new XMLMessageProvider(
72                  Thread.currentThread().getContextClassLoader().getResourceAsStream("testMessages.xml"));
73  
74          super.testGetText(xmlmp);
75  
76          // TODO: Wait for Daniels reply on whether this is intended
77          // assertEquals("Fallback when only in default", "This entry is not translated to any other languages",
78          //         xmlmp.getText("helloWorld", "notTranslated", Locale.GERMAN));
79  
80          // TODO: Wait for Daniels reply on whether this is intended
81          /*
82          try {
83              String s = xmlmp.getText("helloWorld", "nonExistentEntry", Locale.US);
84              fail("Entry does not exist, should throw exception. Entry was: '" + s + "'");
85          }
86          catch(MessageNotFoundException mnfex) {
87              assertEquals("No message entries found for bundle with key helloWorld", mnfex.getMessage());
88          }
89          */
90      }
91  
92      public void testGetTextVariants() {
93          XMLMessageProvider xmlmp = new XMLMessageProvider(
94                  Thread.currentThread().getContextClassLoader().getResourceAsStream("variantTestMessages.xml"));
95  
96          assertEquals("hello world", xmlmp.getText("variants", "theKey", Locale.ENGLISH));
97          assertEquals("Botswana", "Hello Botswana", xmlmp.getText("variants", "theKey", new Locale("", "BW")));
98          assertEquals("Awrite warld", xmlmp.getText("variants", "theKey", new Locale("en", "GB", "scottish")));
99          assertEquals("Ga, ga, ga", xmlmp.getText("variants", "theKey", new Locale("en", "", "baby")));
100     }
101 
102     public void testGetEntries() {
103         final XMLMessageProvider xmlMessageProvider = new XMLMessageProvider(
104                         Thread.currentThread().getContextClassLoader().getResourceAsStream("testMessages.xml"));
105 
106         super.testGetEntries(xmlMessageProvider, true);
107     }
108 
109     /**
110      * Constructor for MessageManagerTest.
111      */
112     public XMLMessageProviderTest(String testName) {
113         super(testName);
114     }
115 }