#119 setup jaxb XSDs, DTOs, mapper and test

This commit is contained in:
sjaakd 2014-02-26 21:20:24 +01:00
parent ab7641731f
commit 6d2120c1bc
10 changed files with 453 additions and 0 deletions

View File

@ -114,6 +114,30 @@
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${project.build.resources[0].directory}/schema/</schemaDirectory>
<schemaIncludes>
<include>**/test1.xsd</include>
</schemaIncludes>
<bindingDirectory>${project.build.resources[0].directory}/binding</bindingDirectory>
<bindingIncludes>
<include>**/binding.xjb</include>
</bindingIncludes>
<extension>true</extension>
<verbose>true</verbose>
<specVersion>2.1</specVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>

View File

@ -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<String> description;
private OrderStatusDto status;
public String getName() {
return name;
}
public void setName( String name ) {
this.name = name;
}
public List<String> getDescription() {
return description;
}
public void setDescription( List<String> description ) {
this.description = description;
}
public OrderStatusDto getStatus() {
return status;
}
public void setStatus( OrderStatusDto status ) {
this.status = status;
}
}

View File

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

View File

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

View File

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

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<jaxb:bindings version="1.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc">
<jaxb:globalBindings
fixedAttributeAsConstantProperty="true"
typesafeEnumBase="xs:string"
typesafeEnumMemberName="generateName"
generateIsSetMethod="true"
generateElementProperty="true" >
<xjc:noValidator/>
<xjc:noValidatingUnmarshaller/>
</jaxb:globalBindings>
</jaxb:bindings>

View File

@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:test1="http://www.mapstruct.org/itest/jaxb/xsd/test1"
xmlns:test2="http://www.mapstruct.org/itest/jaxb/xsd/test2"
targetNamespace="http://www.mapstruct.org/itest/jaxb/xsd/test1"
elementFormDefault="qualified" version="1.0.0">
<import namespace="http://www.mapstruct.org/itest/jaxb/xsd/test2" schemaLocation="test2.xsd"/>
<element name="Order" type="test1:OrderType" />
<complexType name="OrderType">
<sequence>
<element name="orderNumber" type="long"/>
<element name="orderDate" type="dateTime"/>
<element name="orderDetails" type="test1:OrderDetailsType"/>
</sequence>
</complexType>
<element name="OrderDetails" type="test1:OrderDetailsType" />
<complexType name="OrderDetailsType">
<sequence>
<element name="name" type="string"/>
<element name="description" minOccurs="1" maxOccurs="5" type="string"/>
<element name="status" type="test2:OrderStatusType"/>
</sequence>
</complexType>
</schema>

View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:test2="http://www.mapstruct.org/itest/jaxb/xsd/test2"
targetNamespace="http://www.mapstruct.org/itest/jaxb/xsd/test2"
elementFormDefault="qualified" version="1.0.0">
<element name="OrderStatus" type="test2:OrderStatusType" />
<simpleType name="OrderStatusType">
<restriction base="string">
<enumeration value="ordered" />
<enumeration value="processed" />
<enumeration value="delivered" />
</restriction>
</simpleType>
</schema>

View File

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

View File

@ -362,6 +362,11 @@
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.6.3.201306030806</version>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.3</version>
</plugin>
</plugins>
</pluginManagement>