File: SchemaPaths.java

package info (click to toggle)
libjibx1.2-java 1.2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,260 kB
  • sloc: java: 75,013; xml: 14,068; makefile: 17
file content (177 lines) | stat: -rw-r--r-- 9,350 bytes parent folder | download | duplicates (9)
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

package org.jibx.schema.elements;

import org.jibx.schema.SchemaTestBase;

/**
 * Test handling of schemas paths.
 */
public class SchemaPaths extends SchemaTestBase
{
    public static final String TEST_SCHEMA =
        "<schema targetNamespace='urn:anything'" +
        "    xmlns='http://www.w3.org/2001/XMLSchema'" +
        "    xmlns:tns='urn:anything'" +
        "    elementFormDefault='qualified'>\n" +
        "  <complexType name='simple'>\n" +
        "    <choice minOccurs='1' maxOccurs='3'>\n" +
        "      <element name='name' type='string'/>\n" +
        "      <element name='age' type='int'/>\n" +
        "      <element ref='tns:simple'/>\n" +
        "    </choice>\n" +
        "    <attribute name='male' use='required' type='boolean'/>\n" +
        "    <attribute name='registered' default='true' type='boolean'/>\n" +
        "  </complexType>\n" +
        "  <element name='simple' type='string'/>\n" +
        "  <complexType name='complex'>\n" +
        "    <sequence>\n" +
        "      <element name='name' type='string'/>\n" +
        "      <element name='age' type='int'/>\n" +
        "      <element name='quality'>\n" +
        "        <simpleType>\n" +
        "          <restriction base='token'>\n" +
        "            <enumeration value='poor'/>\n" +
        "            <enumeration value='fair'/>\n" +
        "            <enumeration value='good'/>\n" +
        "            <enumeration value='excellent'/>\n" +
        "          </restriction>\n" +
        "        </simpleType>\n" +
        "      </element>\n" +
        "    </sequence>\n" +
        "    <attribute name='male' use='required' type='boolean'/>\n" +
        "    <attribute name='registered' default='true' type='boolean'/>\n" +
        "  </complexType>\n" +
        "  <element name='complex' type='tns:complex'/>\n" +
        "</schema>";
    
    private void verifyNoProblems() {
        assertFalse(getProblemText(m_validationContext), hasProblem(m_validationContext));
    }
    
    public void testDirectSpecification() throws Exception {
        SchemaElement schema = prepareSchema(TEST_SCHEMA);
        SchemaPath path = SchemaPath.buildPath(null, "complexType", "simple", null, this, m_validationContext);
        verifyNoProblems();
        OpenAttrBase match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched", match instanceof ComplexTypeElement);
        path = SchemaPath.buildPath(null, "complexType", null, "1", this, m_validationContext);
        verifyNoProblems();
        match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched", match instanceof ComplexTypeElement);
        path = SchemaPath.buildPath(null, "complexType", "simple", "1", this, m_validationContext);
        verifyNoProblems();
        match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched", match instanceof ComplexTypeElement);
    }
    
    public void testDirectPath() throws Exception {
        SchemaElement schema = prepareSchema(TEST_SCHEMA);
        SchemaPath path = SchemaPath.buildPath("complexType[@name=simple]", "complexType", null, null, this,
            m_validationContext);
        verifyNoProblems();
        OpenAttrBase match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched", match instanceof ComplexTypeElement);
        path = SchemaPath.buildPath("complexType[1]", "complexType", null, null, this, m_validationContext);
        verifyNoProblems();
        match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched", match instanceof ComplexTypeElement);
        path = SchemaPath.buildPath("complexType[@name=simple][1]", "complexType", null, null, this,
            m_validationContext);
        verifyNoProblems();
        match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched", match instanceof ComplexTypeElement);
        path = SchemaPath.buildPath("[@name=simple][1]", "complexType", null, null, this, m_validationContext);
        verifyNoProblems();
        match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched", match instanceof ComplexTypeElement);
    }
    
    public void testPathAndSpecification() throws Exception {
        SchemaElement schema = prepareSchema(TEST_SCHEMA);
        SchemaPath path = SchemaPath.buildPath("complexType[@name=complex]/", "attribute", "male", null, this,
            m_validationContext);
        verifyNoProblems();
        OpenAttrBase match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched",
            match instanceof AttributeElement && ((AttributeElement)match).getName().equals("male"));
        path = SchemaPath.buildPath("complexType[1]/", "attribute", null, "2", this, m_validationContext);
        verifyNoProblems();
        match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched",
            match instanceof AttributeElement && ((AttributeElement)match).getName().equals("registered"));
        path = SchemaPath.buildPath("complexType[2]/", "sequence", null, null, this, m_validationContext);
        verifyNoProblems();
        match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched", match instanceof SequenceElement);
    }
    
    public void testWildcardPath() throws Exception {
        SchemaElement schema = prepareSchema(TEST_SCHEMA);
        SchemaPath path = SchemaPath.buildPath("complexType[@name=complex]/*/element[@name=age]", "element", null, null,
            this, m_validationContext);
        verifyNoProblems();
        OpenAttrBase match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched",
            match instanceof ElementElement && ((ElementElement)match).getName().equals("age"));
        path = SchemaPath.buildPath("complexType[1]/*/element[3]", "element", null, null, this, m_validationContext);
        verifyNoProblems();
        match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched", match instanceof ElementElement && ((ElementElement)match).getName() == null);
        path = SchemaPath.buildPath("complexType[1]/*/[3]", "element", null, null, this, m_validationContext);
        verifyNoProblems();
        match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched", match instanceof ElementElement && ((ElementElement)match).getName() == null);
        path = SchemaPath.buildPath("complexType[1]/**/[3]", "element", null, null, this, m_validationContext);
        verifyNoProblems();
        match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched", match instanceof ElementElement && ((ElementElement)match).getName() == null);
        path = SchemaPath.buildPath("complexType[2]/**/[3]", "enumeration", null, null, this, m_validationContext);
        verifyNoProblems();
        match = path.matchUnique(schema);
        verifyNoProblems();
        assertTrue("Path not matched", match instanceof FacetElement.Enumeration &&
            ((FacetElement.Enumeration)match).getValue().equals("good"));
    }
    
    public void testErrors() throws Exception {
        SchemaElement schema = prepareSchema(TEST_SCHEMA);
        SchemaPath path = SchemaPath.buildPath("complexType[1][1]/", "element", null, null, this, m_validationContext);
        assertTrue("Multiple position predicates on path step", hasProblem(m_validationContext));
        m_validationContext.getProblems().clear();
        path = SchemaPath.buildPath("complexType[1][@name=xxx]/", "element", null, null, this, m_validationContext);
        assertTrue("Predicates in wrong order on path step", hasProblem(m_validationContext));
        m_validationContext.getProblems().clear();
        path = SchemaPath.buildPath("complexType[1]/*/[3]", "element", null, "2", this, m_validationContext);
        assertTrue("Position predicate mismatch with specified value", hasProblem(m_validationContext));
        m_validationContext.getProblems().clear();
        path = SchemaPath.buildPath("complexType[1]/*/[@name=xxx]", "element", "yyy", null, this, m_validationContext);
        assertTrue("Name predicate mismatch with specified value", hasProblem(m_validationContext));
        m_validationContext.getProblems().clear();
        path = SchemaPath.buildPath("complexType[1]/**/", "element", null, null, this, m_validationContext);
        verifyNoProblems();
        OpenAttrBase match = path.matchUnique(schema);
        assertTrue("Multiple matches for path expression", hasProblem(m_validationContext));
        m_validationContext.getProblems().clear();
        assertNull("Multiple matches for path expression", match);
        path = SchemaPath.buildPath("complexType[2]/**/[7]", "enumeration", null, null, this, m_validationContext);
        verifyNoProblems();
        match = path.matchUnique(schema);
        assertTrue("No match for path expression", hasProblem(m_validationContext));
        m_validationContext.getProblems().clear();
        assertNull("No match for path expression", match);
    }
}