From b5b94bde46bfaa27494ce44939016ed8b697a86f Mon Sep 17 00:00:00 2001 From: thunderhook <8238759+thunderhook@users.noreply.github.com> Date: Thu, 22 Aug 2024 22:10:10 +0200 Subject: [PATCH] #3678 write a separate test --- .../ap/test/bugs/_3678/Issue3678Mapper.java | 93 +++++++++++++++++++ .../ap/test/bugs/_3678/Issue3678Test.java | 33 +++++++ .../BuilderLifecycleCallbacksTest.java | 5 - .../builder/lifecycle/OrderMapperImpl.java | 91 ------------------ 4 files changed, 126 insertions(+), 96 deletions(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_3678/Issue3678Mapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/bugs/_3678/Issue3678Test.java delete mode 100644 processor/src/test/resources/fixtures/org/mapstruct/ap/test/builder/lifecycle/OrderMapperImpl.java diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_3678/Issue3678Mapper.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_3678/Issue3678Mapper.java new file mode 100644 index 000000000..882047d5c --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_3678/Issue3678Mapper.java @@ -0,0 +1,93 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package org.mapstruct.ap.test.bugs._3678; + +import org.mapstruct.AfterMapping; +import org.mapstruct.Mapper; +import org.mapstruct.ReportingPolicy; + +@Mapper(unmappedSourcePolicy = ReportingPolicy.ERROR) +public abstract class Issue3678Mapper { + + abstract SimpleDestination sourceToDestination(SimpleSource source); + + int afterMappingInvocationCount = 0; + + @AfterMapping + void afterMapping(SimpleSource simpleSource) { + afterMappingInvocationCount++; + } + + public int getAfterMappingInvocationCount() { + return afterMappingInvocationCount; + } + + public static class SimpleSource { + + private final String name; + private final String description; + + public SimpleSource(String name, String description) { + this.name = name; + this.description = description; + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } + } + + public static final class SimpleDestination { + + private final String name; + private final String description; + + private SimpleDestination(String name, String description) { + this.name = name; + this.description = description; + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + + private String name; + private String description; + + public Builder() { + } + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder description(String description) { + this.description = description; + return this; + } + + public SimpleDestination build() { + return new SimpleDestination( this.name, this.description ); + } + } + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/bugs/_3678/Issue3678Test.java b/processor/src/test/java/org/mapstruct/ap/test/bugs/_3678/Issue3678Test.java new file mode 100644 index 000000000..60495ea1b --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/bugs/_3678/Issue3678Test.java @@ -0,0 +1,33 @@ +/* + * Copyright MapStruct Authors. + * + * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ + +package org.mapstruct.ap.test.bugs._3678; + +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.ProcessorTest; +import org.mapstruct.ap.testutil.WithClasses; +import org.mapstruct.factory.Mappers; + +import static org.assertj.core.api.Assertions.assertThat; + +@IssueKey("3678") +public class Issue3678Test { + + @WithClasses({ + Issue3678Mapper.class, + }) + @ProcessorTest + void ignoreMappingsWithoutSourceShouldBeInvertible() { + + Issue3678Mapper mapper = Mappers.getMapper( Issue3678Mapper.class ); + Issue3678Mapper.SimpleSource source = new Issue3678Mapper.SimpleSource( "name", "description" ); + Issue3678Mapper.SimpleDestination simpleDestination = mapper.sourceToDestination( source ); + + assertThat( mapper.getAfterMappingInvocationCount() ) + .isOne(); + } + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/builder/lifecycle/BuilderLifecycleCallbacksTest.java b/processor/src/test/java/org/mapstruct/ap/test/builder/lifecycle/BuilderLifecycleCallbacksTest.java index b60759c11..02bc692d0 100644 --- a/processor/src/test/java/org/mapstruct/ap/test/builder/lifecycle/BuilderLifecycleCallbacksTest.java +++ b/processor/src/test/java/org/mapstruct/ap/test/builder/lifecycle/BuilderLifecycleCallbacksTest.java @@ -7,11 +7,9 @@ package org.mapstruct.ap.test.builder.lifecycle; import java.util.Arrays; -import org.junit.jupiter.api.extension.RegisterExtension; import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.testutil.ProcessorTest; import org.mapstruct.ap.testutil.WithClasses; -import org.mapstruct.ap.testutil.runner.GeneratedSource; import static org.assertj.core.api.Assertions.assertThat; @@ -29,9 +27,6 @@ import static org.assertj.core.api.Assertions.assertThat; } ) public class BuilderLifecycleCallbacksTest { - @RegisterExtension - final GeneratedSource source = new GeneratedSource().addComparisonToFixtureFor( OrderMapper.class ); - @ProcessorTest public void lifecycleMethodsShouldBeInvoked() { OrderDto source = new OrderDto(); diff --git a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/builder/lifecycle/OrderMapperImpl.java b/processor/src/test/resources/fixtures/org/mapstruct/ap/test/builder/lifecycle/OrderMapperImpl.java deleted file mode 100644 index 6221010b2..000000000 --- a/processor/src/test/resources/fixtures/org/mapstruct/ap/test/builder/lifecycle/OrderMapperImpl.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright MapStruct Authors. - * - * Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 - */ -package org.mapstruct.ap.test.builder.lifecycle; - -import java.util.ArrayList; -import java.util.List; -import javax.annotation.processing.Generated; - -@Generated( - value = "org.mapstruct.ap.MappingProcessor", - date = "2024-08-21T23:18:51+0200", - comments = "version: , compiler: javac, environment: Java 21.0.1 (Oracle Corporation)" -) -public class OrderMapperImpl implements OrderMapper { - - @Override - public Order map(OrderDto source, MappingContext context) { - context.beforeWithoutParameters(); - context.beforeWithSource( source ); - context.beforeWithBuilderTargetType( source, Order.Builder.class ); - - context.beforeWithTargetType( source, Order.class ); - - if ( source == null ) { - return null; - } - - Order.Builder order = Order.builder(); - - context.beforeWithBuilderTarget( source, order ); - - order.items( itemDtoListToItemList( source.getItems(), context ) ); - - context.afterWithoutParameters(); - context.afterWithSource( source ); - context.afterWithBuilderTargetType( source, Order.Builder.class ); - context.afterWithBuilderTarget( source, order ); - Order target = context.afterWithBuilderTargetReturningTarget( order ); - if ( target != null ) { - return target; - } - - Order orderResult = order.create(); - - context.afterWithTargetType( source, Order.class ); - context.afterWithTarget( source, orderResult ); - Order target1 = context.afterWithTargetReturningTarget( orderResult ); - if ( target1 != null ) { - return target1; - } - - return orderResult; - } - - @Override - public Item map(ItemDto source, MappingContext context) { - context.beforeWithoutParameters(); - - if ( source == null ) { - return null; - } - - Item.Builder item = Item.builder(); - - item.name( source.getName() ); - - context.afterWithoutParameters(); - - return item.create(); - } - - protected List itemDtoListToItemList(List list, MappingContext context) { - context.beforeWithoutParameters(); - - if ( list == null ) { - return null; - } - - List list1 = new ArrayList( list.size() ); - for ( ItemDto itemDto : list ) { - list1.add( map( itemDto, context ) ); - } - - context.afterWithoutParameters(); - - return list1; - } -}