1 /******************************************************************************
2 * Copyright (c) 2000-2015 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 ******************************************************************************/
10 import from NamespaceDef all;
12 modulepar boolean NamespaceTest_verbose := false;
13 #define verbose NamespaceTest_verbose
14 #include "../macros.ttcnin"
16 type component water {}
18 DECLARE_XER_ENCODERS(MyRecordType, rec);
19 DECLARE_EXER_ENCODERS(MyRecordType, rec);
21 const universal charstring etalon := // for E-XER
22 "<ns0:MyRecordType xmlns:ns0=\'http://www.example.org/\' xmlns:ns1=\'http://www.example.org/example1\'>\n" &
23 "\t<ns1:field1>3</ns1:field1>\n" &
24 "\t<ns0:field2>four</ns0:field2>\n" &
25 "</ns0:MyRecordType>\n\n";
27 /* and not like this, with the namespace declaration for nsl in the field1 tag:
29 //<?xml version="1.0" encoding="UTF-8"?>
30 "<ns0:MyRecordType xmlns:ns0=http://www.example.org/>\n" &
31 "\t<ns1:field1 xmlns:ns1=http://www.example.org/example1>3</ns1:field1>\n" &
32 "\t<ns0:field2>four</ ns0:field2>\n" &
33 "</ns0:MyRecordType>\n\n";
36 // MyRecordType requires a namespace, decoding this should fail
37 const universal charstring etalon_nons := // for E-XER
38 "<MyRecordType xmlns:ns0=\'http://www.example.org/\' xmlns:ns1=\'http://www.example.org/example1\'>\n" &
39 "\t<ns1:field1>3</ns1:field1>\n" &
40 "\t<ns0:field2>four</ns0:field2>\n" &
41 "</MyRecordType>\n\n";
44 const universal charstring etalon_b := // for B-XER
46 "\t<field1>3</field1>\n" &
47 "\t<field2>four</field2>\n" &
48 "</MyRecordType>\n\n";
50 const universal charstring etalon_c := // for C-XER (no indenting)
52 "<field1>3</field1>" &
53 "<field2>four</field2>" &
56 testcase enc_ns() runs on water
58 CHECK_METHOD(bxer_enc_rec, valueof(NamespaceDef.MyTemplate), etalon_b);
59 CHECK_METHOD(cxer_enc_rec, valueof(NamespaceDef.MyTemplate), etalon_c);
60 CHECK_METHOD(exer_enc_rec, valueof(NamespaceDef.MyTemplate), etalon);
64 template charstring etalon_b := pattern
65 "<MyRecordType>\n#(,1)" &
66 "\t#(,1)<field1>3</field1>\n#(,1)" &
67 "\t#(,1)<field2>four</field2>\n#(,1)" &
68 "</MyRecordType>\n#(,1)\n";
70 testcase enc_ns() runs on water
72 CHECK_METHOD(bxer_enc_rec, valueof(NamespaceDef.MyTemplate), etalon_b);
73 CHECK_METHOD(cxer_enc_rec, valueof(NamespaceDef.MyTemplate), etalon_b);
74 CHECK_METHOD(exer_enc_rec, valueof(NamespaceDef.MyTemplate), etalon);
79 // Alternative representation: different prefix for the same namespace.
80 // The prefix is just a shorthand and can be anything. This should be decoded
81 // exactly the same as "etalon". Regression test for HK86210.
82 const universal charstring etalon_2 :=
83 "<myns:MyRecordType xmlns:myns=\'http://www.example.org/\' xmlns:ns1=\'http://www.example.org/example1\'>\n" &
84 "\t<ns1:field1>3</ns1:field1>\n" &
85 "\t<myns:field2>four</myns:field2>\n" &
86 "</myns:MyRecordType>\n\n";
90 testcase dec_ns() runs on water
92 var MyRecordType expected := valueof(NamespaceDef.MyTemplate);
94 CHECK_DECODE(bxer_dec_rec, etalon_b, MyRecordType, expected);
95 CHECK_DECODE(cxer_dec_rec, etalon_c, MyRecordType, expected);
96 CHECK_DECODE(exer_dec_rec, etalon , MyRecordType, expected);
98 CHECK_DECODE(exer_dec_rec, etalon_2, MyRecordType, expected);
99 //this is expected to fail: CHECK_DECODE(exer_dec_rec, etalon_nons, MyRecordType, expected);
102 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
104 type record of MyRecordType ManyRecords;
106 DECLARE_XER_ENCODERS(ManyRecords, recs);
107 DECLARE_EXER_ENCODERS(ManyRecords, recs);
110 const ManyRecords recs := {
112 // semantic error above: Reference to a constant value was expected instead of template `@NamespaceDef.MyTemplate'
113 // probably because of const
118 const ManyRecords myrecs := {
124 const universal charstring str_recs_b :=
126 "\t<MyRecordType>\n" &
127 "\t\t<field1>3</field1>\n" &
128 "\t\t<field2>four</field2>\n" &
129 "\t</MyRecordType>\n" &
130 "\t<MyRecordType>\n" &
131 "\t\t<field1>2</field1>\n" &
132 "\t\t<field2>second</field2>\n" &
133 "\t</MyRecordType>\n" &
134 "</ManyRecords>\n\n";
136 const universal charstring str_recs_e :=
137 "<ManyRecords xmlns:ns0=\'http://www.example.org/\' xmlns:ns1=\'http://www.example.org/example1\'>\n" &
138 "\t<ns0:MyRecordType>\n" &
139 "\t\t<ns1:field1>3</ns1:field1>\n" &
140 "\t\t<ns0:field2>four</ns0:field2>\n" &
141 "\t</ns0:MyRecordType>\n" &
142 "\t<ns0:MyRecordType>\n" &
143 "\t\t<ns1:field1>2</ns1:field1>\n" &
144 "\t\t<ns0:field2>second</ns0:field2>\n" &
145 "\t</ns0:MyRecordType>\n" &
146 "</ManyRecords>\n\n";
148 const ManyRecords norecs := {}
150 const universal charstring str_norecs_b :=
151 "<ManyRecords/>\n\n";
153 const universal charstring str_norecs_e :=
154 "<ManyRecords/>\n\n";
155 //"<ManyRecords xmlns:ns0=\'http://www.example.org/\' xmlns:ns1=\'http://www.example.org/example1\'/>\n\n";
157 // NsRecords is an imported "record of MyRecordType"
158 DECLARE_XER_ENCODERS(NsRecords, nsrecs);
159 DECLARE_EXER_ENCODERS(NsRecords, nsrecs);
161 const NsRecords /* the imported record-of */ nsrecs := {
166 const universal charstring str_nsrecs_b :=
168 "\t<MyRecordType>\n" &
169 "\t\t<field1>3</field1>\n" &
170 "\t\t<field2>four</field2>\n" &
171 "\t</MyRecordType>\n" &
172 "\t<MyRecordType>\n" &
173 "\t\t<field1>2</field1>\n" &
174 "\t\t<field2>second</field2>\n" &
175 "\t</MyRecordType>\n" &
179 const universal charstring str_nsrecs_e :=
180 "<ns0:NsRecords xmlns:ns0=\'http://www.example.org/\' xmlns:ns1=\'http://www.example.org/example1\'>\n" &
181 "\t<ns0:MyRecordType>\n" &
182 "\t\t<ns1:field1>3</ns1:field1>\n" &
183 "\t\t<ns0:field2>four</ns0:field2>\n" &
184 "\t</ns0:MyRecordType>\n" &
185 "\t<ns0:MyRecordType>\n" &
186 "\t\t<ns1:field1>2</ns1:field1>\n" &
187 "\t\t<ns0:field2>second</ns0:field2>\n" &
188 "\t</ns0:MyRecordType>\n" &
189 "</ns0:NsRecords>\n\n";
192 const NsRecords nsnorecs := {}
194 const universal charstring str_nsnorecs_b :=
197 const universal charstring str_nsnorecs_e :=
198 "<ns0:NsRecords xmlns:ns0=\'http://www.example.org/\'/>\n\n";
200 testcase enc_recs() runs on water
202 CHECK_METHOD(bxer_enc_recs, myrecs, str_recs_b);
203 CHECK_METHOD(exer_enc_recs, myrecs, str_recs_e);
205 CHECK_METHOD(bxer_enc_recs, norecs, str_norecs_b);
206 CHECK_METHOD(exer_enc_recs, norecs, str_norecs_e);
208 CHECK_METHOD(bxer_enc_nsrecs, nsrecs, str_nsrecs_b);
209 CHECK_METHOD(exer_enc_nsrecs, nsrecs, str_nsrecs_e);
211 CHECK_METHOD(bxer_enc_nsrecs, nsnorecs, str_nsnorecs_b);
212 CHECK_METHOD(exer_enc_nsrecs, nsnorecs, str_nsnorecs_e);
215 testcase dec_recs() runs on water
218 CHECK_DECODE(bxer_dec_recs, str_recs_b, ManyRecords, myrecs);
219 CHECK_DECODE(exer_dec_recs, str_recs_e, ManyRecords, myrecs);
221 CHECK_DECODE(bxer_dec_recs, str_norecs_b, ManyRecords, norecs);
222 CHECK_DECODE(exer_dec_recs, str_norecs_e, ManyRecords, norecs);
224 CHECK_DECODE(bxer_dec_nsrecs, str_nsrecs_b, NsRecords, nsrecs);
225 CHECK_DECODE(exer_dec_nsrecs, str_nsrecs_e, NsRecords, nsrecs);
227 CHECK_DECODE(bxer_dec_nsrecs, str_nsnorecs_b, NsRecords, nsnorecs);
228 CHECK_DECODE(exer_dec_nsrecs, str_nsnorecs_e, NsRecords, nsnorecs);
231 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
232 // Check that alternatives are correctly identified based on name+namespace,
233 // not just name alone (i.e. if names match but the namespace is empty => mismatch)
235 type charstring MyOtherType
236 with { variant "name as 'MyRecordType'" }
238 type union MyUnionType {
239 MyRecordType myrecord,
243 DECLARE_XER_ENCODERS(MyUnionType, u);
244 DECLARE_EXER_ENCODERS(MyUnionType, u);
246 const MyUnionType uni_rec := {
247 myrecord := { 3, "four" }
250 const universal charstring bstr_unirec :=
253 "\t\t<field1>3</field1>\n" &
254 "\t\t<field2>four</field2>\n" &
256 "</MyUnionType>\n\n";
258 const universal charstring estr_unirec :=
259 "<MyUnionType xmlns:ns0='http://www.example.org/' xmlns:ns1='http://www.example.org/example1'>\n" &
260 "\t<ns0:myrecord>\n" &
261 "\t\t<ns1:field1>3</ns1:field1>\n" &
262 "\t\t<ns0:field2>four</ns0:field2>\n" &
263 "\t</ns0:myrecord>\n" &
264 "</MyUnionType>\n\n";
266 // Same thing with different namespace prefix (and order) (decoding only)
267 const universal charstring estr_unirec_2 :=
268 "<MyUnionType xmlns:ns-zero='http://www.example.org/' xmlns:ns-one='http://www.example.org/example1'>\n" &
269 "\t<ns-zero:myrecord>\n" &
270 "\t\t<ns-one:field1>3</ns-one:field1>\n" &
271 "\t\t<ns-zero:field2>four</ns-zero:field2>\n" &
272 "\t</ns-zero:myrecord>\n" &
273 "</MyUnionType>\n\n";
275 const MyUnionType uni_str := {
276 spy := "I spy with my little eye"
279 const universal charstring bstr_unistr :=
281 "\t<spy>I spy with my little eye</spy>\n" &
282 "</MyUnionType>\n\n";
284 const universal charstring estr_unistr :=
286 "\t<spy>I spy with my little eye</spy>\n" &
287 "</MyUnionType>\n\n";
289 testcase enc_union() runs on water
291 CHECK_METHOD(bxer_enc_u, uni_rec, bstr_unirec);
292 CHECK_METHOD(exer_enc_u, uni_rec, estr_unirec);
294 CHECK_METHOD(bxer_enc_u, uni_str, bstr_unistr);
295 CHECK_METHOD(exer_enc_u, uni_str, estr_unistr);
298 testcase dec_union() runs on water
300 CHECK_DECODE(bxer_dec_u, bstr_unirec, MyUnionType, uni_rec);
301 CHECK_DECODE(exer_dec_u, estr_unirec, MyUnionType, uni_rec);
302 CHECK_DECODE(exer_dec_u, estr_unirec_2, MyUnionType, uni_rec);
304 CHECK_DECODE(bxer_dec_u, bstr_unistr, MyUnionType, uni_str);
305 CHECK_DECODE(exer_dec_u, estr_unistr, MyUnionType, uni_str);
306 // No alternative for the spy
316 execute(enc_union());
317 execute(dec_union());
321 with { encode "XML" }