Polishing

This commit is contained in:
Juergen Hoeller
2016-11-03 23:20:33 +01:00
parent e83c11667f
commit 7e7504f5ea
16 changed files with 98 additions and 129 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2016 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.
@@ -30,9 +30,10 @@ import static org.junit.Assert.*;
*
* @author Phillip Webb
*/
public class MockitoUtils {
public abstract class MockitoUtils {
private static final MockUtil mockUtil = new MockUtil();
private static MockUtil mockUtil = new MockUtil();
/**
* Verify the same invocations have been applied to two mocks. This is generally not
@@ -48,16 +49,18 @@ public class MockitoUtils {
verifySameInvocations(expectedInvocations, actualInvocations, argumentAdapters);
}
private static void verifySameInvocations(List<Invocation> expectedInvocations, List<Invocation> actualInvocations, InvocationArgumentsAdapter... 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);
private static void verifySameInvocation(Invocation expectedInvocation, Invocation actualInvocation,
InvocationArgumentsAdapter... argumentAdapters) {
assertThat(expectedInvocation.getMethod(), is(equalTo(actualInvocation.getMethod())));
Object[] expectedArguments = getInvocationArguments(expectedInvocation, argumentAdapters);
Object[] actualArguments = getInvocationArguments(actualInvocation, argumentAdapters);
@@ -72,16 +75,18 @@ public class MockitoUtils {
return arguments;
}
/**
* Adapter strategy that can be used to change invocation arguments.
*/
public static interface InvocationArgumentsAdapter {
public interface InvocationArgumentsAdapter {
/**
* Change the arguments if required
* Change the arguments if required.
* @param arguments the source arguments
* @return updated or original arguments (never {@code null})
*/
Object[] adaptArguments(Object[] arguments);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -18,7 +18,6 @@ package org.springframework.util.xml;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.transform.Transformer;
@@ -28,15 +27,8 @@ import javax.xml.transform.sax.SAXSource;
import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.tests.MockitoUtils;
import org.springframework.tests.MockitoUtils.InvocationArgumentsAdapter;
import org.w3c.dom.Node;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
@@ -47,6 +39,11 @@ 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;
import org.springframework.tests.MockitoUtils;
import org.springframework.tests.MockitoUtils.InvocationArgumentsAdapter;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
@@ -58,6 +55,7 @@ public abstract class AbstractStaxXMLReaderTestCase {
private ContentHandler standardContentHandler;
@Before
public void setUp() throws Exception {
inputFactory = XMLInputFactory.newInstance();
@@ -66,6 +64,7 @@ public abstract class AbstractStaxXMLReaderTestCase {
standardReader.setContentHandler(standardContentHandler);
}
@Test
public void contentHandlerNamespacesNoPrefixes() throws Exception {
standardReader.setFeature("http://xml.org/sax/features/namespaces", true);
@@ -147,12 +146,8 @@ public abstract class AbstractStaxXMLReaderTestCase {
inputFactory.setProperty("javax.xml.stream.isSupportingExternalEntities", Boolean.FALSE);
LexicalHandler actualLexicalHandler = mockLexicalHandler();
willAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return invocation.getArguments()[0] = "element";
}
}).given(actualLexicalHandler).startDTD(anyString(), anyString(), anyString());
willAnswer(invocation -> 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());
@@ -160,6 +155,7 @@ public abstract class AbstractStaxXMLReaderTestCase {
verifyIdenticalInvocations(expectedLexicalHandler, actualLexicalHandler);
}
private LexicalHandler mockLexicalHandler() throws Exception {
LexicalHandler lexicalHandler = mock(LexicalHandler.class);
willAnswer(new CopyCharsAnswer()).given(lexicalHandler).comment(any(char[].class), anyInt(), anyInt());
@@ -170,8 +166,6 @@ public abstract class AbstractStaxXMLReaderTestCase {
return getClass().getResourceAsStream("testContentHandler.xml");
}
protected abstract AbstractStaxXMLReader createStaxXmlReader(InputStream inputStream) throws XMLStreamException;
protected final ContentHandler mockContentHandler() throws Exception {
ContentHandler contentHandler = mock(ContentHandler.class);
willAnswer(new CopyCharsAnswer()).given(contentHandler).characters(any(char[].class), anyInt(), anyInt());
@@ -191,7 +185,11 @@ public abstract class AbstractStaxXMLReaderTestCase {
new SkipLocatorArgumentsAdapter(), new CharArrayToStringAdapter(), new PartialAttributesAdapter());
}
protected abstract AbstractStaxXMLReader createStaxXmlReader(InputStream inputStream) throws XMLStreamException;
private static class SkipLocatorArgumentsAdapter implements InvocationArgumentsAdapter {
@Override
public Object[] adaptArguments(Object[] arguments) {
for (int i = 0; i < arguments.length; i++) {
@@ -203,7 +201,9 @@ public abstract class AbstractStaxXMLReaderTestCase {
}
}
private static class CharArrayToStringAdapter implements InvocationArgumentsAdapter {
@Override
public Object[] adaptArguments(Object[] arguments) {
if (arguments.length == 3 && arguments[0] instanceof char[]
@@ -214,7 +214,9 @@ public abstract class AbstractStaxXMLReaderTestCase {
}
}
private static class PartialAttributesAdapter implements InvocationArgumentsAdapter {
@Override
public Object[] adaptArguments(Object[] arguments) {
for (int i = 0; i < arguments.length; i++) {
@@ -226,7 +228,9 @@ public abstract class AbstractStaxXMLReaderTestCase {
}
}
private static class CopyCharsAnswer implements Answer<Object> {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
char[] chars = (char[]) invocation.getArguments()[0];
@@ -237,19 +241,15 @@ public abstract class AbstractStaxXMLReaderTestCase {
}
}
private static class PartialAttributes {
private Attributes attributes;
private final Attributes attributes;
public PartialAttributes(Attributes attributes) {
this.attributes = attributes;
}
@Override
public int hashCode() {
return 1;
}
@Override
public boolean equals(Object obj) {
Attributes other = ((PartialAttributes) obj).attributes;
@@ -273,5 +273,11 @@ public abstract class AbstractStaxXMLReaderTestCase {
}
return true;
}
@Override
public int hashCode() {
return 1;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -18,18 +18,15 @@ package org.springframework.util.xml;
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.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
public class StaxEventXMLReaderTests extends AbstractStaxXMLReaderTestCase {
@@ -45,7 +42,7 @@ public class StaxEventXMLReaderTests extends AbstractStaxXMLReaderTestCase {
public void partial() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(CONTENT));
eventReader.nextTag(); // skip to root
eventReader.nextTag(); // skip to root
StaxEventXMLReader xmlReader = new StaxEventXMLReader(eventReader);
ContentHandler contentHandler = mock(ContentHandler.class);
xmlReader.setContentHandler(contentHandler);
@@ -55,5 +52,5 @@ public class StaxEventXMLReaderTests extends AbstractStaxXMLReaderTestCase {
verify(contentHandler).endElement("http://springframework.org/spring-ws", "child", "child");
verify(contentHandler).endDocument();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -18,21 +18,18 @@ package org.springframework.util.xml;
import java.io.InputStream;
import java.io.StringReader;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.junit.Test;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
public class StaxStreamXMLReaderTests extends AbstractStaxXMLReaderTestCase {
@@ -48,10 +45,10 @@ public class StaxStreamXMLReaderTests extends AbstractStaxXMLReaderTestCase {
public void partial() throws Exception {
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(CONTENT));
streamReader.nextTag(); // skip to root
streamReader.nextTag(); // skip to root
assertEquals("Invalid element", new QName("http://springframework.org/spring-ws", "root"),
streamReader.getName());
streamReader.nextTag(); // skip to child
streamReader.nextTag(); // skip to child
assertEquals("Invalid element", new QName("http://springframework.org/spring-ws", "child"),
streamReader.getName());
StaxStreamXMLReader xmlReader = new StaxStreamXMLReader(streamReader);