1 /*
2 * Copyright (C) 2021 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 #include <iostream>
18 #include <fstream>
19 #include <string>
20 #include <optional>
21
22 #include <android-base/macros.h>
23 #include <gtest/gtest.h>
24 #include "nested_type.h"
25 #include "purchase_simple.h"
26 #include "simple_complex_content.h"
27
28 #include "predefined_types.h"
29 #include "reference.h"
30 #include "attr_group_simple.h"
31 #include "group.h"
32 #include "xmltest.h"
33
34 using namespace std;
35
TEST_F(XmlTest,Predefinedtypes)36 TEST_F(XmlTest, Predefinedtypes) {
37 using namespace predefined::types;
38 Types type = *read(Resource("predefined_types.xml").c_str());
39
40 StringTypes stringTypes = *type.getFirstStringTypes();
41 DateTypes dateTypes = *type.getFirstDateTypes();
42 NumericTypes numericTypes = *type.getFirstNumericTypes();
43 MiscTypes miscTypes = *type.getFirstMiscTypes();
44 ListPrimitiveTypes listPrimitiveTypes = *type.getFirstListPrimitiveTypes();
45
46 EXPECT_EQ(stringTypes.getString(), "abcd");
47 EXPECT_EQ(stringTypes.getToken(), "abcd");
48 EXPECT_EQ(stringTypes.getNormalizedString(), "abcd");
49 EXPECT_EQ(stringTypes.getLanguage(), "abcd");
50 EXPECT_EQ(stringTypes.getEntity(), "abcd");
51 EXPECT_EQ(stringTypes.getEntities()[0], "a");
52 EXPECT_EQ(stringTypes.getEntities()[1], "b");
53 EXPECT_EQ(stringTypes.getEntities()[2], "c");
54 EXPECT_EQ(stringTypes.getEntities()[3], "d");
55 EXPECT_EQ(stringTypes.getId(), "abcd");
56 EXPECT_EQ(stringTypes.getName(), "abcd");
57 EXPECT_EQ(stringTypes.getNcname(), "abcd");
58 EXPECT_EQ(stringTypes.getNmtoken(), "abcd");
59 EXPECT_EQ(stringTypes.getNmtokens()[0], "a");
60 EXPECT_EQ(stringTypes.getNmtokens()[1], "b");
61 EXPECT_EQ(stringTypes.getNmtokens()[2], "c");
62 EXPECT_EQ(stringTypes.getNmtokens()[3], "d");
63
64
65 EXPECT_EQ(dateTypes.getDate(), "2018-06-18");
66 EXPECT_EQ(dateTypes.getDateTime(), "2018-06-18T21:32:52");
67 EXPECT_EQ(dateTypes.getDuration(), "P3M");
68 EXPECT_EQ(dateTypes.getGDay(), "---18");
69 EXPECT_EQ(dateTypes.getGMonth(), "--06");
70 EXPECT_EQ(dateTypes.getGMonthDay(), "--06-18");
71 EXPECT_EQ(dateTypes.getGYear(), "2018");
72 EXPECT_EQ(dateTypes.getGYearMonth(), "2018-06");
73 EXPECT_EQ(dateTypes.getTime(), "21:32:52");
74
75 EXPECT_EQ(numericTypes.getDecimal(), 1234.57);
76 EXPECT_EQ(numericTypes.getInteger(), 1234567890123456789);
77 EXPECT_EQ(numericTypes.get_long(), 9223372036854775807);
78 EXPECT_EQ(numericTypes.get_int(), 2147483647);
79 EXPECT_EQ(numericTypes.get_short(), 32767);
80 EXPECT_EQ((int)numericTypes.getByte(), 127);
81 EXPECT_EQ(numericTypes.getNegativeInteger(), -1234);
82 EXPECT_EQ(numericTypes.getNonNegativeInteger(), 1234);
83 EXPECT_EQ(numericTypes.getPositiveInteger(), 1234);
84 EXPECT_EQ(numericTypes.getNonPositiveInteger(), -1234);
85 EXPECT_EQ(numericTypes.getUnsignedLong(), 1234);
86 EXPECT_EQ(numericTypes.getUnsignedInt(), 1234);
87 EXPECT_EQ(numericTypes.getUnsignedShort(), 1234);
88 EXPECT_EQ((int)(numericTypes.getUnsignedByte()), 255);
89
90 EXPECT_EQ(miscTypes.get_double(), 1234.57);
91 EXPECT_EQ(miscTypes.getAnyURI(), "https://www.google.com");
92 EXPECT_EQ(miscTypes.getBase64Binary(), "Z29vZ2xl");
93 EXPECT_TRUE(miscTypes.getBoolean());
94 EXPECT_EQ(miscTypes.getHexBinary(), "016a75cb56d7e7");
95 EXPECT_EQ(miscTypes.getQName(), "abcd");
96 EXPECT_EQ(miscTypes.getIDREF(), "abcd");
97 EXPECT_EQ(miscTypes.getIDREFS()[0], "abcd");
98 EXPECT_EQ(miscTypes.getIDREFS()[1], "abcd");
99 EXPECT_EQ(miscTypes.getAnyType(), "abcd");
100
101 EXPECT_EQ(listPrimitiveTypes.getListInt()[0], -2147483648);
102 EXPECT_EQ(listPrimitiveTypes.getListInt()[1], 2147483647);
103 EXPECT_EQ(listPrimitiveTypes.getListShort()[0], -32768);
104 EXPECT_EQ(listPrimitiveTypes.getListShort()[1], 32767);
105 EXPECT_EQ((int)listPrimitiveTypes.getListByte()[0], -128);
106 EXPECT_EQ((int)listPrimitiveTypes.getListByte()[1], 127);
107 EXPECT_EQ(listPrimitiveTypes.getListDouble()[0], 1234.56);
108 EXPECT_EQ(listPrimitiveTypes.getListDouble()[1], 5678.12);
109 EXPECT_TRUE(listPrimitiveTypes.getListBoolean()[0]);
110 EXPECT_FALSE(listPrimitiveTypes.getListBoolean()[1]);
111
112 ofstream out("old_predefined_types.xml");
113 write(out, type);
114 Types type2 = *read("old_predefined_types.xml");
115
116 NumericTypes numericTypes2 = *type.getFirstNumericTypes();
117 ListPrimitiveTypes listPrimitiveTypes2 = *type.getFirstListPrimitiveTypes();
118
119 EXPECT_EQ(numericTypes.getDecimal(), numericTypes2.getDecimal());
120 EXPECT_EQ(numericTypes.getInteger(), numericTypes2.getInteger());
121 EXPECT_EQ(numericTypes.get_long(), numericTypes2.get_long());
122 EXPECT_EQ(numericTypes.get_int(), numericTypes2.get_int());
123 EXPECT_EQ(numericTypes.get_short(), numericTypes2.get_short());
124 EXPECT_EQ(numericTypes.getByte(), numericTypes2.getByte());
125 EXPECT_EQ(numericTypes.getNegativeInteger(), numericTypes2.getNegativeInteger());
126 EXPECT_EQ(numericTypes.getNonNegativeInteger(), numericTypes2.getNonNegativeInteger());
127 EXPECT_EQ(numericTypes.getPositiveInteger(), numericTypes2.getPositiveInteger());
128 EXPECT_EQ(numericTypes.getNonPositiveInteger(), numericTypes2.getNonPositiveInteger());
129 EXPECT_EQ(numericTypes.getUnsignedLong(), numericTypes2.getUnsignedLong());
130 EXPECT_EQ(numericTypes.getUnsignedInt(), numericTypes2.getUnsignedInt());
131 EXPECT_EQ(numericTypes.getUnsignedShort(), numericTypes2.getUnsignedShort());
132 EXPECT_EQ((numericTypes.getUnsignedByte()), numericTypes2.getUnsignedByte());
133
134 EXPECT_EQ(listPrimitiveTypes.getListInt()[0], listPrimitiveTypes2.getListInt()[0]);
135 EXPECT_EQ(listPrimitiveTypes.getListInt()[1], listPrimitiveTypes2.getListInt()[1]);
136 EXPECT_EQ(listPrimitiveTypes.getListShort()[0], listPrimitiveTypes2.getListShort()[0]);
137 EXPECT_EQ(listPrimitiveTypes.getListShort()[1], listPrimitiveTypes2.getListShort()[1]);
138 EXPECT_EQ(listPrimitiveTypes.getListByte()[0], listPrimitiveTypes2.getListByte()[0]);
139 EXPECT_EQ(listPrimitiveTypes.getListByte()[1], listPrimitiveTypes2.getListByte()[1]);
140 EXPECT_EQ(listPrimitiveTypes.getListDouble()[0], listPrimitiveTypes2.getListDouble()[0]);
141 EXPECT_EQ(listPrimitiveTypes.getListDouble()[1], listPrimitiveTypes2.getListDouble()[1]);
142 EXPECT_EQ(listPrimitiveTypes.getListBoolean()[0], listPrimitiveTypes2.getListBoolean()[0]);
143 EXPECT_EQ(listPrimitiveTypes.getListBoolean()[1], listPrimitiveTypes2.getListBoolean()[1]);
144 }
145
TEST_F(XmlTest,Nestedtype)146 TEST_F(XmlTest, Nestedtype) {
147 using namespace nested::type;
148 Employee employee = *read(Resource("nested_type.xml").c_str());
149
150 Employee::Address address = *employee.getFirstAddress();
151 Employee::Address::Extra extra = *address.getFirstExtra();
152
153 EXPECT_EQ((int)employee.getId(), 1);
154 EXPECT_EQ(employee.getName(), "Peter");
155 EXPECT_EQ(address.getCountry(), "US");
156 EXPECT_EQ(address.getState(), "Mountain View");
157 EXPECT_EQ(address.getZip(), 3342);
158 EXPECT_EQ(extra.getLine1(), "Donga 303-111");
159 EXPECT_EQ(extra.getLine2(), "Good Street");
160
161 ofstream out("old_nested_type.xml");
162 write(out, employee);
163 }
164
TEST_F(XmlTest,Purchasesimple)165 TEST_F(XmlTest, Purchasesimple) {
166 using namespace purchase::simple;
167 PurchaseOrderType orderType = *read(Resource("purchase_simple.xml").c_str());
168
169 EXPECT_EQ(orderType.getOrderDate(), "1900-01-01");
170
171 EXPECT_EQ(orderType.getShipTo()[0].getName(), "name1");
172 EXPECT_EQ(orderType.getShipTo()[0].getStreet(), "street1");
173 EXPECT_EQ(orderType.getShipTo()[0].getCity(), "city1");
174 EXPECT_EQ(orderType.getShipTo()[0].getState(), "state1");
175 EXPECT_EQ(orderType.getShipTo()[0].getZip(), 1);
176 EXPECT_EQ(orderType.getShipTo()[0].getCountry(), "US");
177 EXPECT_EQ(orderType.getShipTo()[1].getName(), "name2");
178 EXPECT_EQ(orderType.getShipTo()[1].getStreet(), "street2");
179 EXPECT_EQ(orderType.getShipTo()[1].getCity(), "city2");
180 EXPECT_EQ(orderType.getShipTo()[1].getState(), "state2");
181 EXPECT_EQ(orderType.getShipTo()[1].getZip(), -7922816251426433759);
182 EXPECT_EQ(orderType.getShipTo()[1].getCountry(), "US");
183
184 EXPECT_EQ(orderType.getBillTo()[0].getName(), "billName");
185 EXPECT_EQ(orderType.getBillTo()[0].getStreet(), "billStreet");
186 EXPECT_EQ(orderType.getBillTo()[0].getCity(), "billCity");
187 EXPECT_EQ(orderType.getBillTo()[0].getState(), "billState");
188 EXPECT_EQ(orderType.getBillTo()[0].getZip(), 1);
189 EXPECT_EQ(orderType.getBillTo()[0].getCountry(), "US");
190
191 ofstream out("old_purchase_simple.xml");
192 write(out, orderType);
193
194 PurchaseOrderType orderType2 = *read("old_purchase_simple.xml");
195
196 EXPECT_EQ(orderType.getOrderDate(), orderType2.getOrderDate());
197
198 EXPECT_EQ(orderType.getShipTo()[0].getName(), orderType2.getShipTo()[0].getName());
199 EXPECT_EQ(orderType.getShipTo()[0].getStreet(), orderType2.getShipTo()[0].getStreet());
200 EXPECT_EQ(orderType.getShipTo()[0].getCity(), orderType2.getShipTo()[0].getCity());
201 EXPECT_EQ(orderType.getShipTo()[0].getState(), orderType2.getShipTo()[0].getState());
202 EXPECT_EQ(orderType.getShipTo()[0].getZip(), orderType2.getShipTo()[0].getZip());
203 EXPECT_EQ(orderType.getShipTo()[0].getCountry(), orderType2.getShipTo()[0].getCountry());
204 EXPECT_EQ(orderType.getShipTo()[1].getName(), orderType2.getShipTo()[1].getName());
205 EXPECT_EQ(orderType.getShipTo()[1].getStreet(), orderType2.getShipTo()[1].getStreet());
206 EXPECT_EQ(orderType.getShipTo()[1].getCity(), orderType2.getShipTo()[1].getCity());
207 EXPECT_EQ(orderType.getShipTo()[1].getState(), orderType2.getShipTo()[1].getState());
208 EXPECT_EQ(orderType.getShipTo()[1].getZip(), orderType2.getShipTo()[1].getZip());
209 EXPECT_EQ(orderType.getShipTo()[1].getCountry(), orderType2.getShipTo()[1].getCountry());
210
211 EXPECT_EQ(orderType.getBillTo()[0].getName(), orderType2.getBillTo()[0].getName());
212 EXPECT_EQ(orderType.getBillTo()[0].getStreet(), orderType2.getBillTo()[0].getStreet());
213 EXPECT_EQ(orderType.getBillTo()[0].getCity(), orderType2.getBillTo()[0].getCity());
214 EXPECT_EQ(orderType.getBillTo()[0].getState(), orderType2.getBillTo()[0].getState());
215 EXPECT_EQ(orderType.getBillTo()[0].getZip(), orderType2.getBillTo()[0].getZip());
216 EXPECT_EQ(orderType.getBillTo()[0].getCountry(), orderType2.getBillTo()[0].getCountry());
217 }
218
TEST_F(XmlTest,Reference)219 TEST_F(XmlTest, Reference) {
220 using namespace reference;
221 Class _class = *read(Resource("reference.xml").c_str());
222
223 EXPECT_EQ(_class.getStudent()[0], "Sam");
224 EXPECT_EQ(_class.getStudent()[1], "Paul");
225 EXPECT_EQ(_class.getStudent()[2], "Peter");
226
227 ofstream out("old_reference.xml");
228 write(out, _class);
229 }
230
TEST_F(XmlTest,Simplecomplexcontent)231 TEST_F(XmlTest, Simplecomplexcontent) {
232 using namespace simple::complex::content;
233 Person person = *readPerson(Resource("simple_complex_content.xml").c_str());
234 USAddressP uSAddressP = *person.getFirstUSAddressP();
235 KRAddress kRAddress = *person.getFirstKRAddress();
236 SubAddress subAddress = *person.getFirstSubAddress();
237
238 EXPECT_EQ(person.getName(), "Petr");
239
240 EXPECT_EQ(uSAddressP.getName(), "404");
241 EXPECT_EQ(uSAddressP.getStreet(), "street fighter");
242 EXPECT_EQ(uSAddressP.getCity(), "New York");
243 EXPECT_EQ(uSAddressP.getState(), "Washington");
244 EXPECT_EQ(uSAddressP.getZipcode(), 323232318329852);
245
246 EXPECT_EQ(kRAddress.getName(), "Donga Studio");
247 EXPECT_EQ(kRAddress.getStreet(), "Nokdu Street");
248 EXPECT_EQ(kRAddress.getCity(), "Seoul");
249
250 EXPECT_EQ(subAddress.getChoice1_optional(), "Temp");
251
252 ofstream out("old_simple_complex_content.xml");
253 write(out, person);
254 }
255
TEST_F(XmlTest,Attrgroupsimple)256 TEST_F(XmlTest, Attrgroupsimple) {
257 using namespace attr::group::simple;
258 Student student = *read(Resource("attr_group_simple.xml").c_str());
259
260 EXPECT_EQ(student.getName(), "Jun");
261 EXPECT_EQ(student.getCity(), "Mountain View");
262 EXPECT_EQ(student.getState(), "CA");
263 EXPECT_EQ(student.getRoad(), "Street 101");
264 EXPECT_EQ(student.getList()[0], 1);
265 EXPECT_EQ(student.getList()[1], 2);
266 EXPECT_EQ(student.getList()[2], 3);
267
268 ofstream out("old_attr_group_simple.xml");
269 write(out, student);
270 }
271
TEST_F(XmlTest,Group)272 TEST_F(XmlTest, Group) {
273 using namespace group;
274 Student student = *read(Resource("group.xml").c_str());
275
276 EXPECT_EQ(student.getCity(), "Mountain View");
277 EXPECT_EQ(student.getState(), "CA");
278 EXPECT_EQ(student.getRoad(), "Street 101");
279
280 ofstream out("old_group.xml");
281 write(out, student);
282 }
283