Skip to content

Commit

Permalink
Mayor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Stamann committed Jan 19, 2024
1 parent 40ccd23 commit 00db7c8
Show file tree
Hide file tree
Showing 34 changed files with 521 additions and 2,165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected <T extends Element> Element getElement(Set<T> elements) {
if (filteredList.size() == 0) {
throw new FailingAssertionException(Constants.Messages.UNIT_TEST_PRECONDITION_MUST_FIND_EXACTLY_ONE_ELEMENT.produceMessage(annotationTypeToUse.getCanonicalName()));
} else if (filteredList.size() > 1) {
throw new FailingAssertionException(Constants.Messages.UNIT_TEST_PRECONDITION_MUST_FIND_EXACTLY_ONE_ELEMENT_WITH_PASSIN_ANNOTATION.produceMessage(annotationTypeToUse.getCanonicalName()));
throw new FailingAssertionException(Constants.Messages.UNIT_TEST_PRECONDITION_MUST_FIND_EXACTLY_ONE_ELEMENT_WITH_PASSIN_ANNOTATION.produceMessage());
} else {
return filteredList.get(0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
package io.toolisticon.cute.impl;

import io.toolisticon.cute.Constants;
import io.toolisticon.cute.CuteFluentApi;
import io.toolisticon.cute.FailingAssertionException;
import io.toolisticon.cute.UnitTest;
import io.toolisticon.cute.UnitTestAnnotationProcessorClass;
import io.toolisticon.cute.UnitTestAnnotationProcessorClassForTestingAnnotationProcessors;
import io.toolisticon.cute.UnitTestAnnotationProcessorClassWithPassIn;
import io.toolisticon.cute.UnitTestAnnotationProcessorClassWithoutPassIn;
import io.toolisticon.cute.UnitTestForTestingAnnotationProcessors;
import io.toolisticon.cute.UnitTestForTestingAnnotationProcessorsWithoutPassIn;
import io.toolisticon.cute.UnitTestWithoutPassIn;
package io.toolisticon.cute;

import javax.annotation.processing.Completion;
import javax.annotation.processing.Messager;
Expand All @@ -23,7 +11,6 @@
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -188,7 +175,7 @@ public static <T extends Processor> AnnotationProcessorWrapper wrapProcessor(Cla
}


static Set<AnnotationProcessorWrapper> getWrappedProcessors(CuteFluentApi.CompilerTestBB compilerTestBB) {
static Set<AnnotationProcessorWrapper> getWrappedProcessors(CuteApi.CompilerTestBB compilerTestBB) {

// return cached wrapped processors if available

Expand All @@ -207,28 +194,35 @@ static Set<AnnotationProcessorWrapper> getWrappedProcessors(CuteFluentApi.Compil
}
*/
if (compilerTestBB.unitTest() != null) {
if (compilerTestBB.testType() == CuteApi.TestType.UNIT && compilerTestBB.unitTest() != null) {
Processor processor = null;
Class<? extends Annotation> annotationToScanFor = Constants.DEFAULT_ANNOTATION;

// This is kind of difficult...
// must determine which annotation must be used as trigger and for scanning
// 1. passed in class - in this case the PassIn or annotationForScan must be used for scanning, DEFAULT ANNOTATION must be used as entry point
// 2. Pass in by source - in this case annotationForScan isn't used, just PassIn or annotationForScan if set
// 3. Implicit PassIn - just the default Source and Entry Point is available TestAnnotation Must be used

if (compilerTestBB.unitTest() instanceof UnitTest) {
if (compilerTestBB.passInConfiguration() != null && compilerTestBB.passInConfiguration().getPassedInClass() != null) {

// This is correct: DEFAULT AS ENTRY POINT AND PASSIN OR ANNOTATIONFORSCAN for scanning
processor = new UnitTestAnnotationProcessorClassWithPassIn<>(
compilerTestBB.passInConfiguration().getPassedInClass(),
annotationToScanFor,
compilerTestBB.passInConfiguration().getAnnotationToScanFor() != null ? compilerTestBB.passInConfiguration().getAnnotationToScanFor() : PassIn.class,
(UnitTest<Element>) compilerTestBB.unitTest());

} else {
// This is the legacy case
processor = new UnitTestAnnotationProcessorClass<>(
annotationToScanFor,
compilerTestBB.passInConfiguration() != null && compilerTestBB.passInConfiguration().getAnnotationToScanFor() != null ? compilerTestBB.passInConfiguration().getAnnotationToScanFor() : (compilerTestBB.passInConfiguration() != null && compilerTestBB.passInConfiguration().getPassInElement() ? PassIn.class : Constants.DEFAULT_ANNOTATION ),
(UnitTest<Element>) compilerTestBB.unitTest());
}

} else if (compilerTestBB.unitTest() instanceof UnitTestWithoutPassIn){
} else if (compilerTestBB.unitTest() instanceof UnitTestWithoutPassIn) {

processor = new UnitTestAnnotationProcessorClassWithoutPassIn(
annotationToScanFor,
Constants.DEFAULT_ANNOTATION,
(UnitTestWithoutPassIn) compilerTestBB.unitTest());

} else if (compilerTestBB.unitTest() instanceof UnitTestForTestingAnnotationProcessors) {
Expand All @@ -240,13 +234,37 @@ static Set<AnnotationProcessorWrapper> getWrappedProcessors(CuteFluentApi.Compil
throw new IllegalArgumentException(Constants.Messages.IAE_CANNOT_INSTANTIATE_PROCESSOR.produceMessage(compilerTestBB.passInConfiguration().getPassedInProcessor().getCanonicalName()));
}

processor = new UnitTestAnnotationProcessorClassForTestingAnnotationProcessors<Processor, Element>(
processorUnderTest,
annotationToScanFor,
(UnitTestForTestingAnnotationProcessors) compilerTestBB.unitTest());
if (compilerTestBB.passInConfiguration() != null && compilerTestBB.passInConfiguration().getPassedInClass() != null) {
processor = new UnitTestAnnotationProcessorClassForTestingAnnotationProcessorsWithPassIn<>(
processorUnderTest,
Constants.DEFAULT_ANNOTATION,
compilerTestBB.passInConfiguration().getPassedInClass(),
compilerTestBB.passInConfiguration().getAnnotationToScanFor() != null ? compilerTestBB.passInConfiguration().getAnnotationToScanFor() : PassIn.class,
(UnitTestForTestingAnnotationProcessors) compilerTestBB.unitTest()

);
} else {
processor = new UnitTestAnnotationProcessorClassForTestingAnnotationProcessors<>(
processorUnderTest,
compilerTestBB.passInConfiguration().getAnnotationToScanFor() != null ? compilerTestBB.passInConfiguration().getAnnotationToScanFor() : (compilerTestBB.passInConfiguration().getPassInElement() ? PassIn.class : Constants.DEFAULT_ANNOTATION ),
(UnitTestForTestingAnnotationProcessors) compilerTestBB.unitTest());
}


} else if (compilerTestBB.unitTest() instanceof UnitTestForTestingAnnotationProcessorsWithoutPassIn) {

Processor processorUnderTest = null;
try {
processorUnderTest = compilerTestBB.passInConfiguration().getPassedInProcessor().getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new IllegalArgumentException(Constants.Messages.IAE_CANNOT_INSTANTIATE_PROCESSOR.produceMessage(compilerTestBB.passInConfiguration().getPassedInProcessor().getCanonicalName()));
}

processor = new UnitTestAnnotationProcessorClassForTestingAnnotationProcessorsWithoutPassIn<>(
processorUnderTest,
Constants.DEFAULT_ANNOTATION,
(UnitTestForTestingAnnotationProcessorsWithoutPassIn) compilerTestBB.unitTest());

}

if (processor != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.toolisticon.cute.impl;
package io.toolisticon.cute;

import java.util.Random;

/**
* Some common utility functions.
*/
public final class CommonUtilities {
final class CommonUtilities {

/**
* Hidden constructor.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.cute.impl;
package io.toolisticon.cute;

import javax.tools.DiagnosticCollector;
import javax.tools.JavaFileObject;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package io.toolisticon.cute.impl;
package io.toolisticon.cute;

import io.toolisticon.cute.Constants;
import io.toolisticon.cute.CuteFluentApi;
import io.toolisticon.cute.FailingAssertionException;
import io.toolisticon.cute.GeneratedFileObjectMatcher;
import io.toolisticon.cute.InvalidTestConfigurationException;
import io.toolisticon.cute.extension.api.AssertionSpiServiceLocator;
import io.toolisticon.cute.extension.api.ModuleSupportSpi;
import io.toolisticon.cute.extension.api.ModuleSupportSpiServiceLocator;
Expand All @@ -26,12 +21,12 @@
/**
* Implementation of a compile test.
*/
public class CompileTest {
class CompileTest {

// Messages


private final CuteFluentApi.CompilerTestBB compileTestConfiguration;
private final CuteApi.CompilerTestBB compileTestConfiguration;

private final Set<AnnotationProcessorWrapper> wrappedAnnotationProcessors;

Expand All @@ -40,7 +35,7 @@ public class CompileTest {
*
* @param compileTestConfiguration the configuration used during tests.
*/
public CompileTest(final CuteFluentApi.CompilerTestBB compileTestConfiguration) {
public CompileTest(final CuteApi.CompilerTestBB compileTestConfiguration) {
this.compileTestConfiguration = compileTestConfiguration;
this.wrappedAnnotationProcessors = AnnotationProcessorWrapper.getWrappedProcessors(compileTestConfiguration);

Expand Down Expand Up @@ -87,8 +82,8 @@ public void executeTest() {
checkMessages(compilationResult.getDiagnostics());


for (CuteFluentApi.GeneratedJavaFileObjectCheckBB generatedJavaFileObjectCheck : this.compileTestConfiguration.javaFileObjectChecks()) {
if (CuteFluentApi.FileObjectCheckType.EXISTS.equals(generatedJavaFileObjectCheck.getCheckType())) {
for (CuteApi.GeneratedJavaFileObjectCheckBB generatedJavaFileObjectCheck : this.compileTestConfiguration.javaFileObjectChecks()) {
if (CuteApi.FileObjectCheckType.EXISTS.equals(generatedJavaFileObjectCheck.getCheckType())) {
if (!compilationResult.getCompileTestFileManager().existsExpectedJavaFileObject(generatedJavaFileObjectCheck.getLocation(), generatedJavaFileObjectCheck.getClassName(), generatedJavaFileObjectCheck.getKind())) {
throw new FailingAssertionException(Constants.Messages.MESSAGE_JFO_DOESNT_EXIST.produceMessage(getJavaFileObjectInfoString(generatedJavaFileObjectCheck)));
} else {
Expand Down Expand Up @@ -129,9 +124,9 @@ public void executeTest() {

}

for (CuteFluentApi.GeneratedFileObjectCheckBB generatedFileObjectCheck : this.compileTestConfiguration.fileObjectChecks()) {
for (CuteApi.GeneratedFileObjectCheckBB generatedFileObjectCheck : this.compileTestConfiguration.fileObjectChecks()) {

if (CuteFluentApi.FileObjectCheckType.EXISTS.equals(generatedFileObjectCheck.getCheckType())) {
if (CuteApi.FileObjectCheckType.EXISTS.equals(generatedFileObjectCheck.getCheckType())) {

if (!compilationResult.getCompileTestFileManager().existsExpectedFileObject(generatedFileObjectCheck.getLocation(), generatedFileObjectCheck.getPackageName(), generatedFileObjectCheck.getRelativeName())) {
throw new FailingAssertionException(Constants.Messages.MESSAGE_FO_DOESNT_EXIST.produceMessage(getFileObjectInfoString(generatedFileObjectCheck)));
Expand Down Expand Up @@ -178,6 +173,12 @@ public void executeTest() {

} catch (RuntimeException e) {

if (e.getCause() != null && AssertionError.class.isAssignableFrom(e.getCause().getClass())) {
FailingAssertionException failingAssertionException = new FailingAssertionException(e.getMessage(), e.getCause());
AssertionSpiServiceLocator.locate().fail(e.getCause().getMessage() + "\n" + DebugOutputGenerator.getDebugOutput(compilationResult, compileTestConfiguration, failingAssertionException));
throw (failingAssertionException);
}

if (e.getCause() != null && FailingAssertionException.class.isAssignableFrom(e.getCause().getClass())) {
// now trigger failing assertion, but also enrich message with debug output
AssertionSpiServiceLocator.locate().fail(e.getCause().getMessage() + "\n" + DebugOutputGenerator.getDebugOutput(compilationResult, compileTestConfiguration, (FailingAssertionException) e.getCause()));
Expand All @@ -192,11 +193,11 @@ public void executeTest() {
}


static String getJavaFileObjectInfoString(CuteFluentApi.GeneratedJavaFileObjectCheckBB generatedJavaFileObjectCheck) {
static String getJavaFileObjectInfoString(CuteApi.GeneratedJavaFileObjectCheckBB generatedJavaFileObjectCheck) {
return generatedJavaFileObjectCheck.getLocation() + "; " + generatedJavaFileObjectCheck.getClassName() + "; " + generatedJavaFileObjectCheck.getKind();
}

static String getFileObjectInfoString(CuteFluentApi.GeneratedFileObjectCheckBB generatedFileObjectCheck) {
static String getFileObjectInfoString(CuteApi.GeneratedFileObjectCheckBB generatedFileObjectCheck) {
return generatedFileObjectCheck.getLocation() + "; " + generatedFileObjectCheck.getPackageName() + "; " + generatedFileObjectCheck.getRelativeName();
}

Expand All @@ -206,7 +207,7 @@ static String getFileObjectInfoString(CuteFluentApi.GeneratedFileObjectCheckBB g
* @param compileTestConfiguration the compile-test configuration to use
* @return the compilation result
*/
public static CompilationResult compile(CuteFluentApi.CompilerTestBB compileTestConfiguration, Set<AnnotationProcessorWrapper> wrappedAnnotationProcessors) {
public static CompilationResult compile(CuteApi.CompilerTestBB compileTestConfiguration, Set<AnnotationProcessorWrapper> wrappedAnnotationProcessors) {

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
Expand Down Expand Up @@ -272,14 +273,14 @@ void checkIfProcessorsHaveBeenApplied(DiagnosticCollector<JavaFileObject> diagno
void checkMessages(DiagnosticCollector<JavaFileObject> diagnostics) {

// Just check messages of matching kind
Map<Diagnostic.Kind, List<CuteFluentApi.CompilerMessageCheckBB>> compileMessageChecks = getCompilerMessageCheckByKindMap(compileTestConfiguration);
Map<Diagnostic.Kind, List<CuteApi.CompilerMessageCheckBB>> compileMessageChecks = getCompilerMessageCheckByKindMap(compileTestConfiguration);

for (Map.Entry<Diagnostic.Kind, List<CuteFluentApi.CompilerMessageCheckBB>> entry : compileMessageChecks.entrySet()) {
for (Map.Entry<Diagnostic.Kind, List<CuteApi.CompilerMessageCheckBB>> entry : compileMessageChecks.entrySet()) {

Set<Diagnostic> filteredDiagnostics = CompileTestUtilities.getDiagnosticByKind(diagnostics, entry.getKey());

outer:
for (CuteFluentApi.CompilerMessageCheckBB messageToCheck : entry.getValue()) {
for (CuteApi.CompilerMessageCheckBB messageToCheck : entry.getValue()) {

for (Diagnostic element : filteredDiagnostics) {

Expand Down Expand Up @@ -333,12 +334,12 @@ void checkMessages(DiagnosticCollector<JavaFileObject> diagnostics) {
}


public Map<Diagnostic.Kind, List<CuteFluentApi.CompilerMessageCheckBB>> getCompilerMessageCheckByKindMap(CuteFluentApi.CompilerTestBB compilerTestBB) {
Map<Diagnostic.Kind, List<CuteFluentApi.CompilerMessageCheckBB>> map = new HashMap<>();
public Map<Diagnostic.Kind, List<CuteApi.CompilerMessageCheckBB>> getCompilerMessageCheckByKindMap(CuteApi.CompilerTestBB compilerTestBB) {
Map<Diagnostic.Kind, List<CuteApi.CompilerMessageCheckBB>> map = new HashMap<>();

for (CuteFluentApi.CompilerMessageCheckBB compilerMessageCheck : compilerTestBB.compilerMessageChecks()) {
for (CuteApi.CompilerMessageCheckBB compilerMessageCheck : compilerTestBB.compilerMessageChecks()) {

List<CuteFluentApi.CompilerMessageCheckBB> checkByKindList = map.get(compilerMessageCheck.getKind());
List<CuteApi.CompilerMessageCheckBB> checkByKindList = map.get(compilerMessageCheck.getKind());
if (checkByKindList == null) {
checkByKindList = new ArrayList<>();
map.put(Diagnostic.Kind.valueOf(compilerMessageCheck.getKind().name()), checkByKindList);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.cute.impl;
package io.toolisticon.cute;

import io.toolisticon.cute.Constants;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.toolisticon.cute.impl;
package io.toolisticon.cute;


import javax.annotation.processing.Processor;
Expand Down
2 changes: 1 addition & 1 deletion cute/src/main/java/io/toolisticon/cute/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public String getMessagePattern(){
public final static Message UNIT_TEST_PRECONDITION_INCOMPATIBLE_ELEMENT_TYPE = new Message("PRECONDITION : Processed Element type doesn't match the one expected by your unit test processor");
public final static Message UNIT_TEST_PRECONDITION_MUST_FIND_ONE_ELEMENT = new Message("PRECONDITION: Expected to find at an element annotated with %s in processed sources files.");
public final static Message UNIT_TEST_PRECONDITION_MUST_FIND_EXACTLY_ONE_ELEMENT = new Message("PRECONDITION: Expected to find exactly one element annotated with %s in processed sources files. Please add PassIn annotation to the one Element you want to pass in.");
public final static Message UNIT_TEST_PRECONDITION_MUST_FIND_EXACTLY_ONE_ELEMENT_WITH_PASSIN_ANNOTATION = new Message("PRECONDITION: Expected to find exactly one element annotated with %s and " + PassIn.class.getName() + " in processed sources files. Found more than one element annotated with " + PassIn.class.getName() +".");
public final static Message UNIT_TEST_PRECONDITION_MUST_FIND_EXACTLY_ONE_ELEMENT_WITH_PASSIN_ANNOTATION = new Message("PRECONDITION: Expected to find exactly one element annotated with " + PassIn.class.getName() + " in processed sources files, but found more than one element annotated with " + PassIn.class.getName() +".");


public final static Message UNIT_TEST_PASS_IN_PRECONDITION_MUST_FIND_EXACTLY_ONE_ELEMENT = new Message("PRECONDITION: Expected to find exactly one element annotated with %s in passed in class %s");
Expand Down
Loading

0 comments on commit 00db7c8

Please sign in to comment.