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
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test;
|
package org.mapstruct.ap.test.conversion;
|
||||||
|
|
||||||
import java.io.File;
|
import java.util.Arrays;
|
||||||
import javax.tools.DiagnosticCollector;
|
import java.util.List;
|
||||||
import javax.tools.JavaFileObject;
|
|
||||||
|
|
||||||
import org.mapstruct.ap.test.conversion.Source;
|
import org.mapstruct.ap.testutil.MapperTestBase;
|
||||||
import org.mapstruct.ap.test.conversion.SourceTargetMapper;
|
|
||||||
import org.mapstruct.ap.test.conversion.Target;
|
|
||||||
import org.testng.annotations.BeforeMethod;
|
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import static org.fest.assertions.Assertions.assertThat;
|
import static org.fest.assertions.Assertions.assertThat;
|
||||||
|
|
||||||
public class ConversionTest extends MapperTestBase {
|
public class ConversionTest extends MapperTestBase {
|
||||||
|
|
||||||
private DiagnosticCollector<JavaFileObject> diagnostics;
|
@Override
|
||||||
|
protected List<Class<?>> getTestClasses() {
|
||||||
public ConversionTest() {
|
return Arrays.<Class<?>>asList(
|
||||||
super( "mapstruct.jar" );
|
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeMethod
|
|
||||||
public void generateMapperImplementation() {
|
|
||||||
diagnostics = new DiagnosticCollector<JavaFileObject>();
|
|
||||||
File[] sourceFiles = getSourceFiles(
|
|
||||||
Source.class,
|
Source.class,
|
||||||
Target.class,
|
Target.class,
|
||||||
SourceTargetMapper.class
|
SourceTargetMapper.class
|
||||||
);
|
);
|
||||||
|
|
||||||
boolean compilationSuccessful = compile( diagnostics, sourceFiles );
|
|
||||||
|
|
||||||
assertThat( compilationSuccessful ).describedAs( "Compilation failed: " + diagnostics.getDiagnostics() )
|
|
||||||
.isTrue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
@ -13,40 +13,23 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* 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.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.tools.DiagnosticCollector;
|
|
||||||
import javax.tools.JavaFileObject;
|
|
||||||
|
|
||||||
import org.mapstruct.ap.test.model.Car;
|
import org.mapstruct.ap.testutil.MapperTestBase;
|
||||||
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.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import static org.fest.assertions.Assertions.assertThat;
|
import static org.fest.assertions.Assertions.assertThat;
|
||||||
|
|
||||||
public class CarMapperTest extends MapperTestBase {
|
public class CarMapperTest extends MapperTestBase {
|
||||||
|
|
||||||
private DiagnosticCollector<JavaFileObject> diagnostics;
|
@Override
|
||||||
|
protected List<Class<?>> getTestClasses() {
|
||||||
public CarMapperTest() {
|
return Arrays.<Class<?>>asList(
|
||||||
super( "mapstruct.jar" );
|
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeMethod
|
|
||||||
public void generateMapperImplementation() {
|
|
||||||
diagnostics = new DiagnosticCollector<JavaFileObject>();
|
|
||||||
File[] sourceFiles = getSourceFiles(
|
|
||||||
Car.class,
|
Car.class,
|
||||||
CarDto.class,
|
CarDto.class,
|
||||||
Person.class,
|
Person.class,
|
||||||
@ -55,11 +38,6 @@ public class CarMapperTest extends MapperTestBase {
|
|||||||
Category.class,
|
Category.class,
|
||||||
DateMapper.class
|
DateMapper.class
|
||||||
);
|
);
|
||||||
|
|
||||||
boolean compilationSuccessful = compile( diagnostics, sourceFiles );
|
|
||||||
|
|
||||||
assertThat( compilationSuccessful ).describedAs( "Compilation failed: " + diagnostics.getDiagnostics() )
|
|
||||||
.isTrue();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
@ -13,7 +13,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
package org.mapstruct.ap.test;
|
package org.mapstruct.ap.testutil;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -33,7 +33,17 @@ import javax.tools.ToolProvider;
|
|||||||
|
|
||||||
import org.mapstruct.ap.MappingProcessor;
|
import org.mapstruct.ap.MappingProcessor;
|
||||||
import org.testng.annotations.BeforeClass;
|
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 {
|
public abstract class MapperTestBase {
|
||||||
|
|
||||||
private JavaCompiler compiler;
|
private JavaCompiler compiler;
|
||||||
@ -42,14 +52,14 @@ public abstract class MapperTestBase {
|
|||||||
private String sourceOutputDir;
|
private String sourceOutputDir;
|
||||||
private List<File> classPath;
|
private List<File> classPath;
|
||||||
private List<String> libraries;
|
private List<String> libraries;
|
||||||
|
private DiagnosticCollector<JavaFileObject> diagnostics;
|
||||||
|
|
||||||
public MapperTestBase(String... libraries) {
|
public MapperTestBase() {
|
||||||
this.libraries = Arrays.asList( libraries );
|
this.libraries = Arrays.asList( "mapstruct.jar" );
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public void setup() throws Exception {
|
public void setup() throws Exception {
|
||||||
|
|
||||||
compiler = ToolProvider.getSystemJavaCompiler();
|
compiler = ToolProvider.getSystemJavaCompiler();
|
||||||
|
|
||||||
String basePath = getBasePath();
|
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 +
|
* Returns the classes to be compiled for this test.
|
||||||
File.separator +
|
*
|
||||||
clazz[i].getName().replace( ".", File.separator ) +
|
* @return A list containing the classes to be compiled for this test
|
||||||
".java"
|
*/
|
||||||
|
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;
|
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 );
|
StandardJavaFileManager fileManager = compiler.getStandardFileManager( null, null, null );
|
||||||
|
|
||||||
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjects( sourceFiles );
|
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles( sourceFiles );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fileManager.setLocation( StandardLocation.CLASS_PATH, classPath );
|
fileManager.setLocation( StandardLocation.CLASS_PATH, classPath );
|
||||||
@ -122,7 +148,6 @@ public abstract class MapperTestBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getBasePath() {
|
private String getBasePath() {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return new File( "." ).getCanonicalPath();
|
return new File( "." ).getCanonicalPath();
|
||||||
}
|
}
|
||||||
@ -132,7 +157,6 @@ public abstract class MapperTestBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void createOutputDirs() {
|
private void createOutputDirs() {
|
||||||
|
|
||||||
File directory = new File( classOutputDir );
|
File directory = new File( classOutputDir );
|
||||||
deleteDirectory( directory );
|
deleteDirectory( directory );
|
||||||
directory.mkdirs();
|
directory.mkdirs();
|
||||||
@ -156,5 +180,4 @@ public abstract class MapperTestBase {
|
|||||||
}
|
}
|
||||||
path.delete();
|
path.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user