#35 Making @WithClasses usable on the method level

This commit is contained in:
Gunnar Morling 2013-06-08 20:44:53 +02:00
parent 826a1b8d64
commit e53adbd817
3 changed files with 32 additions and 7 deletions

View File

@ -29,5 +29,11 @@ import java.lang.annotation.RetentionPolicy;
*/ */
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface IssueKey { public @interface IssueKey {
/**
* The issue number.
*
* @return the issue number
*/
String value(); String value();
} }

View File

@ -45,9 +45,8 @@ import org.testng.annotations.BeforeMethod;
import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Assertions.assertThat;
/** /**
* Base class for all mapper tests. Sub-classes must implement * Base class for all mapper tests. The classes to be compiled for a given test
* {@link #getTestClasses()} to return the classes to be compiled for a given * method must be specified via {@link WithClasses}.
* test.
* *
* @author Gunnar Morling * @author Gunnar Morling
*/ */
@ -95,7 +94,7 @@ public abstract class MapperTestBase {
@BeforeMethod @BeforeMethod
public void generateMapperImplementation(Method testMethod) { public void generateMapperImplementation(Method testMethod) {
diagnostics = new DiagnosticCollector<JavaFileObject>(); diagnostics = new DiagnosticCollector<JavaFileObject>();
List<File> sourceFiles = getSourceFiles( getTestClasses() ); List<File> sourceFiles = getSourceFiles( getTestClasses( testMethod ) );
boolean compilationSuccessful = compile( diagnostics, sourceFiles ); boolean compilationSuccessful = compile( diagnostics, sourceFiles );
@ -125,11 +124,24 @@ public abstract class MapperTestBase {
/** /**
* Returns the classes to be compiled for this test. * Returns the classes to be compiled for this test.
* *
* @param testMethod The test method of interest
*
* @return A list containing the classes to be compiled for this test * @return A list containing the classes to be compiled for this test
*/ */
private List<Class<?>> getTestClasses() { private List<Class<?>> getTestClasses(Method testMethod) {
WithClasses withClasses = this.getClass().getAnnotation( WithClasses.class ); WithClasses withClasses = testMethod.getAnnotation( WithClasses.class );
return withClasses != null ? Arrays.asList( withClasses.value() ) : Collections.<Class<?>>emptyList();
if ( withClasses == null ) {
withClasses = this.getClass().getAnnotation( WithClasses.class );
}
if ( withClasses == null || withClasses.value().length == 0 ) {
throw new IllegalStateException(
"The classes to be compiled during the test must be specified via @WithClasses."
);
}
return Arrays.asList( withClasses.value() );
} }
private List<File> getSourceFiles(List<Class<?>> classes) { private List<File> getSourceFiles(List<Class<?>> classes) {

View File

@ -18,13 +18,20 @@
*/ */
package org.mapstruct.ap.testutil; package org.mapstruct.ap.testutil;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** /**
* Specifies the classes to compile during an annotation processor test. If
* given both on the class-level and the method-level for a given test, the
* settings on the method take precedence.
*
* @author Gunnar Morling * @author Gunnar Morling
*/ */
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface WithClasses { public @interface WithClasses {
Class<?>[] value(); Class<?>[] value();
} }