mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
#782 Adding negative test for @MappingTarget with immutable classes
This commit is contained in:
parent
73711cc683
commit
6291631af7
@ -403,6 +403,11 @@ public class MethodRetrievalProcessor implements ModelElementProcessor<Void, Lis
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( targetParameter != null && targetParameter.getType().getBuilderType() != null ) {
|
||||
messager.printMessage( method, Message.RETRIEVAL_IMMUTABLE_TARGET );
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( isVoid( resultType ) ) {
|
||||
messager.printMessage( method, Message.RETRIEVAL_VOID_MAPPING_METHOD );
|
||||
return false;
|
||||
|
@ -100,6 +100,7 @@ public enum Message {
|
||||
|
||||
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_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_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." ),
|
||||
|
@ -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.
|
||||
*/
|
||||
@WithClasses({
|
||||
Product.class,
|
||||
AbstractProductBuilder.class,
|
||||
AbstractImmutableProduct.class,
|
||||
ImmutableProduct.class,
|
||||
|
@ -21,7 +21,7 @@ package org.mapstruct.ap.test.builder.abstractBuilder;
|
||||
/**
|
||||
* @author Filip Hrisafov
|
||||
*/
|
||||
public abstract class AbstractImmutableProduct implements Product {
|
||||
public abstract class AbstractImmutableProduct {
|
||||
|
||||
private final String name;
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
||||
*/
|
||||
package org.mapstruct.ap.test.builder.abstractBuilder;
|
||||
|
||||
public abstract class AbstractProductBuilder<T extends Product> {
|
||||
public abstract class AbstractProductBuilder<T extends AbstractImmutableProduct> {
|
||||
|
||||
protected String name;
|
||||
|
||||
|
@ -31,7 +31,6 @@ public class ImmutableProduct extends AbstractImmutableProduct {
|
||||
return new ImmutableProductBuilder();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
@ -26,6 +26,9 @@ import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
|
||||
import org.mapstruct.ap.testutil.runner.GeneratedSource;
|
||||
|
||||
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({
|
||||
SimpleMutableSource.class,
|
||||
@ -50,4 +53,18 @@ public class BuilderInfoTargetTest {
|
||||
assertThat( targetObject.getAge() ).isEqualTo( 3 );
|
||||
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() {
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -19,8 +19,9 @@
|
||||
package org.mapstruct.ap.test.builder.mappingTarget.simple;
|
||||
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.MappingTarget;
|
||||
|
||||
@Mapper
|
||||
public interface InvalidSimpleBuilderMapper {
|
||||
SimpleImmutableTarget toImmutable(SimpleMutableSource source);
|
||||
public interface ErroneousSimpleBuilderMapper {
|
||||
void toImmutable(SimpleMutableSource source, @MappingTarget SimpleImmutableTarget target);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user