#3678 write a separate test

This commit is contained in:
thunderhook 2024-08-22 22:10:10 +02:00
parent b74bde5c22
commit b5b94bde46
4 changed files with 126 additions and 96 deletions

View File

@ -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 );
}
}
}
}

View File

@ -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();
}
}

View File

@ -7,11 +7,9 @@ package org.mapstruct.ap.test.builder.lifecycle;
import java.util.Arrays; import java.util.Arrays;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mapstruct.ap.testutil.IssueKey; import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest; import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses; import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.GeneratedSource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -29,9 +27,6 @@ import static org.assertj.core.api.Assertions.assertThat;
} ) } )
public class BuilderLifecycleCallbacksTest { public class BuilderLifecycleCallbacksTest {
@RegisterExtension
final GeneratedSource source = new GeneratedSource().addComparisonToFixtureFor( OrderMapper.class );
@ProcessorTest @ProcessorTest
public void lifecycleMethodsShouldBeInvoked() { public void lifecycleMethodsShouldBeInvoked() {
OrderDto source = new OrderDto(); OrderDto source = new OrderDto();

View File

@ -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<Item> itemDtoListToItemList(List<ItemDto> list, MappingContext context) {
context.beforeWithoutParameters();
if ( list == null ) {
return null;
}
List<Item> list1 = new ArrayList<Item>( list.size() );
for ( ItemDto itemDto : list ) {
list1.add( map( itemDto, context ) );
}
context.afterWithoutParameters();
return list1;
}
}