1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
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
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
77
78
79
80
81
82
83
84
85
86
87
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
111
112 public XMLMessageProviderTest(String testName) {
113 super(testName);
114 }
115 }