#782 Adding negative test for @MappingTarget with immutable classes

This commit is contained in:
sjaakd 2018-03-18 13:12:14 +01:00 committed by Filip Hrisafov
parent 73711cc683
commit 6291631af7
9 changed files with 28 additions and 31 deletions

View File

@ -403,6 +403,11 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
return false; return false;
} }
if ( targetParameter != null && targetParameter.getType().getBuilderType() != null ) {
messager.printMessage( method, Message.RETRIEVAL_IMMUTABLE_TARGET );
return false;
}
if ( isVoid( resultType ) ) { if ( isVoid( resultType ) ) {
messager.printMessage( method, Message.RETRIEVAL_VOID_MAPPING_METHOD ); messager.printMessage( method, Message.RETRIEVAL_VOID_MAPPING_METHOD );
return false; return false;

View File

@ -100,6 +100,7 @@ public enum Message {
RETRIEVAL_NO_INPUT_ARGS( "Can't generate mapping method with no input arguments." ), RETRIEVAL_NO_INPUT_ARGS( "Can't generate mapping method with no input arguments." ),
RETRIEVAL_DUPLICATE_MAPPING_TARGETS( "Can't generate mapping method with more than one @MappingTarget parameter." ), RETRIEVAL_DUPLICATE_MAPPING_TARGETS( "Can't generate mapping method with more than one @MappingTarget parameter." ),
RETRIEVAL_IMMUTABLE_TARGET( "Can't generate mapping method when @MappingTarget is supposed to be immutable (has a builder)." ),
RETRIEVAL_VOID_MAPPING_METHOD( "Can't generate mapping method with return type void." ), RETRIEVAL_VOID_MAPPING_METHOD( "Can't generate mapping method with return type void." ),
RETRIEVAL_NON_ASSIGNABLE_RESULTTYPE( "The result type is not assignable to the the return type." ), RETRIEVAL_NON_ASSIGNABLE_RESULTTYPE( "The result type is not assignable to the the return type." ),
RETRIEVAL_ITERABLE_TO_NON_ITERABLE( "Can't generate mapping method from iterable type to non-iterable type." ), RETRIEVAL_ITERABLE_TO_NON_ITERABLE( "Can't generate mapping method from iterable type to non-iterable type." ),

View File

@ -30,7 +30,6 @@ import static org.assertj.core.api.Assertions.assertThat;
* builder class, and some of the properties are written by the concrete builder implementation. * builder class, and some of the properties are written by the concrete builder implementation.
*/ */
@WithClasses({ @WithClasses({
Product.class,
AbstractProductBuilder.class, AbstractProductBuilder.class,
AbstractImmutableProduct.class, AbstractImmutableProduct.class,
ImmutableProduct.class, ImmutableProduct.class,

View File

@ -21,7 +21,7 @@ package org.mapstruct.ap.test.builder.abstractBuilder;
/** /**
* @author Filip Hrisafov * @author Filip Hrisafov
*/ */
public abstract class AbstractImmutableProduct implements Product { public abstract class AbstractImmutableProduct {
private final String name; private final String name;

View File

@ -18,7 +18,7 @@
*/ */
package org.mapstruct.ap.test.builder.abstractBuilder; package org.mapstruct.ap.test.builder.abstractBuilder;
public abstract class AbstractProductBuilder<T extends Product> { public abstract class AbstractProductBuilder<T extends AbstractImmutableProduct> {
protected String name; protected String name;

View File

@ -31,7 +31,6 @@ public class ImmutableProduct extends AbstractImmutableProduct {
return new ImmutableProductBuilder(); return new ImmutableProductBuilder();
} }
@Override
public Integer getPrice() { public Integer getPrice() {
return price; return price;
} }

View File

@ -1,25 +0,0 @@
/**
* Copyright 2012-2017 Gunnar Morling (http://www.gunnarmorling.de/)
* and/or other contributors as indicated by the @authors tag. See the
* copyright.txt file in the distribution for a full listing of all
* contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mapstruct.ap.test.builder.abstractBuilder;
public interface Product {
String getName();
Integer getPrice();
}

View File

@ -26,6 +26,9 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import org.mapstruct.ap.testutil.runner.GeneratedSource; import org.mapstruct.ap.testutil.runner.GeneratedSource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic;
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
@WithClasses({ @WithClasses({
SimpleMutableSource.class, SimpleMutableSource.class,
@ -50,4 +53,18 @@ public class BuilderInfoTargetTest {
assertThat( targetObject.getAge() ).isEqualTo( 3 ); assertThat( targetObject.getAge() ).isEqualTo( 3 );
assertThat( targetObject.getName() ).isEqualTo( "Bob" ); assertThat( targetObject.getName() ).isEqualTo( "Bob" );
} }
@WithClasses(ErroneousSimpleBuilderMapper.class)
@Test
@ExpectedCompilationOutcome(value = CompilationResult.FAILED,
diagnostics = {
@Diagnostic(type = ErroneousSimpleBuilderMapper.class,
kind = javax.tools.Diagnostic.Kind.ERROR,
line = 26,
messageRegExp = "^Can't generate mapping method when @MappingTarget is supposed to be immutable "
+ "\\(has a builder\\)\\.$")
})
public void shouldFailCannotModifyImmutable() {
}
} }

View File

@ -19,8 +19,9 @@
package org.mapstruct.ap.test.builder.mappingTarget.simple; package org.mapstruct.ap.test.builder.mappingTarget.simple;
import org.mapstruct.Mapper; import org.mapstruct.Mapper;
import org.mapstruct.MappingTarget;
@Mapper @Mapper
public interface InvalidSimpleBuilderMapper { public interface ErroneousSimpleBuilderMapper {
SimpleImmutableTarget toImmutable(SimpleMutableSource source); void toImmutable(SimpleMutableSource source, @MappingTarget SimpleImmutableTarget target);
} }