This commit is contained in:
Gunnar Morling 2014-03-04 20:12:37 +01:00
parent 859c51368d
commit fd3aa63e2d
8 changed files with 49 additions and 40 deletions

View File

@ -142,7 +142,7 @@
<groupId>org.bsc.maven</groupId> <groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId> <artifactId>maven-processor-plugin</artifactId>
<configuration> <configuration>
<defaultOutputDirectory>${project.build.directory}/generated-sources</defaultOutputDirectory> <defaultOutputDirectory>${project.build.directory}/generated-sources/mapstruct</defaultOutputDirectory>
<processors> <processors>
<processor>org.mapstruct.ap.MappingProcessor</processor> <processor>org.mapstruct.ap.MappingProcessor</processor>
</processors> </processors>

View File

@ -21,6 +21,7 @@ package org.mapstruct.itest.jaxb;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.xml.bind.JAXBElement; import javax.xml.bind.JAXBElement;
import org.mapstruct.itest.jaxb.xsd.test1.ObjectFactory; import org.mapstruct.itest.jaxb.xsd.test1.ObjectFactory;
/** /**
@ -37,12 +38,13 @@ public class JaxbMapper {
* the proper factory method for Lists * the proper factory method for Lists
* *
* @param orderDetailsDescriptions * @param orderDetailsDescriptions
*
* @return * @return
*/ */
List<JAXBElement<String>> toJaxbList( List<String> orderDetailsDescriptions ) { List<JAXBElement<String>> toJaxbList(List<String> orderDetailsDescriptions) {
List<JAXBElement<String>> result = new ArrayList<JAXBElement<String>>(); List<JAXBElement<String>> result = new ArrayList<JAXBElement<String>>();
for (String orderDetailDescription : orderDetailsDescriptions) { for ( String orderDetailDescription : orderDetailsDescriptions ) {
result.add( of.createOrderDetailsTypeDescription( orderDetailDescription ) ); result.add( of.createOrderDetailsTypeDescription( orderDetailDescription ) );
} }
return result; return result;

View File

@ -21,7 +21,6 @@ package org.mapstruct.itest.jaxb;
import java.util.List; import java.util.List;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
public class OrderDetailsDto { public class OrderDetailsDto {
@ -34,7 +33,7 @@ public class OrderDetailsDto {
return name; return name;
} }
public void setName( String name ) { public void setName(String name) {
this.name = name; this.name = name;
} }
@ -42,7 +41,7 @@ public class OrderDetailsDto {
return description; return description;
} }
public void setDescription( List<String> description ) { public void setDescription(List<String> description) {
this.description = description; this.description = description;
} }
@ -50,7 +49,7 @@ public class OrderDetailsDto {
return status; return status;
} }
public void setStatus( OrderStatusDto status ) { public void setStatus(OrderStatusDto status) {
this.status = status; this.status = status;
} }
} }

View File

@ -22,7 +22,6 @@ package org.mapstruct.itest.jaxb;
import java.util.Date; import java.util.Date;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
public class OrderDto { public class OrderDto {
@ -37,7 +36,7 @@ public class OrderDto {
return orderNumber; return orderNumber;
} }
public void setOrderNumber( Long orderNumber ) { public void setOrderNumber(Long orderNumber) {
this.orderNumber = orderNumber; this.orderNumber = orderNumber;
} }
@ -45,7 +44,7 @@ public class OrderDto {
return orderDate; return orderDate;
} }
public void setOrderDate( Date orderDate ) { public void setOrderDate(Date orderDate) {
this.orderDate = orderDate; this.orderDate = orderDate;
} }
@ -53,7 +52,7 @@ public class OrderDto {
return orderDetails; return orderDetails;
} }
public void setOrderDetails( OrderDetailsDto orderDetails ) { public void setOrderDetails(OrderDetailsDto orderDetails) {
this.orderDetails = orderDetails; this.orderDetails = orderDetails;
} }
@ -61,7 +60,7 @@ public class OrderDto {
return shippingAddress; return shippingAddress;
} }
public void setShippingAddress( ShippingAddressDto shippingAddress ) { public void setShippingAddress(ShippingAddressDto shippingAddress) {
this.shippingAddress = shippingAddress; this.shippingAddress = shippingAddress;
} }

View File

@ -19,14 +19,13 @@
package org.mapstruct.itest.jaxb; package org.mapstruct.itest.jaxb;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
public enum OrderStatusDto { public enum OrderStatusDto {
ORDERED("small"), ORDERED( "small" ),
PROCESSED("medium"), PROCESSED( "medium" ),
DELIVERED("large"); DELIVERED( "large" );
private final String value; private final String value;
OrderStatusDto(String v) { OrderStatusDto(String v) {
@ -38,12 +37,12 @@ public enum OrderStatusDto {
} }
public static OrderStatusDto fromValue(String v) { public static OrderStatusDto fromValue(String v) {
for (OrderStatusDto c: OrderStatusDto.values()) { for ( OrderStatusDto c : OrderStatusDto.values() ) {
if ( c.value.equals( v ) ) { if ( c.value.equals( v ) ) {
return c; return c;
} }
} }
throw new IllegalArgumentException(v); throw new IllegalArgumentException( v );
} }
} }

View File

@ -19,7 +19,6 @@
package org.mapstruct.itest.jaxb; package org.mapstruct.itest.jaxb;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
public class ShippingAddressDto { public class ShippingAddressDto {
@ -33,7 +32,7 @@ public class ShippingAddressDto {
return street; return street;
} }
public void setStreet( String street ) { public void setStreet(String street) {
this.street = street; this.street = street;
} }
@ -41,7 +40,7 @@ public class ShippingAddressDto {
return houseNumber; return houseNumber;
} }
public void setHouseNumber( String houseNumber ) { public void setHouseNumber(String houseNumber) {
this.houseNumber = houseNumber; this.houseNumber = houseNumber;
} }
@ -49,7 +48,7 @@ public class ShippingAddressDto {
return city; return city;
} }
public void setCity( String city ) { public void setCity(String city) {
this.city = city; this.city = city;
} }
@ -57,7 +56,7 @@ public class ShippingAddressDto {
return country; return country;
} }
public void setCountry( String country ) { public void setCountry(String country) {
this.country = country; this.country = country;
} }

View File

@ -28,25 +28,32 @@ import org.mapstruct.itest.jaxb.xsd.test2.ShippingAddressType;
/** /**
*
* @author Sjaak Derksen * @author Sjaak Derksen
*/ */
@Mapper(uses = { org.mapstruct.itest.jaxb.xsd.test1.ObjectFactory.class, @Mapper(uses = {
org.mapstruct.itest.jaxb.xsd.test2.ObjectFactory.class, org.mapstruct.itest.jaxb.xsd.test1.ObjectFactory.class,
JaxbMapper.class }) org.mapstruct.itest.jaxb.xsd.test2.ObjectFactory.class,
public interface SourceTargetMapper { JaxbMapper.class
})
public interface SourceTargetMapper {
SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class ); SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
// source 2 target methods // source 2 target methods
OrderDto sourceToTarget(OrderType source); OrderDto sourceToTarget(OrderType source);
OrderDetailsDto detailsToDto(OrderDetailsType source); OrderDetailsDto detailsToDto(OrderDetailsType source);
OrderStatusDto statusToDto(OrderStatusType source); OrderStatusDto statusToDto(OrderStatusType source);
ShippingAddressDto shippingAddressToDto(ShippingAddressType source); ShippingAddressDto shippingAddressToDto(ShippingAddressType source);
// target 2 source methods // target 2 source methods
OrderType targetToSource(OrderDto target); OrderType targetToSource(OrderDto target);
OrderDetailsType dtoToDetails(OrderDetailsDto target); OrderDetailsType dtoToDetails(OrderDetailsDto target);
OrderStatusType dtoToStatus(OrderStatusDto target); OrderStatusType dtoToStatus(OrderStatusDto target);
ShippingAddressType dtoToShippingAddress(ShippingAddressDto source); ShippingAddressType dtoToShippingAddress(ShippingAddressDto source);
} }

View File

@ -32,11 +32,11 @@ import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian; import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive; import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.mapstruct.itest.jaxb.xsd.test1.ObjectFactory;
import org.mapstruct.itest.jaxb.xsd.test1.OrderType;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Assertions.assertThat;
import org.mapstruct.itest.jaxb.xsd.test1.ObjectFactory;
import org.mapstruct.itest.jaxb.xsd.test1.OrderType;
/** /**
* Test for generation of JAXB based mapper implementations. * Test for generation of JAXB based mapper implementations.
@ -48,9 +48,9 @@ public class JaxbBasedMapperTest extends Arquillian {
@Deployment @Deployment
public static JavaArchive createDeployment() { public static JavaArchive createDeployment() {
return ShrinkWrap.create( JavaArchive.class ) return ShrinkWrap.create( JavaArchive.class )
.addPackage( SourceTargetMapper.class.getPackage() ) .addPackage( SourceTargetMapper.class.getPackage() )
.addPackage( org.mapstruct.itest.jaxb.xsd.test1.ObjectFactory.class.getPackage() ) .addPackage( org.mapstruct.itest.jaxb.xsd.test1.ObjectFactory.class.getPackage() )
.addPackage( org.mapstruct.itest.jaxb.xsd.test2.ObjectFactory.class.getPackage() ); .addPackage( org.mapstruct.itest.jaxb.xsd.test2.ObjectFactory.class.getPackage() );
} }
@Test @Test
@ -67,7 +67,7 @@ public class JaxbBasedMapperTest extends Arquillian {
source1.getShippingAddress().setHouseNumber( "11a" ); source1.getShippingAddress().setHouseNumber( "11a" );
source1.getShippingAddress().setStreet( "Awesome rd" ); source1.getShippingAddress().setStreet( "Awesome rd" );
source1.getShippingAddress().setCountry( "USA" ); source1.getShippingAddress().setCountry( "USA" );
source1.getOrderDetails().setDescription( new ArrayList() ); source1.getOrderDetails().setDescription( new ArrayList<String>() );
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( "1 MapStruct" );
source1.getOrderDetails().getDescription().add( "3 Lines of Code" ); source1.getOrderDetails().getDescription().add( "3 Lines of Code" );
@ -88,23 +88,27 @@ public class JaxbBasedMapperTest extends Arquillian {
assertThat( source2.getOrderNumber() ).isEqualTo( source1.getOrderNumber() ); assertThat( source2.getOrderNumber() ).isEqualTo( source1.getOrderNumber() );
assertThat( source2.getOrderDate() ).isEqualTo( source1.getOrderDate() ); assertThat( source2.getOrderDate() ).isEqualTo( source1.getOrderDate() );
assertThat( source2.getOrderDetails().getDescription().size() ).isEqualTo( assertThat( source2.getOrderDetails().getDescription().size() ).isEqualTo(
source1.getOrderDetails().getDescription().size() ); source1.getOrderDetails().getDescription().size()
);
assertThat( source2.getOrderDetails().getDescription().get( 0 ) ).isEqualTo( assertThat( source2.getOrderDetails().getDescription().get( 0 ) ).isEqualTo(
source1.getOrderDetails().getDescription().get( 0 ) ); source1.getOrderDetails().getDescription().get( 0 )
);
assertThat( source2.getOrderDetails().getDescription().get( 1 ) ).isEqualTo( assertThat( source2.getOrderDetails().getDescription().get( 1 ) ).isEqualTo(
source1.getOrderDetails().getDescription().get( 1 ) ); source1.getOrderDetails().getDescription().get( 1 )
);
assertThat( source2.getOrderDetails().getDescription().get( 2 ) ).isEqualTo( assertThat( source2.getOrderDetails().getDescription().get( 2 ) ).isEqualTo(
source1.getOrderDetails().getDescription().get( 2 ) ); source1.getOrderDetails().getDescription().get( 2 )
);
assertThat( source2.getOrderDetails().getName() ).isEqualTo( source1.getOrderDetails().getName() ); assertThat( source2.getOrderDetails().getName() ).isEqualTo( source1.getOrderDetails().getName() );
assertThat( source2.getOrderDetails().getStatus() ).isEqualTo( source1.getOrderDetails().getStatus() ); assertThat( source2.getOrderDetails().getStatus() ).isEqualTo( source1.getOrderDetails().getStatus() );
} }
private Date createDate( String date ) throws ParseException { private Date createDate(String date) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat( "dd-M-yyyy hh:mm:ss" ); SimpleDateFormat sdf = new SimpleDateFormat( "dd-M-yyyy hh:mm:ss" );
return sdf.parse( date ); return sdf.parse( date );
} }
private String toXml( JAXBElement element ) throws JAXBException { private String toXml(JAXBElement<?> element) throws JAXBException {
JAXBContext jc = JAXBContext.newInstance( element.getValue().getClass() ); JAXBContext jc = JAXBContext.newInstance( element.getValue().getClass() );
Marshaller marshaller = jc.createMarshaller(); Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE ); marshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE );