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