Merge pull request #618 from jesus-dayo/master

added code to mapstruct tutorial
This commit is contained in:
Alex Theedom 2016-08-22 18:29:14 +01:00 committed by GitHub
commit 638e666e4d
14 changed files with 387 additions and 202 deletions

49
mapstruct/bin/pom.xml Normal file
View File

@ -0,0 +1,49 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<artifactId>mapstruct</artifactId>
<name>mapstruct</name>
<groupId>com.baeldung</groupId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<org.mapstruct.version>1.0.0.Final</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>
</dependencies>
<build>
<finalName>mapstruct</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</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>

View File

@ -24,6 +24,17 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.2.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>mapstruct</finalName>

View File

@ -0,0 +1,33 @@
package org.baeldung.dto;
public class DivisionDTO {
public DivisionDTO() {
}
public DivisionDTO(int id, String name) {
super();
this.id = id;
this.name = name;
}
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -2,41 +2,41 @@ package org.baeldung.dto;
public class EmployeeDTO {
private int employeeId;
private String employeeName;
private int divisionId;
private String divisionName;
private int employeeId;
private String employeeName;
private DivisionDTO division;
private String employeeStartDt;
public int getEmployeeId() {
return employeeId;
}
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
public int getDivisionId() {
return divisionId;
}
public DivisionDTO getDivision() {
return division;
}
public void setDivisionId(int divisionId) {
this.divisionId = divisionId;
}
public void setDivision(DivisionDTO division) {
this.division = division;
}
public String getDivisionName() {
return divisionName;
}
public String getEmployeeStartDt() {
return employeeStartDt;
}
public void setDivisionName(String divisionName) {
this.divisionName = divisionName;
}
public void setEmployeeStartDt(String employeeStartDt) {
this.employeeStartDt = employeeStartDt;
}
}

View File

@ -2,23 +2,23 @@ package org.baeldung.dto;
public class SimpleSource {
private String name;
private String description;
public String getName() {
return name;
}
private String name;
private String description;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -2,32 +2,32 @@ package org.baeldung.entity;
public class Division {
public Division() {
}
public Division() {
}
public Division(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Division(int id, String name) {
super();
this.id = id;
this.name = name;
}
private int id;
private String name;
private int id;
private String name;
public int getId() {
return id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -1,33 +1,44 @@
package org.baeldung.entity;
import java.util.Date;
public class Employee {
private int id;
private String name;
private Division division;
private int id;
private String name;
private Division division;
private Date startDt;
public int getId() {
return id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public Division getDivision() {
return division;
}
public Division getDivision() {
return division;
}
public void setDivision(Division division) {
this.division = division;
}
public void setDivision(Division division) {
this.division = division;
}
public Date getStartDt() {
return startDt;
}
public void setStartDt(Date startDt) {
this.startDt = startDt;
}
}

View File

@ -2,23 +2,23 @@ package org.baeldung.entity;
public class SimpleDestination {
private String name;
private String description;
public String getName() {
return name;
}
private String name;
private String description;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getDescription() {
return description;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -1,6 +1,10 @@
package org.baeldung.mapper;
import java.util.List;
import org.baeldung.dto.DivisionDTO;
import org.baeldung.dto.EmployeeDTO;
import org.baeldung.entity.Division;
import org.baeldung.entity.Employee;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
@ -9,19 +13,18 @@ import org.mapstruct.Mappings;
@Mapper
public interface EmployeeMapper {
@Mappings({
@Mapping(target="divisionId",source="entity.division.id"),
@Mapping(target="divisionName",source="entity.division.name"),
@Mapping(target="employeeId",source="entity.id"),
@Mapping(target="employeeName",source="entity.name")
})
EmployeeDTO employeeToEmployeeDTO(Employee entity);
@Mappings({
@Mapping(target="id",source="dto.employeeId"),
@Mapping(target="name",source="dto.employeeName"),
@Mapping(target="division",expression="java(new org.baeldung.entity.Division(dto.getDivisionId(),dto.getDivisionName()))")
})
Employee employeeDTOtoEmployee(EmployeeDTO dto);
@Mappings({ @Mapping(target = "employeeId", source = "entity.id"), @Mapping(target = "employeeName", source = "entity.name"), @Mapping(target = "employeeStartDt", source = "entity.startDt", dateFormat = "dd-MM-yyyy HH:mm:ss") })
EmployeeDTO employeeToEmployeeDTO(Employee entity);
@Mappings({ @Mapping(target = "id", source = "dto.employeeId"), @Mapping(target = "name", source = "dto.employeeName"), @Mapping(target = "startDt", source = "dto.employeeStartDt", dateFormat = "dd-MM-yyyy HH:mm:ss") })
Employee employeeDTOtoEmployee(EmployeeDTO dto);
DivisionDTO divisionToDivisionDTO(Division entity);
Division divisionDTOtoDivision(DivisionDTO dto);
List<Employee> convertEmployeeDTOListToEmployeeList(List<EmployeeDTO> list);
List<EmployeeDTO> convertEmployeeListToEmployeeDTOList(List<Employee> list);
}

View File

@ -4,10 +4,11 @@ import org.baeldung.dto.SimpleSource;
import org.baeldung.entity.SimpleDestination;
import org.mapstruct.Mapper;
@Mapper
@Mapper(componentModel = "spring")
public interface SimpleSourceDestinationMapper {
SimpleDestination sourceToDestination(SimpleSource source);
SimpleDestination sourceToDestination(SimpleSource source);
SimpleSource destinationToSource(SimpleDestination destination);
}

View File

@ -1,13 +0,0 @@
package org.baeldung.mapper;
import org.baeldung.dto.SimpleSource;
import org.baeldung.entity.SimpleDestination;
import org.mapstruct.Mapper;
@Mapper(componentModel="spring")
public interface SimpleSourceDestinationSpringMapper {
SimpleDestination sourceToDestination(SimpleSource source);
SimpleSource destinationToSource(SimpleDestination destination);
}

View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="org.baeldung" />
</beans>

View File

@ -1,7 +1,14 @@
package org.baeldung.mapper;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.baeldung.dto.DivisionDTO;
import org.baeldung.dto.EmployeeDTO;
import org.baeldung.entity.Division;
import org.baeldung.entity.Employee;
@ -9,40 +16,108 @@ import org.junit.Test;
import org.mapstruct.factory.Mappers;
public class EmployeeMapperTest {
@Test
public void givenEmployeeDTOtoEmployee_whenMaps_thenCorrect(){
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
EmployeeDTO dto = new EmployeeDTO();
dto.setDivisionId(1);
dto.setDivisionName("IT Division");
dto.setEmployeeId(1);
dto.setEmployeeName("John");
Employee entity = mapper.employeeDTOtoEmployee(dto);
assertEquals(dto.getDivisionId(), entity.getDivision().getId());
assertEquals(dto.getDivisionName(), entity.getDivision().getName());
assertEquals(dto.getEmployeeId(),entity.getId());
assertEquals(dto.getEmployeeName(),entity.getName());
}
@Test
public void givenEmployeetoEmployeeDTO_whenMaps_thenCorrect(){
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
Employee entity = new Employee();
entity.setDivision(new Division(1,"IT Division"));
entity.setId(1);
entity.setName("John");
EmployeeDTO dto = mapper.employeeToEmployeeDTO(entity);
assertEquals(dto.getDivisionId(), entity.getDivision().getId());
assertEquals(dto.getDivisionName(), entity.getDivision().getName());
assertEquals(dto.getEmployeeId(),entity.getId());
assertEquals(dto.getEmployeeName(),entity.getName());
}
EmployeeMapper mapper = Mappers.getMapper(EmployeeMapper.class);
private static final String DATE_FORMAT = "dd-MM-yyyy HH:mm:ss";
@Test
public void givenEmployeeDTOwithDiffNametoEmployee_whenMaps_thenCorrect() {
EmployeeDTO dto = new EmployeeDTO();
dto.setEmployeeId(1);
dto.setEmployeeName("John");
Employee entity = mapper.employeeDTOtoEmployee(dto);
assertEquals(dto.getEmployeeId(), entity.getId());
assertEquals(dto.getEmployeeName(), entity.getName());
}
@Test
public void givenEmployeewithDiffNametoEmployeeDTO_whenMaps_thenCorrect() {
Employee entity = new Employee();
entity.setId(1);
entity.setName("John");
EmployeeDTO dto = mapper.employeeToEmployeeDTO(entity);
assertEquals(dto.getEmployeeId(), entity.getId());
assertEquals(dto.getEmployeeName(), entity.getName());
}
@Test
public void givenEmployeeDTOwithNestedMappingToEmployee_whenMaps_thenCorrect() {
EmployeeDTO dto = new EmployeeDTO();
dto.setDivision(new DivisionDTO(1, "Division1"));
Employee entity = mapper.employeeDTOtoEmployee(dto);
assertEquals(dto.getDivision().getId(), entity.getDivision().getId());
assertEquals(dto.getDivision().getName(), entity.getDivision().getName());
}
@Test
public void givenEmployeeWithNestedMappingToEmployeeDTO_whenMaps_thenCorrect() {
Employee entity = new Employee();
entity.setDivision(new Division(1, "Division1"));
EmployeeDTO dto = mapper.employeeToEmployeeDTO(entity);
assertEquals(dto.getDivision().getId(), entity.getDivision().getId());
assertEquals(dto.getDivision().getName(), entity.getDivision().getName());
}
@Test
public void givenEmployeeListToEmployeeDTOList_whenMaps_thenCorrect() {
List<Employee> employeeList = new ArrayList<>();
Employee emp = new Employee();
emp.setId(1);
emp.setName("EmpName");
emp.setDivision(new Division(1, "Division1"));
employeeList.add(emp);
List<EmployeeDTO> employeeDtoList = mapper.convertEmployeeListToEmployeeDTOList(employeeList);
EmployeeDTO employeeDTO = employeeDtoList.get(0);
assertEquals(employeeDTO.getEmployeeId(), emp.getId());
assertEquals(employeeDTO.getEmployeeName(), emp.getName());
assertEquals(employeeDTO.getDivision().getId(), emp.getDivision().getId());
assertEquals(employeeDTO.getDivision().getName(), emp.getDivision().getName());
}
@Test
public void givenEmployeeDTOListToEmployeeList_whenMaps_thenCorrect() {
List<EmployeeDTO> employeeDTOList = new ArrayList<>();
EmployeeDTO empDTO = new EmployeeDTO();
empDTO.setEmployeeId(1);
empDTO.setEmployeeName("EmpName");
empDTO.setDivision(new DivisionDTO(1, "Division1"));
employeeDTOList.add(empDTO);
List<Employee> employeeList = mapper.convertEmployeeDTOListToEmployeeList(employeeDTOList);
Employee employee = employeeList.get(0);
assertEquals(employee.getId(), empDTO.getEmployeeId());
assertEquals(employee.getName(), empDTO.getEmployeeName());
assertEquals(employee.getDivision().getId(), empDTO.getDivision().getId());
assertEquals(employee.getDivision().getName(), empDTO.getDivision().getName());
}
@Test
public void givenEmployeeWithStartDateMappingToEmployeeDTO_whenMaps_thenCorrect() throws ParseException {
Employee entity = new Employee();
entity.setStartDt(new Date());
EmployeeDTO dto = mapper.employeeToEmployeeDTO(entity);
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
assertEquals(format.parse(dto.getEmployeeStartDt()).toString(), entity.getStartDt().toString());
}
@Test
public void givenEmployeeDTOWithStartDateMappingToEmployee_whenMaps_thenCorrect() throws ParseException {
EmployeeDTO dto = new EmployeeDTO();
dto.setEmployeeStartDt("01-04-2016 01:00:00");
Employee entity = mapper.employeeDTOtoEmployee(dto);
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
assertEquals(format.parse(dto.getEmployeeStartDt()).toString(), entity.getStartDt().toString());
}
}

View File

@ -1,44 +1,44 @@
package org.baeldung.mapper;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import org.baeldung.dto.SimpleSource;
import org.baeldung.entity.SimpleDestination;
import org.junit.Test;
import org.mapstruct.factory.Mappers;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SimpleSourceDestinationMapperTest {
@Test
public void givenSimpleSourceToSimpleDestination_whenMaps_thenCorrect() {
SimpleSourceDestinationMapper simpleSourceDestinationMapper = Mappers
.getMapper(SimpleSourceDestinationMapper.class);
SimpleSource simpleSource = new SimpleSource();
simpleSource.setName("SourceName");
simpleSource.setDescription("SourceDescription");
SimpleDestination destination =
simpleSourceDestinationMapper.sourceToDestination(simpleSource);
assertEquals(simpleSource.getName(), destination.getName());
assertEquals(simpleSource.getDescription(), destination.getDescription());
}
@Test
public void givenSimpleDestinationToSourceDestination_whenMaps_thenCorrect() {
SimpleSourceDestinationMapper simpleSourceDestinationMapper = Mappers
.getMapper(SimpleSourceDestinationMapper.class);
SimpleDestination destination = new SimpleDestination();
destination.setName("DestinationName");
destination.setDescription("DestinationDescription");
SimpleSource source =
simpleSourceDestinationMapper.destinationToSource(destination);
assertEquals(destination.getName(), source.getName());
assertEquals(destination.getDescription(), source.getDescription());
}
@Autowired
SimpleSourceDestinationMapper simpleSourceDestinationMapper;
@Test
public void givenSimpleSourceToSimpleDestination_whenMaps_thenCorrect() {
SimpleSource simpleSource = new SimpleSource();
simpleSource.setName("SourceName");
simpleSource.setDescription("SourceDescription");
SimpleDestination destination = simpleSourceDestinationMapper.sourceToDestination(simpleSource);
assertEquals(simpleSource.getName(), destination.getName());
assertEquals(simpleSource.getDescription(), destination.getDescription());
}
@Test
public void givenSimpleDestinationToSourceDestination_whenMaps_thenCorrect() {
SimpleDestination destination = new SimpleDestination();
destination.setName("DestinationName");
destination.setDescription("DestinationDescription");
SimpleSource source = simpleSourceDestinationMapper.destinationToSource(destination);
assertEquals(destination.getName(), source.getName());
assertEquals(destination.getDescription(), source.getDescription());
}
}