mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#7 Adding integration test
This commit is contained in:
parent
861d92dc64
commit
a34849cb86
82
integrationtest/pom.xml
Normal file
82
integrationtest/pom.xml
Normal file
@ -0,0 +1,82 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
|
||||
Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
|
||||
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.
|
||||
|
||||
-->
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct-parent</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<relativePath>../parent/pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>mapstruct-integrationtest</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<name>MapStruct Integration Test</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.easytesting</groupId>
|
||||
<artifactId>fest-assert</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.bsc.maven</groupId>
|
||||
<artifactId>maven-processor-plugin</artifactId>
|
||||
<configuration>
|
||||
<defaultOutputDirectory>${project.build.directory}/generated-sources</defaultOutputDirectory>
|
||||
<processors>
|
||||
<processor>org.mapstruct.ap.MappingProcessor</processor>
|
||||
</processors>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>process</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>process</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>mapstruct-processor</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
*
|
||||
* 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;
|
||||
|
||||
public class Source {
|
||||
|
||||
private int foo;
|
||||
private Long bar;
|
||||
private int qax;
|
||||
private Long baz;
|
||||
private int zip;
|
||||
|
||||
public int getFoo() {
|
||||
return foo;
|
||||
}
|
||||
|
||||
public void setFoo(int foo) {
|
||||
this.foo = foo;
|
||||
}
|
||||
|
||||
public Long getBar() {
|
||||
return bar;
|
||||
}
|
||||
|
||||
public void setBar(Long bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public int getQax() {
|
||||
return qax;
|
||||
}
|
||||
|
||||
public void setQax(int qax) {
|
||||
this.qax = qax;
|
||||
}
|
||||
|
||||
public Long getBaz() {
|
||||
return baz;
|
||||
}
|
||||
|
||||
public void setBaz(Long baz) {
|
||||
this.baz = baz;
|
||||
}
|
||||
|
||||
public int getZip() {
|
||||
return zip;
|
||||
}
|
||||
|
||||
public void setZip(int zip) {
|
||||
this.zip = zip;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
*
|
||||
* 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;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mappers;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.Mappings;
|
||||
|
||||
@Mapper
|
||||
public interface SourceTargetMapper {
|
||||
|
||||
public static SourceTargetMapper INSTANCE = Mappers.getMapper( SourceTargetMapper.class );
|
||||
|
||||
@Mappings({
|
||||
@Mapping(source = "qax", target = "baz"),
|
||||
@Mapping(source = "baz", target = "qax")
|
||||
})
|
||||
Target sourceToTarget(Source source);
|
||||
|
||||
Source targetToSource(Target target);
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
*
|
||||
* 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;
|
||||
|
||||
public class Target {
|
||||
|
||||
private Long foo;
|
||||
private int bar;
|
||||
private Long baz;
|
||||
private int qax;
|
||||
private String zip;
|
||||
|
||||
public Long getFoo() {
|
||||
return foo;
|
||||
}
|
||||
|
||||
public void setFoo(Long foo) {
|
||||
this.foo = foo;
|
||||
}
|
||||
|
||||
public int getBar() {
|
||||
return bar;
|
||||
}
|
||||
|
||||
public void setBar(int bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public Long getBaz() {
|
||||
return baz;
|
||||
}
|
||||
|
||||
public void setBaz(Long baz) {
|
||||
this.baz = baz;
|
||||
}
|
||||
|
||||
public int getQax() {
|
||||
return qax;
|
||||
}
|
||||
|
||||
public void setQax(int qax) {
|
||||
this.qax = qax;
|
||||
}
|
||||
|
||||
public String getZip() {
|
||||
return zip;
|
||||
}
|
||||
|
||||
public void setZip(String zip) {
|
||||
this.zip = zip;
|
||||
}
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
/**
|
||||
* Copyright 2012-2013 Gunnar Morling (http://www.gunnarmorling.de/)
|
||||
*
|
||||
* 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;
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
public class ConversionTest {
|
||||
|
||||
@Test
|
||||
public void shouldApplyConversions() {
|
||||
Source source = new Source();
|
||||
source.setFoo( 42 );
|
||||
source.setBar( 23L );
|
||||
source.setZip( 73 );
|
||||
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 42 ) );
|
||||
assertThat( target.getBar() ).isEqualTo( 23 );
|
||||
assertThat( target.getZip() ).isEqualTo( "73" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleNulls() {
|
||||
Source source = new Source();
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getFoo() ).isEqualTo( Long.valueOf( 0 ) );
|
||||
assertThat( target.getBar() ).isEqualTo( 0 );
|
||||
assertThat( target.getZip() ).isEqualTo( "0" );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldApplyConversionsToMappedProperties() {
|
||||
Source source = new Source();
|
||||
source.setQax( 42 );
|
||||
source.setBaz( 23L );
|
||||
|
||||
Target target = SourceTargetMapper.INSTANCE.sourceToTarget( source );
|
||||
|
||||
assertThat( target ).isNotNull();
|
||||
assertThat( target.getBaz() ).isEqualTo( Long.valueOf( 42 ) );
|
||||
assertThat( target.getQax() ).isEqualTo( 23 );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldApplyConversionsForReverseMapping() {
|
||||
Target target = new Target();
|
||||
target.setFoo( 42L );
|
||||
target.setBar( 23 );
|
||||
target.setZip( "73" );
|
||||
|
||||
Source source = SourceTargetMapper.INSTANCE.targetToSource( target );
|
||||
|
||||
assertThat( source ).isNotNull();
|
||||
assertThat( source.getFoo() ).isEqualTo( 42 );
|
||||
assertThat( source.getBar() ).isEqualTo( 23 );
|
||||
assertThat( source.getZip() ).isEqualTo( 73 );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldApplyConversionsToMappedPropertiesForReverseMapping() {
|
||||
Target target = new Target();
|
||||
target.setQax( 42 );
|
||||
target.setBaz( 23L );
|
||||
|
||||
Source source = SourceTargetMapper.INSTANCE.targetToSource( target );
|
||||
|
||||
assertThat( source ).isNotNull();
|
||||
assertThat( source.getBaz() ).isEqualTo( 42 );
|
||||
assertThat( source.getQax() ).isEqualTo( 23 );
|
||||
}
|
||||
}
|
@ -60,6 +60,11 @@
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>mapstruct-processor</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
@ -73,6 +78,7 @@
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
<compilerArgument>-proc:none</compilerArgument>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
|
3
pom.xml
3
pom.xml
@ -36,5 +36,6 @@
|
||||
<module>parent</module>
|
||||
<module>core</module>
|
||||
<module>processor</module>
|
||||
</modules>
|
||||
<module>integrationtest</module>
|
||||
</modules>
|
||||
</project>
|
||||
|
@ -45,6 +45,11 @@
|
||||
<artifactId>hickory</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
@ -55,11 +60,6 @@
|
||||
<artifactId>fest-assert</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -81,13 +81,6 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<compilerArgument>-proc:none</compilerArgument>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.bsc.maven</groupId>
|
||||
<artifactId>maven-processor-plugin</artifactId>
|
||||
|
Loading…
x
Reference in New Issue
Block a user