SPR-5449: XStreamMarshaller.setImplicitCollection(Map) is insufficient

This commit is contained in:
Arjen Poutsma
2009-03-02 11:19:26 +00:00
parent ff83255ace
commit 03f3bf09d6
5 changed files with 97 additions and 113 deletions

View File

@@ -1,34 +0,0 @@
/*
* Copyright 2002-2009 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.oxm.xstream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("flight")
public class AnnotatedFlight {
@XStreamAlias("number")
private long flightNumber;
public long getFlightNumber() {
return flightNumber;
}
public void setFlightNumber(long number) {
this.flightNumber = number;
}
}

View File

@@ -0,0 +1,27 @@
package org.springframework.oxm.xstream;
import java.util.ArrayList;
import java.util.List;
public class Flights {
private List<Flight> flights = new ArrayList<Flight>();
private List<String> strings = new ArrayList<String>();
public List<Flight> getFlights() {
return flights;
}
public void setFlights(List<Flight> flights) {
this.flights = flights;
}
public List<String> getStrings() {
return strings;
}
public void setStrings(List<String> strings) {
this.strings = strings;
}
}

View File

@@ -40,7 +40,7 @@ import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;
import static org.custommonkey.xmlunit.XMLAssert.*;
import org.easymock.MockControl;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -49,6 +49,7 @@ import org.junit.Test;
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;
@@ -62,15 +63,15 @@ public class XStreamMarshallerTests {
private XStreamMarshaller marshaller;
private AnnotatedFlight flight;
private Flight flight;
@Before
public void createMarshaller() throws Exception {
marshaller = new XStreamMarshaller();
Map<String, Class> aliases = new HashMap<String, Class>();
aliases.put("flight", AnnotatedFlight.class);
aliases.put("flight", Flight.class);
marshaller.setAliases(aliases);
flight = new AnnotatedFlight();
flight = new Flight();
flight.setFlightNumber(42L);
}
@@ -139,21 +140,19 @@ public class XStreamMarshallerTests {
@Test
public void marshalSaxResult() throws Exception {
MockControl handlerControl = MockControl.createStrictControl(ContentHandler.class);
handlerControl.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
ContentHandler handlerMock = (ContentHandler) handlerControl.getMock();
ContentHandler handlerMock = createStrictMock(ContentHandler.class);
handlerMock.startDocument();
handlerMock.startElement("", "flight", "flight", null);
handlerMock.startElement("", "number", "number", null);
handlerMock.characters(new char[]{'4', '2'}, 0, 2);
handlerMock.endElement("", "number", "number");
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();
handlerControl.replay();
replay(handlerMock);
SAXResult result = new SAXResult(handlerMock);
marshaller.marshal(flight, result);
handlerControl.verify();
verify(handlerMock);
}
@Test
@@ -208,28 +207,46 @@ public class XStreamMarshallerTests {
@Test
public void useAttributesForClassStringMap() throws Exception {
marshaller.setUseAttributeFor(Collections.singletonMap(AnnotatedFlight.class, "flightNumber"));
marshaller.setUseAttributeFor(Collections.singletonMap(Flight.class, "flightNumber"));
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
String expected = "<flight flightNumber=\"42\" />";
assertXMLEqual("Marshaller does not use attributes", expected, writer.toString());
}
@Test
public void omitField() throws Exception {
marshaller.addOmittedField(AnnotatedFlight.class, "flightNumber");
@SuppressWarnings("unchecked")
public void omitFields() throws Exception {
Map omittedFieldsMap = Collections.singletonMap(Flight.class, "flightNumber");
marshaller.setOmittedFields(omittedFieldsMap);
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
assertXpathNotExists("/flight/flightNumber", writer.toString());
}
@Test
public void omitFields() throws Exception {
Map omittedFieldsMap = Collections.singletonMap(AnnotatedFlight.class, "flightNumber");
marshaller.setOmittedFields(omittedFieldsMap);
@SuppressWarnings("unchecked")
public void implicitCollections() throws Exception {
Flights flights = new Flights();
flights.getFlights().add(flight);
flights.getStrings().add("42");
Map<String, Class> aliases = new HashMap<String, Class>();
aliases.put("flight", Flight.class);
aliases.put("flights", Flights.class);
marshaller.setAliases(aliases);
Map implicitCollections = Collections.singletonMap(Flights.class, "flights,strings");
marshaller.setImplicitCollections(implicitCollections);
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
assertXpathNotExists("/flight/flightNumber", writer.toString());
marshaller.marshal(flights, new StreamResult(writer));
String result = writer.toString();
assertXpathNotExists("/flights/flights", result);
assertXpathExists("/flights/flight", result);
assertXpathNotExists("/flights/strings", result);
assertXpathExists("/flights/string", result);
}
@Test
@@ -239,18 +256,18 @@ public class XStreamMarshallerTests {
marshaller.marshal(flight, new StreamResult(writer));
assertEquals("Invalid result", "{\"flight\":{\"flightNumber\":42}}", writer.toString());
Object o = marshaller.unmarshal(new StreamSource(new StringReader(writer.toString())));
assertTrue("Unmarshalled object is not Flights", o instanceof AnnotatedFlight);
AnnotatedFlight unflight = (AnnotatedFlight) o;
assertTrue("Unmarshalled object is not Flights", o instanceof Flight);
Flight unflight = (Flight) o;
assertNotNull("Flight is null", unflight);
assertEquals("Number is invalid", 42L, unflight.getFlightNumber());
}
@Test
public void testMarshalStreamResultWriter() throws Exception {
marshaller.setAnnotatedClass(AnnotatedFlight.class);
public void testAnnotatedMarshalStreamResultWriter() throws Exception {
marshaller.setAnnotatedClass(Flight.class);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
AnnotatedFlight flight = new AnnotatedFlight();
Flight flight = new Flight();
flight.setFlightNumber(42);
marshaller.marshal(flight, result);
String expected = "<flight><number>42</number></flight>";

View File

@@ -49,13 +49,13 @@ public class XStreamUnmarshallerTests {
public void creteUnmarshaller() throws Exception {
unmarshaller = new XStreamMarshaller();
Map<String, Class> aliases = new HashMap<String, Class>();
aliases.put("flight", AnnotatedFlight.class);
aliases.put("flight", Flight.class);
unmarshaller.setAliases(aliases);
}
private void testFlight(Object o) {
assertTrue("Unmarshalled object is not Flights", o instanceof AnnotatedFlight);
AnnotatedFlight flight = (AnnotatedFlight) o;
assertTrue("Unmarshalled object is not Flights", o instanceof Flight);
Flight flight = (Flight) o;
assertNotNull("Flight is null", flight);
assertEquals("Number is invalid", 42L, flight.getFlightNumber());
}