diff --git a/org.springframework.oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java b/org.springframework.oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java index bb2e01110e8..a7156199ffc 100644 --- a/org.springframework.oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java +++ b/org.springframework.oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java @@ -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"); } } diff --git a/org.springframework.oxm/src/test/java/org/springframework/oxm/castor/CastorUnmarshallerTests.java b/org.springframework.oxm/src/test/java/org/springframework/oxm/castor/CastorUnmarshallerTests.java index c9a9c44f40e..5171f33bccd 100644 --- a/org.springframework.oxm/src/test/java/org/springframework/oxm/castor/CastorUnmarshallerTests.java +++ b/org.springframework.oxm/src/test/java/org/springframework/oxm/castor/CastorUnmarshallerTests.java @@ -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 = "" + + "" + + "" + + ""; + + 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()); } + } diff --git a/org.springframework.oxm/src/test/java/org/springframework/oxm/castor/OrderItem.java b/org.springframework.oxm/src/test/java/org/springframework/oxm/castor/OrderItem.java new file mode 100644 index 00000000000..eea8e864f71 --- /dev/null +++ b/org.springframework.oxm/src/test/java/org/springframework/oxm/castor/OrderItem.java @@ -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; + } +} diff --git a/org.springframework.oxm/src/test/resources/org/springframework/oxm/castor/order-mapping.xml b/org.springframework.oxm/src/test/resources/org/springframework/oxm/castor/order-mapping.xml new file mode 100644 index 00000000000..687f80ac9ec --- /dev/null +++ b/org.springframework.oxm/src/test/resources/org/springframework/oxm/castor/order-mapping.xml @@ -0,0 +1,11 @@ + + + + + + + + + + +