forked from mapstruct/mapstruct-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
320 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright 2012-2017 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. | ||
--> | ||
<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> | ||
|
||
<groupId>org.mapstruct.examples.clone</groupId> | ||
<artifactId>mapstruct-deep-clone</artifactId> | ||
<version>1.0.0</version> | ||
|
||
<properties> | ||
<org.mapstruct.version>1.3.0.Beta2</org.mapstruct.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.mapstruct</groupId> | ||
<artifactId>mapstruct-jdk8</artifactId> | ||
<version>${org.mapstruct.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<version>3.6.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.6.2</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
<annotationProcessorPaths> | ||
<path> | ||
<groupId>org.mapstruct</groupId> | ||
<artifactId>mapstruct-processor</artifactId> | ||
<version>${org.mapstruct.version}</version> | ||
</path> | ||
</annotationProcessorPaths> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
66 changes: 66 additions & 0 deletions
66
mapstruct-clone/src/main/java/org/mapstruct/example/dto/CustomerDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Copyright 2012-2017 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. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.example.dto; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Sjaak Derksen | ||
*/ | ||
public class CustomerDto { | ||
|
||
private Long id; | ||
private String customerName; | ||
private List<OrderItemDto> orders; | ||
private Map<OrderItemKeyDto, OrderItemDto> stock; | ||
|
||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getCustomerName() { | ||
return customerName; | ||
} | ||
|
||
public void setCustomerName(String customerName) { | ||
this.customerName = customerName; | ||
} | ||
|
||
public List<OrderItemDto> getOrders() { | ||
return orders; | ||
} | ||
|
||
public void setOrders(List<OrderItemDto> orders) { | ||
this.orders = orders; | ||
} | ||
|
||
public Map<OrderItemKeyDto, OrderItemDto> getStock() { | ||
return stock; | ||
} | ||
|
||
public void setStock(Map<OrderItemKeyDto, OrderItemDto> stock) { | ||
this.stock = stock; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
mapstruct-clone/src/main/java/org/mapstruct/example/dto/OrderItemDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.mapstruct.example.dto; | ||
|
||
/** | ||
* @author Sjaak Derksen | ||
*/ | ||
public class OrderItemDto { | ||
|
||
private String name; | ||
private Long quantity; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public Long getQuantity() { | ||
return quantity; | ||
} | ||
|
||
public void setQuantity(Long quantity) { | ||
this.quantity = quantity; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
mapstruct-clone/src/main/java/org/mapstruct/example/dto/OrderItemKeyDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.mapstruct.example.dto; | ||
|
||
/** | ||
* @author Sjaak Derksen | ||
*/ | ||
public class OrderItemKeyDto { | ||
|
||
private long stockNumber; | ||
|
||
public long getStockNumber() { | ||
return stockNumber; | ||
} | ||
|
||
public void setStockNumber(long stockNumber) { | ||
this.stockNumber = stockNumber; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
mapstruct-clone/src/main/java/org/mapstruct/example/mapper/Cloner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright 2012-2017 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. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.example.mapper; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.mapstruct.InheritInverseConfiguration; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.example.dto.CustomerDto; | ||
import org.mapstruct.example.dto.OrderItemDto; | ||
import org.mapstruct.example.dto.OrderItemKeyDto; | ||
import org.mapstruct.factory.Mappers; | ||
|
||
/** | ||
* @author Sjaak Derksen | ||
* | ||
* By defining all methods, we force MapStruct to generate new objects for each mapper in stead of | ||
* taking shortcuts by mapping an object directly. | ||
*/ | ||
@Mapper | ||
public interface Cloner { | ||
|
||
Cloner MAPPER = Mappers.getMapper( Cloner.class ); | ||
|
||
CustomerDto clone(CustomerDto customerDto); | ||
|
||
List<OrderItemDto> clone(List<OrderItemDto> orders); | ||
|
||
OrderItemDto clone(OrderItemDto order); | ||
|
||
Map<OrderItemKeyDto, OrderItemDto> clone(Map<OrderItemKeyDto, OrderItemDto> stock); | ||
|
||
OrderItemKeyDto clone( OrderItemKeyDto orderItemKeyDto ); | ||
} |
80 changes: 80 additions & 0 deletions
80
mapstruct-clone/src/test/java/org/mapstruct/example/ClonerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright 2012-2017 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.example; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.tuple; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.Test; | ||
import org.mapstruct.example.dto.CustomerDto; | ||
import org.mapstruct.example.dto.OrderItemDto; | ||
import org.mapstruct.example.dto.OrderItemKeyDto; | ||
import org.mapstruct.example.mapper.Cloner; | ||
|
||
/** | ||
* | ||
* @author Sjaak Derksen | ||
*/ | ||
public class ClonerTest { | ||
|
||
@Test | ||
public void testMapDtoToEntity() { | ||
|
||
CustomerDto customerDto = new CustomerDto(); | ||
customerDto.setId( 10L ); | ||
customerDto.setCustomerName("Jaques" ); | ||
OrderItemDto order1 = new OrderItemDto(); | ||
order1.setName ("Table" ); | ||
order1.setQuantity( 2L ); | ||
customerDto.setOrders( new ArrayList<>( Collections.singleton( order1 ) ) ); | ||
OrderItemKeyDto key = new OrderItemKeyDto(); | ||
key.setStockNumber( 5 ); | ||
Map stock = new HashMap( ); | ||
stock.put( key, order1 ); | ||
customerDto.setStock( stock ); | ||
|
||
CustomerDto customer = Cloner.MAPPER.clone( customerDto ); | ||
|
||
// check if cloned | ||
assertThat( customer.getId() ).isEqualTo( 10 ); | ||
assertThat( customer.getCustomerName() ).isEqualTo( "Jaques" ); | ||
assertThat( customer.getOrders() ) | ||
.extracting( "name", "quantity" ) | ||
.containsExactly( tuple( "Table", 2L ) ); | ||
assertThat( customer.getStock() ).isNotNull(); | ||
assertThat( customer.getStock() ).hasSize( 1 ); | ||
|
||
Map.Entry<OrderItemKeyDto, OrderItemDto> entry = customer.getStock().entrySet().iterator().next(); | ||
assertThat( entry.getKey().getStockNumber() ).isEqualTo( 5 ); | ||
assertThat( entry.getValue().getName() ).isEqualTo( "Table" ); | ||
assertThat( entry.getValue().getQuantity() ).isEqualTo( 2L ); | ||
|
||
// check mapper really created new objects | ||
assertThat( customer ).isNotSameAs( customerDto ); | ||
assertThat( customer.getOrders().get( 0 ) ).isNotEqualTo( order1 ); | ||
assertThat( entry.getKey() ).isNotEqualTo( key ); | ||
assertThat( entry.getValue() ).isNotEqualTo( order1 ); | ||
assertThat( entry.getValue() ).isNotEqualTo( customer.getOrders().get( 0 ) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters