#35 Allowing to specify processor options for processor tests

This commit is contained in:
Gunnar Morling 2013-06-09 23:38:45 +02:00
parent 76c3aebd5d
commit 04beaacbe1
2 changed files with 86 additions and 6 deletions

View File

@ -40,6 +40,7 @@ import javax.tools.ToolProvider;
import org.mapstruct.ap.MappingProcessor; import org.mapstruct.ap.MappingProcessor;
import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult; import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult;
import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome; import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome;
import org.mapstruct.ap.testutil.compilation.annotation.ProcessorOption;
import org.mapstruct.ap.testutil.compilation.model.CompilationOutcomeDescriptor; import org.mapstruct.ap.testutil.compilation.model.CompilationOutcomeDescriptor;
import org.mapstruct.ap.testutil.compilation.model.DiagnosticDescriptor; import org.mapstruct.ap.testutil.compilation.model.DiagnosticDescriptor;
import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeClass;
@ -51,9 +52,15 @@ import static org.fest.assertions.Assertions.assertThat;
* Base class for all mapper tests. * Base class for all mapper tests.
* </p> * </p>
* The classes to be compiled for a given test method must be specified via * The classes to be compiled for a given test method must be specified via
* {@link WithClasses}. Optionally the expected compilation outcome and expected * {@link WithClasses}. In addition the following things can be configured
* diagnostics can be specified via {@link ExpectedCompilationOutcome}. If no * optionally :
* outcome is specified, a successful compilation is assumed. * <ul>
* <li>Processor options to be considered during compilation via
* {@link ProcessorOption}.</li>
* <li>The expected compilation outcome and expected diagnostics can be
* specified via {@link ExpectedCompilationOutcome}. If no outcome is specified,
* a successful compilation is assumed.</li>
* </ul>
* *
* @author Gunnar Morling * @author Gunnar Morling
*/ */
@ -104,8 +111,9 @@ public abstract class MapperTestBase {
public void generateMapperImplementation(Method testMethod) { public void generateMapperImplementation(Method testMethod) {
diagnostics = new DiagnosticCollector<JavaFileObject>(); diagnostics = new DiagnosticCollector<JavaFileObject>();
List<File> sourceFiles = getSourceFiles( getTestClasses( testMethod ) ); List<File> sourceFiles = getSourceFiles( getTestClasses( testMethod ) );
List<String> processorOptions = getProcessorOptions( testMethod );
boolean compilationSuccessful = compile( diagnostics, sourceFiles ); boolean compilationSuccessful = compile( diagnostics, sourceFiles, processorOptions );
CompilationOutcomeDescriptor actualResult = CompilationOutcomeDescriptor.forResult( CompilationOutcomeDescriptor actualResult = CompilationOutcomeDescriptor.forResult(
sourceDir, sourceDir,
@ -196,6 +204,28 @@ public abstract class MapperTestBase {
return Arrays.asList( withClasses.value() ); return Arrays.asList( withClasses.value() );
} }
/**
* Returns the processor options to be used this test.
*
* @param testMethod The test method of interest
*
* @return A list containing the processor options to be used for this test
*/
private List<String> getProcessorOptions(Method testMethod) {
ProcessorOption processorOption = testMethod.getAnnotation( ProcessorOption.class );
if ( processorOption == null ) {
processorOption = this.getClass().getAnnotation( ProcessorOption.class );
}
return processorOption != null ? Arrays.asList( asOptionString( processorOption ) ) :
Collections.<String>emptyList();
}
private String asOptionString(ProcessorOption processorOption) {
return String.format( "-A%s=%s", processorOption.name(), processorOption.value() );
}
private List<File> getSourceFiles(List<Class<?>> classes) { private List<File> getSourceFiles(List<Class<?>> classes) {
List<File> sourceFiles = new ArrayList<File>( classes.size() ); List<File> sourceFiles = new ArrayList<File>( classes.size() );
@ -213,7 +243,8 @@ public abstract class MapperTestBase {
return sourceFiles; return sourceFiles;
} }
private boolean compile(DiagnosticCollector<JavaFileObject> diagnostics, Iterable<File> sourceFiles) { private boolean compile(DiagnosticCollector<JavaFileObject> diagnostics, Iterable<File> sourceFiles,
List<String> processorOptions) {
StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null ); StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null );
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles( sourceFiles ); Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles( sourceFiles );
@ -231,7 +262,7 @@ public abstract class MapperTestBase {
null, null,
fileManager, fileManager,
diagnostics, diagnostics,
Collections.<String>emptyList(), processorOptions,
null, null,
compilationUnits compilationUnits
); );

View File

@ -0,0 +1,49 @@
/**
* Copyright 2012-2013 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.testutil.compilation.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* An annotation processor option to be used for a test. Will be passed in the
* form {@code -Aname=value} to the compiler.
*
* @author Gunnar Morling
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface ProcessorOption {
/**
* The name of the processor option.
*
* @return The name of the processor option.
*/
String name();
/**
* The value of the processor option.
*
* @return The value of the processor option.
*/
String value();
}