mirror of
https://github.com/mapstruct/mapstruct.git
synced 2025-07-12 00:00:08 +08:00
Pulling up common logic from tests to MapperTestBase
This commit is contained in:
parent
5c4356b8c2
commit
f6228d8915
@ -13,41 +13,25 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test;
|
||||
package org.mapstruct.ap.test.conversion;
|
||||
|
||||
import java.io.File;
|
||||
import javax.tools.DiagnosticCollector;
|
||||
import javax.tools.JavaFileObject;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.mapstruct.ap.test.conversion.Source;
|
||||
import org.mapstruct.ap.test.conversion.SourceTargetMapper;
|
||||
import org.mapstruct.ap.test.conversion.Target;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.mapstruct.ap.testutil.MapperTestBase;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
public class ConversionTest extends MapperTestBase {
|
||||
|
||||
private DiagnosticCollector<JavaFileObject> diagnostics;
|
||||
|
||||
public ConversionTest() {
|
||||
super( "mapstruct.jar" );
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void generateMapperImplementation() {
|
||||
diagnostics = new DiagnosticCollector<JavaFileObject>();
|
||||
File[] sourceFiles = getSourceFiles(
|
||||
@Override
|
||||
protected List<Class<?>> getTestClasses() {
|
||||
return Arrays.<Class<?>>asList(
|
||||
Source.class,
|
||||
Target.class,
|
||||
SourceTargetMapper.class
|
||||
);
|
||||
|
||||
boolean compilationSuccessful = compile( diagnostics, sourceFiles );
|
||||
|
||||
assertThat( compilationSuccessful ).describedAs( "Compilation failed: " + diagnostics.getDiagnostics() )
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
@ -13,40 +13,23 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test;
|
||||
package org.mapstruct.ap.test.model;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
import javax.tools.DiagnosticCollector;
|
||||
import javax.tools.JavaFileObject;
|
||||
|
||||
import org.mapstruct.ap.test.model.Car;
|
||||
import org.mapstruct.ap.test.model.CarDto;
|
||||
import org.mapstruct.ap.test.model.CarMapper;
|
||||
import org.mapstruct.ap.test.model.Category;
|
||||
import org.mapstruct.ap.test.model.DateMapper;
|
||||
import org.mapstruct.ap.test.model.Person;
|
||||
import org.mapstruct.ap.test.model.PersonDto;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.mapstruct.ap.testutil.MapperTestBase;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
public class CarMapperTest extends MapperTestBase {
|
||||
|
||||
private DiagnosticCollector<JavaFileObject> diagnostics;
|
||||
|
||||
public CarMapperTest() {
|
||||
super( "mapstruct.jar" );
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void generateMapperImplementation() {
|
||||
diagnostics = new DiagnosticCollector<JavaFileObject>();
|
||||
File[] sourceFiles = getSourceFiles(
|
||||
@Override
|
||||
protected List<Class<?>> getTestClasses() {
|
||||
return Arrays.<Class<?>>asList(
|
||||
Car.class,
|
||||
CarDto.class,
|
||||
Person.class,
|
||||
@ -55,11 +38,6 @@ public class CarMapperTest extends MapperTestBase {
|
||||
Category.class,
|
||||
DateMapper.class
|
||||
);
|
||||
|
||||
boolean compilationSuccessful = compile( diagnostics, sourceFiles );
|
||||
|
||||
assertThat( compilationSuccessful ).describedAs( "Compilation failed: " + diagnostics.getDiagnostics() )
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.mapstruct.ap.test;
|
||||
package org.mapstruct.ap.testutil;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
@ -33,7 +33,17 @@ import javax.tools.ToolProvider;
|
||||
|
||||
import org.mapstruct.ap.MappingProcessor;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
|
||||
import static org.fest.assertions.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Base class for all mapper tests. Sub-classes must implement
|
||||
* {@link #getTestClasses()} to return the classes to be compiled for a given
|
||||
* test.
|
||||
*
|
||||
* @author Gunnar Morling
|
||||
*/
|
||||
public abstract class MapperTestBase {
|
||||
|
||||
private JavaCompiler compiler;
|
||||
@ -42,14 +52,14 @@ public abstract class MapperTestBase {
|
||||
private String sourceOutputDir;
|
||||
private List<File> classPath;
|
||||
private List<String> libraries;
|
||||
private DiagnosticCollector<JavaFileObject> diagnostics;
|
||||
|
||||
public MapperTestBase(String... libraries) {
|
||||
this.libraries = Arrays.asList( libraries );
|
||||
public MapperTestBase() {
|
||||
this.libraries = Arrays.asList( "mapstruct.jar" );
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public void setup() throws Exception {
|
||||
|
||||
compiler = ToolProvider.getSystemJavaCompiler();
|
||||
|
||||
String basePath = getBasePath();
|
||||
@ -75,29 +85,45 @@ public abstract class MapperTestBase {
|
||||
);
|
||||
}
|
||||
|
||||
protected File[] getSourceFiles(Class<?>... clazz) {
|
||||
@BeforeMethod
|
||||
public void generateMapperImplementation() {
|
||||
diagnostics = new DiagnosticCollector<JavaFileObject>();
|
||||
List<File> sourceFiles = getSourceFiles( getTestClasses() );
|
||||
|
||||
File[] sourceFiles = new File[clazz.length];
|
||||
boolean compilationSuccessful = compile( diagnostics, sourceFiles );
|
||||
|
||||
for ( int i = 0; i < clazz.length; i++ ) {
|
||||
assertThat( compilationSuccessful ).describedAs( "Compilation failed: " + diagnostics.getDiagnostics() )
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
sourceFiles[i] = new File(
|
||||
sourceDir +
|
||||
File.separator +
|
||||
clazz[i].getName().replace( ".", File.separator ) +
|
||||
".java"
|
||||
/**
|
||||
* Returns the classes to be compiled for this test.
|
||||
*
|
||||
* @return A list containing the classes to be compiled for this test
|
||||
*/
|
||||
protected abstract List<Class<?>> getTestClasses();
|
||||
|
||||
private List<File> getSourceFiles(List<Class<?>> classes) {
|
||||
List<File> sourceFiles = new ArrayList<File>( classes.size() );
|
||||
|
||||
for ( Class<?> clazz : classes ) {
|
||||
sourceFiles.add(
|
||||
new File(
|
||||
sourceDir +
|
||||
File.separator +
|
||||
clazz.getName().replace( ".", File.separator ) +
|
||||
".java"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return sourceFiles;
|
||||
}
|
||||
|
||||
protected boolean compile(DiagnosticCollector<JavaFileObject> diagnostics, File... sourceFiles) {
|
||||
|
||||
private boolean compile(DiagnosticCollector<JavaFileObject> diagnostics, Iterable<File> sourceFiles) {
|
||||
StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null );
|
||||
|
||||
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects( sourceFiles );
|
||||
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles( sourceFiles );
|
||||
|
||||
try {
|
||||
fileManager.setLocation( StandardLocation.CLASS_PATH, classPath );
|
||||
@ -122,7 +148,6 @@ public abstract class MapperTestBase {
|
||||
}
|
||||
|
||||
private String getBasePath() {
|
||||
|
||||
try {
|
||||
return new File( "." ).getCanonicalPath();
|
||||
}
|
||||
@ -132,7 +157,6 @@ public abstract class MapperTestBase {
|
||||
}
|
||||
|
||||
private void createOutputDirs() {
|
||||
|
||||
File directory = new File( classOutputDir );
|
||||
deleteDirectory( directory );
|
||||
directory.mkdirs();
|
||||
@ -156,5 +180,4 @@ public abstract class MapperTestBase {
|
||||
}
|
||||
path.delete();
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user