1
1
package wstxtest .msv ;
2
2
3
+ import java .io .StringReader ;
4
+ import java .io .StringWriter ;
5
+ import java .io .Writer ;
6
+
3
7
import javax .xml .stream .*;
4
8
5
9
import org .codehaus .stax2 .*;
6
10
import org .codehaus .stax2 .validation .*;
7
11
12
+ import com .ctc .wstx .stax .WstxInputFactory ;
13
+ import com .ctc .wstx .stax .WstxOutputFactory ;
14
+
8
15
import wstxtest .vstream .BaseValidationTest ;
9
16
10
17
/**
@@ -78,10 +85,10 @@ public class TestW3CSchema
78
85
final static String SIMPLE_XML = "<personnel>"
79
86
+ "<person id='a123' contr='true'>" + " <name>"
80
87
+ "<family>Family</family><given>Fred</given>" + " </name>"
81
- + " <url href='urn:something' />" + " </person>"
88
+ + " <url href='urn:something'/>" + " </person>"
82
89
+ " <person id='b12'>"
83
90
+ " <name><family>Blow</family><given>Joe</given>"
84
- + " </name>" + " <url />" + " </person>" + "</personnel>" ;
91
+ + " </name>" + " <url/>" + " </person>" + "</personnel>" ;
85
92
86
93
/**
87
94
* Test validation against a simple document valid according to a very
@@ -90,40 +97,59 @@ public class TestW3CSchema
90
97
public void testSimpleNonNs () throws XMLStreamException
91
98
{
92
99
XMLValidationSchema schema = parseW3CSchema (SIMPLE_NON_NS_SCHEMA );
93
- XMLStreamReader2 sr = getReader (SIMPLE_XML );
100
+ String XML = SIMPLE_XML ;
101
+ XMLStreamReader2 sr = getReader (XML );
94
102
sr .validateAgainst (schema );
95
103
104
+ StringWriter writer = new StringWriter ();
105
+ XMLStreamWriter2 sw = (XMLStreamWriter2 ) getOutputFactory ().createXMLStreamWriter (writer );
106
+ sw .validateAgainst (schema );
107
+ sw .copyEventFromReader (sr , false );
108
+
96
109
try {
97
110
assertTokenType (START_ELEMENT , sr .next ());
98
111
assertEquals ("personnel" , sr .getLocalName ());
112
+ sw .copyEventFromReader (sr , false );
99
113
100
114
while (sr .hasNext ()) {
101
115
/* int type = */ sr .next ();
116
+ sw .copyEventFromReader (sr , false );
102
117
}
103
118
} catch (XMLValidationException vex ) {
104
119
fail ("Did not expect validation exception, got: " + vex );
105
120
}
106
121
assertTokenType (END_DOCUMENT , sr .getEventType ());
122
+ assertEquals (XML .replace ("'" , "\" " ), writer .toString ());
107
123
}
108
124
109
125
public void testSimplePartialNonNs () throws XMLStreamException
110
126
{
111
127
XMLValidationSchema schema = parseW3CSchema (SIMPLE_NON_NS_SCHEMA );
112
- XMLStreamReader2 sr = getReader (SIMPLE_XML );
128
+ String XML = SIMPLE_XML ;
129
+ XMLStreamReader2 sr = getReader (XML );
113
130
131
+ StringWriter writer = new StringWriter ();
132
+ XMLStreamWriter2 sw = (XMLStreamWriter2 ) getOutputFactory ().createXMLStreamWriter (writer );
133
+ sw .copyEventFromReader (sr , false );
134
+
114
135
assertTokenType (START_ELEMENT , sr .next ());
115
136
assertEquals ("personnel" , sr .getLocalName ());
137
+ sw .copyEventFromReader (sr , false );
116
138
sr .validateAgainst (schema );
139
+ sw .validateAgainst (schema );
117
140
try {
118
141
assertTokenType (START_ELEMENT , sr .next ());
119
142
assertEquals ("person" , sr .getLocalName ());
143
+ sw .copyEventFromReader (sr , false );
120
144
while (sr .hasNext ()) {
121
145
/* int type = */ sr .next ();
146
+ sw .copyEventFromReader (sr , false );
122
147
}
123
148
} catch (XMLValidationException vex ) {
124
149
fail ("Did not expect validation exception, got: " + vex );
125
150
}
126
151
assertTokenType (END_DOCUMENT , sr .getEventType ());
152
+ assertEquals (XML .replace ("'" , "\" " ), writer .toString ());
127
153
}
128
154
129
155
/**
@@ -192,6 +218,18 @@ public void testSimpleDataTypes() throws XMLStreamException
192
218
}
193
219
sr .close ();
194
220
221
+ // validate the same document on the writer side
222
+ sr = getReader (XML );
223
+ StringWriter writer = new StringWriter ();
224
+ XMLStreamWriter2 sw = (XMLStreamWriter2 ) getOutputFactory ().createXMLStreamWriter (writer );
225
+ sw .validateAgainst (schema );
226
+ sw .copyEventFromReader (sr , true );
227
+ while (sr .hasNext ()) {
228
+ /* int type = */ sr .next ();
229
+ sw .copyEventFromReader (sr , true );
230
+ }
231
+ assertEquals (XML .replace ("\r " , "" ), writer .toString ());
232
+
195
233
// Then invalid (wrong type for value)
196
234
XML = "<item><quantity>34b</quantity><price>1.00</price></item>" ;
197
235
sr .validateAgainst (schema );
@@ -224,33 +262,28 @@ public void testSimpleText() throws XMLStreamException
224
262
+ "</xs:schema>" ;
225
263
XMLValidationSchema schema = parseW3CSchema (SCHEMA );
226
264
227
- // First, 3 valid docs:
228
- String XML = "<root>xyz</root>" ;
229
- XMLStreamReader2 sr = getReader ( XML );
230
- sr . validateAgainst ( schema ) ;
231
- streamThrough ( sr );
232
- sr . close ();
233
-
234
- XML = "<root />" ;
235
- sr = getReader ( XML );
236
- sr . validateAgainst ( schema ) ;
237
- streamThrough ( sr );
238
- sr . close ();
265
+ String XML = null ;
266
+ for ( ValidationMode mode : ValidationMode . values ()) {
267
+ // First, 3 valid docs:
268
+ XML = "<root>xyz</root>" ;
269
+ mode . validate ( schema , XML );
270
+
271
+ XML = "<root />" ;
272
+ mode . validate ( schema , XML , "<root/>" ) ;
273
+
274
+ XML = "<root></root>" ;
275
+ mode . validate ( schema , XML , "<root/>" );
276
+ }
239
277
240
- XML = "<root></root>" ;
241
- sr = getReader (XML );
242
- sr .validateAgainst (schema );
243
- streamThrough (sr );
244
- sr .close ();
245
278
246
279
// Then invalid?
247
280
XML = "<foobar />" ;
248
- sr = getReader (XML );
281
+ XMLStreamReader2 sr = getReader (XML );
249
282
sr .validateAgainst (schema );
250
283
verifyFailure (XML , schema , "should warn about wrong root element" ,
251
284
"tag name \" foobar\" is not allowed" , false );
252
285
}
253
-
286
+
254
287
/**
255
288
* Test for reproducing [WSTX-191]
256
289
*/
@@ -283,20 +316,24 @@ public void testConstrainedText() throws XMLStreamException
283
316
"<description><![CDATA[Du Texte]]></description>" );
284
317
_testValidDesc (schema ,
285
318
"<description>??</description><description><![CDATA[...]]></description>" );
286
- _testValidDesc (schema , "<description></description>" );
287
- _testValidDesc (schema , "<description />" );
319
+ _testValidDesc (schema , "<description></description>" , "<description/>" );
320
+ _testValidDesc (schema , "<description />" , "<description/>" );
288
321
_testValidDesc (schema , "<description><![CDATA[]]></description>" );
289
322
}
290
323
291
- private void _testValidDesc (XMLValidationSchema schema , String descSnippet ) throws XMLStreamException
324
+ private void _testValidDesc (XMLValidationSchema schema , String descSnippet ) throws XMLStreamException {
325
+ _testValidDesc (schema , descSnippet , descSnippet );
326
+ }
327
+ private void _testValidDesc (XMLValidationSchema schema , String descSnippet , String expectedSnippet ) throws XMLStreamException
292
328
{
293
- // These should all be valid according to the schema
294
- String XML = "<catalog xmlns='http://www.mondomaine.fr/framework'>"
295
- + descSnippet + "</catalog>" ;
296
- XMLStreamReader2 sr = getReader (XML );
297
- sr .validateAgainst (schema );
298
- streamThrough (sr );
299
- sr .close ();
329
+ for (ValidationMode mode : ValidationMode .values ()) {
330
+ // These should all be valid according to the schema
331
+ String XML = "<catalog xmlns='http://www.mondomaine.fr/framework'>"
332
+ + descSnippet + "</catalog>" ;
333
+ String expectedXML = "<catalog xmlns='http://www.mondomaine.fr/framework'>"
334
+ + expectedSnippet + "</catalog>" ;
335
+ mode .validate (schema , XML , expectedXML .replace ("'" , "\" " ));
336
+ }
300
337
}
301
338
302
339
public void testValidationHandler () throws XMLStreamException
@@ -361,4 +398,5 @@ public XMLValidationProblem getProblem() {
361
398
return problem ;
362
399
}
363
400
}
401
+
364
402
}
0 commit comments