Moved Spring-WS to separate dir.

This commit is contained in:
Arjen Poutsma
2006-09-24 19:22:56 +00:00
commit 66d3aef26c
623 changed files with 44122 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.namespace;
import javax.xml.namespace.QName;
import junit.framework.TestCase;
public class QNameEditorTest extends TestCase {
private QNameEditor editor;
protected void setUp() throws Exception {
editor = new QNameEditor();
}
public void testNamespaceLocalPartPrefix() throws Exception {
QName qname = new QName("namespace", "localpart", "prefix");
doTest(qname);
}
public void testNamespaceLocalPart() throws Exception {
QName qname = new QName("namespace", "localpart");
doTest(qname);
}
public void testLocalPart() throws Exception {
QName qname = new QName("localpart");
doTest(qname);
}
private void doTest(QName qname) {
editor.setValue(qname);
String text = editor.getAsText();
assertNotNull("getAsText returns null", text);
editor.setAsText(text);
QName result = (QName) editor.getValue();
assertNotNull("getValue returns null", result);
assertEquals("Parsed QName local part is not equal to original", qname.getLocalPart(), result.getLocalPart());
assertEquals("Parsed QName prefix is not equal to original", qname.getPrefix(), result.getPrefix());
assertEquals("Parsed QName namespace is not equal to original", qname.getNamespaceURI(),
result.getNamespaceURI());
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.namespace;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import junit.framework.TestCase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.springframework.util.StringUtils;
public class QNameUtilsTest extends TestCase {
public void testValidQNames() {
assertTrue("Namespace QName not validated", QNameUtils.validateQName("{namespace}local"));
assertTrue("No Namespace QName not validated", QNameUtils.validateQName("local"));
}
public void testInvalidQNames() {
assertFalse("Null QName validated", QNameUtils.validateQName(null));
assertFalse("Empty QName validated", QNameUtils.validateQName(""));
assertFalse("Invalid QName validated", QNameUtils.validateQName("{namespace}"));
assertFalse("Invalid QName validated", QNameUtils.validateQName("{namespace"));
}
public void testGetQNameForNodeNoNamespace() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element element = document.createElement("localname");
QName qName = QNameUtils.getQNameForNode(element);
assertNotNull("getQNameForNode returns null", qName);
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
assertFalse("Qname has invalid namespace", StringUtils.hasLength(qName.getNamespaceURI()));
assertFalse("Qname has invalid prefix", StringUtils.hasLength(qName.getPrefix()));
}
public void testGetQNameForNodeNoPrefix() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element element = document.createElementNS("namespace", "localname");
QName qName = QNameUtils.getQNameForNode(element);
assertNotNull("getQNameForNode returns null", qName);
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
assertFalse("Qname has invalid prefix", StringUtils.hasLength(qName.getPrefix()));
}
public void testGetQNameForNode() throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
Element element = document.createElementNS("namespace", "prefix:localname");
QName qName = QNameUtils.getQNameForNode(element);
assertNotNull("getQNameForNode returns null", qName);
assertEquals("QName has invalid localname", "localname", qName.getLocalPart());
assertEquals("Qname has invalid namespace", "namespace", qName.getNamespaceURI());
assertEquals("Qname has invalid prefix", "prefix", qName.getPrefix());
}
public void testToQualifiedNamePrefix() throws Exception {
QName qName = new QName("namespace", "localName", "prefix");
String result = QNameUtils.toQualifiedName(qName);
assertEquals("Invalid result", "prefix:localName", result);
}
public void testToQualifiedNameNoPrefix() throws Exception {
QName qName = new QName("localName");
String result = QNameUtils.toQualifiedName(qName);
assertEquals("Invalid result", "localName", result);
}
public void testToQNamePrefix() throws Exception {
QName result = QNameUtils.toQName("namespace", "prefix:localName");
assertEquals("invalid namespace", "namespace", result.getNamespaceURI());
assertEquals("invalid prefix", "prefix", result.getPrefix());
assertEquals("invalid localname", "localName", result.getLocalPart());
}
public void testToQNameNoPrefix() throws Exception {
QName result = QNameUtils.toQName("namespace", "localName");
assertEquals("invalid namespace", "namespace", result.getNamespaceURI());
assertEquals("invalid prefix", "", result.getPrefix());
assertEquals("invalid localname", "localName", result.getLocalPart());
}
}

View File

@@ -0,0 +1,104 @@
/*
* Copyright 2005 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.namespace;
import java.util.Collections;
import java.util.Iterator;
import javax.xml.XMLConstants;
import junit.framework.TestCase;
public class SimpleNamespaceContextTest extends TestCase {
private SimpleNamespaceContext context;
protected void setUp() throws Exception {
context = new SimpleNamespaceContext();
context.bindNamespaceUri("prefix", "namespaceURI");
}
public void testGetNamespaceURI() {
assertEquals("Invalid namespaceURI for default namespace", "", context
.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
String defaultNamespaceUri = "defaultNamespace";
context.bindNamespaceUri(XMLConstants.DEFAULT_NS_PREFIX, defaultNamespaceUri);
assertEquals("Invalid namespaceURI for default namespace", defaultNamespaceUri, context
.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX));
assertEquals("Invalid namespaceURI for bound prefix", "namespaceURI", context.getNamespaceURI("prefix"));
assertEquals("Invalid namespaceURI for unbound prefix", "", context.getNamespaceURI("unbound"));
assertEquals("Invalid namespaceURI for namespace prefix", XMLConstants.XML_NS_URI, context
.getNamespaceURI(XMLConstants.XML_NS_PREFIX));
assertEquals("Invalid namespaceURI for attribute prefix", XMLConstants.XMLNS_ATTRIBUTE_NS_URI, context
.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE));
}
public void testGetPrefix() {
assertEquals("Invalid prefix for default namespace", XMLConstants.DEFAULT_NS_PREFIX, context.getPrefix(""));
assertEquals("Invalid prefix for bound namespace", "prefix", context.getPrefix("namespaceURI"));
assertNull("Invalid prefix for unbound namespace", context.getPrefix("unbound"));
assertEquals("Invalid prefix for namespace", XMLConstants.XML_NS_PREFIX, context
.getPrefix(XMLConstants.XML_NS_URI));
assertEquals("Invalid prefix for attribute namespace", XMLConstants.XMLNS_ATTRIBUTE, context
.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI));
}
public void testGetPrefixes() {
assertPrefixes("", XMLConstants.DEFAULT_NS_PREFIX);
assertPrefixes("namespaceURI", "prefix");
assertFalse("Invalid prefix for unbound namespace", context.getPrefixes("unbound").hasNext());
assertPrefixes(XMLConstants.XML_NS_URI, XMLConstants.XML_NS_PREFIX);
assertPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE);
}
public void testMultiplePrefixes() {
context.bindNamespaceUri("prefix1", "namespace");
context.bindNamespaceUri("prefix2", "namespace");
Iterator iterator = context.getPrefixes("namespace");
assertNotNull("getPrefixes returns null", iterator);
assertTrue("iterator is empty", iterator.hasNext());
String result = (String) iterator.next();
assertTrue("Invalid prefix", result.equals("prefix1") || result.equals("prefix2"));
assertTrue("iterator is empty", iterator.hasNext());
result = (String) iterator.next();
assertTrue("Invalid prefix", result.equals("prefix1") || result.equals("prefix2"));
assertFalse("iterator contains more than two values", iterator.hasNext());
}
private void assertPrefixes(String namespaceUri, String prefix) {
Iterator iterator = context.getPrefixes(namespaceUri);
assertNotNull("getPrefixes returns null", iterator);
assertTrue("iterator is empty", iterator.hasNext());
String result = (String) iterator.next();
assertEquals("Invalid prefix", prefix, result);
assertFalse("iterator contains multiple values", iterator.hasNext());
}
public void testGetBoundPrefixes() throws Exception {
Iterator iterator = context.getBoundPrefixes();
assertNotNull("getPrefixes returns null", iterator);
assertTrue("iterator is empty", iterator.hasNext());
String result = (String) iterator.next();
assertEquals("Invalid prefix", "prefix", result);
assertFalse("iterator contains multiple values", iterator.hasNext());
}
public void testSetBindings() throws Exception {
context.setBindings(Collections.singletonMap("prefix", "namespace"));
assertEquals("Invalid namespace uri", "namespace", context.getNamespaceURI("prefix"));
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.stream;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import javax.xml.stream.XMLStreamException;
import org.custommonkey.xmlunit.XMLTestCase;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
public abstract class AbstractStaxContentHandlerTestCase extends XMLTestCase {
private static final String XML_CONTENT_HANDLER =
"<?xml version='1.0' encoding='UTF-8'?><?pi content?><root xmlns='namespace'><prefix:child xmlns:prefix='namespace2' prefix:attr='value'>content</prefix:child></root>";
private XMLReader xmlReader;
protected void setUp() throws Exception {
xmlReader = XMLReaderFactory.createXMLReader();
}
public void testContentHandler() throws Exception {
StringWriter stringWriter = new StringWriter();
StaxContentHandler handler = createStaxContentHandler(stringWriter);
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(new StringReader(XML_CONTENT_HANDLER)));
assertXMLEqual("Invalid result", XML_CONTENT_HANDLER, stringWriter.toString());
}
public void testContentHandlerNamespacePrefixes() throws Exception {
StringWriter stringWriter = new StringWriter();
StaxContentHandler handler = createStaxContentHandler(stringWriter);
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(new StringReader(XML_CONTENT_HANDLER)));
assertXMLEqual("Invalid result", XML_CONTENT_HANDLER, stringWriter.toString());
}
protected abstract StaxContentHandler createStaxContentHandler(Writer writer) throws XMLStreamException;
}

View File

@@ -0,0 +1,202 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.stream;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.Arrays;
import javax.xml.stream.XMLStreamException;
import junit.framework.TestCase;
import org.easymock.AbstractMatcher;
import org.easymock.MockControl;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
public abstract class AbstractStaxXmlReaderTestCase extends TestCase {
private static final String XML_DTD_HANDLER =
"<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'><beans />";
private static final String XML_CONTENT_HANDLER =
"<?pi content?><root xmlns='namespace'><prefix:child xmlns:prefix='namespace2'>content</prefix:child></root>";
private static final String XML_CONTENT_HANDLER_ATTS = "<element xmlns='namespace' attr='value'/>";
private XMLReader reader;
protected void setUp() throws Exception {
reader = XMLReaderFactory.createXMLReader();
reader.setFeature("http://xml.org/sax/features/namespaces", true);
reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
}
public void testContentHandler() throws SAXException, IOException, XMLStreamException {
// record the callbacks by parsing the XML with a regular SAX parser
MockControl control = MockControl.createStrictControl(ContentHandler.class);
control.setDefaultMatcher(new SaxArgumentMatcher());
ContentHandler mock = (ContentHandler) control.getMock();
reader.setContentHandler(mock);
reader.parse(new InputSource(new StringReader(XML_CONTENT_HANDLER)));
control.replay();
StaxXmlReader staxXmlReader = createStaxXmlReader(new StringReader(XML_CONTENT_HANDLER));
staxXmlReader.setContentHandler(mock);
staxXmlReader.parse(new InputSource());
control.verify();
}
public void testContentHandlerAttributes() throws SAXException, IOException, XMLStreamException {
MockControl control = MockControl.createStrictControl(ContentHandler.class);
control.setDefaultMatcher(new SaxArgumentMatcher());
ContentHandler mock = (ContentHandler) control.getMock();
reader.setContentHandler(mock);
reader.parse(new InputSource(new StringReader(XML_CONTENT_HANDLER_ATTS)));
control.replay();
StaxXmlReader staxXmlReader = createStaxXmlReader(new StringReader(XML_CONTENT_HANDLER_ATTS));
staxXmlReader.setContentHandler(mock);
staxXmlReader.parse(new InputSource());
control.verify();
}
public void testDtdHandler() throws IOException, SAXException, XMLStreamException {
// record the callbacks by parsing the XML with a regular SAX parser
MockControl control = MockControl.createStrictControl(DTDHandler.class);
control.setDefaultMatcher(new SaxArgumentMatcher());
DTDHandler mock = (DTDHandler) control.getMock();
reader.setDTDHandler(mock);
reader.parse(new InputSource(new StringReader(XML_DTD_HANDLER)));
control.replay();
StaxXmlReader staxXmlReader = createStaxXmlReader(new StringReader(XML_DTD_HANDLER));
staxXmlReader.setDTDHandler(mock);
staxXmlReader.parse(new InputSource());
control.verify();
}
protected abstract StaxXmlReader createStaxXmlReader(Reader reader) throws XMLStreamException;
/**
* Easymock <code>ArgumentMatcher</code> implementation that matches SAX arguments.
*/
private static class SaxArgumentMatcher extends AbstractMatcher {
public boolean matches(Object[] expected, Object[] actual) {
if (expected == actual) {
return true;
}
if (expected == null || actual == null) {
return false;
}
if (expected.length != actual.length) {
throw new IllegalArgumentException("Expected and actual arguments must have the same size");
}
if (expected.length == 3 && (expected[0] instanceof char[]) && (expected[1] instanceof Integer) &&
(expected[2] instanceof Integer)) {
// handling of the character(char[], int, int) methods
String expectedString = new String((char[]) expected[0], ((Integer) expected[1]).intValue(),
((Integer) expected[2]).intValue());
String actualString = new String((char[]) actual[0], ((Integer) actual[1]).intValue(),
((Integer) actual[2]).intValue());
return (expectedString.equals(actualString));
}
else if (expected.length == 1 && (expected[0] instanceof Locator)) {
return true;
}
else {
return super.matches(expected, actual);
}
}
protected boolean argumentMatches(Object expected, Object actual) {
if (expected instanceof char[]) {
return Arrays.equals((char[]) expected, (char[]) actual);
}
else if (expected instanceof Attributes) {
Attributes expectedAttributes = (Attributes) expected;
Attributes actualAttributes = (Attributes) actual;
if (expectedAttributes.getLength() != actualAttributes.getLength()) {
return false;
}
for (int i = 0; i < expectedAttributes.getLength(); i++) {
if (!expectedAttributes.getURI(i).equals(actualAttributes.getURI(i)) ||
!expectedAttributes.getQName(i).equals(actualAttributes.getQName(i)) ||
!expectedAttributes.getType(i).equals(actualAttributes.getType(i)) ||
!expectedAttributes.getValue(i).equals(actualAttributes.getValue(i))) {
return false;
}
}
return true;
}
else if (expected instanceof Locator) {
Locator expectedLocator = (Locator) expected;
Locator actualLocator = (Locator) actual;
return (expectedLocator.getColumnNumber() == actualLocator.getColumnNumber() &&
expectedLocator.getLineNumber() == actualLocator.getLineNumber());
}
return super.argumentMatches(expected, actual);
}
protected String argumentToString(Object argument) {
if (argument instanceof char[]) {
char[] array = (char[]) argument;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < array.length; i++) {
buffer.append(array[i]);
}
return buffer.toString();
}
else if (argument instanceof Attributes) {
Attributes attributes = (Attributes) argument;
StringBuffer buffer = new StringBuffer("[");
for (int i = 0; i < attributes.getLength(); i++) {
buffer.append('{');
buffer.append(attributes.getURI(i));
buffer.append('}');
buffer.append(attributes.getQName(i));
buffer.append('=');
buffer.append(attributes.getValue(i));
if (i < attributes.getLength() - 1) {
buffer.append(", ");
}
}
buffer.append(']');
return buffer.toString();
}
else if (argument instanceof Locator) {
Locator locator = (Locator) argument;
StringBuffer buffer = new StringBuffer("[");
buffer.append(locator.getLineNumber());
buffer.append(',');
buffer.append(locator.getColumnNumber());
buffer.append(']');
return buffer.toString();
}
else {
return super.argumentToString(argument);
}
}
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.stream;
import java.io.Writer;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
public class StaxEventContentHandlerTest extends AbstractStaxContentHandlerTestCase {
protected StaxContentHandler createStaxContentHandler(Writer writer) throws XMLStreamException {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
return new StaxEventContentHandler(outputFactory.createXMLEventWriter(writer));
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.stream;
import java.io.Reader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
public class StaxEventXmlReaderTest extends AbstractStaxXmlReaderTestCase {
protected StaxXmlReader createStaxXmlReader(Reader reader) throws XMLStreamException {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
return new StaxEventXmlReader(inputFactory.createXMLEventReader(reader));
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.stream;
import java.io.Writer;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
public class StaxStreamContentHandlerTest extends AbstractStaxContentHandlerTestCase {
protected StaxContentHandler createStaxContentHandler(Writer writer) throws XMLStreamException {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
return new StaxStreamContentHandler(outputFactory.createXMLStreamWriter(writer));
}
}

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.stream;
import java.io.Reader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
public class StaxStreamXmlReaderTest extends AbstractStaxXmlReaderTestCase {
protected StaxXmlReader createStaxXmlReader(Reader reader) throws XMLStreamException {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
return new StaxStreamXmlReader(inputFactory.createXMLStreamReader(reader));
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.stream;
import java.io.StringReader;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import org.custommonkey.xmlunit.XMLTestCase;
import org.springframework.xml.transform.StaxSource;
import org.springframework.xml.transform.StringResult;
public class XmlEventStreamReaderTest extends XMLTestCase {
private static final String XML =
"<?pi content?><root xmlns='namespace'><prefix:child xmlns:prefix='namespace2'>content</prefix:child></root>";
private XmlEventStreamReader streamReader;
protected void setUp() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
streamReader = new XmlEventStreamReader(eventReader);
}
public void testReadAll() throws Exception {
while (streamReader.hasNext()) {
streamReader.next();
}
}
public void testReadCorrect() throws Exception {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StaxSource source = new StaxSource(streamReader);
StringResult result = new StringResult();
transformer.transform(source, result);
assertXMLEqual(XML, result.toString());
}
}

View File

@@ -0,0 +1,65 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.transform;
import java.io.StringWriter;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import org.custommonkey.xmlunit.XMLTestCase;
public class StaxResultTest extends XMLTestCase {
private static final String XML = "<root xmlns='namespace'><child/></root>";
private Transformer transformer;
private XMLOutputFactory inputFactory;
protected void setUp() throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
inputFactory = XMLOutputFactory.newInstance();
}
public void testStreamWriterSource() throws Exception {
StringWriter stringWriter = new StringWriter();
XMLStreamWriter streamWriter = inputFactory.createXMLStreamWriter(stringWriter);
Source source = new StringSource(XML);
StaxResult result = new StaxResult(streamWriter);
assertEquals("Invalid streamWriter returned", streamWriter, result.getXMLStreamWriter());
assertNull("EventWriter returned", result.getXMLEventWriter());
transformer.transform(source, result);
assertXMLEqual("Invalid result", XML, stringWriter.toString());
}
public void testEventWriterSource() throws Exception {
StringWriter stringWriter = new StringWriter();
XMLEventWriter eventWriter = inputFactory.createXMLEventWriter(stringWriter);
Source source = new StringSource(XML);
StaxResult result = new StaxResult(eventWriter);
assertEquals("Invalid eventWriter returned", eventWriter, result.getXMLEventWriter());
assertNull("StreamWriter returned", result.getXMLStreamWriter());
transformer.transform(source, result);
assertXMLEqual("Invalid result", XML, stringWriter.toString());
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.transform;
import java.io.StringReader;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import org.custommonkey.xmlunit.XMLTestCase;
public class StaxSourceTest extends XMLTestCase {
private static final String XML = "<root xmlns='namespace'><child/></root>";
private Transformer transformer;
private XMLInputFactory inputFactory;
protected void setUp() throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformer = transformerFactory.newTransformer();
inputFactory = XMLInputFactory.newInstance();
}
public void testStreamReaderSource() throws Exception {
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML));
StaxSource source = new StaxSource(streamReader);
assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader());
assertNull("EventReader returned", source.getXMLEventReader());
Result result = new StringResult();
transformer.transform(source, result);
assertXMLEqual("Invalid result", XML, result.toString());
}
public void testEventReaderSource() throws Exception {
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
StaxSource source = new StaxSource(eventReader);
assertEquals("Invalid eventReader returned", eventReader, source.getXMLEventReader());
assertNull("StreamReader returned", source.getXMLStreamReader());
Result result = new StringResult();
transformer.transform(source, result);
assertXMLEqual("Invalid result", XML, result.toString());
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.transform;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import org.custommonkey.xmlunit.XMLTestCase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
public class StringResultTest extends XMLTestCase {
public void testStringResult()
throws TransformerException, ParserConfigurationException, IOException, SAXException {
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element element = document.createElementNS("namespace", "prefix:localName");
document.appendChild(element);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringResult result = new StringResult();
transformer.transform(new DOMSource(document), result);
assertXMLEqual("Invalid result", "<prefix:localName xmlns:prefix='namespace'/>", result.toString());
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.transform;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import junit.framework.TestCase;
import org.w3c.dom.Element;
public class StringSourceTest extends TestCase {
public void testStringSource() throws TransformerException {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
String content = "<prefix:content xmlns:prefix='namespace'/>";
DOMResult result = new DOMResult();
transformer.transform(new StringSource(content), result);
Element rootElement = (Element) result.getNode().getFirstChild();
assertEquals("Invalid local name", "content", rootElement.getLocalName());
assertEquals("Invalid prefix", "prefix", rootElement.getPrefix());
assertEquals("Invalid namespace", "namespace", rootElement.getNamespaceURI());
}
}

View File

@@ -0,0 +1,105 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.validation;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamSource;
import junit.framework.TestCase;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;
public abstract class AbstractValidatorFactoryTestCase extends TestCase {
private XmlValidator validator;
private InputStream validInputStream;
private InputStream invalidInputStream;
protected void setUp() throws Exception {
Resource[] schemaResource =
new Resource[]{new ClassPathResource("schema.xsd", AbstractValidatorFactoryTestCase.class)};
validator = createValidator(schemaResource, XmlValidatorFactory.SCHEMA_W3C_XML);
validInputStream = AbstractValidatorFactoryTestCase.class.getResourceAsStream("validDocument.xml");
invalidInputStream = AbstractValidatorFactoryTestCase.class.getResourceAsStream("invalidDocument.xml");
}
protected void tearDown() throws Exception {
validInputStream.close();
invalidInputStream.close();
}
protected abstract XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws Exception;
public void testHandleValidMessageStream() throws Exception {
SAXParseException[] errors = validator.validate(new StreamSource(validInputStream));
assertNotNull("Null returned for errors", errors);
assertEquals("ValidationErrors returned", 0, errors.length);
}
public void testValidateTwice() throws Exception {
validator.validate(new StreamSource(validInputStream));
validInputStream = AbstractValidatorFactoryTestCase.class.getResourceAsStream("validDocument.xml");
validator.validate(new StreamSource(validInputStream));
}
public void testHandleInvalidMessageStream() throws Exception {
SAXParseException[] errors = validator.validate(new StreamSource(invalidInputStream));
assertNotNull("Null returned for errors", errors);
assertEquals("ValidationErrors returned", 3, errors.length);
}
public void testHandleValidMessageSax() throws Exception {
SAXParseException[] errors = validator.validate(new SAXSource(new InputSource(validInputStream)));
assertNotNull("Null returned for errors", errors);
assertEquals("ValidationErrors returned", 0, errors.length);
}
public void testHandleInvalidMessageSax() throws Exception {
SAXParseException[] errors = validator.validate(new SAXSource(new InputSource(invalidInputStream)));
assertNotNull("Null returned for errors", errors);
assertEquals("ValidationErrors returned", 3, errors.length);
}
public void testHandleValidMessageDom() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
Document document = documentBuilderFactory.newDocumentBuilder()
.parse(new InputSource(validInputStream));
SAXParseException[] errors = validator.validate(new DOMSource(document));
assertNotNull("Null returned for errors", errors);
assertEquals("ValidationErrors returned", 0, errors.length);
}
public void testHandleInvalidMessageDom() throws Exception {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
Document document = documentBuilderFactory.newDocumentBuilder()
.parse(new InputSource(invalidInputStream));
SAXParseException[] errors = validator.validate(new DOMSource(document));
assertNotNull("Null returned for errors", errors);
assertEquals("ValidationErrors returned", 3, errors.length);
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.validation;
import java.io.IOException;
import org.springframework.core.io.Resource;
public class Jaxp10ValidatorFactoryTest extends AbstractValidatorFactoryTestCase {
protected XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws IOException {
return Jaxp10ValidatorFactory.createValidator(schemaResources, schemaLanguage);
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.validation;
import java.io.IOException;
import org.springframework.core.io.Resource;
public class Jaxp13ValidatorFactoryTest extends AbstractValidatorFactoryTestCase {
protected XmlValidator createValidator(Resource[] schemaResources, String schemaLanguage) throws IOException {
return Jaxp13ValidatorFactory.createValidator(schemaResources, schemaLanguage);
}
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.validation;
import javax.xml.XMLConstants;
import javax.xml.validation.Schema;
import junit.framework.TestCase;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
public class SchemaLoaderUtilsTest extends TestCase {
public void testLoadSchema() throws Exception {
Resource resource = new ClassPathResource("schema.xsd", getClass());
Schema schema = SchemaLoaderUtils.loadSchema(resource, XMLConstants.W3C_XML_SCHEMA_NS_URI);
assertNotNull("No schema returned", schema);
assertFalse("Resource not closed", resource.isOpen());
}
public void testLoadNonExistantSchema() throws Exception {
try {
Resource nonExistant = new ClassPathResource("bla");
SchemaLoaderUtils.loadSchema(nonExistant, XMLConstants.W3C_XML_SCHEMA_NS_URI);
fail("Should have thrown an IllegalArgumentException");
}
catch (IllegalArgumentException e) {
// expected
}
}
public void testLoadNullSchema() throws Exception {
try {
SchemaLoaderUtils.loadSchema((Resource) null, XMLConstants.W3C_XML_SCHEMA_NS_URI);
fail("Should have thrown an IllegalArgumentException");
}
catch (IllegalArgumentException e) {
// expected
}
}
public void testLoadMultipleSchemas() throws Exception {
Resource envelope = new UrlResource("http://schemas.xmlsoap.org/soap/envelope/");
Resource encoding = new UrlResource("http://schemas.xmlsoap.org/soap/encoding/");
Schema schema =
SchemaLoaderUtils.loadSchema(new Resource[]{envelope, encoding}, XMLConstants.W3C_XML_SCHEMA_NS_URI);
assertNotNull("No schema returned", schema);
assertFalse("Resource not closed", envelope.isOpen());
assertFalse("Resource not closed", encoding.isOpen());
}
}

View File

@@ -0,0 +1,92 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.validation;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import junit.framework.TestCase;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class XmlValidatorFactoryTest extends TestCase {
public void testCreateValidator() throws Exception {
Resource resource = new ClassPathResource("schema.xsd", AbstractValidatorFactoryTestCase.class);
XmlValidator validator = XmlValidatorFactory.createValidator(resource, XmlValidatorFactory.SCHEMA_W3C_XML);
assertNotNull("No validator returned", validator);
}
public void testNonExistentResource() throws Exception {
Resource resource = new NonExistentResource();
try {
XmlValidatorFactory.createValidator(resource, XmlValidatorFactory.SCHEMA_W3C_XML);
fail("IllegalArgumentException expected");
}
catch (IllegalArgumentException ex) {
// expected
}
}
public void testInvalidSchemaLanguage() throws Exception {
Resource resource = new ClassPathResource("schema.xsd", AbstractValidatorFactoryTestCase.class);
try {
XmlValidatorFactory.createValidator(resource, "bla");
fail("IllegalArgumentException expected");
}
catch (IllegalArgumentException ex) {
// expected
}
}
private static class NonExistentResource implements Resource {
public Resource createRelative(String relativePath) throws IOException {
throw new IOException();
}
public boolean exists() {
return false;
}
public String getDescription() {
return null;
}
public File getFile() throws IOException {
throw new IOException();
}
public String getFilename() {
return null;
}
public URL getURL() throws IOException {
throw new IOException();
}
public boolean isOpen() {
return false;
}
public InputStream getInputStream() throws IOException {
throw new IOException();
}
}
}

View File

@@ -0,0 +1,192 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.xpath;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import junit.framework.TestCase;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
import org.springframework.util.StringUtils;
public abstract class AbstractXPathExpressionFactoryTestCase extends TestCase {
private Document noNamespacesDocument;
private Document namespacesDocument;
private Map namespaces = new HashMap();
protected void setUp() throws Exception {
namespaces.put("prefix1", "namespace1");
namespaces.put("prefix2", "namespace2");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
InputStream inputStream = getClass().getResourceAsStream("nonamespaces.xml");
try {
noNamespacesDocument = documentBuilder.parse(inputStream);
}
finally {
inputStream.close();
}
inputStream = getClass().getResourceAsStream("namespaces.xml");
try {
namespacesDocument = documentBuilder.parse(inputStream);
}
finally {
inputStream.close();
}
}
public void testEvaluateAsBooleanInvalidNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:otherchild", namespaces);
boolean result = expression.evaluateAsBoolean(namespacesDocument);
assertFalse("Invalid result [" + result + "]", result);
}
public void testEvaluateAsBooleanInvalidNoNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/root/otherchild");
boolean result = expression.evaluateAsBoolean(noNamespacesDocument);
assertFalse("Invalid result [" + result + "]", result);
}
public void testEvaluateAsBooleanNamespaces() throws IOException, SAXException {
XPathExpression expression =
createXPathExpression("/prefix1:root/prefix2:child/prefix2:boolean/text()", namespaces);
boolean result = expression.evaluateAsBoolean(namespacesDocument);
assertTrue("Invalid result", result);
}
public void testEvaluateAsBooleanNoNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/root/child/boolean/text()");
boolean result = expression.evaluateAsBoolean(noNamespacesDocument);
assertTrue("Invalid result", result);
}
public void testEvaluateAsDoubleInvalidNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:otherchild", namespaces);
double result = expression.evaluateAsNumber(noNamespacesDocument);
assertTrue("Invalid result [" + result + "]", Double.isNaN(result));
}
public void testEvaluateAsDoubleInvalidNoNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/root/otherchild");
double result = expression.evaluateAsNumber(noNamespacesDocument);
assertTrue("Invalid result [" + result + "]", Double.isNaN(result));
}
public void testEvaluateAsDoubleNamespaces() throws IOException, SAXException {
XPathExpression expression =
createXPathExpression("/prefix1:root/prefix2:child/prefix2:number/text()", namespaces);
double result = expression.evaluateAsNumber(namespacesDocument);
assertEquals("Invalid result", 42D, result, 0D);
}
public void testEvaluateAsDoubleNoNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/root/child/number/text()");
double result = expression.evaluateAsNumber(noNamespacesDocument);
assertEquals("Invalid result", 42D, result, 0D);
}
public void testEvaluateAsNodeInvalidNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:otherchild", namespaces);
Node result = expression.evaluateAsNode(namespacesDocument);
assertNull("Invalid result [" + result + "]", result);
}
public void testEvaluateAsNodeInvalidNoNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/root/otherchild");
Node result = expression.evaluateAsNode(noNamespacesDocument);
assertNull("Invalid result [" + result + "]", result);
}
public void testEvaluateAsNodeNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:child", namespaces);
Node result = expression.evaluateAsNode(namespacesDocument);
assertNotNull("Invalid result", result);
assertEquals("Invalid localname", "child", result.getLocalName());
}
public void testEvaluateAsNodeNoNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/root/child");
Node result = expression.evaluateAsNode(noNamespacesDocument);
assertNotNull("Invalid result", result);
assertEquals("Invalid localname", "child", result.getLocalName());
}
public void testEvaluateAsNodesNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:child/*", namespaces);
Node[] results = expression.evaluateAsNodes(namespacesDocument);
assertNotNull("Invalid result", results);
assertEquals("Invalid amount of results", 3, results.length);
}
public void testEvaluateAsNodesNoNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/root/child/*");
Node[] results = expression.evaluateAsNodes(noNamespacesDocument);
assertNotNull("Invalid result", results);
assertEquals("Invalid amount of results", 3, results.length);
}
public void testEvaluateAsStringInvalidNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/prefix1:root/prefix2:otherchild", namespaces);
String result = expression.evaluateAsString(namespacesDocument);
assertFalse("Invalid result [" + result + "]", StringUtils.hasText(result));
}
public void testEvaluateAsStringInvalidNoNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/root/otherchild");
String result = expression.evaluateAsString(noNamespacesDocument);
assertFalse("Invalid result [" + result + "]", StringUtils.hasText(result));
}
public void testEvaluateAsStringNamespaces() throws IOException, SAXException {
XPathExpression expression =
createXPathExpression("/prefix1:root/prefix2:child/prefix2:text/text()", namespaces);
String result = expression.evaluateAsString(namespacesDocument);
assertEquals("Invalid result", "text", result);
}
public void testEvaluateAsStringNoNamespaces() throws IOException, SAXException {
XPathExpression expression = createXPathExpression("/root/child/text/text()");
String result = expression.evaluateAsString(noNamespacesDocument);
assertEquals("Invalid result", "text", result);
}
public void testInvalidExpression() {
try {
createXPathExpression("\\");
fail("No XPathParseException thrown");
}
catch (XPathParseException ex) {
// Expected behaviour
}
}
protected abstract XPathExpression createXPathExpression(String expression);
protected abstract XPathExpression createXPathExpression(String expression, Map namespaces);
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.xpath;
import java.io.IOException;
import java.util.Map;
import org.xml.sax.SAXException;
public class JaxenXPathExpressionFactoryTest extends AbstractXPathExpressionFactoryTestCase {
protected XPathExpression createXPathExpression(String expression) {
return JaxenXPathExpressionFactory.createXPathExpression(expression);
}
protected XPathExpression createXPathExpression(String expression, Map namespaces) {
return JaxenXPathExpressionFactory.createXPathExpression(expression, namespaces);
}
public void testEvaluateAsDoubleNoNamespaces() throws IOException, SAXException {
// Currently not working on Jaxen 1.1 beta 8, hence the override here
}
public void testEvaluateAsDoubleNamespaces() throws IOException, SAXException {
// Currently not working on Jaxen 1.1 beta 8, hence the override here
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.xpath;
import java.util.Map;
public class Jaxp13XPathExpressionFactoryTest extends AbstractXPathExpressionFactoryTestCase {
protected XPathExpression createXPathExpression(String expression) {
return Jaxp13XPathExpressionFactory.createXPathExpression(expression);
}
protected XPathExpression createXPathExpression(String expression, Map namespaces) {
return Jaxp13XPathExpressionFactory.createXPathExpression(expression, namespaces);
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.xpath;
import junit.framework.TestCase;
public class XPathExpressionFactoryTest extends TestCase {
public void testCreateXPathExpression() throws Exception {
XPathExpression expression = XPathExpressionFactory.createXPathExpression("/root");
assertNotNull("No expression returned", expression);
}
public void testCreateEmptyXPathExpression() throws Exception {
try {
XPathExpressionFactory.createXPathExpression("");
fail("Should have thrown an Exception");
}
catch (IllegalArgumentException ex) {
// expected
}
}
}

View File

@@ -0,0 +1,33 @@
/*
* Copyright 2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.xml.xpath;
import java.util.Map;
/**
* @author Arjen Poutsma
*/
public class XalanXPathExpressionFactoryTest extends AbstractXPathExpressionFactoryTestCase {
protected XPathExpression createXPathExpression(String expression) {
return XalanXPathExpressionFactory.createXPathExpression(expression);
}
protected XPathExpression createXPathExpression(String expression, Map prefixes) {
return XalanXPathExpressionFactory.createXPathExpression(expression, prefixes);
}
}