From 859c51368ddc62f1fa3977969843e37691aa90c8 Mon Sep 17 00:00:00 2001 From: sjaakd Date: Tue, 4 Mar 2014 11:58:38 +0100 Subject: [PATCH] #119 Added Test Code, adding shipping address to trigger use of the 2nd object factory --- .../org/mapstruct/itest/jaxb/JaxbMapper.java | 51 +++++++++++++++ .../org/mapstruct/itest/jaxb/OrderDto.java | 10 +++ .../itest/jaxb/ShippingAddressDto.java | 64 +++++++++++++++++++ .../itest/jaxb/SourceTargetMapper.java | 10 ++- .../src/main/resources/schema/test1.xsd | 1 + .../src/main/resources/schema/test2.xsd | 10 +++ .../itest/jaxb/JaxbBasedMapperTest.java | 37 +++++++++-- pom.xml | 7 +- 8 files changed, 180 insertions(+), 10 deletions(-) create mode 100644 integrationtest/src/main/java/org/mapstruct/itest/jaxb/JaxbMapper.java create mode 100644 integrationtest/src/main/java/org/mapstruct/itest/jaxb/ShippingAddressDto.java diff --git a/integrationtest/src/main/java/org/mapstruct/itest/jaxb/JaxbMapper.java b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/JaxbMapper.java new file mode 100644 index 000000000..7aa7f6d6c --- /dev/null +++ b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/JaxbMapper.java @@ -0,0 +1,51 @@ +/** + * Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * 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.mapstruct.itest.jaxb; + +import java.util.ArrayList; +import java.util.List; +import javax.xml.bind.JAXBElement; +import org.mapstruct.itest.jaxb.xsd.test1.ObjectFactory; + +/** + * This class can be removed as soon as MapStruct is capable of generating List mappings. + * + * @author Sjaak Derksen + */ +public class JaxbMapper { + + private final ObjectFactory of = new ObjectFactory(); + + /** + * This method is needed, because currently MapStruct is not capable of selecting + * the proper factory method for Lists + * + * @param orderDetailsDescriptions + * @return + */ + List> toJaxbList( List orderDetailsDescriptions ) { + + List> result = new ArrayList>(); + for (String orderDetailDescription : orderDetailsDescriptions) { + result.add( of.createOrderDetailsTypeDescription( orderDetailDescription ) ); + } + return result; + } + +} diff --git a/integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderDto.java b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderDto.java index 67e52dcdb..a8f604c1d 100644 --- a/integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderDto.java +++ b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderDto.java @@ -30,6 +30,7 @@ public class OrderDto { private Long orderNumber; private Date orderDate; private OrderDetailsDto orderDetails; + private ShippingAddressDto shippingAddress; public Long getOrderNumber() { @@ -55,4 +56,13 @@ public class OrderDto { public void setOrderDetails( OrderDetailsDto orderDetails ) { this.orderDetails = orderDetails; } + + public ShippingAddressDto getShippingAddress() { + return shippingAddress; + } + + public void setShippingAddress( ShippingAddressDto shippingAddress ) { + this.shippingAddress = shippingAddress; + } + } diff --git a/integrationtest/src/main/java/org/mapstruct/itest/jaxb/ShippingAddressDto.java b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/ShippingAddressDto.java new file mode 100644 index 000000000..af85bb7ce --- /dev/null +++ b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/ShippingAddressDto.java @@ -0,0 +1,64 @@ +/** + * Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * 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.mapstruct.itest.jaxb; + +/** + * + * @author Sjaak Derksen + */ +public class ShippingAddressDto { + + private String street; + private String houseNumber; + private String city; + private String country; + + public String getStreet() { + return street; + } + + public void setStreet( String street ) { + this.street = street; + } + + public String getHouseNumber() { + return houseNumber; + } + + public void setHouseNumber( String houseNumber ) { + this.houseNumber = houseNumber; + } + + public String getCity() { + return city; + } + + public void setCity( String city ) { + this.city = city; + } + + public String getCountry() { + return country; + } + + public void setCountry( String country ) { + this.country = country; + } + +} diff --git a/integrationtest/src/main/java/org/mapstruct/itest/jaxb/SourceTargetMapper.java b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/SourceTargetMapper.java index 6a99fe425..3444e6e46 100644 --- a/integrationtest/src/main/java/org/mapstruct/itest/jaxb/SourceTargetMapper.java +++ b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/SourceTargetMapper.java @@ -23,6 +23,8 @@ import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; import org.mapstruct.itest.jaxb.xsd.test1.OrderDetailsType; import org.mapstruct.itest.jaxb.xsd.test1.OrderType; +import org.mapstruct.itest.jaxb.xsd.test2.OrderStatusType; +import org.mapstruct.itest.jaxb.xsd.test2.ShippingAddressType; /** @@ -30,7 +32,8 @@ import org.mapstruct.itest.jaxb.xsd.test1.OrderType; * @author Sjaak Derksen */ @Mapper(uses = { org.mapstruct.itest.jaxb.xsd.test1.ObjectFactory.class, - org.mapstruct.itest.jaxb.xsd.test2.ObjectFactory.class }) + org.mapstruct.itest.jaxb.xsd.test2.ObjectFactory.class, + JaxbMapper.class }) public interface SourceTargetMapper { SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class ); @@ -38,9 +41,12 @@ public interface SourceTargetMapper { // source 2 target methods OrderDto sourceToTarget(OrderType source); OrderDetailsDto detailsToDto(OrderDetailsType source); + OrderStatusDto statusToDto(OrderStatusType source); + ShippingAddressDto shippingAddressToDto(ShippingAddressType source); // target 2 source methods OrderType targetToSource(OrderDto target); OrderDetailsType dtoToDetails(OrderDetailsDto target); - + OrderStatusType dtoToStatus(OrderStatusDto target); + ShippingAddressType dtoToShippingAddress(ShippingAddressDto source); } diff --git a/integrationtest/src/main/resources/schema/test1.xsd b/integrationtest/src/main/resources/schema/test1.xsd index 4eecaa3f4..c816b3b38 100644 --- a/integrationtest/src/main/resources/schema/test1.xsd +++ b/integrationtest/src/main/resources/schema/test1.xsd @@ -33,6 +33,7 @@ + diff --git a/integrationtest/src/main/resources/schema/test2.xsd b/integrationtest/src/main/resources/schema/test2.xsd index d53da0aeb..150e750cc 100644 --- a/integrationtest/src/main/resources/schema/test2.xsd +++ b/integrationtest/src/main/resources/schema/test2.xsd @@ -33,4 +33,14 @@ + + + + + + + + + + diff --git a/integrationtest/src/test/java/org/mapstruct/itest/jaxb/JaxbBasedMapperTest.java b/integrationtest/src/test/java/org/mapstruct/itest/jaxb/JaxbBasedMapperTest.java index a829d12eb..ba1e7023e 100644 --- a/integrationtest/src/test/java/org/mapstruct/itest/jaxb/JaxbBasedMapperTest.java +++ b/integrationtest/src/test/java/org/mapstruct/itest/jaxb/JaxbBasedMapperTest.java @@ -18,10 +18,15 @@ */ package org.mapstruct.itest.jaxb; +import java.io.ByteArrayOutputStream; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.testng.Arquillian; @@ -30,6 +35,7 @@ import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.testng.annotations.Test; import static org.fest.assertions.Assertions.assertThat; +import org.mapstruct.itest.jaxb.xsd.test1.ObjectFactory; import org.mapstruct.itest.jaxb.xsd.test1.OrderType; /** @@ -39,17 +45,16 @@ import org.mapstruct.itest.jaxb.xsd.test1.OrderType; */ public class JaxbBasedMapperTest extends Arquillian { - @Deployment public static JavaArchive createDeployment() { return ShrinkWrap.create( JavaArchive.class ) - .addPackage( SourceTargetMapper.class.getPackage() ) - .addPackage( org.mapstruct.itest.jaxb.xsd.test1.ObjectFactory.class.getPackage() ) - .addPackage( org.mapstruct.itest.jaxb.xsd.test2.ObjectFactory.class.getPackage() ); + .addPackage( SourceTargetMapper.class.getPackage() ) + .addPackage( org.mapstruct.itest.jaxb.xsd.test1.ObjectFactory.class.getPackage() ) + .addPackage( org.mapstruct.itest.jaxb.xsd.test2.ObjectFactory.class.getPackage() ); } @Test - public void shouldMapJaxb() throws ParseException { + public void shouldMapJaxb() throws ParseException, JAXBException { SourceTargetMapper mapper = SourceTargetMapper.INSTANCE; @@ -57,8 +62,13 @@ public class JaxbBasedMapperTest extends Arquillian { source1.setOrderDetails( new OrderDetailsDto() ); source1.setOrderNumber( 11L ); source1.setOrderDate( createDate( "31-08-1982 10:20:56" ) ); + source1.setShippingAddress( new ShippingAddressDto() ); + source1.getShippingAddress().setCity( "SmallTown" ); + source1.getShippingAddress().setHouseNumber( "11a" ); + source1.getShippingAddress().setStreet( "Awesome rd" ); + source1.getShippingAddress().setCountry( "USA" ); source1.getOrderDetails().setDescription( new ArrayList() ); - source1.getOrderDetails().setName( "Shopping list for a Mapper"); + source1.getOrderDetails().setName( "Shopping list for a Mapper" ); source1.getOrderDetails().getDescription().add( "1 MapStruct" ); source1.getOrderDetails().getDescription().add( "3 Lines of Code" ); source1.getOrderDetails().getDescription().add( "1 Dose of Luck" ); @@ -66,6 +76,11 @@ public class JaxbBasedMapperTest extends Arquillian { // map to JAXB OrderType target = mapper.targetToSource( source1 ); + + // do a pretty print + ObjectFactory of = new ObjectFactory(); + System.out.println( toXml( of.createOrder( target ) ) ); + // map back from JAXB OrderDto source2 = mapper.sourceToTarget( target ); @@ -88,4 +103,14 @@ public class JaxbBasedMapperTest extends Arquillian { SimpleDateFormat sdf = new SimpleDateFormat( "dd-M-yyyy hh:mm:ss" ); return sdf.parse( date ); } + + private String toXml( JAXBElement element ) throws JAXBException { + JAXBContext jc = JAXBContext.newInstance( element.getValue().getClass() ); + Marshaller marshaller = jc.createMarshaller(); + marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE ); + + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + marshaller.marshal( element, baos ); + return baos.toString(); + } } diff --git a/pom.xml b/pom.xml index 1d5d616dc..9ddd86ac5 100644 --- a/pom.xml +++ b/pom.xml @@ -56,6 +56,9 @@ maven-license-plugin
etc/license.txt
+ + XML_STYLE +
@@ -66,8 +69,8 @@ scm:git:git://github.com/mapstruct/mapstruct.git scm:git:git@github.com:mapstruct/mapstruct.git https://github.com/mapstruct/mapstruct/ - HEAD - + HEAD +