01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.axis2.databinding.utils;
20:
21: import junit.framework.TestCase;
22: import org.apache.axiom.om.OMElement;
23: import org.apache.axiom.om.util.StAXUtils;
24: import org.apache.axis2.util.StreamWrapper;
25:
26: import javax.xml.namespace.QName;
27: import javax.xml.stream.XMLStreamReader;
28: import java.io.StringReader;
29:
30: public class NamedStaxOMBuilderTest extends TestCase {
31:
32: public void testNamedOMBuilder() throws Exception {
33:
34: String xmlDoc = "<wrapper><myIntVal>200</myIntVal></wrapper>";
35: XMLStreamReader reader = StAXUtils
36: .createXMLStreamReader(new StringReader(xmlDoc));
37:
38: NamedStaxOMBuilder sm = new NamedStaxOMBuilder(reader,
39: new QName("wrapper"));
40: OMElement elt = sm.getOMElement();
41:
42: assertNotNull(elt);
43: assertEquals(elt.getLocalName(), "wrapper");
44:
45: }
46:
47: public void testNamedOMBuilder1() throws Exception {
48:
49: String xmlDoc = "<wrapper><myIntVal>200</myIntVal></wrapper>";
50: XMLStreamReader reader = StAXUtils
51: .createXMLStreamReader(new StringReader(xmlDoc));
52:
53: //move upto the myIntVal start element first
54: boolean done = false;
55: QName nameToMatch = new QName("myIntVal");
56: while (!done) {
57: if (reader.isStartElement()
58: && nameToMatch.equals(reader.getName())) {
59: done = true;
60: } else {
61: reader.next();
62: }
63: }
64:
65: //we need the wrapper here - it is the nature of the builders that
66: //they expect the *next* event to be the start element (not the
67: //current one) So we need the wrapper to simulate a full fledged
68: //
69: NamedStaxOMBuilder sm = new NamedStaxOMBuilder(
70: new StreamWrapper(reader), nameToMatch);
71: OMElement elt = sm.getOMElement();
72:
73: assertNotNull(elt);
74: assertEquals(elt.getLocalName(), "myIntVal");
75:
76: }
77:
78: }
|