#3561 Add test case

This commit is contained in:
Filip Hrisafov 2024-04-29 08:13:46 +02:00
parent 0a2a0aa526
commit b33942a010
2 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,60 @@
/*
* 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._3561;
import org.mapstruct.Condition;
import org.mapstruct.Context;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
@Mapper
public interface Issue3561Mapper {
Issue3561Mapper INSTANCE = Mappers.getMapper( Issue3561Mapper.class );
@Mapping(target = "value", conditionQualifiedByName = "shouldMapValue")
Target map(Source source, @Context boolean shouldMapValue);
@Condition
@Named("shouldMapValue")
default boolean shouldMapValue(@Context boolean shouldMapValue) {
return shouldMapValue;
}
class Target {
private final String value;
public Target(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
class Source {
private String value;
private boolean valueInitialized;
public String getValue() {
if ( valueInitialized ) {
return value;
}
throw new IllegalStateException( "value is not initialized" );
}
public void setValue(String value) {
this.valueInitialized = true;
this.value = value;
}
}
}

View File

@ -0,0 +1,35 @@
/*
* 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._3561;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.ProcessorTest;
import org.mapstruct.ap.testutil.WithClasses;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* @author Filip Hrisafov
*/
@WithClasses(Issue3561Mapper.class)
@IssueKey("3561")
class Issue3561Test {
@ProcessorTest
void shouldCorrectlyUseConditionWithContext() {
Issue3561Mapper.Source source = new Issue3561Mapper.Source();
assertThatThrownBy( () -> Issue3561Mapper.INSTANCE.map( source, true ) )
.isInstanceOf( IllegalStateException.class )
.hasMessage( "value is not initialized" );
Issue3561Mapper.Target target = Issue3561Mapper.INSTANCE.map( source, false );
assertThat( target ).isNotNull();
assertThat( target.getValue() ).isNull();
}
}