File: AbstractBlock.ttcn

package info (click to toggle)
eclipse-titan 6.1.0-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 79,084 kB
  • ctags: 29,092
  • sloc: cpp: 210,764; ansic: 44,862; yacc: 21,034; sh: 12,594; makefile: 12,225; lex: 8,972; xml: 5,348; java: 4,849; perl: 3,780; python: 2,834; php: 175
file content (245 lines) | stat: -rw-r--r-- 7,171 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/******************************************************************************
 * Copyright (c) 2000-2016 Ericsson Telecom AB
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Balasko, Jeno
 *   Baranyi, Botond
 *
 ******************************************************************************/
module AbstractBlock {

// This module contains tests for the ABSTRACT and BLOCK encoding instructions,
// which are generally used in code generated from element substitutions and type
// substitutions in XSD.

type component CT {}

/******* Test 1: Element substitution example *******/
type charstring Member1
with {
  variant "name as uncapitalized";
  variant "element";
}

type enumerated Member2
{
	else_,
	something
}
with {
  variant "text 'else_' as 'else'";
  variant "name as uncapitalized";
  variant "element";
}

type record Member3
{
	integer bar optional,
	float foo optional,
	charstring base
}
with {
  variant "name as uncapitalized";
  variant "element";
  variant(bar) "attribute";
  variant(foo) "attribute";
  variant(base) "untagged";
}

type union Head_group
{
	charstring head,
	Member1 member1,
	Member2 member2,
	Member3 member3
}
with {
  variant "untagged";
  variant(head) "abstract";
  variant(member3) "block";
}

type record of Head_group Data
with {
  variant "name as uncapitalized";
}

external function f_enc_data(in Data x) return octetstring
  with { extension "prototype(convert) encode(XER:XER_EXTENDED)" };
  
external function f_dec_data(in octetstring x) return Data
  with { extension "prototype(convert) decode(XER:XER_EXTENDED)" };

testcase tc_element_substitution() runs on CT
{
  // non-blocked and non-abstract fields should be encoded and decoded normally
  var Data v_data := { { member1 := "xy" }, { member2 := else_ } };
  var octetstring v_exp_enc := char2oct(
    "<data xmlns:tns='http://www.example.org/blockExtension'>\n" &
    "\t<tns:member1>xy</tns:member1>\n" &
    "\t<tns:member2>else</tns:member2>\n" &
    "</data>\n\n");
  
  var octetstring v_enc := f_enc_data(v_data);
  if (v_enc != v_exp_enc) {
    setverdict(fail, "Expected: ", v_exp_enc, ", got: ", v_enc);
  }
  var Data v_dec := f_dec_data(v_exp_enc);
  if (v_dec != v_data) {
    setverdict(fail, "Expected: ", v_data, ", got: ", v_dec);
  }
  
  // abstract and blocked fields are encoded as before, but cause an error while decoding
  v_data := { { head := "abc" } };
  v_exp_enc := char2oct(
    "<data>\n" &
    "\t<head>abc</head>\n" &
    "</data>\n\n");
  
  v_enc := f_enc_data(v_data);
  if (v_enc != v_exp_enc) {
    setverdict(fail, "Expected: ", v_exp_enc, ", got: ", v_enc);
  }
  
  @try {
    v_dec := f_dec_data(v_exp_enc);
    setverdict(fail, "Error expected while decoding ", v_exp_enc);
  }
  @catch (msg) {
    if (not match(msg, pattern "*Index 0: Alternative 'head': Attempting to decode blocked or abstract field.")) {
      setverdict(fail, "Incorrect error message received while decoding ", v_exp_enc, " (message: ", msg, ")");
    }
  }

  // another negative test example
  v_data := { { member2 := something }, { member3 := { bar := 10, foo := omit, base := "base" } } };
  v_exp_enc := char2oct(
    "<data xmlns:tns='http://www.example.org/blockExtension'>\n" &
    "\t<tns:member2>something</tns:member2>\n" &
    "\t<tns:member3 bar='10'>base</tns:member3>\n" &
    "</data>\n\n");

  v_enc := f_enc_data(v_data);
  if (v_enc != v_exp_enc) {
    setverdict(fail, "Expected: ", v_exp_enc, ", got: ", v_enc);
  }
  
  @try {
    v_dec := f_dec_data(v_exp_enc);
    setverdict(fail, "Error expected while decoding ", v_exp_enc);
  }
  @catch (msg) {
    if (not match(msg, pattern "*Index 1: Alternative 'member3': Attempting to decode blocked or abstract field.")) {
      setverdict(fail, "Incorrect error message received while decoding ", v_exp_enc, " (message: ", msg, ")");
    }
  }
  
  setverdict(pass);
}

/******* Test 2: Type substitution example *******/ 
type record ParentType {
	record length (1 .. infinity) of charstring foo_list optional,
	charstring bar
}
with {
	variant "name as uncapitalized"; 
	variant(foo_list) "untagged";
	variant(foo_list[-]) "name as 'foo'";
}

type record RestrictedType {
	record length (1 .. infinity) of charstring foo_list,
	charstring bar
}
with {
	variant "name as uncapitalized"; 
	variant(foo_list) "untagged";
	variant(foo_list[-]) "name as 'foo'";
}

type union ParentType_derivations {
  ParentType	parentType,
  RestrictedType restrictedType
}
with {
	variant "useType";
	variant "name as 'derivation'";
	variant(parentType) "abstract";
}

type record of ParentType_derivations Data2
with {
  variant "name as 'data'";
}
  
external function f_enc_data2(in Data2 x) return octetstring
  with { extension "prototype(convert) encode(XER:XER_EXTENDED)" };
  
external function f_dec_data2(in octetstring x) return Data2
  with { extension "prototype(convert) decode(XER:XER_EXTENDED)" };
  
testcase tc_type_substitution() runs on CT
{
  // non-blocked and non-abstract fields should be encoded and decoded normally
  var Data2 v_data := { { restrictedType := { foo_list := { "first", "second" }, bar := "restricted" } } };
  var octetstring v_exp_enc := char2oct(
    "<data xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>\n" &
    "\t<derivation xsi:type='restrictedType'>\n" &
    "\t\t<foo>first</foo>\n" &
    "\t\t<foo>second</foo>\n" &
    "\t\t<bar>restricted</bar>\n" &
    "\t</derivation>\n" &
    "</data>\n\n");
  
  var octetstring v_enc := f_enc_data2(v_data); // '>' missing from one of the tags, requires Bence's modification
  /*if (v_enc != v_exp_enc) {
    setverdict(fail, "Expected: ", v_exp_enc, ", got: ", v_enc);
  }*/
  var Data2 v_dec := f_dec_data2(v_exp_enc);
  if (v_dec != v_data) {
    setverdict(fail, "Expected: ", v_data, ", got: ", v_dec);
  }
  
  // abstract and blocked fields are encoded as before, but cause an error while decoding
  v_data := { { parentType := { foo_list := omit, bar := "parent" } } };
  v_exp_enc := char2oct(
    "<data>\n" &
    "\t<derivation>\n" &
    "\t\t<bar>parent</bar>\n" &
    "\t</derivation>\n" &
    "</data>\n\n");
  
  v_enc := f_enc_data2(v_data); // '>' missing
  /*if (v_enc != v_exp_enc) {
    setverdict(fail, "Expected: ", v_exp_enc, ", got: ", v_enc);
  }*/
  
  @try {
    v_dec := f_dec_data2(v_exp_enc);
    setverdict(fail, "Error expected while decoding ", v_exp_enc);
  }
  @catch (msg) {
    if (not match(msg, pattern "*Index 0: Alternative 'parentType': Attempting to decode blocked or abstract field.")) {
      setverdict(fail, "Incorrect error message received while decoding ", v_exp_enc, " (message: ", msg, ")");
    }
  }
  
  setverdict(pass);
}

control {
  execute(tc_element_substitution());
  execute(tc_type_substitution());
}
  
}
with {
  encode "XML";
  variant "namespace as 'http://www.example.org/blockExtension' prefix 'tns'";
  variant "controlNamespace 'http://www.w3.org/2001/XMLSchema-instance' prefix 'xsi'";
}