SPR-5634 - OXM does not permit targetClass and mapping file for CastorMarshaller
This commit is contained in:
parent
de18d64b5c
commit
c1667687d5
|
|
@ -183,18 +183,14 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
|
|||
|
||||
|
||||
public final void afterPropertiesSet() throws CastorMappingException, IOException {
|
||||
if (this.mappingLocations != null && this.targetClass != null) {
|
||||
throw new IllegalArgumentException("Cannot set both the 'mappingLocations' and 'targetClass' property. " +
|
||||
"Set 'targetClass' for unmarshalling a single class, and 'mappingLocations' for multiple classes.");
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
if (this.mappingLocations != null) {
|
||||
logger.info("Configured using " + StringUtils.arrayToCommaDelimitedString(this.mappingLocations));
|
||||
}
|
||||
else if (this.targetClass != null) {
|
||||
if (this.targetClass != null) {
|
||||
logger.info("Configured for target class [" + this.targetClass.getName() + "]");
|
||||
}
|
||||
else {
|
||||
if (this.mappingLocations == null && this.targetClass == null) {
|
||||
logger.info("Using default configuration");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,19 +18,18 @@ package org.springframework.oxm.castor;
|
|||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.oxm.AbstractUnmarshallerTests;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
*/
|
||||
/** @author Arjen Poutsma */
|
||||
public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
|
||||
|
||||
@Override
|
||||
|
|
@ -67,12 +66,27 @@ public class CastorUnmarshallerTests extends AbstractUnmarshallerTests {
|
|||
testFlights(flights);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void testSetBothTargetClassAndMapping() throws IOException {
|
||||
CastorMarshaller marshaller = new CastorMarshaller();
|
||||
marshaller.setMappingLocation(new ClassPathResource("mapping.xml", CastorMarshaller.class));
|
||||
marshaller.setTargetClass(getClass());
|
||||
marshaller.afterPropertiesSet();
|
||||
CastorMarshaller unmarshaller = new CastorMarshaller();
|
||||
unmarshaller.setMappingLocation(new ClassPathResource("order-mapping.xml", CastorMarshaller.class));
|
||||
unmarshaller.setTargetClass(ArrayList.class);
|
||||
unmarshaller.afterPropertiesSet();
|
||||
|
||||
String xml = "<order>" +
|
||||
"<order-item id=\"1\" quantity=\"15\"/>" +
|
||||
"<order-item id=\"3\" quantity=\"20\"/>" +
|
||||
"</order>";
|
||||
|
||||
ArrayList result = (ArrayList) unmarshaller.unmarshal(new StreamSource(new StringReader(xml)));
|
||||
assertEquals("Invalid amount of items", 2, result.size());
|
||||
OrderItem item = (OrderItem) result.get(0);
|
||||
assertEquals("Invalid items", "1", item.getId());
|
||||
assertEquals("Invalid items", new Integer(15), item.getQuantity());
|
||||
item = (OrderItem) result.get(1);
|
||||
assertEquals("Invalid items", "3", item.getId());
|
||||
assertEquals("Invalid items", new Integer(20), item.getQuantity());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* 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.castor;
|
||||
|
||||
public class OrderItem {
|
||||
|
||||
private String id;
|
||||
|
||||
private Integer quantity;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getQuantity() {
|
||||
return quantity;
|
||||
}
|
||||
|
||||
public void setQuantity(Integer quantity) {
|
||||
this.quantity = quantity;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
<mapping>
|
||||
<class name="org.springframework.oxm.castor.OrderItem">
|
||||
<field name="Id" type="java.lang.String">
|
||||
<bind-xml name="id" node="attribute" />
|
||||
</field>
|
||||
<field name="Quantity" type="java.lang.Integer">
|
||||
<bind-xml name="quantity" node="attribute" />
|
||||
</field>
|
||||
</class>
|
||||
</mapping>
|
||||
Loading…
Reference in New Issue