1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   */
18  package org.apache.commons.i18n;
19  
20  import java.util.Locale;
21  import java.util.Map;
22  
23  /**
24   * @author Mattias Jiderhamn
25   */
26  public class MessageManagerTest extends MockProviderTestBase {
27  
28      /** Dummy to add to constructor to coverage report */
29      public void testDummy() {
30          new MessageManager();
31      }
32  
33      public void testGetText() {
34          assertEquals("Default text used", "defaultText",
35                  MessageManager.getText("dummyId", "dummyEntry", null, Locale.US, "defaultText"));
36          assertEquals("Default text with arguments", "defaultText with value",
37                  MessageManager.getText("dummyId", "dummyEntry", new String[] {"with value"},
38                          Locale.US, "defaultText {0}"));
39          try {
40              MessageManager.getText("dummyId", "dummyEntry", null, Locale.US);
41              fail("Entry not found should cause error");
42          }
43          catch(MessageNotFoundException mnfex) {
44              assertEquals("Error text", "No MessageProvider registered", mnfex.getMessage());
45          }
46  
47          addThrowingMockProvider(); // Add mock provider always throwing exceptions
48  
49          try {
50              MessageManager.getText("dummyId", "dummyEntry", null, Locale.US);
51              fail("Mock provider should throw Exception");
52          }
53          catch(MessageNotFoundException mnfex) {
54              // TODO: What error message is most correct (here and elsewhere)?
55              // assertEquals("Error text", "No message entries found for bundle with key dummyId", mnfex.getMessage());
56              assertEquals("Error text", "Message bundle with key dummyId does not contain an entry with key dummyEntry", mnfex.getMessage());
57          }
58  
59          addMockProvider(); // Add mock provider
60  
61          assertEquals("Throwing mock not used", "Source=mock Id=dummyId Entry=dummyEntry Locale=en_US",
62                  MessageManager.getText("dummyId", "dummyEntry", null, Locale.US, "defaultText"));
63  
64          removeThrowingMockProvider(); // Removing throwing mock and keep only normal mock
65  
66          assertEquals("Default text not used", "Source=mock Id=dummyId Entry=dummyEntry Locale=en_US",
67                  MessageManager.getText("dummyId", "dummyEntry", null, Locale.US, "defaultText"));
68  
69          assertEquals("Normal lookup", "Source=mock Id=id Entry=entry Locale=en_US",
70                  MessageManager.getText("id", "entry", null, Locale.US));
71          assertEquals("Single argument",
72                  "Source=mock Id=id Entry=entry value1 Locale=en_US",
73                  MessageManager.getText("id", "entry {0}", new String[] {"value1"}, Locale.US));
74          assertEquals("Multiple arguments",
75                  "Source=mock Id=id Entry=entry value0: value1 Locale=en_US",
76                  MessageManager.getText("id", "entry {0}: {1}", new String[] {"value0", "value1"},Locale.US));
77  
78          assertEquals("Single argument and default",
79                  "Source=mock Id=id Entry=entry value1 Locale=en_US",
80                  MessageManager.getText("id", "entry {0}", new String[] {"value1"},Locale.US, "defaultText"));
81  
82          // Named provider not found
83          try {
84              MessageManager.getText("mockProvider2", "dummyId", "dummyEntry", null, Locale.US);
85              fail("Unknown provider should throw Exception");
86          }
87          catch(MessageNotFoundException mnfex) {
88              assertEquals("Error text", "Provider 'mockProvider2' not installed", mnfex.getMessage());
89          }
90  
91          assertEquals("Default text used", "defaultText",
92                  MessageManager.getText("mockProvider2", "dummyId", "dummyEntry", null, Locale.US, "defaultText"));
93  
94  
95          // Named provider found
96          addMockProvider("mockProvider2");
97  
98          assertEquals("Default text not used, qualified lookup", "Source=mockProvider2 Id=dummyId Entry=dummyEntry Locale=en_US",
99                  MessageManager.getText("mockProvider2", "dummyId", "dummyEntry", null, Locale.US, "defaultText"));
100 
101         assertEquals("Normal qualified lookup", "Source=mockProvider2 Id=id Entry=entry Locale=en_US",
102                 MessageManager.getText("mockProvider2", "id", "entry", null, Locale.US));
103 
104         // Named provider found, but fails to find message
105         addThrowingMockProvider(); // Add mock provider returning null for unknown entries
106         try {
107             MessageManager.getText("throwingMock", "dummyId", "dummyEntry", null, Locale.US);
108             fail("Unknown provider should throw Exception");
109         }
110         catch(MessageNotFoundException mnfex) {
111             assertEquals("Error text", "Message bundle with key dummyId does not contain an entry with key dummyEntry", mnfex.getMessage());
112         }
113 
114     }
115 
116     public void testGetEntries() {
117         try {
118             MessageManager.getEntries("dummyId", Locale.US);
119             fail("Entry not found should cause error");
120         }
121         catch(MessageNotFoundException mnfex) {
122             assertEquals("Error text", "No MessageProvider registered", mnfex.getMessage());
123         }
124 
125         addThrowingMockProvider(); // Add mock provider always throwing exceptions
126 
127         try {
128             MessageManager.getEntries("dummyId", Locale.US);
129             fail("Mock provider should throw Exception");
130         }
131         catch(MessageNotFoundException mnfex) {
132             assertEquals("Error text", "Mock exception from getEntries()", mnfex.getMessage());
133         }
134 
135         addMockProvider(); // Add mock provider
136 
137         Map entries = MessageManager.getEntries("dummyId", Locale.US);
138         assertEquals("No of entries", 2, entries.size());
139         assertEquals("Entry 1 match", "Source=mock Id=dummyId Entry=entry1 Locale=en_US", entries.get("entry1"));
140         assertEquals("Entry 2 match", "Source=mock Id=dummyId Entry=entry2 Locale=en_US", entries.get("entry2"));
141 
142         removeThrowingMockProvider(); // Removing throwing mock and keep only normal mock
143 
144         addMockProvider(); // Add mock provider
145 
146         entries = MessageManager.getEntries("dummyId", Locale.US);
147         assertEquals("No of entries", 2, entries.size());
148         assertEquals("Entry 1 match", "Source=mock Id=dummyId Entry=entry1 Locale=en_US", entries.get("entry1"));
149         assertEquals("Entry 2 match", "Source=mock Id=dummyId Entry=entry2 Locale=en_US", entries.get("entry2"));
150 
151         // Named provider not found
152         try {
153             MessageManager.getEntries("mockProvider2", "dummyId", Locale.US);
154             fail("Unknown provider should throw Exception");
155         }
156         catch(MessageNotFoundException mnfex) {
157             assertEquals("Error text", "Provider 'mockProvider2' not installed", mnfex.getMessage());
158         }
159 
160         // Named provider found
161         addMockProvider("mockProvider2");
162 
163         entries = MessageManager.getEntries("mockProvider2", "dummyId", Locale.US);
164         assertEquals("No of entries", 2, entries.size());
165         assertEquals("Entry 1 match", "Source=mockProvider2 Id=dummyId Entry=entry1 Locale=en_US", entries.get("entry1"));
166         assertEquals("Entry 2 match", "Source=mockProvider2 Id=dummyId Entry=entry2 Locale=en_US", entries.get("entry2"));
167     }
168 }