Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
Verify compile-testing succeeds without warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <bluhmdj@ornl.gov>
  • Loading branch information
dbluhm committed Aug 3, 2020
1 parent f55eb3c commit 3793759
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Expand Up @@ -13,22 +13,34 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Locale;

import javax.annotation.processing.Processor;
import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;

import org.eclipse.ice.dev.annotations.processors.DataElementProcessor;

import com.google.testing.compile.Compilation;

import static com.google.testing.compile.Compiler.*;
import static com.google.testing.compile.CompilationSubject.*;

/**
* Helper class for testing DataElement related annotations.
* @author Daniel Bluhm
*/
public class DataElementAnnotationTestHelper {

private boolean showDiagnostics = false;

public DataElementAnnotationTestHelper() {
String show = System.getenv("SHOW_DIAGNOSTICS");
if (show != null && show.equals("true")) {
this.showDiagnostics = true;
}
}

/**
* Retrieve an instance of Lombok's Annotation Processor.
*
Expand All @@ -55,16 +67,32 @@ private Processor getLombokAnnotationProcessor() {
return p;
}

private void printDiagnostics(Compilation compilation) {
for (Diagnostic<? extends JavaFileObject> diag :
compilation.diagnostics()
) {
System.err.println(String.format(
"[%s]: %s",
diag.getKind().toString(),
diag.getMessage(Locale.ENGLISH)
));
}
}

/**
* Compile the sources with needed processors.
* @param sources to compile
* @return Compilation result
*/
public Compilation compile(JavaFileObject... sources) {
return javac()
Compilation compilation = javac()
.withProcessors(
getLombokAnnotationProcessor(),
new DataElementProcessor()
).compile(sources);
if (showDiagnostics) {
printDiagnostics(compilation);
}
return compilation;
}
}
Expand Up @@ -220,6 +220,7 @@ void testAnnotateEnumFails() {
@Test
void testNoDataFieldsSucceeds() {
Compilation compilation = helper.compile(Inputs.NO_DATAFIELDS.get());
assertThat(compilation).succeededWithoutWarnings();
assertDefaultsPresent(compilation);
}

Expand All @@ -229,6 +230,7 @@ void testNoDataFieldsSucceeds() {
@Test
void testWithSingleDataFieldSucceeds() {
Compilation compilation = helper.compile(Inputs.SINGLE.get());
assertThat(compilation).succeededWithoutWarnings();
assertDefaultsPresent(compilation);
assertInterfaceMatches(compilation, Patterns.SINGLE_INT.get());
assertImplementationMatches(compilation, Patterns.SINGLE_IMPL.get());
Expand All @@ -240,6 +242,7 @@ void testWithSingleDataFieldSucceeds() {
@Test
void testWithManyDataFieldsSucceeds() {
Compilation compilation = helper.compile(Inputs.MANY.get());
assertThat(compilation).succeededWithoutWarnings();
assertDefaultsPresent(compilation);
assertInterfaceMatches(compilation, Patterns.MANY_INT.get());
assertImplementationMatches(compilation, Patterns.MANY_IMPL.get());
Expand All @@ -251,6 +254,7 @@ void testWithManyDataFieldsSucceeds() {
@Test
void testSingleNonPrimitiveDataFieldSucceeds() {
Compilation compilation = helper.compile(Inputs.SINGLE_NON_PRIMITIVE.get());
assertThat(compilation).succeededWithoutWarnings();
assertDefaultsPresent(compilation);
assertInterfaceMatches(compilation, Patterns.SINGLE_NON_PRIMITIVE_INT.get());
assertImplementationMatches(compilation, Patterns.SINGLE_NON_PRIMITIVE_IMPL.get());
Expand All @@ -262,6 +266,7 @@ void testSingleNonPrimitiveDataFieldSucceeds() {
@Test
void testManyNonPrimitiveDataFieldSucceeds() {
Compilation compilation = helper.compile(Inputs.MANY_NON_PRIMITIVE.get());
assertThat(compilation).succeededWithoutWarnings();
assertDefaultsPresent(compilation);
assertInterfaceMatches(compilation, Patterns.MANY_NON_PRIMITIVE_INT.get());
assertImplementationMatches(compilation, Patterns.MANY_NON_PRIMITIVE_IMPL.get());
Expand All @@ -274,6 +279,7 @@ void testManyNonPrimitiveDataFieldSucceeds() {
@Test
void testDocStringsPreserved() {
Compilation compilation = helper.compile(Inputs.SINGLE.get());
assertThat(compilation).succeededWithoutWarnings();
assertThat(compilation).generatedSourceFile(IMPLEMENTATION)
.contentsAsUtf8String()
.contains("* A UNIQUE STRING IN THE DOC STRING.");
Expand All @@ -289,6 +295,7 @@ void testDocStringsPreserved() {
@Test
void testAccessibilityPreserved() {
Compilation compilation = helper.compile(Inputs.ACCESSIBILITY_PRESERVED.get());
assertThat(compilation).succeededWithoutWarnings();
assertImplementationMatches(compilation, Patterns.ACCESSIBILITY_PRESERVED.get());
}

Expand Down Expand Up @@ -321,6 +328,7 @@ void testDataFieldOnMethodFails() {
@Test
void testDataFieldGetterOption() {
Compilation compilation = helper.compile(Inputs.DATAFIELD_GETTER.get());
assertThat(compilation).succeededWithoutWarnings();
assertThat(compilation)
.generatedSourceFile(INTERFACE)
.hasSourceEquivalentTo(Patterns.DATAFIELD_GETTER_INT.get());
Expand All @@ -332,6 +340,7 @@ void testDataFieldGetterOption() {
@Test
void testDataFieldSetterOption() {
Compilation compilation = helper.compile(Inputs.DATAFIELD_SETTER.get());
assertThat(compilation).succeededWithoutWarnings();
assertThat(compilation)
.generatedSourceFile(INTERFACE)
.hasSourceEquivalentTo(Patterns.DATAFIELD_SETTER_INT.get());
Expand All @@ -343,6 +352,7 @@ void testDataFieldSetterOption() {
@Test
void testDataFieldMatchOption() {
Compilation compilation = helper.compile(Inputs.DATAFIELD_MATCH.get());
assertThat(compilation).succeededWithoutWarnings();
assertThat(compilation)
.generatedSourceFile(IMPLEMENTATION)
.contentsAsUtf8String()
Expand All @@ -359,6 +369,7 @@ void testDataFieldMatchOption() {
@Test
void testDataFieldDefaultNonString() {
Compilation compilation = helper.compile(Inputs.DEFAULT_NON_STRING.get());
assertThat(compilation).succeededWithoutWarnings();
assertImplementationMatches(
compilation,
Patterns.DEFAULT_NON_STRING_IMPL.get()
Expand All @@ -371,6 +382,7 @@ void testDataFieldDefaultNonString() {
@Test
void testDataFieldDefaultString() {
Compilation compilation = helper.compile(Inputs.DEFAULT_STRING.get());
assertThat(compilation).succeededWithoutWarnings();
assertImplementationMatches(
compilation,
Patterns.DEFAULT_STRING_IMPL.get()
Expand Down

0 comments on commit 3793759

Please sign in to comment.