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