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 junit.framework.TestCase;
21
22 import java.util.Locale;
23 import java.util.Map;
24 import java.util.HashMap;
25 import java.text.MessageFormat;
26
27
28
29
30
31
32
33 public abstract class MockProviderTestBase extends TestCase {
34
35 public void tearDown() {
36
37 MessageManager.clearMessageProviders();
38 }
39
40
41
42
43 protected void addMockProvider() {
44 addMockProvider("mock");
45 }
46
47
48
49
50 protected void addMockProvider(final String providerId) {
51
52 MessageProvider mockMessageProvider = new MessageProvider() {
53 public String getText(String id, String entry, Locale locale) throws MessageNotFoundException {
54 return MockProviderTestBase.getMockString(providerId, id, entry, locale);
55 }
56
57 public Map getEntries(String id, Locale locale) throws MessageNotFoundException {
58 Map output = new HashMap();
59 output.put("entry1", MockProviderTestBase.getMockString(providerId,id,"entry1",locale));
60 output.put("entry2", MockProviderTestBase.getMockString(providerId,id,"entry2",locale));
61 return output;
62 }
63 };
64
65 MessageManager.addMessageProvider(providerId, mockMessageProvider);
66 }
67
68
69
70
71 protected void addThrowingMockProvider() {
72 MessageManager.addMessageProvider("throwingMock", new MessageProvider() {
73 public String getText(String id, String entry, Locale locale) throws MessageNotFoundException {
74 return null;
75 }
76
77 public Map getEntries(String id, Locale locale) throws MessageNotFoundException {
78 throw new MessageNotFoundException("Mock exception from getEntries()");
79 }
80 });
81 }
82
83 protected void removeThrowingMockProvider() {
84 MessageManager.removeMessageProvider("throwingMock");
85 }
86
87
88
89
90
91 public static String getMockString(String providerId, String id, String entry, Locale locale) throws MessageNotFoundException {
92 return ((providerId != null) ? "Source=" + providerId + " " : "") +
93 "Id=" + id + " Entry=" + entry + " Locale=" + locale + "";
94 }
95
96 public static String getMockString(String id, String entry, Locale locale) throws MessageNotFoundException {
97 return getMockString("mock", id, entry, locale);
98 }
99
100 public static String getFormattedMockString(String providerId, String id, String entry, String[] arguments, Locale locale) {
101 return MessageFormat.format(getMockString(providerId, id, entry, locale), arguments);
102 }
103
104 public static String getFormattedMockString(String id, String entry, String[] arguments, Locale locale) {
105 return MessageFormat.format(getMockString(id, entry, locale), arguments);
106 }
107 }