Migrate JUnit 3 tests to JUnit 4
This commit migrates all remaining tests from JUnit 3 to JUnit 4, with the exception of Spring's legacy JUnit 3.8 based testing framework that is still in use in the spring-orm module. Issue: SPR-13514
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2015 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,6 +18,7 @@ package org.springframework.oxm;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.StringWriter;
|
||||
|
||||
import javax.xml.parsers.DocumentBuilder;
|
||||
import javax.xml.parsers.DocumentBuilderFactory;
|
||||
import javax.xml.stream.XMLEventWriter;
|
||||
@@ -29,8 +30,10 @@ import javax.xml.transform.stax.StAXResult;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.w3c.dom.Attr;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
@@ -43,17 +46,18 @@ import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public abstract class AbstractMarshallerTests {
|
||||
|
||||
protected Marshaller marshaller;
|
||||
|
||||
protected Object flights;
|
||||
public abstract class AbstractMarshallerTests<M extends Marshaller> {
|
||||
|
||||
protected static final String EXPECTED_STRING =
|
||||
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
|
||||
|
||||
protected M marshaller;
|
||||
|
||||
protected Object flights;
|
||||
|
||||
@Before
|
||||
public final void setUp() throws Exception {
|
||||
marshaller = createMarshaller();
|
||||
@@ -61,7 +65,7 @@ public abstract class AbstractMarshallerTests {
|
||||
XMLUnit.setIgnoreWhitespace(true);
|
||||
}
|
||||
|
||||
protected abstract Marshaller createMarshaller() throws Exception;
|
||||
protected abstract M createMarshaller() throws Exception;
|
||||
|
||||
protected abstract Object createFlights();
|
||||
|
||||
@@ -168,4 +172,5 @@ public abstract class AbstractMarshallerTests {
|
||||
marshaller.marshal(flights, result);
|
||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -45,10 +45,11 @@ import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public abstract class AbstractUnmarshallerTests {
|
||||
public abstract class AbstractUnmarshallerTests<U extends Unmarshaller> {
|
||||
|
||||
protected Unmarshaller unmarshaller;
|
||||
protected U unmarshaller;
|
||||
|
||||
protected static final String INPUT_STRING =
|
||||
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
@@ -59,7 +60,7 @@ public abstract class AbstractUnmarshallerTests {
|
||||
unmarshaller = createUnmarshaller();
|
||||
}
|
||||
|
||||
protected abstract Unmarshaller createUnmarshaller() throws Exception;
|
||||
protected abstract U createUnmarshaller() throws Exception;
|
||||
|
||||
protected abstract void testFlights(Object o);
|
||||
|
||||
@@ -154,4 +155,5 @@ public abstract class AbstractUnmarshallerTests {
|
||||
Object flight = unmarshaller.unmarshal(source);
|
||||
testFlight(flight);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
@@ -27,16 +28,19 @@ import org.custommonkey.xmlunit.NamespaceContext;
|
||||
import org.custommonkey.xmlunit.SimpleNamespaceContext;
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
import org.custommonkey.xmlunit.XpathEngine;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.mockito.InOrder;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NodeList;
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.ContentHandler;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.oxm.AbstractMarshallerTests;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.NodeList;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.ContentHandler;
|
||||
|
||||
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -49,8 +53,9 @@ import static org.mockito.Mockito.*;
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Jakub Narloch
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class CastorMarshallerTests extends AbstractMarshallerTests {
|
||||
public class CastorMarshallerTests extends AbstractMarshallerTests<CastorMarshaller> {
|
||||
|
||||
/**
|
||||
* Represents the expected result that doesn't contain the xml declaration.
|
||||
@@ -108,7 +113,7 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
|
||||
|
||||
|
||||
@Override
|
||||
protected Marshaller createMarshaller() throws Exception {
|
||||
protected CastorMarshaller createMarshaller() throws Exception {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
ClassPathResource mappingLocation = new ClassPathResource("mapping.xml", CastorMarshaller.class);
|
||||
marshaller.setMappingLocation(mappingLocation);
|
||||
@@ -155,40 +160,40 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuppressNamespacesTrue() throws Exception {
|
||||
getCastorMarshaller().setSuppressNamespaces(true);
|
||||
public void suppressNamespacesTrue() throws Exception {
|
||||
marshaller.setSuppressNamespaces(true);
|
||||
String result = marshalFlights();
|
||||
assertXMLEqual("Marshaller wrote invalid result", SUPPRESSED_NAMESPACE_EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuppressNamespacesFalse() throws Exception {
|
||||
getCastorMarshaller().setSuppressNamespaces(false);
|
||||
public void suppressNamespacesFalse() throws Exception {
|
||||
marshaller.setSuppressNamespaces(false);
|
||||
String result = marshalFlights();
|
||||
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuppressXsiTypeTrue() throws Exception {
|
||||
public void suppressXsiTypeTrue() throws Exception {
|
||||
CastorObject castorObject = createCastorObject();
|
||||
getCastorMarshaller().setSuppressXsiType(true);
|
||||
getCastorMarshaller().setRootElement("objects");
|
||||
marshaller.setSuppressXsiType(true);
|
||||
marshaller.setRootElement("objects");
|
||||
String result = marshal(Arrays.asList(castorObject));
|
||||
assertXMLEqual("Marshaller wrote invalid result", SUPPRESSED_XSI_EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuppressXsiTypeFalse() throws Exception {
|
||||
public void suppressXsiTypeFalse() throws Exception {
|
||||
CastorObject castorObject = createCastorObject();
|
||||
getCastorMarshaller().setSuppressXsiType(false);
|
||||
getCastorMarshaller().setRootElement("objects");
|
||||
marshaller.setSuppressXsiType(false);
|
||||
marshaller.setRootElement("objects");
|
||||
String result = marshal(Arrays.asList(castorObject));
|
||||
assertXMLEqual("Marshaller wrote invalid result", XSI_EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMarshalAsDocumentTrue() throws Exception {
|
||||
getCastorMarshaller().setMarshalAsDocument(true);
|
||||
public void marshalAsDocumentTrue() throws Exception {
|
||||
marshaller.setMarshalAsDocument(true);
|
||||
String result = marshalFlights();
|
||||
assertXMLEqual("Marshaller wrote invalid result", DOCUMENT_EXPECTED_STRING, result);
|
||||
assertTrue("Result doesn't contain xml declaration.",
|
||||
@@ -196,24 +201,24 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMarshalAsDocumentFalse() throws Exception {
|
||||
getCastorMarshaller().setMarshalAsDocument(true);
|
||||
public void marshalAsDocumentFalse() throws Exception {
|
||||
marshaller.setMarshalAsDocument(true);
|
||||
String result = marshalFlights();
|
||||
assertXMLEqual("Marshaller wrote invalid result", EXPECTED_STRING, result);
|
||||
assertFalse("Result contains xml declaration.", result.matches("<\\?\\s*xml"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRootElement() throws Exception {
|
||||
getCastorMarshaller().setRootElement("canceledFlights");
|
||||
public void rootElement() throws Exception {
|
||||
marshaller.setRootElement("canceledFlights");
|
||||
String result = marshalFlights();
|
||||
assertXMLEqual("Marshaller wrote invalid result", ROOT_ELEMENT_EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoNamespaceSchemaLocation() throws Exception {
|
||||
public void noNamespaceSchemaLocation() throws Exception {
|
||||
String noNamespaceSchemaLocation = "flights.xsd";
|
||||
getCastorMarshaller().setNoNamespaceSchemaLocation(noNamespaceSchemaLocation);
|
||||
marshaller.setNoNamespaceSchemaLocation(noNamespaceSchemaLocation);
|
||||
String result = marshalFlights();
|
||||
assertXpathEvaluatesTo("The xsi:noNamespaceSchemaLocation hasn't been written or has invalid value.",
|
||||
noNamespaceSchemaLocation, "/tns:flights/@xsi:noNamespaceSchemaLocation", result);
|
||||
@@ -221,9 +226,9 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSchemaLocation() throws Exception {
|
||||
public void schemaLocation() throws Exception {
|
||||
String schemaLocation = "flights.xsd";
|
||||
getCastorMarshaller().setSchemaLocation(schemaLocation);
|
||||
marshaller.setSchemaLocation(schemaLocation);
|
||||
String result = marshalFlights();
|
||||
assertXpathEvaluatesTo("The xsi:noNamespaceSchemaLocation hasn't been written or has invalid value.",
|
||||
schemaLocation, "/tns:flights/@xsi:schemaLocation", result);
|
||||
@@ -231,34 +236,30 @@ public class CastorMarshallerTests extends AbstractMarshallerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUseXsiTypeAsRootTrue() throws Exception {
|
||||
public void useXsiTypeAsRootTrue() throws Exception {
|
||||
CastorObject castorObject = createCastorObject();
|
||||
getCastorMarshaller().setSuppressXsiType(false);
|
||||
getCastorMarshaller().setUseXSITypeAtRoot(true);
|
||||
getCastorMarshaller().setRootElement("objects");
|
||||
marshaller.setSuppressXsiType(false);
|
||||
marshaller.setUseXSITypeAtRoot(true);
|
||||
marshaller.setRootElement("objects");
|
||||
String result = marshal(Arrays.asList(castorObject));
|
||||
assertXMLEqual("Marshaller wrote invalid result", ROOT_WITH_XSI_EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUseXsiTypeAsRootFalse() throws Exception {
|
||||
public void useXsiTypeAsRootFalse() throws Exception {
|
||||
CastorObject castorObject = createCastorObject();
|
||||
getCastorMarshaller().setSuppressXsiType(false);
|
||||
getCastorMarshaller().setUseXSITypeAtRoot(false);
|
||||
getCastorMarshaller().setRootElement("objects");
|
||||
marshaller.setSuppressXsiType(false);
|
||||
marshaller.setUseXSITypeAtRoot(false);
|
||||
marshaller.setRootElement("objects");
|
||||
String result = marshal(Arrays.asList(castorObject));
|
||||
assertXMLEqual("Marshaller wrote invalid result", ROOT_WITHOUT_XSI_EXPECTED_STRING, result);
|
||||
}
|
||||
|
||||
|
||||
private CastorMarshaller getCastorMarshaller() {
|
||||
return (CastorMarshaller) marshaller;
|
||||
}
|
||||
|
||||
private String marshal(Object object) throws Exception {
|
||||
StringWriter writer = new StringWriter();
|
||||
StreamResult result = new StreamResult(writer);
|
||||
getCastorMarshaller().marshal(object, result);
|
||||
marshaller.marshal(object, result);
|
||||
return writer.toString();
|
||||
}
|
||||
|
||||
|
||||
@@ -25,13 +25,13 @@ import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.oxm.AbstractUnmarshallerTests;
|
||||
import org.springframework.oxm.MarshallingException;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
@@ -39,8 +39,9 @@ import static org.junit.Assert.*;
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Jakub Narloch
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
public class CastorUnmarshallerTests extends AbstractUnmarshallerTests<CastorMarshaller> {
|
||||
|
||||
/**
|
||||
* Represents the xml with additional attribute that is not mapped in Castor config.
|
||||
@@ -58,6 +59,15 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
"</tns:flight></tns:flights>";
|
||||
|
||||
|
||||
@Override
|
||||
protected CastorMarshaller createUnmarshaller() throws Exception {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
ClassPathResource mappingLocation = new ClassPathResource("mapping.xml", CastorMarshaller.class);
|
||||
marshaller.setMappingLocation(mappingLocation);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void testFlights(Object o) {
|
||||
Flights flights = (Flights) o;
|
||||
@@ -73,15 +83,6 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
assertThat("Number is invalid", flight.getNumber(), equalTo(42L));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
ClassPathResource mappingLocation = new ClassPathResource("mapping.xml", CastorMarshaller.class);
|
||||
marshaller.setMappingLocation(mappingLocation);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void unmarshalTargetClass() throws Exception {
|
||||
@@ -94,7 +95,7 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetBothTargetClassesAndMapping() throws IOException {
|
||||
public void setBothTargetClassesAndMapping() throws IOException {
|
||||
CastorMarshaller unmarshaller = new CastorMarshaller();
|
||||
unmarshaller.setMappingLocation(new ClassPathResource("order-mapping.xml", CastorMarshaller.class));
|
||||
unmarshaller.setTargetClasses(new Class[] {Order.class});
|
||||
@@ -116,62 +117,62 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhitespacePreserveTrue() throws Exception {
|
||||
getCastorUnmarshaller().setWhitespacePreserve(true);
|
||||
public void whitespacePreserveTrue() throws Exception {
|
||||
unmarshaller.setWhitespacePreserve(true);
|
||||
Object result = unmarshalFlights();
|
||||
testFlights(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWhitespacePreserveFalse() throws Exception {
|
||||
getCastorUnmarshaller().setWhitespacePreserve(false);
|
||||
public void whitespacePreserveFalse() throws Exception {
|
||||
unmarshaller.setWhitespacePreserve(false);
|
||||
Object result = unmarshalFlights();
|
||||
testFlights(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIgnoreExtraAttributesTrue() throws Exception {
|
||||
getCastorUnmarshaller().setIgnoreExtraAttributes(true);
|
||||
public void ignoreExtraAttributesTrue() throws Exception {
|
||||
unmarshaller.setIgnoreExtraAttributes(true);
|
||||
Object result = unmarshal(EXTRA_ATTRIBUTES_STRING);
|
||||
testFlights(result);
|
||||
}
|
||||
|
||||
@Test(expected = MarshallingException.class)
|
||||
public void testIgnoreExtraAttributesFalse() throws Exception {
|
||||
getCastorUnmarshaller().setIgnoreExtraAttributes(false);
|
||||
public void ignoreExtraAttributesFalse() throws Exception {
|
||||
unmarshaller.setIgnoreExtraAttributes(false);
|
||||
unmarshal(EXTRA_ATTRIBUTES_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Not working yet")
|
||||
public void testIgnoreExtraElementsTrue() throws Exception {
|
||||
getCastorUnmarshaller().setIgnoreExtraElements(true);
|
||||
getCastorUnmarshaller().setValidating(false);
|
||||
public void ignoreExtraElementsTrue() throws Exception {
|
||||
unmarshaller.setIgnoreExtraElements(true);
|
||||
unmarshaller.setValidating(false);
|
||||
Object result = unmarshal(EXTRA_ELEMENTS_STRING);
|
||||
testFlights(result);
|
||||
}
|
||||
|
||||
@Test(expected = MarshallingException.class)
|
||||
public void testIgnoreExtraElementsFalse() throws Exception {
|
||||
getCastorUnmarshaller().setIgnoreExtraElements(false);
|
||||
public void ignoreExtraElementsFalse() throws Exception {
|
||||
unmarshaller.setIgnoreExtraElements(false);
|
||||
unmarshal(EXTRA_ELEMENTS_STRING);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRootObject() throws Exception {
|
||||
public void rootObject() throws Exception {
|
||||
Flights flights = new Flights();
|
||||
getCastorUnmarshaller().setRootObject(flights);
|
||||
unmarshaller.setRootObject(flights);
|
||||
Object result = unmarshalFlights();
|
||||
testFlights(result);
|
||||
assertSame("Result Flights is different object.", flights, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClearCollectionsTrue() throws Exception {
|
||||
public void clearCollectionsTrue() throws Exception {
|
||||
Flights flights = new Flights();
|
||||
flights.setFlight(new Flight[]{new Flight()});
|
||||
getCastorUnmarshaller().setRootObject(flights);
|
||||
getCastorUnmarshaller().setClearCollections(true);
|
||||
unmarshaller.setRootObject(flights);
|
||||
unmarshaller.setClearCollections(true);
|
||||
Object result = unmarshalFlights();
|
||||
|
||||
assertSame("Result Flights is different object.", flights, result);
|
||||
@@ -181,11 +182,11 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
|
||||
@Test
|
||||
@Ignore("Fails on the build server for some reason")
|
||||
public void testClearCollectionsFalse() throws Exception {
|
||||
public void clearCollectionsFalse() throws Exception {
|
||||
Flights flights = new Flights();
|
||||
flights.setFlight(new Flight[] {new Flight(), null});
|
||||
getCastorUnmarshaller().setRootObject(flights);
|
||||
getCastorUnmarshaller().setClearCollections(false);
|
||||
unmarshaller.setRootObject(flights);
|
||||
unmarshaller.setClearCollections(false);
|
||||
Object result = unmarshalFlights();
|
||||
|
||||
assertSame("Result Flights is different object.", flights, result);
|
||||
@@ -249,11 +250,6 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
assertEquals(true, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
|
||||
}
|
||||
|
||||
|
||||
private CastorMarshaller getCastorUnmarshaller() {
|
||||
return (CastorMarshaller) unmarshaller;
|
||||
}
|
||||
|
||||
private Object unmarshalFlights() throws Exception {
|
||||
return unmarshal(INPUT_STRING);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2015 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,14 +17,13 @@
|
||||
package org.springframework.oxm.config;
|
||||
|
||||
import org.apache.xmlbeans.XmlOptions;
|
||||
import org.junit.Before;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.oxm.castor.CastorMarshaller;
|
||||
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
|
||||
import org.springframework.oxm.xmlbeans.XmlBeansMarshaller;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@@ -33,19 +32,18 @@ import static org.junit.Assert.*;
|
||||
*
|
||||
* @author Arjen Poustma
|
||||
* @author Jakub Narloch
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class OxmNamespaceHandlerTests {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Before
|
||||
public void createAppContext() throws Exception {
|
||||
applicationContext = new ClassPathXmlApplicationContext("oxmNamespaceHandlerTest.xml", getClass());
|
||||
}
|
||||
private final ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||
"oxmNamespaceHandlerTest.xml", getClass());
|
||||
|
||||
@Test
|
||||
public void xmlBeansMarshaller() throws Exception {
|
||||
XmlBeansMarshaller marshaller = applicationContext.getBean("xmlBeansMarshaller", XmlBeansMarshaller.class);
|
||||
org.springframework.oxm.xmlbeans.XmlBeansMarshaller marshaller = applicationContext.getBean(
|
||||
org.springframework.oxm.xmlbeans.XmlBeansMarshaller.class);
|
||||
XmlOptions options = marshaller.getXmlOptions();
|
||||
assertNotNull("Options not set", options);
|
||||
assertTrue("option not set", options.hasOption("SAVE_PRETTY_PRINT"));
|
||||
|
||||
@@ -35,8 +35,10 @@ import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import org.xml.sax.Attributes;
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
@@ -45,7 +47,6 @@ import org.xml.sax.Locator;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.oxm.AbstractMarshallerTests;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.oxm.UncategorizedMappingException;
|
||||
import org.springframework.oxm.XmlMappingException;
|
||||
import org.springframework.oxm.jaxb.test.FlightType;
|
||||
@@ -65,19 +66,18 @@ import static org.mockito.BDDMockito.*;
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Biju Kunjummen
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
|
||||
public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshaller> {
|
||||
|
||||
private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb.test";
|
||||
|
||||
private Jaxb2Marshaller marshaller;
|
||||
|
||||
private Flights flights;
|
||||
|
||||
|
||||
@Override
|
||||
public Marshaller createMarshaller() throws Exception {
|
||||
marshaller = new Jaxb2Marshaller();
|
||||
protected Jaxb2Marshaller createMarshaller() throws Exception {
|
||||
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
||||
marshaller.setContextPath(CONTEXT_PATH);
|
||||
marshaller.afterPropertiesSet();
|
||||
return marshaller;
|
||||
@@ -389,7 +389,6 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
|
||||
public static class DummyRootElement {
|
||||
|
||||
private DummyType t = new DummyType();
|
||||
|
||||
}
|
||||
|
||||
@XmlType
|
||||
@@ -397,7 +396,6 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests {
|
||||
public static class DummyType {
|
||||
|
||||
private String s = "Hello";
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.oxm.jaxb;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
|
||||
import javax.activation.DataHandler;
|
||||
import javax.activation.FileDataSource;
|
||||
import javax.xml.bind.JAXBElement;
|
||||
@@ -32,7 +33,6 @@ import org.junit.Test;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.oxm.AbstractUnmarshallerTests;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.oxm.jaxb.test.FlightType;
|
||||
import org.springframework.oxm.jaxb.test.Flights;
|
||||
import org.springframework.oxm.mime.MimeContainer;
|
||||
@@ -44,23 +44,37 @@ import static org.mockito.BDDMockito.*;
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Biju Kunjummen
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests<Jaxb2Marshaller> {
|
||||
|
||||
private static final String INPUT_STRING = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
|
||||
|
||||
private Jaxb2Marshaller unmarshaller;
|
||||
|
||||
@Override
|
||||
public Unmarshaller createUnmarshaller() throws Exception {
|
||||
unmarshaller = new Jaxb2Marshaller();
|
||||
protected Jaxb2Marshaller createUnmarshaller() throws Exception {
|
||||
Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();
|
||||
unmarshaller.setContextPath("org.springframework.oxm.jaxb.test");
|
||||
unmarshaller.setSchema(new ClassPathResource("org/springframework/oxm/flight.xsd"));
|
||||
unmarshaller.afterPropertiesSet();
|
||||
return unmarshaller;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void testFlights(Object o) {
|
||||
Flights flights = (Flights) o;
|
||||
assertNotNull("Flights is null", flights);
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.getFlight().size());
|
||||
testFlight(flights.getFlight().get(0));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void testFlight(Object o) {
|
||||
FlightType flight = (FlightType) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void marshalAttachments() throws Exception {
|
||||
unmarshaller = new Jaxb2Marshaller();
|
||||
@@ -93,21 +107,6 @@ public class Jaxb2UnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
assertNotNull("datahandler property not set", object.getSwaDataHandler());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void testFlights(Object o) {
|
||||
Flights flights = (Flights) o;
|
||||
assertNotNull("Flights is null", flights);
|
||||
assertEquals("Invalid amount of flight elements", 1, flights.getFlight().size());
|
||||
testFlight(flights.getFlight().get(0));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void testFlight(Object o) {
|
||||
FlightType flight = (FlightType) o;
|
||||
assertNotNull("Flight is null", flight);
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -20,11 +20,11 @@ import java.io.StringWriter;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.custommonkey.xmlunit.XMLUnit;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.oxm.AbstractMarshallerTests;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.tests.Assume;
|
||||
import org.springframework.tests.TestGroup;
|
||||
|
||||
@@ -33,12 +33,13 @@ import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* NOTE: These tests fail under Eclipse/IDEA because JiBX binding does not occur by
|
||||
* default. The Gradle build should succeed, however.
|
||||
*
|
||||
* NOTE: These tests fail under Eclipse/IDEA because JiBX binding does
|
||||
* not occur by default. The Gradle build should succeed, however.
|
||||
* @author Arjen Poutsma
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class JibxMarshallerTests extends AbstractMarshallerTests {
|
||||
public class JibxMarshallerTests extends AbstractMarshallerTests<JibxMarshaller> {
|
||||
|
||||
@BeforeClass
|
||||
public static void compilerAssumptions() {
|
||||
@@ -46,7 +47,7 @@ public class JibxMarshallerTests extends AbstractMarshallerTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Marshaller createMarshaller() throws Exception {
|
||||
protected JibxMarshaller createMarshaller() throws Exception {
|
||||
JibxMarshaller marshaller = new JibxMarshaller();
|
||||
marshaller.setTargetPackage("org.springframework.oxm.jibx");
|
||||
marshaller.afterPropertiesSet();
|
||||
@@ -70,7 +71,7 @@ public class JibxMarshallerTests extends AbstractMarshallerTests {
|
||||
|
||||
@Test
|
||||
public void indentation() throws Exception {
|
||||
((JibxMarshaller) marshaller).setIndent(4);
|
||||
marshaller.setIndent(4);
|
||||
StringWriter writer = new StringWriter();
|
||||
marshaller.marshal(flights, new StreamResult(writer));
|
||||
XMLUnit.setIgnoreWhitespace(false);
|
||||
@@ -82,8 +83,8 @@ public class JibxMarshallerTests extends AbstractMarshallerTests {
|
||||
|
||||
@Test
|
||||
public void encodingAndStandalone() throws Exception {
|
||||
((JibxMarshaller) marshaller).setEncoding("ISO-8859-1");
|
||||
((JibxMarshaller) marshaller).setStandalone(Boolean.TRUE);
|
||||
marshaller.setEncoding("ISO-8859-1");
|
||||
marshaller.setStandalone(Boolean.TRUE);
|
||||
StringWriter writer = new StringWriter();
|
||||
marshaller.marshal(flights, new StreamResult(writer));
|
||||
assertTrue("Encoding and standalone not set",
|
||||
@@ -92,8 +93,8 @@ public class JibxMarshallerTests extends AbstractMarshallerTests {
|
||||
|
||||
@Test
|
||||
public void dtd() throws Exception {
|
||||
((JibxMarshaller) marshaller).setDocTypeRootElementName("flights");
|
||||
((JibxMarshaller) marshaller).setDocTypeSystemId("flights.dtd");
|
||||
marshaller.setDocTypeRootElementName("flights");
|
||||
marshaller.setDocTypeSystemId("flights.dtd");
|
||||
StringWriter writer = new StringWriter();
|
||||
marshaller.marshal(flights, new StreamResult(writer));
|
||||
assertTrue("doc type not written",
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -20,24 +20,22 @@ import java.io.ByteArrayInputStream;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.oxm.AbstractUnmarshallerTests;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.tests.Assume;
|
||||
import org.springframework.tests.TestGroup;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*
|
||||
* NOTE: These tests fail under Eclipse/IDEA because JiBX binding does
|
||||
* not occur by default. The Gradle build should succeed, however.
|
||||
*
|
||||
* @author Arjen Poutsma
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class JibxUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
public class JibxUnmarshallerTests extends AbstractUnmarshallerTests<JibxMarshaller> {
|
||||
|
||||
protected static final String INPUT_STRING_WITH_SPECIAL_CHARACTERS =
|
||||
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
@@ -48,8 +46,9 @@ public class JibxUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
Assume.group(TestGroup.CUSTOM_COMPILATION);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
protected JibxMarshaller createUnmarshaller() throws Exception {
|
||||
JibxMarshaller unmarshaller = new JibxMarshaller();
|
||||
unmarshaller.setTargetClass(Flights.class);
|
||||
unmarshaller.afterPropertiesSet();
|
||||
@@ -71,8 +70,8 @@ public class JibxUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
@Ignore
|
||||
public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
|
||||
// JiBX does not support reading XML fragments, hence the override here
|
||||
}
|
||||
@@ -80,7 +79,7 @@ public class JibxUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
@Test
|
||||
public void unmarshalStreamSourceInputStreamUsingNonDefaultEncoding() throws Exception {
|
||||
String encoding = "ISO-8859-1";
|
||||
((JibxMarshaller)unmarshaller).setEncoding(encoding);
|
||||
unmarshaller.setEncoding(encoding);
|
||||
|
||||
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING_WITH_SPECIAL_CHARACTERS.getBytes(encoding)));
|
||||
Object flights = unmarshaller.unmarshal(source);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -20,10 +20,10 @@ import java.io.ByteArrayOutputStream;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
|
||||
import org.apache.xmlbeans.XmlObject;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.oxm.AbstractMarshallerTests;
|
||||
import org.springframework.oxm.Marshaller;
|
||||
import org.springframework.samples.flight.FlightType;
|
||||
import org.springframework.samples.flight.FlightsDocument;
|
||||
|
||||
@@ -31,11 +31,14 @@ import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
public class XmlBeansMarshallerTests extends AbstractMarshallerTests {
|
||||
@SuppressWarnings("deprecation")
|
||||
public class XmlBeansMarshallerTests extends
|
||||
AbstractMarshallerTests<org.springframework.oxm.xmlbeans.XmlBeansMarshaller> {
|
||||
|
||||
@Override
|
||||
protected Marshaller createMarshaller() throws Exception {
|
||||
protected XmlBeansMarshaller createMarshaller() throws Exception {
|
||||
return new XmlBeansMarshaller();
|
||||
}
|
||||
|
||||
@@ -49,7 +52,7 @@ public class XmlBeansMarshallerTests extends AbstractMarshallerTests {
|
||||
}
|
||||
|
||||
@Test(expected = ClassCastException.class)
|
||||
public void testMarshalNonXmlObject() throws Exception {
|
||||
public void marshalNonXmlObject() throws Exception {
|
||||
marshaller.marshal(new Object(), new StreamResult(new ByteArrayOutputStream()));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2015 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.xmlbeans;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
||||
import javax.xml.namespace.QName;
|
||||
import javax.xml.stream.XMLInputFactory;
|
||||
import javax.xml.stream.XMLStreamReader;
|
||||
@@ -26,7 +27,6 @@ import javax.xml.transform.stream.StreamSource;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.oxm.AbstractUnmarshallerTests;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.oxm.ValidationFailureException;
|
||||
import org.springframework.samples.flight.FlightDocument;
|
||||
import org.springframework.samples.flight.FlightType;
|
||||
@@ -38,10 +38,12 @@ import static org.junit.Assert.*;
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
public class XmlBeansUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
@SuppressWarnings("deprecation")
|
||||
public class XmlBeansUnmarshallerTests extends
|
||||
AbstractUnmarshallerTests<org.springframework.oxm.xmlbeans.XmlBeansMarshaller> {
|
||||
|
||||
@Override
|
||||
protected Unmarshaller createUnmarshaller() throws Exception {
|
||||
protected XmlBeansMarshaller createUnmarshaller() throws Exception {
|
||||
return new XmlBeansMarshaller();
|
||||
}
|
||||
|
||||
@@ -68,6 +70,7 @@ public class XmlBeansUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Override
|
||||
public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
|
||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||
@@ -84,8 +87,8 @@ public class XmlBeansUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
}
|
||||
|
||||
@Test(expected = ValidationFailureException.class)
|
||||
public void testValidate() throws Exception {
|
||||
((XmlBeansMarshaller) unmarshaller).setValidating(true);
|
||||
public void validate() throws Exception {
|
||||
unmarshaller.setValidating(true);
|
||||
String invalidInput = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||
"<tns:flight><tns:number>abc</tns:number></tns:flight></tns:flights>";
|
||||
unmarshaller.unmarshal(new StreamSource(new StringReader(invalidInput)));
|
||||
|
||||
Reference in New Issue
Block a user