#664 Add test to verify that jsr330 mappers are annotated with @Singleton and @Named.

This commit is contained in:
Andreas Gudian 2016-02-02 20:18:25 +01:00
parent 5ede0e91db
commit b09f32f926
2 changed files with 31 additions and 6 deletions

View File

@ -18,6 +18,7 @@
*/
package org.mapstruct.ap.test.decorator.jsr330;
import static java.lang.System.lineSeparator;
import static org.fest.assertions.Assertions.assertThat;
import java.util.Calendar;
@ -27,6 +28,7 @@ import javax.inject.Named;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mapstruct.ap.test.decorator.Address;
@ -36,6 +38,7 @@ import org.mapstruct.ap.test.decorator.PersonDto;
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.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
@ -60,11 +63,18 @@ import org.springframework.context.annotation.Configuration;
@Configuration
public class Jsr330DecoratorTest {
private final GeneratedSource generatedSource = new GeneratedSource();
@Inject
@Named
private PersonMapper personMapper;
private ConfigurableApplicationContext context;
@Rule
public GeneratedSource getGeneratedSource() {
return generatedSource;
}
@Before
public void springUp() {
context = new AnnotationConfigApplicationContext( getClass() );
@ -80,15 +90,12 @@ public class Jsr330DecoratorTest {
@Test
public void shouldInvokeDecoratorMethods() {
//given
Calendar birthday = Calendar.getInstance();
birthday.set( 1928, 4, 23 );
Person person = new Person( "Gary", "Crant", birthday.getTime(), new Address( "42 Ocean View Drive" ) );
//when
PersonDto personDto = personMapper.personToPersonDto( person );
//then
assertThat( personDto ).isNotNull();
assertThat( personDto.getName() ).isEqualTo( "Gary Crant" );
assertThat( personDto.getAddress() ).isNotNull();
@ -97,14 +104,22 @@ public class Jsr330DecoratorTest {
@Test
public void shouldDelegateNonDecoratedMethodsToDefaultImplementation() {
//given
Address address = new Address( "42 Ocean View Drive" );
//when
AddressDto addressDto = personMapper.addressToAddressDto( address );
//then
assertThat( addressDto ).isNotNull();
assertThat( addressDto.getAddressLine() ).isEqualTo( "42 Ocean View Drive" );
}
@IssueKey("664")
@Test
public void hasSingletonAnnotation() {
// check the decorator
generatedSource.forMapper( PersonMapper.class ).content()
.contains( "@Singleton" + lineSeparator() + "@Named" );
// check the plain mapper
generatedSource.forDecoratedMapper( PersonMapper.class ).content()
.contains( "@Singleton" + lineSeparator() + "@Named" );
}
}

View File

@ -68,6 +68,16 @@ public class GeneratedSource implements TestRule {
return forJavaFile( generatedJavaFileName );
}
/**
* @param mapperClass the class annotated with {@code @Mapper} and {@code @DecoratedWith(..)}
*
* @return an assert for the *Impl_.java for the given mapper
*/
public JavaFileAssert forDecoratedMapper(Class<?> mapperClass) {
String generatedJavaFileName = mapperClass.getName().replace( '.', '/' ).concat( "Impl_.java" );
return forJavaFile( generatedJavaFileName );
}
/**
* @param path the path relative to the source output directory of the java file to return an assert for
*