SPR-7636 - XStreamMarshaller - Add support for configuring XStream with aliases by class (type) and also defining multiple attributes for the same class.

This commit is contained in:
Arjen Poutsma
2010-10-12 11:55:29 +00:00
parent 1f296403da
commit 043ec2c8b2
3 changed files with 130 additions and 15 deletions

View File

@@ -0,0 +1,24 @@
/*
* Copyright 2002-2010 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;
/**
* @author Arjen Poutsma
*/
public class FlightSubclass extends Flight {
}

View File

@@ -216,6 +216,42 @@ public class XStreamMarshallerTests {
String expected = "<flight flightNumber=\"42\" />";
assertXMLEqual("Marshaller does not use attributes", expected, writer.toString());
}
@Test
public void useAttributesForClassStringListMap() throws Exception {
marshaller
.setUseAttributeFor(Collections.singletonMap(Flight.class, Collections.singletonList("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 aliasesByTypeStringClassMap() throws Exception {
Map<String, Class<?>> aliases = new HashMap<String, Class<?>>();
aliases.put("flight", Flight.class);
FlightSubclass flight = new FlightSubclass();
flight.setFlightNumber(42);
marshaller.setAliasesByType(aliases);
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
assertXMLEqual("Marshaller does not use attributes", EXPECTED_STRING, writer.toString());
}
@Test
public void aliasesByTypeStringStringMap() throws Exception {
Map<String, String> aliases = new HashMap<String, String>();
aliases.put("flight", Flight.class.getName());
FlightSubclass flight = new FlightSubclass();
flight.setFlightNumber(42);
marshaller.setAliasesByType(aliases);
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
assertXMLEqual("Marshaller does not use attributes", EXPECTED_STRING, writer.toString());
}
@Test
public void fieldAliases() throws Exception {
@@ -288,7 +324,7 @@ public class XStreamMarshallerTests {
}
@Test
public void testAnnotatedMarshalStreamResultWriter() throws Exception {
public void annotatedMarshalStreamResultWriter() throws Exception {
marshaller.setAnnotatedClass(Flight.class);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);