• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.xsdc.tests;
18 
19 import static org.hamcrest.core.Is.is;
20 import static org.junit.Assert.*;
21 
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.xml.sax.SAXException;
26 
27 import javax.xml.datatype.DatatypeFactory;
28 import javax.xml.datatype.Duration;
29 import javax.xml.datatype.XMLGregorianCalendar;
30 
31 import java.io.*;
32 import java.math.BigDecimal;
33 import java.math.BigInteger;
34 import java.util.*;
35 
36 public class XmlParserTest {
37     @Test
testPurchaseSimple()38     public void testPurchaseSimple() throws Exception {
39         TestCompilationResult result;
40         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
41                 "purchase_simple/purchase_simple.xsd")) {
42             result = TestHelper.parseXsdAndCompile(in);
43         }
44 
45         Class<?> xmlParser = result.loadClass("XmlParser");
46         Class<?> purchaseOrderType = result.loadClass("PurchaseOrderType");
47         Class<?> usAddress = result.loadClass("USAddress");
48 
49         Object instance;
50         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
51                 "purchase_simple.xml")) {
52             instance = xmlParser.getMethod("read", InputStream.class).invoke(null, in);
53         }
54         Object billTo = purchaseOrderType.getMethod("getBillTo").invoke(instance);
55         List shipToList = (List) purchaseOrderType.getMethod("getShipTo").invoke(instance);
56 
57         String name = (String) usAddress.getMethod("getName").invoke(billTo);
58         BigInteger zip = (BigInteger) usAddress.getMethod("getZip").invoke(billTo);
59         String street = (String) usAddress.getMethod("getStreet").invoke(shipToList.get(0));
60         BigInteger largeZip = (BigInteger) usAddress.getMethod("getZip").invoke(shipToList.get(1));
61         XMLGregorianCalendar orderDate = (XMLGregorianCalendar) purchaseOrderType.getMethod(
62                 "getOrderDate").invoke(instance);
63 
64         assertThat(name, is("billName"));
65         assertThat(zip, is(new BigInteger("1")));
66         assertThat(street, is("street1"));
67         assertThat(largeZip, is(new BigInteger("-7922816251426433759")));
68         assertThat(orderDate,
69                 is(javax.xml.datatype.DatatypeFactory.newInstance().newXMLGregorianCalendar(
70                         "1900-01-01")));
71     }
72 
73     @Test
testNestedType()74     public void testNestedType() throws Exception {
75         TestCompilationResult result;
76         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
77                 "nested_type/nested_type.xsd")) {
78             result = TestHelper.parseXsdAndCompile(in);
79         }
80 
81         Class<?> xmlParser = result.loadClass("XmlParser");
82         Class<?> employee = result.loadClass("Employee");
83         Class<?> address = result.loadClass("Employee$Address");
84         Class<?> extra = result.loadClass("Employee$Address$Extra");
85 
86         Object instance;
87         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
88                 "nested_type.xml")) {
89             instance = xmlParser.getMethod("read", InputStream.class).invoke(null, in);
90         }
91 
92         String name = (String) employee.getMethod("getName").invoke(instance);
93         Object addressInstance = employee.getMethod("getAddress").invoke(instance);
94         String country = (String) address.getMethod("getCountry").invoke(addressInstance);
95         Object extraInstance = address.getMethod("getExtra").invoke(addressInstance);
96         String line2 = (String) extra.getMethod("getLine2").invoke(extraInstance);
97 
98         assertThat(name, is("Peter"));
99         assertThat(country, is("US"));
100         assertThat(line2, is("Good Street"));
101     }
102 
103     @Test
testSimpleComplexContent()104     public void testSimpleComplexContent() throws Exception {
105         TestCompilationResult result;
106         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
107                 "simple_complex_content/simple_complex_content.xsd")) {
108             result = TestHelper.parseXsdAndCompile(in);
109         }
110 
111         Class<?> xmlParser = result.loadClass("XmlParser");
112         Class<?> person = result.loadClass("Person");
113         Class<?> usAddress = result.loadClass("USAddressP");
114         Class<?> krAddress = result.loadClass("KRAddress");
115 
116         Object instance;
117         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
118                 "simple_complex_content.xml")) {
119             instance = xmlParser.getMethod("readPerson", InputStream.class).invoke(null, in);
120         }
121 
122         String name = (String) person.getMethod("getName").invoke(instance);
123 
124         Object usAddressInstance = person.getMethod("getUSAddressP").invoke(instance);
125         String usStreet = (String) usAddress.getMethod("getStreet").invoke(usAddressInstance);
126         BigInteger usZipcode = (BigInteger) usAddress.getMethod("getZipcode").invoke(
127                 usAddressInstance);
128 
129         Object krAddressInstance = person.getMethod("getKRAddress").invoke(instance);
130         String krStreet = (String) krAddress.getMethod("getStreet").invoke(krAddressInstance);
131 
132         assertThat(name, is("Petr"));
133         assertThat(usStreet, is("street fighter"));
134         assertThat(usZipcode, is(new BigInteger("323232318329852")));
135         assertThat(krStreet, is("Nokdu Street"));
136     }
137 
138     @Test
testPredefinedTypes()139     public void testPredefinedTypes() throws Exception {
140         TestCompilationResult result;
141         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
142                 "predefined_types/predefined_types.xsd")) {
143             result = TestHelper.parseXsdAndCompile(in);
144         }
145 
146         Class<?> xmlParser = result.loadClass("XmlParser");
147         Class<?> types = result.loadClass("Types");
148         Class<?> stringTypes = result.loadClass("StringTypes");
149         Class<?> dateTypes = result.loadClass("DateTypes");
150         Class<?> numericTypes = result.loadClass("NumericTypes");
151         Class<?> miscTypes = result.loadClass("MiscTypes");
152         Class<?> listPrimitiveTypes = result.loadClass("ListPrimitiveTypes");
153 
154         Object instance;
155         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
156                 "predefined_types.xml")) {
157             instance = xmlParser.getMethod("read", InputStream.class).invoke(null, in);
158         }
159 
160         {
161             Object stringTypesInstance = types.getMethod("getStringTypes").invoke(instance);
162             String string = (String) stringTypes.getMethod("getString").invoke(stringTypesInstance);
163             String token = (String) stringTypes.getMethod("getToken").invoke(stringTypesInstance);
164             String normalizedString = (String) stringTypes.getMethod("getNormalizedString").invoke(
165                     stringTypesInstance);
166             String language = (String) stringTypes.getMethod("getLanguage").invoke(
167                     stringTypesInstance);
168             String entity = (String) stringTypes.getMethod("getEntity").invoke(stringTypesInstance);
169             List entities = (List) stringTypes.getMethod("getEntities").invoke(stringTypesInstance);
170             String id = (String) stringTypes.getMethod("getId").invoke(stringTypesInstance);
171             String name = (String) stringTypes.getMethod("getName").invoke(stringTypesInstance);
172             String ncName = (String) stringTypes.getMethod("getNcname").invoke(stringTypesInstance);
173             String nmToken = (String) stringTypes.getMethod("getNmtoken").invoke(
174                     stringTypesInstance);
175             List nmTokens = (List) stringTypes.getMethod("getNmtokens").invoke(stringTypesInstance);
176 
177             assertThat(string, is("abcd"));
178             assertThat(token, is("abcd"));
179             assertThat(normalizedString, is("abcd"));
180             assertThat(language, is("abcd"));
181             assertThat(entity, is("abcd"));
182             assertThat(entities, is(Arrays.asList("a", "b", "c", "d")));
183             assertThat(id, is("abcd"));
184             assertThat(name, is("abcd"));
185             assertThat(ncName, is("abcd"));
186             assertThat(nmToken, is("abcd"));
187             assertThat(nmTokens, is(Arrays.asList("a", "b", "c", "d")));
188         }
189 
190         {
191             Object dateTypesInstance = types.getMethod("getDateTypes").invoke(instance);
192             XMLGregorianCalendar date = (XMLGregorianCalendar) dateTypes.getMethod(
193                     "getDate").invoke(dateTypesInstance);
194             XMLGregorianCalendar dateTime = (XMLGregorianCalendar) dateTypes.getMethod(
195                     "getDateTime").invoke(dateTypesInstance);
196             Duration duration = (Duration) dateTypes.getMethod("getDuration").invoke(
197                     dateTypesInstance);
198             XMLGregorianCalendar gDay = (XMLGregorianCalendar) dateTypes.getMethod(
199                     "getGDay").invoke(dateTypesInstance);
200             XMLGregorianCalendar gMonth = (XMLGregorianCalendar) dateTypes.getMethod(
201                     "getGMonth").invoke(dateTypesInstance);
202             XMLGregorianCalendar gMonthDay = (XMLGregorianCalendar) dateTypes.getMethod(
203                     "getGMonthDay").invoke(dateTypesInstance);
204             XMLGregorianCalendar gYear = (XMLGregorianCalendar) dateTypes.getMethod(
205                     "getGYear").invoke(dateTypesInstance);
206             XMLGregorianCalendar gYearMonth = (XMLGregorianCalendar) dateTypes.getMethod(
207                     "getGYearMonth").invoke(dateTypesInstance);
208             XMLGregorianCalendar time = (XMLGregorianCalendar) dateTypes.getMethod(
209                     "getTime").invoke(dateTypesInstance);
210 
211             DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
212             assertThat(date, is(datatypeFactory.newXMLGregorianCalendar("2018-06-18")));
213             assertThat(dateTime,
214                     is(datatypeFactory.newXMLGregorianCalendar("2018-06-18T21:32:52")));
215             assertThat(duration, is(datatypeFactory.newDuration("P3M")));
216             assertThat(gDay, is(datatypeFactory.newXMLGregorianCalendar("---18")));
217             assertThat(gMonth, is(datatypeFactory.newXMLGregorianCalendar("--06")));
218             assertThat(gMonthDay, is(datatypeFactory.newXMLGregorianCalendar("--06-18")));
219             assertThat(gYear, is(datatypeFactory.newXMLGregorianCalendar("2018")));
220             assertThat(gYearMonth, is(datatypeFactory.newXMLGregorianCalendar("2018-06")));
221             assertThat(time, is(datatypeFactory.newXMLGregorianCalendar("21:32:52")));
222         }
223 
224         {
225             Object numericTypesInstance = types.getMethod("getNumericTypes").invoke(instance);
226             BigDecimal decimal = (BigDecimal) numericTypes.getMethod("getDecimal").invoke(
227                     numericTypesInstance);
228             BigInteger integer = (BigInteger) numericTypes.getMethod("getInteger").invoke(
229                     numericTypesInstance);
230             long _long = (long) numericTypes.getMethod("get_long").invoke(numericTypesInstance);
231             int _int = (int) numericTypes.getMethod("get_int").invoke(numericTypesInstance);
232             short _short = (short) numericTypes.getMethod("get_short").invoke(numericTypesInstance);
233             byte _byte = (byte) numericTypes.getMethod("get_byte").invoke(numericTypesInstance);
234             BigInteger negativeInteger = (BigInteger) numericTypes.getMethod(
235                     "getNegativeInteger").invoke(numericTypesInstance);
236             BigInteger nonNegativeInteger = (BigInteger) numericTypes.getMethod(
237                     "getNonNegativeInteger").invoke(numericTypesInstance);
238             BigInteger positiveInteger = (BigInteger) numericTypes.getMethod(
239                     "getPositiveInteger").invoke(numericTypesInstance);
240             BigInteger nonPositiveInteger = (BigInteger) numericTypes.getMethod(
241                     "getNonPositiveInteger").invoke(numericTypesInstance);
242             BigInteger unsignedLong = (BigInteger) numericTypes.getMethod("getUnsignedLong").invoke(
243                     numericTypesInstance);
244             long unsignedInt = (long) numericTypes.getMethod("getUnsignedInt").invoke(
245                     numericTypesInstance);
246             int unsignedShort = (int) numericTypes.getMethod("getUnsignedShort").invoke(
247                     numericTypesInstance);
248             short unsignedByte = (short) numericTypes.getMethod("getUnsignedByte").invoke(
249                     numericTypesInstance);
250 
251             assertThat(decimal, is(new BigDecimal("1234.57")));
252             assertThat(integer, is(new BigInteger("1234567890123456789")));
253             assertThat(_long, is(9223372036854775807L));
254             assertThat(_int, is(2147483647));
255             assertThat(_short, is((short) 32767));
256             assertThat(_byte, is((byte) 127));
257             assertThat(negativeInteger, is(new BigInteger("-1234")));
258             assertThat(nonNegativeInteger, is(new BigInteger("1234")));
259             assertThat(positiveInteger, is(new BigInteger("1234")));
260             assertThat(nonPositiveInteger, is(new BigInteger("-1234")));
261             assertThat(unsignedLong, is(new BigInteger("1234")));
262             assertThat(unsignedInt, is(1234L));
263             assertThat(unsignedShort, is(1234));
264             assertThat(unsignedByte, is((short) 255));
265         }
266 
267         {
268             Object miscTypesInstance = types.getMethod("getMiscTypes").invoke(instance);
269             double _double = (double) miscTypes.getMethod("get_double").invoke(miscTypesInstance);
270             float _float = (float) miscTypes.getMethod("get_float").invoke(miscTypesInstance);
271             String anyURI = (String) miscTypes.getMethod("getAnyURI").invoke(miscTypesInstance);
272             byte[] base64Binary = (byte[]) miscTypes.getMethod("getBase64Binary").invoke(
273                     miscTypesInstance);
274             boolean _boolean = (boolean) miscTypes.getMethod("get_boolean").invoke(
275                     miscTypesInstance);
276             BigInteger hexBinary = (BigInteger) miscTypes.getMethod("getHexBinary").invoke(
277                     miscTypesInstance);
278             String qName = (String) miscTypes.getMethod("getQName").invoke(miscTypesInstance);
279             String iDREF = (String) miscTypes.getMethod("getIDREF").invoke(miscTypesInstance);
280             List iDREFS = (List) miscTypes.getMethod("getIDREFS").invoke(miscTypesInstance);
281             String anyType = (String) miscTypes.getMethod("getAnyType").invoke(miscTypesInstance);
282 
283             assertThat(_double, is(1234.57));
284             assertThat(_float, is(123.4f));
285             assertThat(anyURI, is("https://www.google.com"));
286             assertThat(base64Binary, is(Base64.getDecoder().decode("Z29vZ2xl")));
287             assertThat(_boolean, is(true));
288             assertThat(hexBinary, is(new BigInteger("516a75cb56d7e7", 16)));
289             assertThat(qName, is("abcd"));
290             assertThat(iDREF, is("abcd"));
291             assertThat(iDREFS, is(Arrays.asList("abcd", "abcd")));
292             assertThat(anyType, is("abcd"));
293         }
294 
295         {
296             Object listPrimitiveTypesInstance = types.getMethod("getListPrimitiveTypes").invoke(
297                     instance);
298             List listLong = (List) listPrimitiveTypes.getMethod("getListLong").invoke(
299                     listPrimitiveTypesInstance);
300             List listInt = (List) listPrimitiveTypes.getMethod("getListInt").invoke(
301                     listPrimitiveTypesInstance);
302             List listShort = (List) listPrimitiveTypes.getMethod("getListShort").invoke(
303                     listPrimitiveTypesInstance);
304             List listByte = (List) listPrimitiveTypes.getMethod("getListByte").invoke(
305                     listPrimitiveTypesInstance);
306             List listDouble = (List) listPrimitiveTypes.getMethod("getListDouble").invoke(
307                     listPrimitiveTypesInstance);
308             List listFloat = (List) listPrimitiveTypes.getMethod("getListFloat").invoke(
309                     listPrimitiveTypesInstance);
310             List listBoolean = (List) listPrimitiveTypes.getMethod("getListBoolean").invoke(
311                     listPrimitiveTypesInstance);
312 
313             assertThat(listLong, is(Arrays.asList(-9223372036854775808L, 9223372036854775807L)));
314             assertThat(listInt, is(Arrays.asList(-2147483648, 2147483647)));
315             assertThat(listShort, is(Arrays.asList((short) -32768, (short) 32767)));
316             assertThat(listByte, is(Arrays.asList((byte) -128, (byte) 127)));
317             assertThat(listDouble, is(Arrays.asList(1234.56, 5678.12)));
318             assertThat(listFloat, is(Arrays.asList(123.4f, 456.1f)));
319             assertThat(listBoolean, is(Arrays.asList(true, false)));
320         }
321     }
322 
323     @Test
testSimpleType()324     public void testSimpleType() throws Exception {
325         TestCompilationResult result;
326         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
327                 "simple_type/simple_type.xsd")) {
328             result = TestHelper.parseXsdAndCompile(in);
329         }
330 
331         Class<?> xmlParser = result.loadClass("XmlParser");
332         Class<?> simpleTypes = result.loadClass("SimpleTypes");
333 
334         Object instance;
335         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
336                 "simple_type.xml")) {
337             instance = xmlParser.getMethod("read", InputStream.class).invoke(null, in);
338         }
339 
340         List listInt = (List) simpleTypes.getMethod("getListInt").invoke(instance);
341         List uniontest = (List) simpleTypes.getMethod("getUnionTest").invoke(instance);
342 
343         assertThat(listInt, is(Arrays.asList(1, 2, 3, 4, 5)));
344         assertThat(uniontest, is(Arrays.asList("100")));
345     }
346 
347     @Test
testReference()348     public void testReference() throws Exception {
349         TestCompilationResult result;
350         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
351                 "reference/reference.xsd")) {
352             result = TestHelper.parseXsdAndCompile(in);
353         }
354 
355         Class<?> xmlParser = result.loadClass("XmlParser");
356         Class<?> cls = result.loadClass("Class");
357 
358         Object instance;
359         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
360                 "reference.xml")) {
361             instance = xmlParser.getMethod("read", InputStream.class).invoke(null, in);
362         }
363 
364         List student = (List) cls.getMethod("getStudent").invoke(instance);
365 
366         assertThat(student, is(Arrays.asList("Sam", "Paul", "Peter")));
367     }
368 
369     @Rule
370     public ExpectedException thrown = ExpectedException.none();
371 
372     @Test
testUnsupportedTag()373     public void testUnsupportedTag() throws Exception {
374         thrown.expect(SAXException.class);
375         thrown.expectMessage("unsupported tag : import");
376 
377         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
378                 "unsupported_tag.xsd")) {
379             TestHelper.parseXsdAndCompile(in);
380         }
381     }
382 
383     @Test
testUnsupportedAttribute()384     public void testUnsupportedAttribute() throws Exception {
385         thrown.expect(SAXException.class);
386         thrown.expectMessage("substitution group of an element is not supported.");
387 
388         try (InputStream in = this.getClass().getClassLoader().getResourceAsStream(
389                 "unsupported_attribute.xsd")) {
390             TestHelper.parseXsdAndCompile(in);
391         }
392     }
393 }
394