#119 Added Test Code, adding shipping address to trigger use of the 2nd object factory

This commit is contained in:
sjaakd 2014-03-04 11:58:38 +01:00
parent 6d2120c1bc
commit 859c51368d
8 changed files with 180 additions and 10 deletions

View File

@ -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<JAXBElement<String>> toJaxbList( List<String> orderDetailsDescriptions ) {
List<JAXBElement<String>> result = new ArrayList<JAXBElement<String>>();
for (String orderDetailDescription : orderDetailsDescriptions) {
result.add( of.createOrderDetailsTypeDescription( orderDetailDescription ) );
}
return result;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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);
}

View File

@ -33,6 +33,7 @@
<element name="orderNumber" type="long"/>
<element name="orderDate" type="dateTime"/>
<element name="orderDetails" type="test1:OrderDetailsType"/>
<element name="shippingAddress" type="test2:ShippingAddressType"/>
</sequence>
</complexType>

View File

@ -33,4 +33,14 @@
</restriction>
</simpleType>
<element name="ShippingAddress" type="test2:ShippingAddressType" />
<complexType name="ShippingAddressType">
<sequence>
<element name="street" type="string"/>
<element name="houseNumber" type="string"/>
<element name="city" type="string"/>
<element name="country" type="string"/>
</sequence>
</complexType>
</schema>

View File

@ -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();
}
}

View File

@ -56,6 +56,9 @@
<artifactId>maven-license-plugin</artifactId>
<configuration>
<header>etc/license.txt</header>
<mapping>
<xjb>XML_STYLE</xjb>
</mapping>
</configuration>
</plugin>
</plugins>
@ -66,8 +69,8 @@
<connection>scm:git:git://github.com/mapstruct/mapstruct.git</connection>
<developerConnection>scm:git:git@github.com:mapstruct/mapstruct.git</developerConnection>
<url>https://github.com/mapstruct/mapstruct/</url>
<tag>HEAD</tag>
</scm>
<tag>HEAD</tag>
</scm>
<profiles>
<profile>