001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.aegis.xml.stax;
019:
020: import javax.xml.stream.FactoryConfigurationError;
021: import javax.xml.stream.XMLStreamException;
022: import javax.xml.stream.XMLStreamReader;
023:
024: import org.apache.cxf.aegis.util.jdom.StaxBuilder;
025: import org.apache.cxf.aegis.xml.MessageReader;
026: import org.apache.cxf.aegis.xml.jdom.JDOMReader;
027: import org.apache.cxf.binding.soap.Soap11;
028: import org.apache.cxf.staxutils.StaxUtils;
029: import org.apache.cxf.test.AbstractCXFTest;
030: import org.jdom.Document;
031: import org.junit.Test;
032:
033: /**
034: * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
035: * @since Nov 4, 2004
036: */
037: public class ReaderTest extends AbstractCXFTest {
038: @Test
039: public void testLiteralReader() throws Exception {
040: ElementReader lr = getStreamReader("bean11.xml");
041: testReading(lr);
042:
043: lr = getStreamReader("read1.xml");
044: testReading2(lr);
045: }
046:
047: private ElementReader getStreamReader(String resource)
048: throws FactoryConfigurationError, XMLStreamException {
049: /*
050: * XMLInputFactory factory = XMLInputFactory.newInstance();
051: * XMLStreamReader reader = factory.createXMLStreamReader(
052: * getResourceAsStream(resource));
053: */
054: XMLStreamReader reader = StaxUtils.createXMLStreamReader(
055: getResourceAsStream(resource), null);
056:
057: while (reader.getEventType() != XMLStreamReader.START_ELEMENT) {
058: reader.next();
059: }
060:
061: return new ElementReader(reader);
062: }
063:
064: @Test
065: public void testJDOMReader() throws Exception {
066: StaxBuilder builder = new StaxBuilder();
067: Document doc = builder.build(getResourceAsReader("bean11.xml"));
068:
069: testReading(new JDOMReader(doc.getRootElement()));
070: }
071:
072: public void testReading(MessageReader reader) {
073: assertTrue(reader.getLocalName().equals("Envelope"));
074:
075: // make sure we can repeat this
076: assertTrue(reader.hasMoreElementReaders());
077: assertTrue(reader.hasMoreElementReaders());
078: assertTrue(reader.hasMoreElementReaders());
079:
080: MessageReader header = reader.getNextElementReader();
081: assertEquals("Header", header.getLocalName());
082: assertEquals(Soap11.getInstance().getNamespace(), header
083: .getNamespace());
084: assertFalse(header.hasMoreElementReaders());
085:
086: MessageReader body = reader.getNextElementReader();
087: assertEquals("Body", body.getLocalName());
088: assertFalse(body.hasMoreElementReaders());
089: }
090:
091: public void testReading2(MessageReader reader) throws Exception {
092: assertEquals("test", reader.getLocalName());
093: assertEquals("urn:test", reader.getNamespace());
094:
095: // make sure we can repeat this
096: assertTrue(reader.hasMoreAttributeReaders());
097: assertTrue(reader.hasMoreAttributeReaders());
098: assertTrue(reader.hasMoreAttributeReaders());
099:
100: MessageReader one = reader.getNextAttributeReader();
101: assertEquals("one", one.getValue());
102:
103: MessageReader two = reader.getNextAttributeReader();
104: assertEquals("two", two.getValue());
105:
106: assertFalse(reader.hasMoreAttributeReaders());
107:
108: assertTrue(reader.hasMoreElementReaders());
109: assertTrue(reader.hasMoreElementReaders());
110: assertTrue(reader.hasMoreElementReaders());
111:
112: MessageReader child = reader.getNextElementReader();
113: assertEquals("child", child.getLocalName());
114: assertTrue(child.hasMoreElementReaders());
115: }
116: }
|