Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Stamann committed Mar 21, 2024
2 parents 733d451 + 99017b3 commit 2f2cf06
Show file tree
Hide file tree
Showing 42 changed files with 312 additions and 492 deletions.
2 changes: 1 addition & 1 deletion coverage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>io.toolisticon.cute</groupId>
<artifactId>cute-parent</artifactId>
<version>1.3.1</version>
<version>1.4.0</version>
</parent>

<name>coverage</name>
Expand Down
2 changes: 1 addition & 1 deletion cute/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>io.toolisticon.cute</groupId>
<artifactId>cute-parent</artifactId>
<version>1.3.1</version>
<version>1.4.0</version>
</parent>

<name>cute</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ protected <T extends Element> Element getElement(Set<T> elements) {

if (elements.size() == 1) {
return elements.iterator().next();
}
if (elements.size() == 0) {
} else if (elements.isEmpty()) {
throw new FailingAssertionException(Constants.Messages.UNIT_TEST_PRECONDITION_MUST_FIND_ONE_ELEMENT.produceMessage());
} else {

Expand All @@ -57,7 +56,7 @@ protected <T extends Element> Element getElement(Set<T> elements) {

}

if (filteredList.size() == 0) {
if (filteredList.isEmpty()) {
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());
Expand All @@ -69,10 +68,4 @@ protected <T extends Element> Element getElement(Set<T> elements) {

}

protected void triggerError(String message) {

this.processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, message);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ protected Element getPassedInElement() {
List<Element> filteredElements = filterByAnnotation(allEnclosedElements);

if (filteredElements.size() != 1) {

throw new FailingAssertionException(Constants.Messages.UNIT_TEST_PASS_IN_PRECONDITION_MUST_FIND_EXACTLY_ONE_ELEMENT.produceMessage(annotationTypeUsedForScan.getCanonicalName(), classToScan.getCanonicalName()));

}

return filteredElements.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.tools.Diagnostic;
import java.util.HashSet;
Expand Down Expand Up @@ -93,6 +94,9 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
, e);
}

// Exception has been found
expectedExceptionWasThrown = true;

} else {

// Got unexpected exception
Expand All @@ -107,13 +111,14 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment

}

// check in last round if expected exception has been thrown
if (roundEnv.processingOver() && expectedExceptionWasThrown && this.expectedThrownException != null) {
// check in last round if expected exception has been thrown - in this case trigger assertion error
if (roundEnv.processingOver() && !expectedExceptionWasThrown && this.expectedThrownException != null) {
throw new FailingAssertionException(
Constants.Messages.ASSERTION_EXPECTED_EXCEPTION_NOT_THROWN.produceMessage(this.expectedThrownException.getCanonicalName())
);
}


return returnValue;
}

Expand Down Expand Up @@ -177,23 +182,8 @@ public static <T extends Processor> AnnotationProcessorWrapper wrapProcessor(Cla

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

// return cached wrapped processors if available


Set<AnnotationProcessorWrapper> wrappedProcessors = new HashSet<>();


// need to add unit test processor


// TODO: THIS CASE HANDLES PROCESSOR INSTANCES - NORMALLY ONLY USED IN UNIT TESTS ?!?
/*-
for (Processor processor : compilerTestBB.processors()) {
wrappedProcessors.add(AnnotationProcessorWrapper.wrapProcessor(processor, expectedThrownException));
}
*/
if (compilerTestBB.testType() == CuteApi.TestType.UNIT && compilerTestBB.unitTest() != null) {
Processor processor = null;

Expand All @@ -210,13 +200,13 @@ static Set<AnnotationProcessorWrapper> getWrappedProcessors(CuteApi.CompilerTest
processor = new UnitTestAnnotationProcessorClassWithPassIn<>(
compilerTestBB.passInConfiguration().getPassedInClass(),
compilerTestBB.passInConfiguration().getAnnotationToScanFor() != null ? compilerTestBB.passInConfiguration().getAnnotationToScanFor() : PassIn.class,
(UnitTest<Element>) compilerTestBB.unitTest());
(UnitTest<?>) compilerTestBB.unitTest());

} else {
// This is the legacy case
processor = new UnitTestAnnotationProcessorClass<>(
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());
(UnitTest<?>) compilerTestBB.unitTest());
}

} else if (compilerTestBB.unitTest() instanceof UnitTestWithoutPassIn) {
Expand All @@ -227,7 +217,7 @@ static Set<AnnotationProcessorWrapper> getWrappedProcessors(CuteApi.CompilerTest

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

Processor processorUnderTest = null;
Processor processorUnderTest;
try {
processorUnderTest = compilerTestBB.passInConfiguration().getPassedInProcessor().getDeclaredConstructor().newInstance();
} catch (Exception e) {
Expand All @@ -240,14 +230,14 @@ static Set<AnnotationProcessorWrapper> getWrappedProcessors(CuteApi.CompilerTest
Constants.DEFAULT_ANNOTATION,
compilerTestBB.passInConfiguration().getPassedInClass(),
compilerTestBB.passInConfiguration().getAnnotationToScanFor() != null ? compilerTestBB.passInConfiguration().getAnnotationToScanFor() : PassIn.class,
(UnitTestForTestingAnnotationProcessors) compilerTestBB.unitTest()
(UnitTestForTestingAnnotationProcessors<Processor,Element>) 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());
(UnitTestForTestingAnnotationProcessors<Processor,Element>) compilerTestBB.unitTest());
}


Expand All @@ -263,7 +253,7 @@ static Set<AnnotationProcessorWrapper> getWrappedProcessors(CuteApi.CompilerTest
processor = new UnitTestAnnotationProcessorClassForTestingAnnotationProcessorsWithoutPassIn<>(
processorUnderTest,
Constants.DEFAULT_ANNOTATION,
(UnitTestForTestingAnnotationProcessorsWithoutPassIn) compilerTestBB.unitTest());
(UnitTestForTestingAnnotationProcessorsWithoutPassIn<Processor>) compilerTestBB.unitTest());

}

Expand All @@ -276,24 +266,16 @@ static Set<AnnotationProcessorWrapper> getWrappedProcessors(CuteApi.CompilerTest
for (Class<? extends Processor> processorType : compilerTestBB.processors()) {

try {
Processor processor = processorType.getDeclaredConstructor().newInstance();

Processor processor = processorType.getDeclaredConstructor().newInstance();
wrappedProcessors.add(AnnotationProcessorWrapper.wrapProcessor(processor, compilerTestBB.getExceptionIsThrown()));

} catch (Exception e) {
throw new IllegalArgumentException("Passed processor " + processorType.getCanonicalName() + " cannot be instantiated.", e);
throw new IllegalArgumentException(Constants.Messages.IAE_CANNOT_INSTANTIATE_PROCESSOR.produceMessage(processorType.getCanonicalName()));
}

}

// TODO: CURRENTLY NOT IMPLEMENTED WITH NEW FLUENT API
/*-
for (CompileTestConfiguration.ProcessorWithExpectedException processor : this.processorsWithExpectedExceptions) {
wrappedProcessors.add(AnnotationProcessorWrapper.wrapProcessor(processor.processorType, processor.throwable != null ? processor.throwable : expectedThrownException));
}
*/


return wrappedProcessors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

/**
* Compilation result.
* For internal usage.
* <p>
* Allows access to DiagnosticCollector and FileManager used during compilation test.
*/
Expand Down
21 changes: 8 additions & 13 deletions cute/src/main/java/io/toolisticon/cute/CompileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ CompilationResult executeTest() {
try {
generatedClassesTest.doTests(cuteClassLoader);
} catch (AssertionError e) {
// make sure AssertionErrors will pass
throw e;
} catch (Exception e) {
throw new FailingAssertionException(Constants.Messages.MESSAGE_GOT_UNEXPECTED_EXCEPTION_DURING_CLASS_TEST_ERROR.produceMessage(generatedClassesTest, e.getMessage()));
Expand Down Expand Up @@ -305,12 +306,12 @@ void checkMessages(DiagnosticCollector<JavaFileObject> diagnostics) {

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

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

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

for (Diagnostic element : filteredDiagnostics) {
for (Diagnostic<?> element : filteredDiagnostics) {

String localizedMessage = element.getMessage(messageToCheck.withLocale());

Expand All @@ -326,7 +327,7 @@ void checkMessages(DiagnosticCollector<JavaFileObject> diagnostics) {
}
case CONTAINS:
default: {
if (!messageToCheck.getSearchString().stream().allMatch(e -> localizedMessage.contains(e))) {
if (!messageToCheck.getSearchString().stream().allMatch(localizedMessage::contains)) {
continue;
}
}
Expand All @@ -353,7 +354,7 @@ void checkMessages(DiagnosticCollector<JavaFileObject> diagnostics) {
}

// Not found ==> assertion fails
throw new FailingAssertionException(Constants.Messages.MESSAGE_HAVENT_FOUND_MESSSAGE.produceMessage(messageToCheck.getSearchString(), messageToCheck.getKind().name()));
throw new FailingAssertionException(Constants.Messages.MESSAGE_HAVENT_FOUND_MESSSAGE.produceMessage(messageToCheck.getSearchString(), messageToCheck.getKind().name(), messageToCheck.toString()));

}

Expand All @@ -366,18 +367,12 @@ public Map<Diagnostic.Kind, List<CuteApi.CompilerMessageCheckBB>> getCompilerMes
Map<Diagnostic.Kind, List<CuteApi.CompilerMessageCheckBB>> map = new HashMap<>();

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

List<CuteApi.CompilerMessageCheckBB> checkByKindList = map.get(compilerMessageCheck.getKind());
if (checkByKindList == null) {
checkByKindList = new ArrayList<>();
map.put(Diagnostic.Kind.valueOf(compilerMessageCheck.getKind().name()), checkByKindList);

}

checkByKindList.add(compilerMessageCheck);
map.computeIfAbsent(compilerMessageCheck.getKind().toDiagnosticsKind(),(k) -> new ArrayList<>()).add(compilerMessageCheck);
}

return map;
}



}
22 changes: 4 additions & 18 deletions cute/src/main/java/io/toolisticon/cute/CompileTestUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.tools.JavaFileObject;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Some static utility functions.
Expand Down Expand Up @@ -52,14 +53,7 @@ static String getAnnotationProcessorWasAppliedMessage(Processor processor) {
*/
static Set<String> getMessages(DiagnosticCollector<JavaFileObject> diagnostics, Diagnostic.Kind kind) {

Set<String> messages = new HashSet<>();
Set<Diagnostic> filteredDiagnostic = getDiagnosticByKind(diagnostics, kind);

for (Diagnostic diagnostic : filteredDiagnostic) {
messages.add(diagnostic.getMessage(null));
}

return messages;
return getDiagnosticByKind(diagnostics,kind).stream().map(e -> e.getMessage(null)).collect(Collectors.toSet());

}

Expand All @@ -70,17 +64,9 @@ static Set<String> getMessages(DiagnosticCollector<JavaFileObject> diagnostics,
* @param kind the kind of the messages to return
* @return a Set containing all Diagnostic element of passed kind, or an empty Set.
*/
static Set<Diagnostic> getDiagnosticByKind(DiagnosticCollector<JavaFileObject> diagnostics, Diagnostic.Kind kind) {

Set<Diagnostic> filteredDiagnostics = new HashSet<>();

for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
if (kind == diagnostic.getKind()) {
filteredDiagnostics.add(diagnostic);
}
}
static Set<Diagnostic<? extends JavaFileObject>> getDiagnosticByKind(DiagnosticCollector<JavaFileObject> diagnostics, Diagnostic.Kind kind) {

return filteredDiagnostics;
return diagnostics.getDiagnostics().stream().filter(e -> e.getKind().equals(kind)).collect(Collectors.toSet());

}

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 @@ -65,7 +65,7 @@ public String getMessagePattern(){
public final static Message MESSAGE_FO_EXISTS_BUT_DOESNT_MATCH_MATCHER = new Message("Expected generated FileObject (%s) exists but doesn't match passed GeneratedFileObjectMatcher: %s");

public final static Message MESSAGE_PROCESSOR_HASNT_BEEN_APPLIED = new Message("Annotation processor %s hasn't been called! Please check that there's at least one source file using an annotation supported by the processor: %s");
public final static Message MESSAGE_HAVENT_FOUND_MESSSAGE = new Message("Haven't found expected message string '%s' of kind %s!");
public final static Message MESSAGE_HAVENT_FOUND_MESSSAGE = new Message("Haven't found expected compiler message string '%s' of kind '%s'! (Check configuration : %s)");

public final static Message MESSAGE_TECHNICAL_ERROR = new Message("TECHNICAL ERROR : %s");

Expand Down
Loading

0 comments on commit 2f2cf06

Please sign in to comment.