From 0fa964038cda4806ca0a83657ef640fc21b4a9c5 Mon Sep 17 00:00:00 2001 From: Christian Bandowski Date: Sun, 15 Jul 2018 20:53:13 +0200 Subject: [PATCH] #1499 Add Protobuf builder integration test * Don't run the ProtobufBuilderTest with the processor_plugin_java8 and eclipse_jdt_java_8 (processor plugin runs before the proto java classes are created and the eclipse plugin has some bug) * Include *.proto for license checks --- .../itest/tests/ProtobufBuilderTest.java | 28 ++++++++ .../resources/protobufBuilderTest/pom.xml | 69 +++++++++++++++++++ .../mapstruct/itest/protobuf/AddressDto.java | 27 ++++++++ .../mapstruct/itest/protobuf/PersonDto.java | 45 ++++++++++++ .../itest/protobuf/PersonMapper.java | 19 +++++ .../src/main/proto/Person.proto | 21 ++++++ .../itest/protobuf/ProtobufMapperTest.java | 44 ++++++++++++ parent/pom.xml | 8 ++- 8 files changed, 260 insertions(+), 1 deletion(-) create mode 100644 integrationtest/src/test/java/org/mapstruct/itest/tests/ProtobufBuilderTest.java create mode 100644 integrationtest/src/test/resources/protobufBuilderTest/pom.xml create mode 100644 integrationtest/src/test/resources/protobufBuilderTest/src/main/java/org/mapstruct/itest/protobuf/AddressDto.java create mode 100644 integrationtest/src/test/resources/protobufBuilderTest/src/main/java/org/mapstruct/itest/protobuf/PersonDto.java create mode 100644 integrationtest/src/test/resources/protobufBuilderTest/src/main/java/org/mapstruct/itest/protobuf/PersonMapper.java create mode 100644 integrationtest/src/test/resources/protobufBuilderTest/src/main/proto/Person.proto create mode 100644 integrationtest/src/test/resources/protobufBuilderTest/src/test/java/org/mapstruct/itest/protobuf/ProtobufMapperTest.java diff --git a/integrationtest/src/test/java/org/mapstruct/itest/tests/ProtobufBuilderTest.java b/integrationtest/src/test/java/org/mapstruct/itest/tests/ProtobufBuilderTest.java new file mode 100644 index 000000000..7e52bed06 --- /dev/null +++ b/integrationtest/src/test/java/org/mapstruct/itest/tests/ProtobufBuilderTest.java @@ -0,0 +1,28 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.itest.tests; + +import org.junit.runner.RunWith; +import org.mapstruct.itest.testutil.runner.ProcessorSuite; +import org.mapstruct.itest.testutil.runner.ProcessorSuiteRunner; + +/** + * ECLIPSE_JDT_JAVA_8 is not working with Protobuf. Use all other available processor types. + * + * @author Christian Bandowski + */ +@RunWith(ProcessorSuiteRunner.class) +@ProcessorSuite(baseDir = "protobufBuilderTest", + processorTypes = { + ProcessorSuite.ProcessorType.ORACLE_JAVA_6, + ProcessorSuite.ProcessorType.ORACLE_JAVA_7, + ProcessorSuite.ProcessorType.ORACLE_JAVA_8, + ProcessorSuite.ProcessorType.ORACLE_JAVA_9, + ProcessorSuite.ProcessorType.ECLIPSE_JDT_JAVA_6, + ProcessorSuite.ProcessorType.ECLIPSE_JDT_JAVA_7 + }) +public class ProtobufBuilderTest { +} diff --git a/integrationtest/src/test/resources/protobufBuilderTest/pom.xml b/integrationtest/src/test/resources/protobufBuilderTest/pom.xml new file mode 100644 index 000000000..ecbbf1d6d --- /dev/null +++ b/integrationtest/src/test/resources/protobufBuilderTest/pom.xml @@ -0,0 +1,69 @@ + + + + 4.0.0 + + + org.mapstruct + mapstruct-it-parent + 1.0.0 + ../pom.xml + + + protobufIntegrationTest + jar + + + 1.6.0 + 0.5.1 + + + + + com.google.protobuf + protobuf-java + provided + + + + + + + kr.motd.maven + os-maven-plugin + ${os.mavenplugin.version} + + + + + org.xolstice.maven.plugins + protobuf-maven-plugin + ${protobuf.mavenplugin.version} + + + generate-sources + + compile + compile-custom + + + com.google.protobuf:protoc:3.2.0:exe:${os.detected.classifier} + + grpc-java + io.grpc:protoc-gen-grpc-java:1.2.0:exe:${os.detected.classifier} + + + + + + + + diff --git a/integrationtest/src/test/resources/protobufBuilderTest/src/main/java/org/mapstruct/itest/protobuf/AddressDto.java b/integrationtest/src/test/resources/protobufBuilderTest/src/main/java/org/mapstruct/itest/protobuf/AddressDto.java new file mode 100644 index 000000000..0956722de --- /dev/null +++ b/integrationtest/src/test/resources/protobufBuilderTest/src/main/java/org/mapstruct/itest/protobuf/AddressDto.java @@ -0,0 +1,27 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.itest.protobuf; + +public class AddressDto { + + private String addressLine; + + public AddressDto() { + + } + + public AddressDto(String addressLine) { + this.addressLine = addressLine; + } + + public String getAddressLine() { + return addressLine; + } + + public void setAddressLine(String addressLine) { + this.addressLine = addressLine; + } +} diff --git a/integrationtest/src/test/resources/protobufBuilderTest/src/main/java/org/mapstruct/itest/protobuf/PersonDto.java b/integrationtest/src/test/resources/protobufBuilderTest/src/main/java/org/mapstruct/itest/protobuf/PersonDto.java new file mode 100644 index 000000000..61ee37b39 --- /dev/null +++ b/integrationtest/src/test/resources/protobufBuilderTest/src/main/java/org/mapstruct/itest/protobuf/PersonDto.java @@ -0,0 +1,45 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.itest.protobuf; + +public class PersonDto { + private String name; + private int age; + private AddressDto address; + + public PersonDto() { + } + + public PersonDto(String name, int age, AddressDto address) { + this.name = name; + this.age = age; + this.address = address; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public AddressDto getAddress() { + return address; + } + + public void setAddress(AddressDto address) { + this.address = address; + } +} diff --git a/integrationtest/src/test/resources/protobufBuilderTest/src/main/java/org/mapstruct/itest/protobuf/PersonMapper.java b/integrationtest/src/test/resources/protobufBuilderTest/src/main/java/org/mapstruct/itest/protobuf/PersonMapper.java new file mode 100644 index 000000000..b5cdc8575 --- /dev/null +++ b/integrationtest/src/test/resources/protobufBuilderTest/src/main/java/org/mapstruct/itest/protobuf/PersonMapper.java @@ -0,0 +1,19 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.itest.protobuf; + +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; +import org.mapstruct.factory.Mappers; + +@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE) // protobuf has a lot of strange additional setters/getters +public interface PersonMapper { + + PersonMapper INSTANCE = Mappers.getMapper( PersonMapper.class ); + + PersonProtos.Person fromDto(PersonDto personDto); + PersonDto toDto(PersonProtos.Person personDto); +} diff --git a/integrationtest/src/test/resources/protobufBuilderTest/src/main/proto/Person.proto b/integrationtest/src/test/resources/protobufBuilderTest/src/main/proto/Person.proto new file mode 100644 index 000000000..209c2220b --- /dev/null +++ b/integrationtest/src/test/resources/protobufBuilderTest/src/main/proto/Person.proto @@ -0,0 +1,21 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +syntax = "proto3"; + +package itest.protobuf; + +option java_package = "org.mapstruct.itest.protobuf"; +option java_outer_classname = "PersonProtos"; + +message Person { + string name = 1; + int32 age = 2; + Address address = 3; + + message Address { + string addressLine = 1; + } +} \ No newline at end of file diff --git a/integrationtest/src/test/resources/protobufBuilderTest/src/test/java/org/mapstruct/itest/protobuf/ProtobufMapperTest.java b/integrationtest/src/test/resources/protobufBuilderTest/src/test/java/org/mapstruct/itest/protobuf/ProtobufMapperTest.java new file mode 100644 index 000000000..ae3740848 --- /dev/null +++ b/integrationtest/src/test/resources/protobufBuilderTest/src/test/java/org/mapstruct/itest/protobuf/ProtobufMapperTest.java @@ -0,0 +1,44 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.itest.protobuf; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Test for generation of Protobuf Builder Mapper implementations + * + * @author Christian Bandowski + */ +public class ProtobufMapperTest { + + @Test + public void testSimpleImmutableBuilderHappyPath() { + PersonDto personDto = PersonMapper.INSTANCE.toDto( PersonProtos.Person.newBuilder() + .setAge( 33 ) + .setName( "Bob" ) + .setAddress( PersonProtos.Person.Address.newBuilder() + .setAddressLine( "Wild Drive" ) + .build() ) + .build() ); + + assertThat( personDto.getAge() ).isEqualTo( 33 ); + assertThat( personDto.getName() ).isEqualTo( "Bob" ); + assertThat( personDto.getAddress() ).isNotNull(); + assertThat( personDto.getAddress().getAddressLine() ).isEqualTo( "Wild Drive" ); + } + + @Test + public void testLombokToImmutable() { + PersonProtos.Person person = PersonMapper.INSTANCE.fromDto( new PersonDto( "Bob", 33, new AddressDto( "Wild Drive" ) ) ); + + assertThat( person.getAge() ).isEqualTo( 33 ); + assertThat( person.getName() ).isEqualTo( "Bob" ); + assertThat( person.getAddress() ).isNotNull(); + assertThat( person.getAddress().getAddressLine() ).isEqualTo( "Wild Drive" ); + } +} diff --git a/parent/pom.xml b/parent/pom.xml index 126e09bb1..332ab2ff2 100644 --- a/parent/pom.xml +++ b/parent/pom.xml @@ -196,6 +196,11 @@ auto-value 1.5 + + com.google.protobuf + protobuf-java + 3.6.0 + org.inferred freebuilder @@ -576,7 +581,7 @@
${basedir}/../etc/license.txt
true - .idea/** + **/.idea/** **/build-config/checkstyle.xml **/build-config/import-control.xml copyright.txt @@ -601,6 +606,7 @@ SLASHSTAR_STYLE + SLASHSTAR_STYLE