1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.mj.test.utils; |
|
20 |
|
21 import java.util.Date; |
|
22 |
|
23 import com.nokia.mj.impl.utils.ResourceLoader; |
|
24 import com.nokia.mj.impl.utils.Formatter; |
|
25 |
|
26 import com.nokia.mj.impl.utils.Logger; |
|
27 |
|
28 import com.nokia.mj.impl.installer.utils.InstallerMain; |
|
29 |
|
30 import j2meunit.framework.Test; |
|
31 import j2meunit.framework.TestCase; |
|
32 import j2meunit.framework.TestMethod; |
|
33 import j2meunit.framework.TestSuite; |
|
34 |
|
35 public class LocalisationTest extends TestCase implements InstallerMain |
|
36 { |
|
37 // Must have no-argument constructor for Class.forname() |
|
38 public LocalisationTest() |
|
39 { |
|
40 } |
|
41 |
|
42 public LocalisationTest(String sTestName, TestMethod rTestMethod) |
|
43 { |
|
44 super(sTestName, rTestMethod); |
|
45 } |
|
46 |
|
47 // Begin j2meunit test framework setup |
|
48 public void installerMain(String[] args) |
|
49 { |
|
50 com.nokia.mj.impl.utils.OmjTestRunner.run(suite()); |
|
51 } |
|
52 |
|
53 |
|
54 public Test suite() |
|
55 { |
|
56 // TestSuite aSuite = new TestSuite(); |
|
57 TestSuite aSuite = new TestSuite(this.getClass().getName()); |
|
58 |
|
59 aSuite.addTest(new LocalisationTest("formatterTest", new TestMethod() |
|
60 { |
|
61 public void run(TestCase tc) |
|
62 { |
|
63 ((LocalisationTest) tc).formatterTest(); |
|
64 } |
|
65 })); |
|
66 |
|
67 aSuite.addTest(new LocalisationTest("resourceLoaderTest", new TestMethod() |
|
68 { |
|
69 public void run(TestCase tc) |
|
70 { |
|
71 ((LocalisationTest) tc).resourceLoaderTest(); |
|
72 } |
|
73 })); |
|
74 |
|
75 return aSuite; |
|
76 } |
|
77 |
|
78 void formatterTest() |
|
79 { |
|
80 // Zero argument |
|
81 assertEquals( |
|
82 "Test", |
|
83 new Formatter("Test").toString()); |
|
84 |
|
85 // One argument tests |
|
86 assertEquals( |
|
87 "Test 100", |
|
88 new Formatter("Test %0N").arg(100).toString()); |
|
89 |
|
90 assertEquals( |
|
91 "Test 1", |
|
92 new Formatter("Test %C").arg('1').toString()); |
|
93 |
|
94 assertEquals( |
|
95 "Test A", |
|
96 new Formatter("Test %0C").arg('A').toString()); |
|
97 |
|
98 // Two argument tests |
|
99 assertEquals( |
|
100 "Test test 100", |
|
101 new Formatter("Test %1U %0N").arg(100).arg("test").toString()); |
|
102 |
|
103 assertEquals( |
|
104 "100 test test", |
|
105 new Formatter("%0 test %1").arg(100).arg("test").toString()); |
|
106 |
|
107 assertEquals( |
|
108 "Test A B C", |
|
109 new Formatter("Test %0C %1 %2C").arg('A').arg('B').arg('C').toString()); |
|
110 |
|
111 // Two argument tests with array |
|
112 assertEquals( |
|
113 "Test test 100", |
|
114 new Formatter("Test %1U %0N").format(new Object[] |
|
115 { |
|
116 new Integer(100), "test" |
|
117 })); |
|
118 |
|
119 assertEquals( |
|
120 "100 test test", |
|
121 new Formatter("%0 test %1").format(new Object[] |
|
122 { |
|
123 new Integer(100), "test" |
|
124 })); |
|
125 |
|
126 // Missing argument, no failure, just unreplaced parameter |
|
127 assertEquals( |
|
128 "100 test %1", |
|
129 new Formatter("%0 test %1").arg(100).toString()); |
|
130 |
|
131 // Extra argument, no failure |
|
132 assertEquals( |
|
133 "100 test", |
|
134 new Formatter("%0 test").arg(100).arg("test").toString()); |
|
135 |
|
136 Date testDate = new Date(1234567890); // 15/01/1970 |
|
137 // Test case assumes emulator locale is default as EDateEuropean. |
|
138 assertEquals("15/01/1970", new Formatter("%0").arg(testDate).toString()); |
|
139 |
|
140 Object obj = testDate; |
|
141 assertEquals("15/01/1970", new Formatter("%0").arg(obj).toString()); |
|
142 |
|
143 obj = new Integer(1); |
|
144 assertEquals("1", new Formatter("%0").arg(obj).toString()); |
|
145 |
|
146 obj = new String("me"); |
|
147 assertEquals("Test me", new Formatter("Test %0").arg(obj).toString()); |
|
148 |
|
149 // Test skip not supported type |
|
150 obj = new Boolean(false); |
|
151 assertEquals("Test %0", new Formatter("Test %0").arg(obj).toString()); |
|
152 |
|
153 // Test MAX %1U[08] |
|
154 assertEquals( |
|
155 "Test testMyTe 100", |
|
156 new Formatter("Test %1U[08] %0N").arg(100).arg("testMyTest").toString()); |
|
157 |
|
158 // Test %U[] |
|
159 assertEquals( |
|
160 "Test testMyTest", |
|
161 new Formatter("Test %U[]").arg("testMyTest").toString()); |
|
162 |
|
163 // Test %U[08] |
|
164 assertEquals( |
|
165 "Test testMyTe", |
|
166 new Formatter("Test %U[08]").arg("testMyTest").toString()); |
|
167 |
|
168 // Test %0[] |
|
169 assertEquals( |
|
170 "Test testMyTest", |
|
171 new Formatter("Test %0[]").arg("testMyTest").toString()); |
|
172 |
|
173 // Test %0[08] |
|
174 assertEquals( |
|
175 "Test testMyTe", |
|
176 new Formatter("Test %0[08]").arg("testMyTest").toString()); |
|
177 |
|
178 // Test %U[20 Skip |
|
179 assertEquals( |
|
180 "Test %U[20", |
|
181 new Formatter("Test %U[20").arg("testMyTest").toString()); |
|
182 |
|
183 // Test %U[300] |
|
184 assertEquals( |
|
185 "Test testMyTest", |
|
186 new Formatter("Test %U[300]").arg("testMyTest").toString()); |
|
187 |
|
188 // Test %U[8] |
|
189 assertEquals( |
|
190 "Test testMyTe", |
|
191 new Formatter("Test %U[8]").arg("testMyTest").toString()); |
|
192 |
|
193 // Test Max is higher than actual string. Expected output: whole string no padding. |
|
194 assertEquals( |
|
195 "Test testMyTest", |
|
196 new Formatter("Test %U[80]").arg("testMyTest").toString()); |
|
197 |
|
198 // Test Max is higher than pattern and string is shorter than pattern. |
|
199 assertEquals( |
|
200 "t to", |
|
201 new Formatter("t %U[09]").arg("to").toString()); |
|
202 |
|
203 // Test 0 len Max |
|
204 assertEquals( |
|
205 "t ", |
|
206 new Formatter("t %U[00]").arg("to").toString()); |
|
207 |
|
208 // Test 1 len Max |
|
209 assertEquals( |
|
210 "t 1", |
|
211 new Formatter("t %U[01]").arg("12").toString()); |
|
212 |
|
213 // Test 0 len Max with 0 len replacement |
|
214 assertEquals( |
|
215 "t ", |
|
216 new Formatter("t %U[00]").arg("").toString()); |
|
217 |
|
218 // Test 1 len Max with 1 len replacement |
|
219 assertEquals( |
|
220 "t 1", |
|
221 new Formatter("t %U[01]").arg("1").toString()); |
|
222 } |
|
223 |
|
224 void resourceLoaderTest() |
|
225 { |
|
226 |
|
227 ResourceLoader res = new ResourceLoader("testresource", "test_"); |
|
228 |
|
229 // Test existing |
|
230 assertEquals( |
|
231 "id1 test", |
|
232 res.format("id_1").arg("test").toString()); |
|
233 assertEquals( |
|
234 "id2 test2", |
|
235 res.format("id_2").arg("test2").toString()); |
|
236 assertEquals( |
|
237 "id3 test3", |
|
238 res.format("id_3").arg("test3").toString()); |
|
239 |
|
240 // Test full name |
|
241 assertEquals( |
|
242 "id1 test", |
|
243 res.format("test_id_1").arg("test").toString()); |
|
244 // Test non-existing, with prefix |
|
245 assertEquals( |
|
246 "test_id_notfound", |
|
247 res.format("test_id_notfound").arg("test3").toString()); |
|
248 |
|
249 // Test non-existing, without prefix |
|
250 assertEquals( |
|
251 "test_id_notfound", |
|
252 res.format("id_notfound").arg("test3").toString()); |
|
253 |
|
254 // Test array |
|
255 assertEquals( |
|
256 "id3 test3", |
|
257 res.format("id_3", new Object[] {"test3"})); |
|
258 |
|
259 // Test enter decoding. |
|
260 assertEquals( |
|
261 "id4 \n test4 \n", |
|
262 res.format("id_4").arg("test4").toString()); |
|
263 |
|
264 // Test backlash decoding. |
|
265 assertEquals( |
|
266 "\\id5 \\ test5", |
|
267 res.format("id_5").arg("test5").toString()); |
|
268 |
|
269 // Test double-quote decoding. |
|
270 assertEquals( |
|
271 "id6 \"\" test6", |
|
272 res.format("id_6").arg("test6").toString()); |
|
273 |
|
274 // Test single-quote decoding. |
|
275 assertEquals( |
|
276 "id7 \' test7", |
|
277 res.format("id_7").arg("test7").toString()); |
|
278 |
|
279 // Test tabulator decoding. |
|
280 assertEquals( |
|
281 "id8\ttest8", |
|
282 res.format("id_8").arg("test8").toString()); |
|
283 |
|
284 // Test character code <0x20AC> |
|
285 assertEquals( |
|
286 "id9 It takes 800 \u20ac to win!", |
|
287 res.format("id_9").arg(800).toString()); |
|
288 |
|
289 // Test two character codes. |
|
290 assertEquals( |
|
291 "id10 It takes \u20ac 800 \u20ac to win!", |
|
292 res.format("id_10").arg(800).toString()); |
|
293 |
|
294 // Test two character codes in a row. Test <0x20ac><0x20ac> |
|
295 assertEquals( |
|
296 "id11 It takes \u20ac\u20ac to win!", |
|
297 res.format("id_11").toString()); |
|
298 |
|
299 // Test only character code. |
|
300 assertEquals( |
|
301 "\u20ac", |
|
302 res.format("id_12").toString()); |
|
303 |
|
304 // Test invalid character code. |
|
305 assertEquals( |
|
306 "id13 It <0x20A>to win!", |
|
307 res.format("id_13").toString()); |
|
308 |
|
309 |
|
310 // Test character code as first character. |
|
311 assertEquals( |
|
312 "\u20ac id14", |
|
313 res.format("id_14").toString()); |
|
314 |
|
315 // Test character code as last character. |
|
316 assertEquals( |
|
317 "id15 \u20ac", |
|
318 res.format("id_15").toString()); |
|
319 |
|
320 // Test invalid character. |
|
321 assertEquals( |
|
322 "id16 <0xG0AC>", |
|
323 res.format("id_16").toString()); |
|
324 } |
|
325 } |
|