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.geronimo.test;
019:
020: import java.io.StringReader;
021: import java.io.StringWriter;
022:
023: import javax.xml.stream.XMLEventWriter;
024: import javax.xml.stream.XMLInputFactory;
025: import javax.xml.stream.XMLOutputFactory;
026: import javax.xml.stream.XMLEventFactory;
027: import javax.xml.stream.XMLStreamReader;
028: import javax.xml.stream.XMLStreamWriter;
029: import javax.xml.stream.XMLStreamConstants;
030:
031: /**
032: * Performs basic tests using the Stax API.
033: * Some tests might be too delicate if tested with other parsers.
034: */
035: public class StaxTest {
036:
037: private static String XML = "<foo attr1=\"value1\"><bar>myData</bar></foo>";
038:
039: public static void testParse(XMLInputFactory inFactory)
040: throws Exception {
041: StringReader reader = new StringReader(XML);
042:
043: XMLStreamReader stream = inFactory
044: .createXMLStreamReader(reader);
045: int depth = 0;
046: while (stream.hasNext()) {
047: int event = stream.next();
048: System.out.println(event);
049: switch (event) {
050: case XMLStreamConstants.START_ELEMENT:
051: if (depth == 0) {
052: if (!stream.getLocalName().equals("foo")) {
053: throw new Exception("unexpected element: "
054: + stream.getLocalName());
055: }
056: if (1 != stream.getAttributeCount()) {
057: throw new Exception(
058: "unexpected number of attributes: "
059: + stream.getAttributeCount());
060: }
061: if (!("value1".equals(stream.getAttributeValue(0)))) {
062: throw new Exception(
063: "unexpected attribute value: "
064: + stream.getAttributeValue(0));
065: }
066: } else if (depth == 1) {
067: if (!stream.getLocalName().equals("bar")) {
068: throw new Exception("unexpected element: "
069: + stream.getLocalName());
070: }
071: if (0 != stream.getAttributeCount()) {
072: throw new Exception(
073: "unexpected number of attributes: "
074: + stream.getAttributeCount());
075: }
076: } else {
077: throw new Exception("unexpected element: "
078: + stream.getLocalName());
079: }
080: depth++;
081: break;
082: case XMLStreamConstants.END_ELEMENT:
083: if (depth == 1) {
084: if (!stream.getLocalName().equals("foo")) {
085: throw new Exception("unexpected element: "
086: + stream.getLocalName());
087: }
088: } else if (depth == 2) {
089: if (!stream.getLocalName().equals("bar")) {
090: throw new Exception("unexpected element: "
091: + stream.getLocalName());
092: }
093: } else {
094: throw new Exception("unexpected element: "
095: + stream.getLocalName());
096: }
097: depth--;
098: break;
099: case XMLStreamConstants.CHARACTERS:
100: if (!(depth == 2 && "myData".equals(stream.getText()))) {
101: throw new Exception("unexpected character data: "
102: + stream.getText());
103: }
104: break;
105: }
106: }
107: }
108:
109: public static void testStreamGenerate(XMLOutputFactory outFactory)
110: throws Exception {
111:
112: StringWriter strWriter = new StringWriter();
113:
114: XMLStreamWriter writer = outFactory
115: .createXMLStreamWriter(strWriter);
116: writer.writeStartElement("foo");
117: writer.writeAttribute("attr1", "value1");
118: writer.writeStartElement("bar");
119: writer.writeCharacters("myData");
120: writer.writeEndElement();
121: writer.writeEndElement();
122:
123: writer.flush();
124: writer.close();
125:
126: String actual = strWriter.toString();
127:
128: System.out.println(actual);
129:
130: if (!XML.equals(actual)) {
131: throw new Exception("Expected: " + XML + " Actual: "
132: + actual);
133: }
134: }
135:
136: public static void testEventGenerate(XMLOutputFactory outFactory,
137: XMLEventFactory eventFactory) throws Exception {
138:
139: StringWriter strWriter = new StringWriter();
140:
141: XMLEventWriter writer = outFactory
142: .createXMLEventWriter(strWriter);
143:
144: writer.add(eventFactory.createStartElement("", null, "foo"));
145: writer.add(eventFactory.createAttribute("attr1", "value1"));
146: writer.add(eventFactory.createStartElement("", null, "bar"));
147: writer.add(eventFactory.createCharacters("myData"));
148: writer.add(eventFactory.createEndElement("", null, "bar"));
149: writer.add(eventFactory.createEndElement("", null, "foo"));
150:
151: writer.flush();
152: writer.close();
153:
154: String actual = strWriter.toString();
155:
156: System.out.println(actual);
157:
158: if (!XML.equals(actual)) {
159: throw new Exception("Expected: " + XML + " Actual: "
160: + actual);
161: }
162: }
163: }
|