From 6d2120c1bcb29fbeb34b6b1618d51f3579cb2250 Mon Sep 17 00:00:00 2001 From: sjaakd Date: Wed, 26 Feb 2014 21:20:24 +0100 Subject: [PATCH] #119 setup jaxb XSDs, DTOs, mapper and test --- integrationtest/pom.xml | 24 +++++ .../mapstruct/itest/jaxb/OrderDetailsDto.java | 56 ++++++++++++ .../org/mapstruct/itest/jaxb/OrderDto.java | 58 ++++++++++++ .../mapstruct/itest/jaxb/OrderStatusDto.java | 49 ++++++++++ .../itest/jaxb/SourceTargetMapper.java | 46 ++++++++++ .../src/main/resources/binding/binding.xjb | 40 ++++++++ .../src/main/resources/schema/test1.xsd | 48 ++++++++++ .../src/main/resources/schema/test2.xsd | 36 ++++++++ .../itest/jaxb/JaxbBasedMapperTest.java | 91 +++++++++++++++++++ parent/pom.xml | 5 + 10 files changed, 453 insertions(+) create mode 100644 integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderDetailsDto.java create mode 100644 integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderDto.java create mode 100644 integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderStatusDto.java create mode 100644 integrationtest/src/main/java/org/mapstruct/itest/jaxb/SourceTargetMapper.java create mode 100644 integrationtest/src/main/resources/binding/binding.xjb create mode 100644 integrationtest/src/main/resources/schema/test1.xsd create mode 100644 integrationtest/src/main/resources/schema/test2.xsd create mode 100644 integrationtest/src/test/java/org/mapstruct/itest/jaxb/JaxbBasedMapperTest.java diff --git a/integrationtest/pom.xml b/integrationtest/pom.xml index 9e4b6c0b3..8e2686f27 100644 --- a/integrationtest/pom.xml +++ b/integrationtest/pom.xml @@ -114,6 +114,30 @@ + + org.jvnet.jaxb2.maven2 + maven-jaxb2-plugin + + + + generate + + + + + ${project.build.resources[0].directory}/schema/ + + **/test1.xsd + + ${project.build.resources[0].directory}/binding + + **/binding.xjb + + true + true + 2.1 + + org.bsc.maven maven-processor-plugin diff --git a/integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderDetailsDto.java b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderDetailsDto.java new file mode 100644 index 000000000..a398bd115 --- /dev/null +++ b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderDetailsDto.java @@ -0,0 +1,56 @@ +/** + * 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.List; + +/** + * + * @author Sjaak Derksen + */ +public class OrderDetailsDto { + + private String name; + private List description; + private OrderStatusDto status; + + public String getName() { + return name; + } + + public void setName( String name ) { + this.name = name; + } + + public List getDescription() { + return description; + } + + public void setDescription( List description ) { + this.description = description; + } + + public OrderStatusDto getStatus() { + return status; + } + + public void setStatus( OrderStatusDto status ) { + this.status = status; + } +} diff --git a/integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderDto.java b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderDto.java new file mode 100644 index 000000000..67e52dcdb --- /dev/null +++ b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderDto.java @@ -0,0 +1,58 @@ +/** + * 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.Date; + +/** + * + * @author Sjaak Derksen + */ +public class OrderDto { + + private Long orderNumber; + private Date orderDate; + private OrderDetailsDto orderDetails; + + + public Long getOrderNumber() { + return orderNumber; + } + + public void setOrderNumber( Long orderNumber ) { + this.orderNumber = orderNumber; + } + + public Date getOrderDate() { + return orderDate; + } + + public void setOrderDate( Date orderDate ) { + this.orderDate = orderDate; + } + + public OrderDetailsDto getOrderDetails() { + return orderDetails; + } + + public void setOrderDetails( OrderDetailsDto orderDetails ) { + this.orderDetails = orderDetails; + } +} diff --git a/integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderStatusDto.java b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderStatusDto.java new file mode 100644 index 000000000..8859f1623 --- /dev/null +++ b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/OrderStatusDto.java @@ -0,0 +1,49 @@ +/** + * 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 enum OrderStatusDto { + + ORDERED("small"), + PROCESSED("medium"), + DELIVERED("large"); + private final String value; + + OrderStatusDto(String v) { + value = v; + } + + public String value() { + return value; + } + + public static OrderStatusDto fromValue(String v) { + for (OrderStatusDto c: OrderStatusDto.values()) { + if ( c.value.equals( v ) ) { + return c; + } + } + throw new IllegalArgumentException(v); + } + +} diff --git a/integrationtest/src/main/java/org/mapstruct/itest/jaxb/SourceTargetMapper.java b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/SourceTargetMapper.java new file mode 100644 index 000000000..6a99fe425 --- /dev/null +++ b/integrationtest/src/main/java/org/mapstruct/itest/jaxb/SourceTargetMapper.java @@ -0,0 +1,46 @@ +/** + * 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 org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; +import org.mapstruct.itest.jaxb.xsd.test1.OrderDetailsType; +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 }) +public interface SourceTargetMapper { + + SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class ); + + // source 2 target methods + OrderDto sourceToTarget(OrderType source); + OrderDetailsDto detailsToDto(OrderDetailsType source); + + // target 2 source methods + OrderType targetToSource(OrderDto target); + OrderDetailsType dtoToDetails(OrderDetailsDto target); + +} diff --git a/integrationtest/src/main/resources/binding/binding.xjb b/integrationtest/src/main/resources/binding/binding.xjb new file mode 100644 index 000000000..323b13e06 --- /dev/null +++ b/integrationtest/src/main/resources/binding/binding.xjb @@ -0,0 +1,40 @@ + + + + + + + + + + + + + diff --git a/integrationtest/src/main/resources/schema/test1.xsd b/integrationtest/src/main/resources/schema/test1.xsd new file mode 100644 index 000000000..4eecaa3f4 --- /dev/null +++ b/integrationtest/src/main/resources/schema/test1.xsd @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/integrationtest/src/main/resources/schema/test2.xsd b/integrationtest/src/main/resources/schema/test2.xsd new file mode 100644 index 000000000..d53da0aeb --- /dev/null +++ b/integrationtest/src/main/resources/schema/test2.xsd @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + diff --git a/integrationtest/src/test/java/org/mapstruct/itest/jaxb/JaxbBasedMapperTest.java b/integrationtest/src/test/java/org/mapstruct/itest/jaxb/JaxbBasedMapperTest.java new file mode 100644 index 000000000..a829d12eb --- /dev/null +++ b/integrationtest/src/test/java/org/mapstruct/itest/jaxb/JaxbBasedMapperTest.java @@ -0,0 +1,91 @@ +/** + * 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.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.testng.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +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.OrderType; + +/** + * Test for generation of JAXB based mapper implementations. + * + * @author Sjaak Derksen + */ +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() ); + } + + @Test + public void shouldMapJaxb() throws ParseException { + + SourceTargetMapper mapper = SourceTargetMapper.INSTANCE; + + OrderDto source1 = new OrderDto(); + source1.setOrderDetails( new OrderDetailsDto() ); + source1.setOrderNumber( 11L ); + source1.setOrderDate( createDate( "31-08-1982 10:20:56" ) ); + source1.getOrderDetails().setDescription( new ArrayList() ); + 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" ); + source1.getOrderDetails().setStatus( OrderStatusDto.ORDERED ); + + // map to JAXB + OrderType target = mapper.targetToSource( source1 ); + // map back from JAXB + OrderDto source2 = mapper.sourceToTarget( target ); + + // verify that source1 and source 2 are equal + assertThat( source2.getOrderNumber() ).isEqualTo( source1.getOrderNumber() ); + assertThat( source2.getOrderDate() ).isEqualTo( source1.getOrderDate() ); + assertThat( source2.getOrderDetails().getDescription().size() ).isEqualTo( + source1.getOrderDetails().getDescription().size() ); + assertThat( source2.getOrderDetails().getDescription().get( 0 ) ).isEqualTo( + source1.getOrderDetails().getDescription().get( 0 ) ); + assertThat( source2.getOrderDetails().getDescription().get( 1 ) ).isEqualTo( + source1.getOrderDetails().getDescription().get( 1 ) ); + assertThat( source2.getOrderDetails().getDescription().get( 2 ) ).isEqualTo( + source1.getOrderDetails().getDescription().get( 2 ) ); + assertThat( source2.getOrderDetails().getName() ).isEqualTo( source1.getOrderDetails().getName() ); + assertThat( source2.getOrderDetails().getStatus() ).isEqualTo( source1.getOrderDetails().getStatus() ); + } + + private Date createDate( String date ) throws ParseException { + SimpleDateFormat sdf = new SimpleDateFormat( "dd-M-yyyy hh:mm:ss" ); + return sdf.parse( date ); + } +} diff --git a/parent/pom.xml b/parent/pom.xml index 49b9fa742..53b6bd319 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -362,6 +362,11 @@ jacoco-maven-plugin 0.6.3.201306030806 + + org.jvnet.jaxb2.maven2 + maven-jaxb2-plugin + 0.8.3 +