Replace EasyMock with Mockito in test sources
Issue: SPR-10126
This commit is contained in:
committed by
Chris Beams
parent
cbf6991d47
commit
d66c733ef4
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright 2002-2012 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.build.test.mockito;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.internal.util.MockUtil;
|
||||
import org.mockito.invocation.Invocation;
|
||||
|
||||
/**
|
||||
* General test utilities for use with {@link Mockito}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class MockitoUtils {
|
||||
|
||||
private static MockUtil mockUtil = new MockUtil();
|
||||
|
||||
/**
|
||||
* Verify the same invocations have been applied to two mocks. This is generally not
|
||||
* the preferred way test with mockito and should be avoided if possible.
|
||||
* @param expected the mock containing expected invocations
|
||||
* @param actual the mock containing actual invocations
|
||||
* @param argumentAdapters adapters that can be used to change argument values before
|
||||
* they are compared
|
||||
*/
|
||||
public static <T> void verifySameInvocations(T expected, T actual, InvocationArgumentsAdapter... argumentAdapters) {
|
||||
List<Invocation> expectedInvocations = mockUtil.getMockHandler(expected).getInvocationContainer().getInvocations();
|
||||
List<Invocation> actualInvocations = mockUtil.getMockHandler(actual).getInvocationContainer().getInvocations();
|
||||
verifySameInvocations(expectedInvocations, actualInvocations, argumentAdapters);
|
||||
}
|
||||
|
||||
private static void verifySameInvocations(List<Invocation> expectedInvocations, List<Invocation> actualInvocations, InvocationArgumentsAdapter... argumentAdapters) {
|
||||
assertThat(expectedInvocations.size(), is(equalTo(actualInvocations.size())));
|
||||
for (int i = 0; i < expectedInvocations.size(); i++) {
|
||||
verifySameInvocation(expectedInvocations.get(i), actualInvocations.get(i), argumentAdapters);
|
||||
}
|
||||
}
|
||||
|
||||
private static void verifySameInvocation(Invocation expectedInvocation, Invocation actualInvocation, InvocationArgumentsAdapter... argumentAdapters) {
|
||||
System.out.println(expectedInvocation);
|
||||
System.out.println(actualInvocation);
|
||||
assertThat(expectedInvocation.getMethod(), is(equalTo(actualInvocation.getMethod())));
|
||||
Object[] expectedArguments = getInvocationArguments(expectedInvocation, argumentAdapters);
|
||||
Object[] actualArguments = getInvocationArguments(actualInvocation, argumentAdapters);
|
||||
assertThat(expectedArguments, is(equalTo(actualArguments)));
|
||||
}
|
||||
|
||||
private static Object[] getInvocationArguments(Invocation invocation, InvocationArgumentsAdapter... argumentAdapters) {
|
||||
Object[] arguments = invocation.getArguments();
|
||||
for (InvocationArgumentsAdapter adapter : argumentAdapters) {
|
||||
arguments = adapter.adaptArguments(arguments);
|
||||
}
|
||||
return arguments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adapter strategy that can be used to change invocation arguments.
|
||||
*/
|
||||
public static interface InvocationArgumentsAdapter {
|
||||
|
||||
/**
|
||||
* Change the arguments if required
|
||||
* @param arguments the source arguments
|
||||
* @return updated or original arguments (never {@code null})
|
||||
*/
|
||||
Object[] adaptArguments(Object[] arguments);
|
||||
}
|
||||
}
|
||||
@@ -16,347 +16,228 @@
|
||||
|
||||
package org.springframework.util.xml;
|
||||
|
||||
import java.io.IOException;
|
||||
import static org.mockito.BDDMockito.willAnswer;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
|
||||
import org.easymock.AbstractMatcher;
|
||||
import org.easymock.MockControl;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.springframework.build.test.mockito.MockitoUtils;
|
||||
import org.springframework.build.test.mockito.MockitoUtils.InvocationArgumentsAdapter;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.Locator;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.ext.LexicalHandler;
|
||||
import org.xml.sax.helpers.AttributesImpl;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
public abstract class AbstractStaxXMLReaderTestCase {
|
||||
|
||||
protected static XMLInputFactory inputFactory;
|
||||
|
||||
private XMLReader standardReader;
|
||||
|
||||
private MockControl contentHandlerControl;
|
||||
|
||||
private ContentHandler contentHandler;
|
||||
private ContentHandler standardContentHandler;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
inputFactory = XMLInputFactory.newInstance();
|
||||
standardReader = XMLReaderFactory.createXMLReader();
|
||||
contentHandlerControl = MockControl.createStrictControl(ContentHandler.class);
|
||||
contentHandlerControl.setDefaultMatcher(new SaxArgumentMatcher());
|
||||
ContentHandler contentHandlerMock = (ContentHandler) contentHandlerControl.getMock();
|
||||
contentHandler = new CopyingContentHandler(contentHandlerMock);
|
||||
standardReader.setContentHandler(contentHandler);
|
||||
standardContentHandler = mockContentHandler();
|
||||
standardReader.setContentHandler(standardContentHandler);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentHandlerNamespacesNoPrefixes() throws Exception {
|
||||
standardReader.setFeature("http://xml.org/sax/features/namespaces", true);
|
||||
standardReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
|
||||
standardReader.parse(new InputSource(createTestInputStream()));
|
||||
|
||||
AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(createTestInputStream());
|
||||
ContentHandler contentHandler = mockContentHandler();
|
||||
staxXmlReader.setFeature("http://xml.org/sax/features/namespaces", true);
|
||||
staxXmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
|
||||
staxXmlReader.setContentHandler(contentHandler);
|
||||
staxXmlReader.parse(new InputSource());
|
||||
|
||||
verifyIdenticalInvocations(standardContentHandler, contentHandler);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentHandlerNamespacesPrefixes() throws Exception {
|
||||
standardReader.setFeature("http://xml.org/sax/features/namespaces", true);
|
||||
standardReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
||||
standardReader.parse(new InputSource(createTestInputStream()));
|
||||
|
||||
AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(createTestInputStream());
|
||||
ContentHandler contentHandler = mockContentHandler();
|
||||
staxXmlReader.setFeature("http://xml.org/sax/features/namespaces", true);
|
||||
staxXmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
||||
staxXmlReader.setContentHandler(contentHandler);
|
||||
staxXmlReader.parse(new InputSource());
|
||||
|
||||
verifyIdenticalInvocations(standardContentHandler, contentHandler);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentHandlerNoNamespacesPrefixes() throws Exception {
|
||||
standardReader.setFeature("http://xml.org/sax/features/namespaces", false);
|
||||
standardReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
||||
standardReader.parse(new InputSource(createTestInputStream()));
|
||||
|
||||
AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(createTestInputStream());
|
||||
ContentHandler contentHandler = mockContentHandler();
|
||||
staxXmlReader.setFeature("http://xml.org/sax/features/namespaces", false);
|
||||
staxXmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
||||
staxXmlReader.setContentHandler(contentHandler);
|
||||
staxXmlReader.parse(new InputSource());
|
||||
|
||||
verifyIdenticalInvocations(standardContentHandler, contentHandler);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lexicalHandler() throws Exception {
|
||||
Resource testLexicalHandlerXml = new ClassPathResource("testLexicalHandler.xml", getClass());
|
||||
|
||||
LexicalHandler expectedLexicalHandler = mockLexicalHandler();
|
||||
standardReader.setContentHandler(null);
|
||||
standardReader.setProperty("http://xml.org/sax/properties/lexical-handler", expectedLexicalHandler);
|
||||
standardReader.parse(new InputSource(testLexicalHandlerXml.getInputStream()));
|
||||
inputFactory.setProperty("javax.xml.stream.isCoalescing", Boolean.FALSE);
|
||||
inputFactory.setProperty("http://java.sun.com/xml/stream/properties/report-cdata-event", Boolean.TRUE);
|
||||
inputFactory.setProperty("javax.xml.stream.isReplacingEntityReferences", Boolean.FALSE);
|
||||
inputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", Boolean.FALSE);
|
||||
|
||||
LexicalHandler actualLexicalHandler = mockLexicalHandler();
|
||||
willAnswer(new Answer<Object>() {
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
return invocation.getArguments()[0] = "element";
|
||||
}
|
||||
}).given(actualLexicalHandler).startDTD(anyString(), anyString(), anyString());
|
||||
AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(testLexicalHandlerXml.getInputStream());
|
||||
staxXmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", actualLexicalHandler);
|
||||
staxXmlReader.parse(new InputSource());
|
||||
|
||||
verifyIdenticalInvocations(expectedLexicalHandler, actualLexicalHandler);
|
||||
}
|
||||
|
||||
private final LexicalHandler mockLexicalHandler() throws Exception {
|
||||
LexicalHandler lexicalHandler = mock(LexicalHandler.class);
|
||||
willAnswer(new CopyCharsAnswer()).given(lexicalHandler).comment(any(char[].class), anyInt(), anyInt());
|
||||
return lexicalHandler;
|
||||
}
|
||||
|
||||
private InputStream createTestInputStream() {
|
||||
return getClass().getResourceAsStream("testContentHandler.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentHandlerNamespacesNoPrefixes() throws SAXException, IOException, XMLStreamException {
|
||||
standardReader.setFeature("http://xml.org/sax/features/namespaces", true);
|
||||
standardReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
|
||||
|
||||
standardReader.parse(new InputSource(createTestInputStream()));
|
||||
contentHandlerControl.replay();
|
||||
|
||||
AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(createTestInputStream());
|
||||
staxXmlReader.setFeature("http://xml.org/sax/features/namespaces", true);
|
||||
staxXmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);
|
||||
|
||||
staxXmlReader.setContentHandler(contentHandler);
|
||||
staxXmlReader.parse(new InputSource());
|
||||
contentHandlerControl.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentHandlerNamespacesPrefixes() throws SAXException, IOException, XMLStreamException {
|
||||
standardReader.setFeature("http://xml.org/sax/features/namespaces", true);
|
||||
standardReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
||||
|
||||
standardReader.parse(new InputSource(createTestInputStream()));
|
||||
contentHandlerControl.replay();
|
||||
|
||||
AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(createTestInputStream());
|
||||
staxXmlReader.setFeature("http://xml.org/sax/features/namespaces", true);
|
||||
staxXmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
||||
|
||||
staxXmlReader.setContentHandler(contentHandler);
|
||||
staxXmlReader.parse(new InputSource());
|
||||
contentHandlerControl.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentHandlerNoNamespacesPrefixes() throws SAXException, IOException, XMLStreamException {
|
||||
standardReader.setFeature("http://xml.org/sax/features/namespaces", false);
|
||||
standardReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
||||
|
||||
standardReader.parse(new InputSource(createTestInputStream()));
|
||||
contentHandlerControl.replay();
|
||||
|
||||
AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(createTestInputStream());
|
||||
staxXmlReader.setFeature("http://xml.org/sax/features/namespaces", false);
|
||||
staxXmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
|
||||
|
||||
staxXmlReader.setContentHandler(contentHandler);
|
||||
staxXmlReader.parse(new InputSource());
|
||||
contentHandlerControl.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void lexicalHandler() throws SAXException, IOException, XMLStreamException {
|
||||
MockControl lexicalHandlerControl = MockControl.createStrictControl(LexicalHandler.class);
|
||||
lexicalHandlerControl.setDefaultMatcher(new SaxArgumentMatcher());
|
||||
LexicalHandler lexicalHandlerMock = (LexicalHandler) lexicalHandlerControl.getMock();
|
||||
LexicalHandler lexicalHandler = new CopyingLexicalHandler(lexicalHandlerMock);
|
||||
|
||||
Resource testLexicalHandlerXml = new ClassPathResource("testLexicalHandler.xml", getClass());
|
||||
|
||||
standardReader.setContentHandler(null);
|
||||
standardReader.setProperty("http://xml.org/sax/properties/lexical-handler", lexicalHandler);
|
||||
standardReader.parse(new InputSource(testLexicalHandlerXml.getInputStream()));
|
||||
lexicalHandlerControl.replay();
|
||||
|
||||
inputFactory.setProperty("javax.xml.stream.isCoalescing", Boolean.FALSE);
|
||||
inputFactory.setProperty("http://java.sun.com/xml/stream/properties/report-cdata-event", Boolean.TRUE);
|
||||
inputFactory.setProperty("javax.xml.stream.isReplacingEntityReferences", Boolean.FALSE);
|
||||
inputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", Boolean.FALSE);
|
||||
|
||||
AbstractStaxXMLReader staxXmlReader = createStaxXmlReader(testLexicalHandlerXml.getInputStream());
|
||||
|
||||
staxXmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", lexicalHandler);
|
||||
staxXmlReader.parse(new InputSource());
|
||||
lexicalHandlerControl.verify();
|
||||
}
|
||||
|
||||
protected abstract AbstractStaxXMLReader createStaxXmlReader(InputStream inputStream) throws XMLStreamException;
|
||||
|
||||
/** Easymock {@code AbstractMatcher} implementation that matches SAX arguments. */
|
||||
@SuppressWarnings("serial")
|
||||
protected static class SaxArgumentMatcher extends AbstractMatcher {
|
||||
protected final ContentHandler mockContentHandler() throws Exception {
|
||||
ContentHandler contentHandler = mock(ContentHandler.class);
|
||||
willAnswer(new CopyCharsAnswer()).given(contentHandler).characters(any(char[].class), anyInt(), anyInt());
|
||||
willAnswer(new CopyCharsAnswer()).given(contentHandler).ignorableWhitespace(any(char[].class), anyInt(), anyInt());
|
||||
willAnswer(new Answer<Object>() {
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
invocation.getArguments()[3] = new AttributesImpl((Attributes) invocation.getArguments()[3]);
|
||||
return null;
|
||||
}
|
||||
}).given(contentHandler).startElement(anyString(), anyString(), anyString(), any(Attributes.class));
|
||||
return contentHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matches(Object[] expected, Object[] actual) {
|
||||
if (expected == actual) {
|
||||
return true;
|
||||
protected <T> void verifyIdenticalInvocations(T expected, T actual) {
|
||||
MockitoUtils.verifySameInvocations(expected, actual,
|
||||
new SkipLocatorArgumentsAdapter(), new CharArrayToStringAdapter(), new PartialAttributesAdapter());
|
||||
}
|
||||
|
||||
private static class SkipLocatorArgumentsAdapter implements InvocationArgumentsAdapter {
|
||||
public Object[] adaptArguments(Object[] arguments) {
|
||||
for(int i=0; i<arguments.length; i++) {
|
||||
if(arguments[i] instanceof Locator) {
|
||||
arguments[i] = null;
|
||||
}
|
||||
}
|
||||
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], (Integer) expected[2]);
|
||||
String actualString = new String((char[]) actual[0], (Integer) actual[1], (Integer) actual[2]);
|
||||
return expectedString.equals(actualString);
|
||||
}
|
||||
else if (expected.length == 1 && (expected[0] instanceof Locator)) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return super.matches(expected, actual);
|
||||
return arguments;
|
||||
}
|
||||
}
|
||||
|
||||
private static class CharArrayToStringAdapter implements InvocationArgumentsAdapter {
|
||||
public Object[] adaptArguments(Object[] arguments) {
|
||||
if(arguments.length == 3 && arguments[0] instanceof char[]
|
||||
&& arguments[1] instanceof Integer && arguments[2] instanceof Integer) {
|
||||
return new Object[] {new String((char[]) arguments[0], (Integer) arguments[1], (Integer) arguments[2])};
|
||||
}
|
||||
return arguments;
|
||||
}
|
||||
}
|
||||
|
||||
private static class PartialAttributesAdapter implements InvocationArgumentsAdapter {
|
||||
public Object[] adaptArguments(Object[] arguments) {
|
||||
for (int i = 0; i < arguments.length; i++) {
|
||||
if(arguments[i] instanceof Attributes) {
|
||||
arguments[i] = new PartialAttributes((Attributes) arguments[i]);
|
||||
}
|
||||
};
|
||||
return arguments;
|
||||
}
|
||||
}
|
||||
|
||||
private static class CopyCharsAnswer implements Answer<Object> {
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
char[] chars = (char[]) invocation.getArguments()[0];
|
||||
char[] copy = new char[chars.length];
|
||||
System.arraycopy(chars, 0, copy, 0, chars.length);
|
||||
invocation.getArguments()[0] = copy;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static class PartialAttributes {
|
||||
|
||||
private Attributes attributes;
|
||||
|
||||
public PartialAttributes(Attributes attributes) {
|
||||
this.attributes = attributes;
|
||||
}
|
||||
|
||||
@Override
|
||||
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()) {
|
||||
public int hashCode() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
Attributes other = ((PartialAttributes) obj).attributes;
|
||||
for (int i = 0; i < other.getLength(); i++) {
|
||||
boolean found = false;
|
||||
for (int j = 0; j < attributes.getLength(); j++) {
|
||||
if (other.getURI(i).equals(attributes.getURI(j))
|
||||
&& other.getQName(i).equals(attributes.getQName(j))
|
||||
&& other.getType(i).equals(attributes.getType(j))
|
||||
&& other.getValue(i).equals(attributes.getValue(j))) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < expectedAttributes.getLength(); i++) {
|
||||
boolean found = false;
|
||||
for (int j = 0; j < actualAttributes.getLength(); j++) {
|
||||
if (expectedAttributes.getURI(i).equals(actualAttributes.getURI(j)) &&
|
||||
expectedAttributes.getQName(i).equals(actualAttributes.getQName(j)) &&
|
||||
expectedAttributes.getType(i).equals(actualAttributes.getType(j)) &&
|
||||
expectedAttributes.getValue(i).equals(actualAttributes.getValue(j))) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return super.argumentMatches(expected, actual);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(Object[] arguments) {
|
||||
if (arguments != null && arguments.length == 3 && arguments[0] instanceof char[] &&
|
||||
arguments[1] instanceof Integer && arguments[2] instanceof Integer) {
|
||||
return new String((char[]) arguments[0], (Integer) arguments[1], (Integer) arguments[2]);
|
||||
}
|
||||
else {
|
||||
return super.toString(arguments);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String argumentToString(Object argument) {
|
||||
if (argument instanceof char[]) {
|
||||
char[] array = (char[]) argument;
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
for (char anArray : array) {
|
||||
buffer.append(anArray);
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
else if (argument instanceof Attributes) {
|
||||
Attributes attributes = (Attributes) argument;
|
||||
StringBuilder buffer = new StringBuilder("[");
|
||||
for (int i = 0; i < attributes.getLength(); i++) {
|
||||
if (attributes.getURI(i).length() != 0) {
|
||||
buffer.append('{');
|
||||
buffer.append(attributes.getURI(i));
|
||||
buffer.append('}');
|
||||
}
|
||||
if (attributes.getQName(i).length() != 0) {
|
||||
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;
|
||||
StringBuilder buffer = new StringBuilder("[");
|
||||
buffer.append(locator.getLineNumber());
|
||||
buffer.append(',');
|
||||
buffer.append(locator.getColumnNumber());
|
||||
buffer.append(']');
|
||||
return buffer.toString();
|
||||
}
|
||||
else {
|
||||
return super.argumentToString(argument);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
private static class CopyingContentHandler implements ContentHandler {
|
||||
|
||||
private final ContentHandler wrappee;
|
||||
|
||||
private CopyingContentHandler(ContentHandler wrappee) {
|
||||
this.wrappee = wrappee;
|
||||
}
|
||||
|
||||
public void setDocumentLocator(Locator locator) {
|
||||
wrappee.setDocumentLocator(locator);
|
||||
}
|
||||
|
||||
public void startDocument() throws SAXException {
|
||||
wrappee.startDocument();
|
||||
}
|
||||
|
||||
public void endDocument() throws SAXException {
|
||||
wrappee.endDocument();
|
||||
}
|
||||
|
||||
public void startPrefixMapping(String prefix, String uri) throws SAXException {
|
||||
wrappee.startPrefixMapping(prefix, uri);
|
||||
}
|
||||
|
||||
public void endPrefixMapping(String prefix) throws SAXException {
|
||||
wrappee.endPrefixMapping(prefix);
|
||||
}
|
||||
|
||||
public void startElement(String uri, String localName, String qName, Attributes attributes)
|
||||
throws SAXException {
|
||||
wrappee.startElement(uri, localName, qName, new AttributesImpl(attributes));
|
||||
}
|
||||
|
||||
public void endElement(String uri, String localName, String qName) throws SAXException {
|
||||
wrappee.endElement(uri, localName, qName);
|
||||
}
|
||||
|
||||
public void characters(char ch[], int start, int length) throws SAXException {
|
||||
wrappee.characters(copy(ch), start, length);
|
||||
}
|
||||
|
||||
public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
|
||||
}
|
||||
|
||||
public void processingInstruction(String target, String data) throws SAXException {
|
||||
wrappee.processingInstruction(target, data);
|
||||
}
|
||||
|
||||
public void skippedEntity(String name) throws SAXException {
|
||||
wrappee.skippedEntity(name);
|
||||
}
|
||||
}
|
||||
|
||||
private static class CopyingLexicalHandler implements LexicalHandler {
|
||||
|
||||
private final LexicalHandler wrappee;
|
||||
|
||||
private CopyingLexicalHandler(LexicalHandler wrappee) {
|
||||
this.wrappee = wrappee;
|
||||
}
|
||||
|
||||
public void startDTD(String name, String publicId, String systemId) throws SAXException {
|
||||
wrappee.startDTD("element", publicId, systemId);
|
||||
}
|
||||
|
||||
public void endDTD() throws SAXException {
|
||||
wrappee.endDTD();
|
||||
}
|
||||
|
||||
public void startEntity(String name) throws SAXException {
|
||||
wrappee.startEntity(name);
|
||||
}
|
||||
|
||||
public void endEntity(String name) throws SAXException {
|
||||
wrappee.endEntity(name);
|
||||
}
|
||||
|
||||
public void startCDATA() throws SAXException {
|
||||
wrappee.startCDATA();
|
||||
}
|
||||
|
||||
public void endCDATA() throws SAXException {
|
||||
wrappee.endCDATA();
|
||||
}
|
||||
|
||||
public void comment(char ch[], int start, int length) throws SAXException {
|
||||
wrappee.comment(copy(ch), start, length);
|
||||
}
|
||||
}
|
||||
|
||||
private static char[] copy(char[] ch) {
|
||||
char[] copy = new char[ch.length];
|
||||
System.arraycopy(ch, 0, copy, 0, ch.length);
|
||||
return copy;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,13 +16,15 @@
|
||||
|
||||
package org.springframework.util.xml;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.StringReader;
|
||||
import javax.xml.stream.XMLEventReader;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.helpers.AttributesImpl;
|
||||
@@ -41,21 +43,13 @@ public class StaxEventXMLReaderTests extends AbstractStaxXMLReaderTestCase {
|
||||
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(CONTENT));
|
||||
eventReader.nextTag(); // skip to root
|
||||
StaxEventXMLReader xmlReader = new StaxEventXMLReader(eventReader);
|
||||
|
||||
MockControl mockControl = MockControl.createStrictControl(ContentHandler.class);
|
||||
mockControl.setDefaultMatcher(new SaxArgumentMatcher());
|
||||
ContentHandler contentHandlerMock = (ContentHandler) mockControl.getMock();
|
||||
|
||||
contentHandlerMock.startDocument();
|
||||
contentHandlerMock.startElement("http://springframework.org/spring-ws", "child", "child", new AttributesImpl());
|
||||
contentHandlerMock.endElement("http://springframework.org/spring-ws", "child", "child");
|
||||
contentHandlerMock.endDocument();
|
||||
|
||||
xmlReader.setContentHandler(contentHandlerMock);
|
||||
mockControl.replay();
|
||||
ContentHandler contentHandler = mock(ContentHandler.class);
|
||||
xmlReader.setContentHandler(contentHandler);
|
||||
xmlReader.parse(new InputSource());
|
||||
mockControl.verify();
|
||||
verify(contentHandler).startDocument();
|
||||
verify(contentHandler).startElement("http://springframework.org/spring-ws", "child", "child", new AttributesImpl());
|
||||
verify(contentHandler).endElement("http://springframework.org/spring-ws", "child", "child");
|
||||
verify(contentHandler).endDocument();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -23,12 +23,17 @@ import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.helpers.AttributesImpl;
|
||||
import org.xml.sax.Locator;
|
||||
|
||||
public class StaxStreamXMLReaderTests extends AbstractStaxXMLReaderTestCase {
|
||||
|
||||
@@ -51,22 +56,15 @@ public class StaxStreamXMLReaderTests extends AbstractStaxXMLReaderTestCase {
|
||||
streamReader.getName());
|
||||
StaxStreamXMLReader xmlReader = new StaxStreamXMLReader(streamReader);
|
||||
|
||||
MockControl mockControl = MockControl.createStrictControl(ContentHandler.class);
|
||||
mockControl.setDefaultMatcher(new SaxArgumentMatcher());
|
||||
ContentHandler contentHandlerMock = (ContentHandler) mockControl.getMock();
|
||||
|
||||
contentHandlerMock.setDocumentLocator(null);
|
||||
mockControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
||||
contentHandlerMock.startDocument();
|
||||
contentHandlerMock.startElement("http://springframework.org/spring-ws", "child", "child", new AttributesImpl());
|
||||
contentHandlerMock.endElement("http://springframework.org/spring-ws", "child", "child");
|
||||
contentHandlerMock.endDocument();
|
||||
|
||||
xmlReader.setContentHandler(contentHandlerMock);
|
||||
mockControl.replay();
|
||||
ContentHandler contentHandler = mock(ContentHandler.class);
|
||||
xmlReader.setContentHandler(contentHandler);
|
||||
xmlReader.parse(new InputSource());
|
||||
mockControl.verify();
|
||||
|
||||
verify(contentHandler).setDocumentLocator(any(Locator.class));
|
||||
verify(contentHandler).startDocument();
|
||||
verify(contentHandler).startElement(eq("http://springframework.org/spring-ws"), eq("child"), eq("child"), any(Attributes.class));
|
||||
verify(contentHandler).endElement("http://springframework.org/spring-ws", "child", "child");
|
||||
verify(contentHandler).endDocument();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user