001: /*
002: * Copyright 2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.xml.stream;
018:
019: import java.io.IOException;
020: import java.io.Reader;
021: import java.io.StringReader;
022: import java.util.Arrays;
023: import javax.xml.stream.XMLInputFactory;
024: import javax.xml.stream.XMLStreamException;
025:
026: import junit.framework.TestCase;
027: import org.easymock.AbstractMatcher;
028: import org.easymock.MockControl;
029: import org.xml.sax.Attributes;
030: import org.xml.sax.ContentHandler;
031: import org.xml.sax.DTDHandler;
032: import org.xml.sax.InputSource;
033: import org.xml.sax.Locator;
034: import org.xml.sax.SAXException;
035: import org.xml.sax.XMLReader;
036: import org.xml.sax.helpers.XMLReaderFactory;
037:
038: public abstract class AbstractStaxXmlReaderTestCase extends TestCase {
039:
040: protected static XMLInputFactory inputFactory = XMLInputFactory
041: .newInstance();
042:
043: private static final String XML_DTD_HANDLER = "<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'><beans />";
044:
045: private static final String XML_CONTENT_HANDLER = "<?pi content?><root xmlns='namespace'><prefix:child xmlns:prefix='namespace2'>content</prefix:child></root>";
046:
047: private static final String XML_CONTENT_HANDLER_ATTS = "<element xmlns='namespace' attr='value'/>";
048:
049: private XMLReader reader;
050:
051: protected void setUp() throws Exception {
052: reader = XMLReaderFactory.createXMLReader();
053: reader.setFeature("http://xml.org/sax/features/namespaces",
054: true);
055: reader
056: .setFeature(
057: "http://xml.org/sax/features/namespace-prefixes",
058: false);
059: }
060:
061: public void testContentHandler() throws SAXException, IOException,
062: XMLStreamException {
063: // record the callbacks by parsing the XML with a regular SAX parser
064: MockControl control = MockControl
065: .createStrictControl(ContentHandler.class);
066: control.setDefaultMatcher(new SaxArgumentMatcher());
067: ContentHandler mock = (ContentHandler) control.getMock();
068: reader.setContentHandler(mock);
069: reader.parse(new InputSource(new StringReader(
070: XML_CONTENT_HANDLER)));
071: control.replay();
072: AbstractStaxXmlReader staxXmlReader = createStaxXmlReader(new StringReader(
073: XML_CONTENT_HANDLER));
074: staxXmlReader.setContentHandler(mock);
075: staxXmlReader.parse(new InputSource());
076: control.verify();
077: }
078:
079: public void testContentHandlerAttributes() throws SAXException,
080: IOException, XMLStreamException {
081: MockControl control = MockControl
082: .createStrictControl(ContentHandler.class);
083: control.setDefaultMatcher(new SaxArgumentMatcher());
084: ContentHandler mock = (ContentHandler) control.getMock();
085: reader.setContentHandler(mock);
086: reader.parse(new InputSource(new StringReader(
087: XML_CONTENT_HANDLER_ATTS)));
088: control.replay();
089: AbstractStaxXmlReader staxXmlReader = createStaxXmlReader(new StringReader(
090: XML_CONTENT_HANDLER_ATTS));
091: staxXmlReader.setContentHandler(mock);
092: staxXmlReader.parse(new InputSource());
093: control.verify();
094: }
095:
096: public void testDtdHandler() throws IOException, SAXException,
097: XMLStreamException {
098: // record the callbacks by parsing the XML with a regular SAX parser
099: MockControl control = MockControl
100: .createStrictControl(DTDHandler.class);
101: control.setDefaultMatcher(new SaxArgumentMatcher());
102: DTDHandler mock = (DTDHandler) control.getMock();
103: reader.setDTDHandler(mock);
104: reader
105: .parse(new InputSource(
106: new StringReader(XML_DTD_HANDLER)));
107: control.replay();
108: AbstractStaxXmlReader staxXmlReader = createStaxXmlReader(new StringReader(
109: XML_DTD_HANDLER));
110: staxXmlReader.setDTDHandler(mock);
111: staxXmlReader.parse(new InputSource());
112: control.verify();
113: }
114:
115: protected abstract AbstractStaxXmlReader createStaxXmlReader(
116: Reader reader) throws XMLStreamException;
117:
118: /** Easymock <code>ArgumentMatcher</code> implementation that matches SAX arguments. */
119: protected static class SaxArgumentMatcher extends AbstractMatcher {
120:
121: public boolean matches(Object[] expected, Object[] actual) {
122: if (expected == actual) {
123: return true;
124: }
125: if (expected == null || actual == null) {
126: return false;
127: }
128: if (expected.length != actual.length) {
129: throw new IllegalArgumentException(
130: "Expected and actual arguments must have the same size");
131: }
132: if (expected.length == 3 && expected[0] instanceof char[]
133: && expected[1] instanceof Integer
134: && expected[2] instanceof Integer) {
135: // handling of the character(char[], int, int) methods
136: String expectedString = new String(
137: (char[]) expected[0], ((Integer) expected[1])
138: .intValue(), ((Integer) expected[2])
139: .intValue());
140: String actualString = new String((char[]) actual[0],
141: ((Integer) actual[1]).intValue(),
142: ((Integer) actual[2]).intValue());
143: return expectedString.equals(actualString);
144: } else if (expected.length == 1
145: && (expected[0] instanceof Locator)) {
146: return true;
147: } else {
148: return super .matches(expected, actual);
149: }
150: }
151:
152: protected boolean argumentMatches(Object expected, Object actual) {
153: if (expected instanceof char[]) {
154: return Arrays
155: .equals((char[]) expected, (char[]) actual);
156: } else if (expected instanceof Attributes) {
157: Attributes expectedAttributes = (Attributes) expected;
158: Attributes actualAttributes = (Attributes) actual;
159: if (expectedAttributes.getLength() != actualAttributes
160: .getLength()) {
161: return false;
162: }
163: for (int i = 0; i < expectedAttributes.getLength(); i++) {
164: if (!expectedAttributes.getURI(i).equals(
165: actualAttributes.getURI(i))
166: || !expectedAttributes.getQName(i).equals(
167: actualAttributes.getQName(i))
168: || !expectedAttributes.getType(i).equals(
169: actualAttributes.getType(i))
170: || !expectedAttributes.getValue(i).equals(
171: actualAttributes.getValue(i))) {
172: return false;
173: }
174: }
175: return true;
176: } else if (expected instanceof Locator) {
177: Locator expectedLocator = (Locator) expected;
178: Locator actualLocator = (Locator) actual;
179: return expectedLocator.getColumnNumber() == actualLocator
180: .getColumnNumber()
181: && expectedLocator.getLineNumber() == actualLocator
182: .getLineNumber();
183: }
184: return super .argumentMatches(expected, actual);
185: }
186:
187: protected String argumentToString(Object argument) {
188: if (argument instanceof char[]) {
189: char[] array = (char[]) argument;
190: StringBuffer buffer = new StringBuffer();
191: for (int i = 0; i < array.length; i++) {
192: buffer.append(array[i]);
193: }
194: return buffer.toString();
195: } else if (argument instanceof Attributes) {
196: Attributes attributes = (Attributes) argument;
197: StringBuffer buffer = new StringBuffer("[");
198: for (int i = 0; i < attributes.getLength(); i++) {
199: buffer.append('{');
200: buffer.append(attributes.getURI(i));
201: buffer.append('}');
202: buffer.append(attributes.getQName(i));
203: buffer.append('=');
204: buffer.append(attributes.getValue(i));
205: if (i < attributes.getLength() - 1) {
206: buffer.append(", ");
207: }
208: }
209: buffer.append(']');
210: return buffer.toString();
211: } else if (argument instanceof Locator) {
212: Locator locator = (Locator) argument;
213: StringBuffer buffer = new StringBuffer("[");
214: buffer.append(locator.getLineNumber());
215: buffer.append(',');
216: buffer.append(locator.getColumnNumber());
217: buffer.append(']');
218: return buffer.toString();
219: } else {
220: return super.argumentToString(argument);
221: }
222: }
223: }
224:
225: }
|