Skip to content

Commit

Permalink
mapstruct#1742 API and UnitTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sjaakd committed May 10, 2019
1 parent 6990b69 commit c1a0c50
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/src/main/java/org/mapstruct/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@
* @return the method that needs to tbe invoked on the builder
*/
String buildMethod() default "build";

/**
* Toggling builders on / off. Builders are sometimes used solely for unit testing (fluent testdata)
* MapStruct will need to use the regular getters /setters in that case.
*
* @return when true, no builder patterns will be applied
*/
boolean noBuilder() default false;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* 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.off;

import org.mapstruct.BeanMapping;
import org.mapstruct.Builder;
import org.mapstruct.CollectionMappingStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;

@Mapper(collectionMappingStrategy = CollectionMappingStrategy.ADDER_PREFERRED)
public interface SimpleMapper {

@BeanMapping( builder = @Builder( noBuilder = true ) )
@Mapping(target = "name", source = "fullName")
SimpleNotRealyImmutablePerson toNotRealyImmutable(SimpleMutablePerson source);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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.off;

import java.util.List;

public class SimpleMutablePerson {
private String fullName;

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.off;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.testutil.IssueKey;
import org.mapstruct.ap.testutil.WithClasses;
import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner;
import org.mapstruct.ap.testutil.runner.GeneratedSource;
import org.mapstruct.factory.Mappers;

import static org.assertj.core.api.Assertions.assertThat;

@IssueKey("1743")
@WithClasses({
SimpleMutablePerson.class,
SimpleNotRealyImmutablePerson.class
})
@RunWith(AnnotationProcessorTestRunner.class)
public class SimpleNotRealyImmutableBuilderTest {

@Rule
public final GeneratedSource generatedSource = new GeneratedSource();

@Test
@WithClasses({ SimpleMapper.class })
public void testSimpleImmutableBuilderHappyPath() {
SimpleMapper mapper = Mappers.getMapper( SimpleMapper.class );
SimpleMutablePerson source = new SimpleMutablePerson();
source.setFullName( "Bob" );

SimpleNotRealyImmutablePerson targetObject = mapper.toNotRealyImmutable( source );

assertThat( targetObject.getName() ).isEqualTo( "Bob" );

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.off;

public class SimpleNotRealyImmutablePerson {

private String name;

public static Builder builder() {
return new Builder();
}

public SimpleNotRealyImmutablePerson() {
}

SimpleNotRealyImmutablePerson(Builder builder) {
this.name = builder.name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public static class Builder {

private String name;

public Builder name(String name) {
throw new IllegalStateException( "name should not be called on builder" );
}

public SimpleNotRealyImmutablePerson build() {
return new SimpleNotRealyImmutablePerson( this );
}

}
}

0 comments on commit c1a0c50

Please sign in to comment.