/
TestDFDLExpressionEvaluation.scala
167 lines (153 loc) · 6.73 KB
/
TestDFDLExpressionEvaluation.scala
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
/* Copyright (c) 2012-2014 Tresys Technology, LLC. All rights reserved.
*
* Developed by: Tresys Technology, LLC
* http://www.tresys.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal with
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimers.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimers in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the names of Tresys Technology, nor the names of its contributors
* may be used to endorse or promote products derived from this Software
* without specific prior written permission.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
* SOFTWARE.
*/
package edu.illinois.ncsa.daffodil.dpath
import junit.framework.Assert._
import org.junit.Test
import edu.illinois.ncsa.daffodil.processors.Infoset
import edu.illinois.ncsa.daffodil.util.SchemaUtils
import scala.xml.Utility
import edu.illinois.ncsa.daffodil.compiler._
import scala.util.parsing.combinator.Parsers
import edu.illinois.ncsa.daffodil.xml.NS
import edu.illinois.ncsa.daffodil.xml.NoNamespace
import edu.illinois.ncsa.daffodil.processors.ElementRuntimeData
import edu.illinois.ncsa.daffodil.xml.StepQName
import edu.illinois.ncsa.daffodil.Implicits._; object INoWarn2 { ImplicitsSuppressUnusedImportWarning() }
import edu.illinois.ncsa.daffodil.processors.VariableMap
import edu.illinois.ncsa.daffodil.processors.PState
import edu.illinois.ncsa.daffodil.util.TestUtils
import edu.illinois.ncsa.daffodil.io.ByteBufferDataInputStream
class TestDFDLExpressionEvaluation extends Parsers {
def testExpr(testSchema: scala.xml.Elem, infosetAsXML: scala.xml.Elem, expr: String)(body: Any => Unit) {
val schemaCompiler = Compiler()
val pf = schemaCompiler.compileNode(testSchema).asInstanceOf[ProcessorFactory]
if (pf.isError) fail("pf compile errors")
val dp = pf.onPath("/")
val sset = pf.sset
val Seq(schema) = sset.schemas
val Seq(schemaDoc, _) = schema.schemaDocuments
val Seq(declf) = schemaDoc.globalElementDecls
val decl = declf.forRoot()
val erd = decl.elementRuntimeData
val infosetRootElem = Infoset.elem2Infoset(erd, infosetAsXML)
val exprCompiler = new DFDLPathExpressionParser(NodeInfo.AnyType, testSchema.scope, erd.dpathCompileInfo, false)
val compiledExpr = exprCompiler.compile(expr)
val doc = Infoset.newDocument(erd)
doc.setRootElement(infosetRootElem)
val dis = ByteBufferDataInputStream(java.nio.ByteBuffer.allocate(0), 0L) // fake. Zero bits available.
val pstate = PState.createInitialPState(doc, erd, dis, dp)
val result = compiledExpr.evaluate(pstate)
body(result)
}
@Test def test_a() = {
val schema = SchemaUtils.dfdlTestSchemaUnqualified(
<dfdl:format ref="tns:daffodilTest1"/>,
<xs:element name="a" type="xs:string" dfdl:lengthKind="explicit" dfdl:length="{ xs:unsignedInt(5) }"/>)
val data = <a xmlns="http://example.com">aaaaa</a>
testExpr(schema, data, "{ /tns:a }") { (res: Any) =>
assertEquals("aaaaa", res)
}
}
@Test def test_ba() {
val schema = SchemaUtils.dfdlTestSchemaUnqualified(
<dfdl:format ref="tns:daffodilTest1"/>,
<xs:element name="b">
<xs:complexType>
<xs:sequence>
<xs:element name="a" type="xs:string" dfdl:lengthKind="explicit" dfdl:length="{ xs:unsignedInt(5) }"/>
</xs:sequence>
</xs:complexType>
</xs:element>)
val data = <b xmlns="http://example.com"><a>aaaaa</a></b>
testExpr(schema, data, "{ /tns:b/a }") { (res: Any) =>
assertEquals("aaaaa", res)
}
}
@Test def test_arrayCount1() {
val schema = SchemaUtils.dfdlTestSchemaUnqualified(
<dfdl:format ref="tns:daffodilTest1"/>,
<xs:element name="b">
<xs:complexType>
<xs:sequence>
<xs:element name="a" maxOccurs="2" type="xs:string" dfdl:lengthKind="explicit" dfdl:length="{ xs:unsignedInt(5) }"/>
</xs:sequence>
</xs:complexType>
</xs:element>)
val data = <b xmlns="http://example.com"><a>aaaaa</a><a>bbbbb</a></b>
testExpr(schema, data, "{ fn:count(/tns:b/a) }") { (res: Any) =>
assertEquals(2L, res)
}
}
@Test def test_arrayIndex1() {
val schema = SchemaUtils.dfdlTestSchemaUnqualified(
<dfdl:format ref="tns:daffodilTest1"/>,
<xs:element name="b">
<xs:complexType>
<xs:sequence>
<xs:element name="a" maxOccurs="2" type="xs:string" dfdl:lengthKind="explicit" dfdl:length="{ xs:unsignedInt(5) }"/>
</xs:sequence>
</xs:complexType>
</xs:element>)
val data = <b xmlns="http://example.com"><a>aaaaa</a><a>bbbbb</a></b>
testExpr(schema, data, "{ /tns:b/a[1] }") { (res: Any) =>
assertEquals("aaaaa", res)
}
}
@Test def test_absPathWithArrayIndex1() {
val schema = SchemaUtils.dfdlTestSchemaUnqualified(
<dfdl:format ref="tns:daffodilTest1"/>,
<xs:element name="b">
<xs:complexType>
<xs:sequence>
<xs:element name="a" maxOccurs="2" type="xs:string" dfdl:lengthKind="explicit" dfdl:length="{ xs:unsignedInt(5) }"/>
</xs:sequence>
</xs:complexType>
</xs:element>)
val data = <b xmlns="http://example.com"><a>aaaaa</a><a>bbbbb</a></b>
testExpr(schema, data, "{ /tns:b/a[2] }") { (res: Any) =>
assertEquals("bbbbb", res)
}
}
@Test def test_ivc1() {
val schema = SchemaUtils.dfdlTestSchemaUnqualified(
<dfdl:format ref="tns:daffodilTest1"/>,
<xs:element name="b">
<xs:complexType>
<xs:sequence>
<xs:element name="i" type="xs:long" dfdl:inputValueCalc="{ 5 }"/>
<xs:element name="a" type="xs:long" dfdl:inputValueCalc="{ ../i }"/>
</xs:sequence>
</xs:complexType>
</xs:element>)
TestUtils.testString(schema, "")
}
}