Skip to content

Commit f3fc2cf

Browse files
committed
Add tests for verifying the contents of the generated gem with the expected content
1 parent bd3a48c commit f3fc2cf

7 files changed

Lines changed: 1272 additions & 3 deletions

File tree

parent/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<com.puppycrawl.tools.checkstyle.version>8.29</com.puppycrawl.tools.checkstyle.version>
2828
<add.release.arguments />
2929
<forkCount>1</forkCount>
30-
<assertj.version>3.11.1</assertj.version>
30+
<assertj.version>3.17.2</assertj.version>
3131
</properties>
3232

3333
<licenses>
@@ -286,7 +286,7 @@
286286
<configuration>
287287
<header>${basedir}/../etc/license.txt</header>
288288
<strictCheck>true</strictCheck>
289-
<excludes>
289+
<excludes combine.children="append">
290290
<exclude>**/.idea/**</exclude>
291291
<exclude>**/.mvn/**</exclude>
292292
<exclude>**/etc/checkstyle.xml</exclude>

processor/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@
6161

6262
<build>
6363
<plugins>
64+
<plugin>
65+
<groupId>com.mycila.maven-license-plugin</groupId>
66+
<artifactId>maven-license-plugin</artifactId>
67+
<configuration>
68+
<excludes>
69+
<exclude>**/src/test/resources/fixtures/**/*.java</exclude>
70+
</excludes>
71+
</configuration>
72+
</plugin>
6473
<plugin>
6574
<groupId>org.apache.maven.plugins</groupId>
6675
<artifactId>maven-jar-plugin</artifactId>

processor/src/test/java/org/mapstruct/tools/gem/processor/ProcessorTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@
55
*/
66
package org.mapstruct.tools.gem.processor;
77

8+
import java.io.File;
9+
import java.io.FileInputStream;
810
import java.io.IOException;
11+
import java.io.InputStream;
12+
import java.io.UncheckedIOException;
913
import java.net.URI;
14+
import java.nio.file.Path;
1015
import java.util.Arrays;
16+
import java.util.Collections;
1117
import javax.annotation.processing.Processor;
1218
import javax.tools.Diagnostic;
1319
import javax.tools.DiagnosticCollector;
@@ -18,6 +24,7 @@
1824
import javax.tools.StandardLocation;
1925
import javax.tools.ToolProvider;
2026

27+
import org.assertj.core.api.JUnitSoftAssertions;
2128
import org.junit.Rule;
2229
import org.junit.Test;
2330
import org.junit.rules.TemporaryFolder;
@@ -29,6 +36,9 @@ public class ProcessorTest {
2936
// CHECKSTYLE:OFF
3037
@Rule
3138
public final TemporaryFolder tempDir = new TemporaryFolder(); //NOSONAR
39+
40+
@Rule
41+
public final JUnitSoftAssertions softly = new JUnitSoftAssertions();
3242
// CHECKSTYLE:ON
3343

3444
@Test
@@ -45,7 +55,10 @@ private void compile(Processor processor, JavaFileObject... compilationUnits) th
4555

4656
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
4757
StandardJavaFileManager fileManager = compiler.getStandardFileManager( diagnostics, null, null );
48-
fileManager.setLocation( StandardLocation.CLASS_OUTPUT, Arrays.asList( tempDir.getRoot() ) );
58+
File classesDir = tempDir.newFolder( "classes" );
59+
fileManager.setLocation( StandardLocation.CLASS_OUTPUT, Collections.singletonList( classesDir ) );
60+
File generatedDir = tempDir.newFolder( "generated" );
61+
fileManager.setLocation( StandardLocation.SOURCE_OUTPUT, Collections.singletonList( generatedDir ) );
4962

5063
JavaCompiler.CompilationTask task = compiler.getTask(
5164
null,
@@ -65,6 +78,32 @@ private void compile(Processor processor, JavaFileObject... compilationUnits) th
6578
System.err.println( diagnostic );
6679
}
6780
assertThat( success ).isTrue();
81+
82+
83+
assertGeneratedFileContent( "BuilderGem", generatedDir );
84+
assertGeneratedFileContent( "SomeAnnotationGem", generatedDir );
85+
assertGeneratedFileContent( "SomeAnnotationsGem", generatedDir );
86+
assertGeneratedFileContent( "SomeArrayAnnotationGem", generatedDir );
87+
}
88+
89+
protected void assertGeneratedFileContent(String gemName, File generatedDir) {
90+
Path gemPath = generatedDir.toPath().resolve( "org/mapstruct/tools/gem/processor/" + gemName + ".java" );
91+
softly.assertThat( gemPath )
92+
.as( gemName )
93+
.exists();
94+
95+
try (InputStream generatedGemStream = new FileInputStream( gemPath.toFile() );
96+
InputStream expectedGemStream = getClass().getClassLoader()
97+
.getResourceAsStream( "fixtures/org/mapstruct/tools/gem/processor/" + gemName + ".java" )
98+
) {
99+
softly.assertThat( generatedGemStream )
100+
.as( gemName )
101+
.hasSameContentAs( expectedGemStream );
102+
}
103+
catch ( IOException e ) {
104+
throw new UncheckedIOException( "Failed to assert generated content for gem " + gemName, e );
105+
}
106+
68107
}
69108

70109
private static class StringJavaFileObject extends SimpleJavaFileObject {
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package org.mapstruct.tools.gem.processor;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import javax.lang.model.element.AnnotationMirror;
9+
import javax.lang.model.element.AnnotationValue;
10+
import javax.lang.model.element.Element;
11+
import javax.lang.model.element.ElementKind;
12+
import javax.lang.model.element.ExecutableElement;
13+
import javax.lang.model.element.TypeElement;
14+
import javax.lang.model.element.VariableElement;
15+
import javax.lang.model.type.TypeMirror;
16+
import javax.lang.model.util.AbstractAnnotationValueVisitor8;
17+
import javax.lang.model.util.ElementFilter;
18+
import org.mapstruct.tools.gem.Gem;
19+
import org.mapstruct.tools.gem.GemValue;
20+
21+
22+
public class BuilderGem implements Gem {
23+
24+
private final boolean isValid;
25+
private final AnnotationMirror mirror;
26+
27+
private BuilderGem( BuilderImpl builder ) {
28+
isValid = true;
29+
mirror = builder.mirror;
30+
}
31+
32+
@Override
33+
public AnnotationMirror mirror( ) {
34+
return mirror;
35+
}
36+
37+
@Override
38+
public boolean isValid( ) {
39+
return isValid;
40+
}
41+
42+
public static BuilderGem instanceOn(Element element) {
43+
return build( element, new BuilderImpl() );
44+
}
45+
46+
public static BuilderGem instanceOn(AnnotationMirror mirror ) {
47+
return build( mirror, new BuilderImpl() );
48+
}
49+
50+
public static <T> T build(Element element, Builder_<T> builder) {
51+
AnnotationMirror mirror = element.getAnnotationMirrors().stream()
52+
.filter( a -> "org.mapstruct.tools.gem.test.Builder".contentEquals( ( ( TypeElement )a.getAnnotationType().asElement() ).getQualifiedName() ) )
53+
.findAny()
54+
.orElse( null );
55+
return build( mirror, builder );
56+
}
57+
58+
public static <T> T build(AnnotationMirror mirror, Builder_<T> builder ) {
59+
60+
// return fast
61+
if ( mirror == null || builder == null ) {
62+
return null;
63+
}
64+
builder.setMirror( mirror );
65+
return builder.build();
66+
}
67+
68+
/**
69+
* A builder that can be implemented by the user to define custom logic e.g. in the
70+
* build method, prior to creating the annotation gem.
71+
*/
72+
public interface Builder_<T> {
73+
74+
/**
75+
* Sets the annotation mirror
76+
*
77+
* @param mirror the mirror which this gem represents
78+
*
79+
* @return the {@link Builder_} for this gem, representing {@link BuilderGem}
80+
*/
81+
Builder_ setMirror( AnnotationMirror mirror );
82+
83+
/**
84+
* The build method can be overriden in a custom custom implementation, which allows
85+
* the user to define his own custom validation on the annotation.
86+
*
87+
* @return the representation of the annotation
88+
*/
89+
T build();
90+
}
91+
92+
private static class BuilderImpl implements Builder_<BuilderGem> {
93+
94+
private AnnotationMirror mirror;
95+
96+
public Builder_ setMirror( AnnotationMirror mirror ) {
97+
this.mirror = mirror;
98+
return this;
99+
}
100+
101+
public BuilderGem build() {
102+
return new BuilderGem( this );
103+
}
104+
}
105+
106+
}

0 commit comments

Comments
 (0)