1
2
3
4
5
6
7
8 /
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public class XMLI18nTest extends LedgeTestCase
57 {
58 /*** i18n component */
59 private XMLI18n i18n;
60
61 public void setUp()
62 throws Exception
63 {
64 FileSystemProvider lfs = new LocalFileSystemProvider("local", "src/test/resources");
65 FileSystemProvider cfs = new ClasspathFileSystemProvider("classpath",
66 getClass().getClassLoader());
67 FileSystem fs = new FileSystem(new FileSystemProvider[] { lfs, cfs }, 4096, 4096);
68 try
69 {
70 InputSource source = new InputSource(
71 fs.getInputStream("config/org.objectledge.logging.LoggingConfigurator.xml"));
72 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
73 Document logConfig = builder.parse(source);
74 LedgeDOMConfigurator configurator = new LedgeDOMConfigurator(fs);
75 configurator.doConfigure(logConfig.getDocumentElement(),
76 LogManager.getLoggerRepository());
77
78 Configuration config = getConfig(fs, "config/org.objectledge.i18n.I18n.xml");
79 Logger logger = Logger.getLogger(XMLI18n.class);
80 XMLValidator validator = new XMLValidator(new XMLGrammarCache());
81 i18n = new XMLI18n(config, new Log4JLogger(logger), fs, validator);
82 }
83 catch (Exception e)
84 {
85 throw new RuntimeException(e);
86 }
87
88 }
89
90 public void testInit()
91 {
92 i18n.reload();
93 }
94
95
96
97
98 public void testGetDefaultLocale()
99 {
100 Locale plLocale = new Locale("pl","PL");
101 assertEquals(plLocale, i18n.getDefaultLocale());
102 }
103
104
105
106
107 public void testGetLocaleString()
108 {
109 Locale plLocale = new Locale("pl","PL");
110 Locale enLocale = new Locale("en","EN");
111 assertEquals("foo2",i18n.get(plLocale,"foo2"));
112 assertEquals("bar",i18n.get(plLocale,"foo.bar.foo"));
113 assertEquals("bar",i18n.get(enLocale,"foo.bar.foo"));
114 assertEquals("bar",i18n.get(enLocale,"bar.foo.bar.foo"));
115 assertEquals("foo&bar",i18n.get(plLocale,"bar.foo.foobar"));
116 }
117
118
119
120
121 public void testGetLocaleStringStringArray()
122 {
123 Locale plLocale = new Locale("pl","PL");
124 String key = "foo_$1_bar_$2";
125 String[] values = new String[]{"foo","bar"};
126 assertEquals("foo_foo_bar_bar", i18n.get(plLocale, key, values));
127 }
128
129 public void testGetTool()
130 {
131 I18nTool tool = new I18nTool(i18n, i18n.getDefaultLocale(), null);
132 assertNotNull(tool);
133 String key = "foo_$1_bar_$2";
134 String[] values = new String[]{"foo","bar"};
135 assertEquals("foo_foo_bar_bar", tool.get(key, values));
136 assertEquals("bar", tool.get("foo.bar.foo"));
137 tool = tool.usePrefix("foo");
138 assertEquals("bar", tool.get("bar.foo"));
139 tool = tool.usePrefix("bar");
140 assertEquals("bar", tool.get("foo"));
141 tool = tool.useLocale("en_EN");
142 assertEquals("bar", tool.get("foo"));
143 String output = tool.get(key, values);
144 assertEquals("foo.bar.foo_foo_bar_bar", output);
145 tool = tool.usePrefix("");
146 }
147
148 public void testUndefined()
149 {
150 Locale plLocale = new Locale("pl","PL");
151 Locale noLocale = new Locale("no","NO");
152 assertEquals("foo2", i18n.get(noLocale, "foo2"));
153 assertEquals("undefined", i18n.get(plLocale, "undefined"));
154 }
155 }