]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/tests/core/src/com/ibm/icu/dev/test/duration/DataReadWriteTest.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / tests / core / src / com / ibm / icu / dev / test / duration / DataReadWriteTest.java
1 /*
2 ******************************************************************************
3 * Copyright (C) 2007-2010, International Business Machines Corporation and   *
4 * others. All Rights Reserved.                                               *
5 ******************************************************************************
6 */
7
8 // Copyright 2006 Google Inc.  All Rights Reserved.
9
10 package com.ibm.icu.dev.test.duration;
11
12 import java.io.StringReader;
13 import java.io.StringWriter;
14
15 import com.ibm.icu.dev.test.TestFmwk;
16 import com.ibm.icu.impl.duration.impl.XMLRecordReader;
17 import com.ibm.icu.impl.duration.impl.XMLRecordWriter;
18
19 public class DataReadWriteTest extends TestFmwk {
20
21     /**
22      * Invoke the tests.
23      */
24     public static void main(String[] args) {
25         new DataReadWriteTest().run(args);
26     }
27
28     // strip line ends and trailing spaces
29     private String normalize(String str) {
30         StringBuffer sb = new StringBuffer();
31         boolean inLine = true;
32         for (int i = 0; i < str.length(); ++i) {
33             char c = str.charAt(i);
34             if (inLine && c == ' ') {
35                 continue;
36             }
37             if (c == '\n') {
38                 inLine = true;
39                 continue;
40             }
41             inLine = false;
42             sb.append("" + c);
43         }
44         return sb.toString();
45     }
46
47     public void testOpenClose() {
48         StringWriter sw = new StringWriter();
49         XMLRecordWriter xrw = new XMLRecordWriter(sw);
50         assertTrue(null, xrw.open("Test"));
51         assertTrue(null, xrw.close());
52         xrw.flush();
53         String str = sw.toString();
54         assertEquals(null, "<Test></Test>", normalize(str));
55
56         StringReader sr = new StringReader(str);
57         XMLRecordReader xrr = new XMLRecordReader(sr);
58         assertTrue(null, xrr.open("Test"));
59         assertTrue(null, xrr.close());
60     }
61
62     public void testBool() {
63         StringWriter sw = new StringWriter();
64         XMLRecordWriter xrw = new XMLRecordWriter(sw);
65         xrw.bool("x", true);
66         xrw.bool("y", false);
67         xrw.flush();
68         String str = sw.toString();
69         assertEquals(null, "<x>true</x><y>false</y>", normalize(str));
70
71         StringReader sr = new StringReader(str);
72         XMLRecordReader xrr = new XMLRecordReader(sr);
73         assertTrue(null, xrr.bool("x"));
74         assertFalse(null, xrr.bool("y"));
75     }
76
77     public void testBoolArray() {
78         boolean[][] datas = {
79             {},
80             { true },
81             { true, false },
82             { true, false, true },
83         };
84     
85         String[] targets = {
86             "<testList></testList>",
87             "<testList><test>true</test></testList>",
88             "<testList><test>true</test><test>false</test></testList>",
89             "<testList><test>true</test><test>false</test>" +
90             "<test>true</test></testList>",
91         };
92
93         for (int j = 0; j < datas.length; ++j) {
94             boolean[] data = datas[j];
95             String target = targets[j];
96
97             StringWriter sw = new StringWriter();
98             XMLRecordWriter xrw = new XMLRecordWriter(sw);
99             xrw.boolArray("test", data);
100             xrw.flush();
101             String str = sw.toString();
102             assertEquals("" + j, target, normalize(str));
103
104             StringReader sr = new StringReader(str);
105             XMLRecordReader xrr = new XMLRecordReader(sr);
106             boolean[] out = xrr.boolArray("test");
107
108             assertNotNull("" + j, out);
109             assertEquals("" + j, data.length, out.length);
110             for (int i = 0; i < data.length; ++i) {
111                 assertEquals("" + j + "/" + i, data[i], out[i]);
112             }
113         }
114     }
115
116     public void testCharacter() {
117         StringWriter sw = new StringWriter();
118         XMLRecordWriter xrw = new XMLRecordWriter(sw);
119         xrw.character("x", 'a');
120         xrw.character("y", 'b');
121         xrw.flush();
122         String str = sw.toString();
123         assertEquals(null, "<x>a</x><y>b</y>", normalize(str));
124
125         StringReader sr = new StringReader(str);
126         XMLRecordReader xrr = new XMLRecordReader(sr);
127         assertEquals(null, 'a', xrr.character("x"));
128         assertEquals(null, 'b', xrr.character("y"));
129     }
130
131     public void testCharacterArray() {
132         char[][] datas = {
133             {},
134             { 'a' },
135             { 'a', 'b' },
136             { 'a', 'b', 'c' },
137         };
138
139         String[] targets = {
140             "<testList></testList>",
141             "<testList><test>a</test></testList>",
142             "<testList><test>a</test><test>b</test></testList>",
143             "<testList><test>a</test><test>b</test>" +
144             "<test>c</test></testList>",
145         };
146
147         for (int j = 0; j < datas.length; ++j) {
148             char[] data = datas[j];
149             String target = targets[j];
150
151             StringWriter sw = new StringWriter();
152             XMLRecordWriter xrw = new XMLRecordWriter(sw);
153             xrw.characterArray("test", data);
154             xrw.flush();
155             String str = sw.toString();
156             assertEquals("" + j, target, normalize(str));
157
158             StringReader sr = new StringReader(str);
159             XMLRecordReader xrr = new XMLRecordReader(sr);
160             char[] out = xrr.characterArray("test");
161
162             assertNotNull("" + j, out);
163             assertEquals("" + j, data.length, out.length);
164             for (int i = 0; i < data.length; ++i) {
165                 assertEquals("" + j + "/" + i, data[i], out[i]);
166             }
167         }
168     }
169
170     public void testNamedIndex() {
171         StringWriter sw = new StringWriter();
172         XMLRecordWriter xrw = new XMLRecordWriter(sw);
173         String[] names = { "zero", "one" };
174
175         xrw.namedIndex("x", names, 0);
176         xrw.namedIndex("y", names, 1);
177         xrw.flush();
178         String str = sw.toString();
179         assertEquals(null, "<x>zero</x><y>one</y>", normalize(str));
180
181         StringReader sr = new StringReader(str);
182         XMLRecordReader xrr = new XMLRecordReader(sr);
183         assertEquals(null, 0, xrr.namedIndex("x", names));
184         assertEquals(null, 1, xrr.namedIndex("y", names));
185     }
186
187     public void testNamedIndexArray() {
188         String[] names = { "zero", "one" };
189         byte[][] datas = {
190             {},
191             { 0 },
192             { 1, 0 },
193             { 0, 1, 0 },
194         };
195
196         String[] targets = {
197             "<testList></testList>",
198             "<testList><test>zero</test></testList>",
199             "<testList><test>one</test><test>zero</test></testList>",
200             "<testList><test>zero</test><test>one</test>" +
201             "<test>zero</test></testList>",
202         };
203
204         for (int j = 0; j < datas.length; ++j) {
205             byte[] data = datas[j];
206             String target = targets[j];
207
208             StringWriter sw = new StringWriter();
209             XMLRecordWriter xrw = new XMLRecordWriter(sw);
210             xrw.namedIndexArray("test", names, data);
211             xrw.flush();
212             String str = sw.toString();
213             assertEquals("" + j, target, normalize(str));
214
215             StringReader sr = new StringReader(str);
216             XMLRecordReader xrr = new XMLRecordReader(sr);
217             byte[] out = xrr.namedIndexArray("test", names);
218
219             assertNotNull("" + j, out);
220             assertEquals("" + j, data.length, out.length);
221             for (int i = 0; i < data.length; ++i) {
222                 assertEquals("" + j + "/" + i, data[i], out[i]);
223             }
224         }
225     }
226
227     public void testString() {
228         StringWriter sw = new StringWriter();
229         XMLRecordWriter xrw = new XMLRecordWriter(sw);
230
231         String s = " This is <a> &&\t test. ";
232         String s1 = " This is <a> && test. ";
233         String t = " This is &lt;a> &amp;&amp; test. ";
234         xrw.string("x", s);
235         xrw.flush();
236         String str = sw.toString();
237         assertEquals("\n'" + normalize(str) + "' = \n'<x>" + t + "</x>", "<x>"
238                 + t + "</x>", normalize(str));
239
240         StringReader sr = new StringReader(str);
241         XMLRecordReader xrr = new XMLRecordReader(sr);
242         String res = xrr.string("x");
243         assertEquals("\n'" + res + "' == \n'" + s1 + "'", s1, res);
244     }
245
246     public void testStringArray() {
247         String s1 = "";
248         String s2 = " ";
249         String s3 = "This is a test";
250         String s4 = "  It is\n   only  a test\t  ";
251         String s4x = " It is only a test ";
252
253         String[][] datas = { 
254             {},
255             { s1 },
256             { s2, s1 },
257             { s3, s2, s1 },
258             { s3, null, s1, null },
259             { s4, s1, s3, s2 }
260         };
261
262         String[] targets = {
263             "<testList></testList>",
264             "<testList><test>" + s1 + "</test></testList>",
265             "<testList><test>" + s2 + "</test><test>" + s1 + "</test></testList>",
266             "<testList><test>" + s3 + "</test><test>" + s2 + 
267                 "</test><test>" + s1 + "</test></testList>",
268             "<testList><test>" + s3 + "</test><test>Null</test><test>" + s1 + 
269                 "</test><test>Null</test></testList>",
270             "<testList><test>" + s4x + "</test><test>" + s1 + 
271                 "</test><test>" + s3 + "</test><test>" + s2 + "</test></testList>",
272         };
273
274         for (int j = 0; j < datas.length; ++j) {
275             String[] data = datas[j];
276             String target = targets[j];
277
278             StringWriter sw = new StringWriter();
279             XMLRecordWriter xrw = new XMLRecordWriter(sw);
280             xrw.stringArray("test", data);
281             xrw.flush();
282             String str = sw.toString();
283             assertEquals("" + j + " '" + str + "'", target, normalize(str));
284
285             StringReader sr = new StringReader(str);
286             XMLRecordReader xrr = new XMLRecordReader(sr);
287             String[] out = xrr.stringArray("test");
288
289             assertNotNull("" + j, out);
290             assertEquals("" + j, data.length, out.length);
291             for (int i = 0; i < data.length; ++i) {
292                 String standin = data[i];
293                 if (s4.equals(standin)) {
294                     standin = s4x;
295                 }
296                 assertEquals("" + j + "/" + i + " '" + out[i] + "'", standin,
297                         out[i]);
298             }
299         }
300     }
301
302     public void testStringTable() {
303         String s1 = "";
304         String s2 = " ";
305         String s3 = "This is a test";
306         String s4 = "It is only a test";
307
308         String[][] table = { 
309             {},
310             { s1 },
311             { s2, s1 },
312             { s3, s2, s1 },
313             null,
314             { s4, s1, s3, s2 }
315         };
316
317         String target = "<testTable>" +
318             "<testList></testList>" +
319             "<testList><test></test></testList>" +
320             "<testList><test> </test><test></test></testList>" +
321             "<testList><test>This is a test</test><test> </test>" + 
322                 "<test></test></testList>" +
323             "<testList>Null</testList>" +
324             "<testList><test>It is only a test</test><test></test>" +
325                 "<test>This is a test</test><test> </test></testList>" +
326             "</testTable>";
327
328         StringWriter sw = new StringWriter();
329         XMLRecordWriter xrw = new XMLRecordWriter(sw);
330         xrw.stringTable("test", table);
331         xrw.flush();
332         String str = sw.toString();
333         assertEquals("'" + str + "'", target, normalize(str));
334     }
335
336     public void testOmittedFields() {
337         StringWriter sw = new StringWriter();
338         XMLRecordWriter xrw = new XMLRecordWriter(sw);
339         xrw.open("omit");
340         xrw.bool("x", true);
341         xrw.bool("y", false);
342         xrw.close();
343         xrw.flush();
344         String str = sw.toString();
345
346         StringReader sr = new StringReader(str);
347         XMLRecordReader xrr = new XMLRecordReader(sr);
348         assertTrue(null, xrr.open("omit"));
349         assertTrue(null, xrr.bool("x"));
350         assertEquals(null, '\uffff', xrr.character("z"));
351         assertFalse(null, xrr.bool("y"));
352         assertTrue(null, xrr.close());
353     }
354 }