Replace EasyMock with Mockito

Issue: SPR-10126
This commit is contained in:
Phillip Webb
2013-03-04 19:46:15 -08:00
parent a399b13354
commit 05765d7520
180 changed files with 2329 additions and 4272 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@@ -16,15 +16,6 @@
package org.springframework.oxm.castor;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.easymock.EasyMock.aryEq;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.HashMap;
@@ -39,6 +30,7 @@ import org.custommonkey.xmlunit.XMLUnit;
import org.custommonkey.xmlunit.XpathEngine;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.InOrder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.oxm.AbstractMarshallerTests;
import org.springframework.oxm.Marshaller;
@@ -47,6 +39,11 @@ import org.w3c.dom.NodeList;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.junit.Assert.*;
import static org.mockito.Matchers.*;
import static org.mockito.BDDMockito.*;
/**
* Tests the {@link CastorMarshaller} class.
*
@@ -126,26 +123,21 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
@Test
public void marshalSaxResult() throws Exception {
ContentHandler handlerMock = createMock(ContentHandler.class);
handlerMock.startDocument();
handlerMock.startPrefixMapping("tns", "http://samples.springframework.org/flight");
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("flights"), eq("tns:flights"),
isA(Attributes.class));
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("flight"), eq("tns:flight"),
isA(Attributes.class));
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("number"), eq("tns:number"),
isA(Attributes.class));
handlerMock.characters(aryEq(new char[]{'4', '2'}), eq(0), eq(2));
handlerMock.endElement("http://samples.springframework.org/flight", "number", "tns:number");
handlerMock.endElement("http://samples.springframework.org/flight", "flight", "tns:flight");
handlerMock.endElement("http://samples.springframework.org/flight", "flights", "tns:flights");
handlerMock.endPrefixMapping("tns");
handlerMock.endDocument();
replay(handlerMock);
SAXResult result = new SAXResult(handlerMock);
ContentHandler contentHandler = mock(ContentHandler.class);
SAXResult result = new SAXResult(contentHandler);
marshaller.marshal(flights, result);
verify(handlerMock);
InOrder ordered = inOrder(contentHandler);
ordered.verify(contentHandler).startDocument();
ordered.verify(contentHandler).startPrefixMapping("tns", "http://samples.springframework.org/flight");
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flights"), eq("tns:flights"), isA(Attributes.class));
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flight"), eq("tns:flight"), isA(Attributes.class));
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("number"), eq("tns:number"), isA(Attributes.class));
ordered.verify(contentHandler).characters(eq(new char[]{'4', '2'}), eq(0), eq(2));
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "number", "tns:number");
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flight", "tns:flight");
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flights", "tns:flights");
ordered.verify(contentHandler).endPrefixMapping("tns");
ordered.verify(contentHandler).endDocument();
}
@Test
@@ -300,7 +292,6 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
Document doc = XMLUnit.buildControlDocument(xmlDoc);
NodeList node = engine.getMatchingNodes(xpath, doc);
assertEquals(msg, expected, node.item(0).getNodeValue());
}

View File

@@ -16,19 +16,6 @@
package org.springframework.oxm.jaxb;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.ByteArrayOutputStream;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
@@ -47,6 +34,7 @@ import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import org.junit.Test;
import org.mockito.InOrder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.oxm.AbstractMarshallerTests;
@@ -63,6 +51,10 @@ import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb.test";
@@ -90,27 +82,22 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
@Test
public void marshalSAXResult() throws Exception {
ContentHandler handlerMock = createStrictMock(ContentHandler.class);
handlerMock.setDocumentLocator(isA(Locator.class));
handlerMock.startDocument();
handlerMock.startPrefixMapping("", "http://samples.springframework.org/flight");
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("flights"), eq("flights"),
isA(Attributes.class));
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("flight"), eq("flight"),
isA(Attributes.class));
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("number"), eq("number"),
isA(Attributes.class));
handlerMock.characters(isA(char[].class), eq(0), eq(2));
handlerMock.endElement("http://samples.springframework.org/flight", "number", "number");
handlerMock.endElement("http://samples.springframework.org/flight", "flight", "flight");
handlerMock.endElement("http://samples.springframework.org/flight", "flights", "flights");
handlerMock.endPrefixMapping("");
handlerMock.endDocument();
replay(handlerMock);
SAXResult result = new SAXResult(handlerMock);
ContentHandler contentHandler = mock(ContentHandler.class);
SAXResult result = new SAXResult(contentHandler);
marshaller.marshal(flights, result);
verify(handlerMock);
InOrder ordered = inOrder(contentHandler);
ordered.verify(contentHandler).setDocumentLocator(isA(Locator.class));
ordered.verify(contentHandler).startDocument();
ordered.verify(contentHandler).startPrefixMapping("", "http://samples.springframework.org/flight");
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flights"), eq("flights"), isA(Attributes.class));
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("flight"), eq("flight"), isA(Attributes.class));
ordered.verify(contentHandler).startElement(eq("http://samples.springframework.org/flight"), eq("number"), eq("number"), isA(Attributes.class));
ordered.verify(contentHandler).characters(isA(char[].class), eq(0), eq(2));
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "number", "number");
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flight", "flight");
ordered.verify(contentHandler).endElement("http://samples.springframework.org/flight", "flights", "flights");
ordered.verify(contentHandler).endPrefixMapping("");
ordered.verify(contentHandler).endDocument();
}
@Test
@@ -280,25 +267,22 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
marshaller.setClassesToBeBound(BinaryObject.class);
marshaller.setMtomEnabled(true);
marshaller.afterPropertiesSet();
MimeContainer mimeContainer = createMock(MimeContainer.class);
MimeContainer mimeContainer = mock(MimeContainer.class);
Resource logo = new ClassPathResource("spring-ws.png", getClass());
DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));
expect(mimeContainer.convertToXopPackage()).andReturn(true);
mimeContainer.addAttachment(isA(String.class), isA(DataHandler.class));
expectLastCall().times(3);
replay(mimeContainer);
given(mimeContainer.convertToXopPackage()).willReturn(true);
byte[] bytes = FileCopyUtils.copyToByteArray(logo.getInputStream());
BinaryObject object = new BinaryObject(bytes, dataHandler);
StringWriter writer = new StringWriter();
marshaller.marshal(object, new StreamResult(writer), mimeContainer);
verify(mimeContainer);
assertTrue("No XML written", writer.toString().length() > 0);
verify(mimeContainer, times(3)).addAttachment(isA(String.class), isA(DataHandler.class));
}
@XmlRootElement
@SuppressWarnings("unused")
public static class DummyRootElement {
private DummyType t = new DummyType();
@@ -306,9 +290,11 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
}
@XmlType
@SuppressWarnings("unused")
public static class DummyType {
private String s = "Hello";
}
@SuppressWarnings("unused")

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2013 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.
@@ -17,6 +17,7 @@
package org.springframework.oxm.jaxb;
import java.io.StringReader;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.bind.JAXBElement;
@@ -25,10 +26,7 @@ import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.oxm.AbstractUnmarshallerTests;
@@ -38,6 +36,9 @@ import org.springframework.oxm.jaxb.test.Flights;
import org.springframework.oxm.mime.MimeContainer;
import org.springframework.util.xml.StaxUtils;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests {
private static final String INPUT_STRING = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
@@ -60,19 +61,15 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests {
unmarshaller.setClassesToBeBound(BinaryObject.class);
unmarshaller.setMtomEnabled(true);
unmarshaller.afterPropertiesSet();
MimeContainer mimeContainer = createMock(MimeContainer.class);
MimeContainer mimeContainer = mock(MimeContainer.class);
Resource logo = new ClassPathResource("spring-ws.png", getClass());
DataHandler dataHandler = new DataHandler(new FileDataSource(logo.getFile()));
expect(mimeContainer.isXopPackage()).andReturn(true);
expect(mimeContainer.getAttachment(
"<6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws>")).andReturn(dataHandler);
expect(mimeContainer.getAttachment(
"<99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws>")).andReturn(dataHandler);
expect(mimeContainer.getAttachment("696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png"))
.andReturn(dataHandler);
replay(mimeContainer);
given(mimeContainer.isXopPackage()).willReturn(true);
given(mimeContainer.getAttachment("<6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws>")).willReturn(dataHandler);
given(mimeContainer.getAttachment("<99bd1592-0521-41a2-9688-a8bfb40192fb@http://springframework.org/spring-ws>")).willReturn(dataHandler);
given(mimeContainer.getAttachment("696cfb9a-4d2d-402f-bb5c-59fa69e7f0b3@spring-ws.png")).willReturn(dataHandler);
String content = "<binaryObject xmlns='http://springframework.org/spring-ws'>" + "<bytes>" +
"<xop:Include href='cid:6b76528d-7a9c-4def-8e13-095ab89e9bb7@http://springframework.org/spring-ws' xmlns:xop='http://www.w3.org/2004/08/xop/include'/>" +
"</bytes>" + "<dataHandler>" +
@@ -84,7 +81,6 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests {
StringReader reader = new StringReader(content);
Object result = unmarshaller.unmarshal(new StreamSource(reader), mimeContainer);
assertTrue("Result is not a BinaryObject", result instanceof BinaryObject);
verify(mimeContainer);
BinaryObject object = (BinaryObject) result;
assertNotNull("bytes property not set", object.getBytes());
assertTrue("bytes property not set", object.getBytes().length > 0);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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.
@@ -25,6 +25,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.stream.XMLEventWriter;
@@ -36,26 +37,28 @@ import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
import com.thoughtworks.xstream.io.json.JsonWriter;
import static org.custommonkey.xmlunit.XMLAssert.*;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InOrder;
import org.springframework.util.xml.StaxUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.springframework.util.xml.StaxUtils;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver;
import com.thoughtworks.xstream.io.json.JsonWriter;
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
import static org.custommonkey.xmlunit.XMLAssert.assertXpathExists;
import static org.custommonkey.xmlunit.XMLAssert.assertXpathNotExists;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* @author Arjen Poutsma
@@ -143,19 +146,17 @@ public class XStreamMarshallerTests {
@Test
public void marshalSaxResult() throws Exception {
ContentHandler handlerMock = createStrictMock(ContentHandler.class);
handlerMock.startDocument();
handlerMock.startElement(eq(""), eq("flight"), eq("flight"), isA(Attributes.class));
handlerMock.startElement(eq(""), eq("flightNumber"), eq("flightNumber"), isA(Attributes.class));
handlerMock.characters(isA(char[].class), eq(0), eq(2));
handlerMock.endElement("", "flightNumber", "flightNumber");
handlerMock.endElement("", "flight", "flight");
handlerMock.endDocument();
replay(handlerMock);
SAXResult result = new SAXResult(handlerMock);
ContentHandler contentHandler = mock(ContentHandler.class);
SAXResult result = new SAXResult(contentHandler);
marshaller.marshal(flight, result);
verify(handlerMock);
InOrder ordered = inOrder(contentHandler);
ordered.verify(contentHandler).startDocument();
ordered.verify(contentHandler).startElement(eq(""), eq("flight"), eq("flight"), isA(Attributes.class));
ordered.verify(contentHandler).startElement(eq(""), eq("flightNumber"), eq("flightNumber"), isA(Attributes.class));
ordered.verify(contentHandler).characters(isA(char[].class), eq(0), eq(2));
ordered.verify(contentHandler).endElement("", "flightNumber", "flightNumber");
ordered.verify(contentHandler).endElement("", "flight", "flight");
ordered.verify(contentHandler).endDocument();
}
@Test