1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.i18n;
19
20 import java.util.Locale;
21 import java.util.Map;
22
23
24
25
26 public class MessageManagerTest extends MockProviderTestBase {
27
28
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();
48
49 try {
50 MessageManager.getText("dummyId", "dummyEntry", null, Locale.US);
51 fail("Mock provider should throw Exception");
52 }
53 catch(MessageNotFoundException mnfex) {
54
55
56 assertEquals("Error text", "Message bundle with key dummyId does not contain an entry with key dummyEntry", mnfex.getMessage());
57 }
58
59 addMockProvider();
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();
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
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
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
105 addThrowingMockProvider();
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();
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();
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();
143
144 addMockProvider();
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
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
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 }