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 Jan 25, 2024
2 parents b0cf73b + 25960c8 commit 00862fb
Show file tree
Hide file tree
Showing 94 changed files with 5,067 additions and 2,468 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest,macos-latest,windows-latest ]
java: [ 7.0.x, 8.0.x, 9.0.x, 10.0.x, 11.0.x, 12.0.x, 13.0.x, 14.0.x, 15.0.x, 16.0.x, 17.0.x ]
java: [ 8.0.x, 9.0.x, 10.0.x, 11.0.x, 12.0.x, 13.0.x, 14.0.x, 15.0.x, 16.0.x, 17.0.x ]

steps:
- uses: actions/checkout@v1
Expand Down
170 changes: 97 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,24 @@ Simply add the following dependencies to your project to be able to use this tes
<dependencies>

<!-- Compile testing framework -->
<dependency>
<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>cute</artifactId>
<version>${currentVersion}</version>
<scope>test</scope>
</dependency>


<!--
Legacy API : Only add it if you don't want to use the new API.
or migration to new API isn't an option.
-->
<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>cute-legacy</artifactId>
<version>${currentVersion}</version>
<scope>test</scope>
</dependency>

<!--
optional : only needed if you want to trigger assertion
errors via your unit test framework
Expand All @@ -56,7 +67,7 @@ Simply add the following dependencies to your project to be able to use this tes
</dependencies>
```
# Tests types
# Tests Types

There are two types of tests: Unit tests and Compilation (or Integration) Tests.

Expand All @@ -70,39 +81,40 @@ For both test types you can test
The CompileTestBuilder always returns immutable CompileTestBuilder instances.
So it's safe to initialize a base CompileTestBuilder instance once in a testclass and to refine it further in the testcases.

## Compilation tests
## Black-Box-Compilation tests

Compilation test allow you to define testcase source files and to apply your processor on it.
Black-Box-Compilation test allow you to define testcase source files and to apply your processor on it during its compilation.

```java
@Test
public void exampleCompilationTest() {

CompileTestBuilder.compilationTest()
.addSources("/exampletestcase/Testcase1.java")
.addProcessors(YourProcessorUnderTest.class)
.compilationShouldSucceed()
.expectWarningMessage()
Cute.blackBoxTest()
.given()
.processors(YourProcessorUnderTest.class)
.andSourceFiles("/exampletestcase/Testcase1.java")
.whenCompiled()
.thenExpectThat()
.compilationSucceeds()
.andThat()
.compilerMessage()
.ofKindWarning()
.atSource("/exampletestcase/Testcase1.java")
.atLineNumber(10L)
.atColumnNumber(20L)
.thatContains("WARNING SNIPPET(will check if a warning exists that contains passed string)")
.expectThatGeneratedSourceFileExists(
"your.test.package.GeneratedFile",
JavaFileObjectUtils.readFromString("package your.test.package;\npublic class GeneratedFile{}"))
.executeTest();

}
.atLine(10L)
.atColumn(20L)
.contains("WARNING SNIPPET(will check if a warning exists that contains passed string)")
.andThat()
.generatedSourceFile("your.test.package.GeneratedFile")
.matches(
CuteApi.ExpectedFileObjectMatcherKind.BINARY,
JavaFileObjectUtils.readFromString("package your.test.package;\npublic class GeneratedFile{}")
)
.executeTest();
```

Additionally, to the explicitly configured assertions it implicitly checks if your annotation processor has been applied and triggers an AssertionError if not.



## Unit tests

Usually - if you are developing annotation processors - your code is likely to rely on the tools provided to the annotation processor via the ProcessingEnvironment like Filer, Messager, Types and Elements.
These classes and the Java compile time model are hard to mock. Thats why unit testing is usually very hard to do.
These classes and the Java compile time model are hard to mock. That's why unit testing is usually very hard to do.
This library helps you to execute unit test at compile time, giving you the ProcessingEnvironment's tools and access to the compile time model for free.

The unit test concept provided by this library uses a default source file and applies a default annotation processor on it.
Expand All @@ -115,24 +127,29 @@ Your unit test code can be declared via the fluent api:
@Test
public void exampleUnitTest() {

CompileTestBuilder.unitTest()
.defineTest(SampleProcesssor.class, new UnitTestForTestingAnnotationProcessors<SampleProcesssor,TypeElement>() {
Cute.unitTest()
.when()
.passInProcessor(SampleProcesssor.class)
.intoUnitTest( new UnitTestForTestingAnnotationProcessorsWithoutPassIn<SampleProcesssor>() {
@Override
public void unitTest(SampleProcesssor unit, ProcessingEnvironment processingEnvironment, TypeElement typeElement) {

public void unitTest(SampleProcesssor unit, ProcessingEnvironment processingEnvironment) {
// Processor's init() method was called by cute framework
String result = unit.yourMethodToTest("ABC");

// AssertionErrors will be passed through your external unit test function
MatcherAssert.assertThat(result, Matchers.is("EXPECTED RESULT"));

}
})
.compilationShouldSucceed()
.expectWarningMessage()
.thatContains("WARNING SNIPPET(will check if a warning exists that contains passed string)")
.thenExpectThat()
.compilationSucceeds()
.andThat()
.compilerMessage()
.ofKindWarning()
.contains("WARNING SNIPPET(will check if a warning exists that contains passed string)")
.executeTest();

}
```

Expand All @@ -146,59 +163,66 @@ Another cute feature is that it's possible to easily pass in elements and even a
For passing in elements all you need to do is to create a static class next to your unit tests and to add the _PassIn_ annotation on the element you want to pass in.
The static class then can be used as parameter in _defineTestWithPassedInElement_ method.

Additional you can pass in an annotation processor by using the annotation processor class as a parameter in _defineTestWithPassedInElement_ method.
Additionally, you can pass in an annotation processor by using the annotation processor class as a parameter in _defineTestWithPassedInElement_ method.
An instance of the annotation processor will be created and initialized.

```java
private static class PassedInElement {
// Add your custom code like
@PassIn
void yourMethodToBePassedIn(){
}
private static class PassedInElement {
// Add your custom code like
@PassIn
void yourMethodToBePassedIn(){
}

// Example 1 : Pass in element
@Test
public void yourUnitTestWithPassedInElement() {
}

CompileTestBuilder
.unitTest()
.defineTestWithPassedInElement(PassedInElement.class, new UnitTest<ExecutableElement>() {
@Override
public void unitTest(ProcessingEnvironment processingEnvironment, ExecutableElement element) {
// Example 1 : Pass in element
@Test
public void yourUnitTestWithPassedInElement() {

// put your unit test code
Cute
.unitTest()
.when().passInElement().<ExecutableElement>fromClass(PassedInElement.class)
.intoUnitTest( new UnitTest<ExecutableElement>() {
@Override
public void unitTest(ProcessingEnvironment processingEnvironment, ExecutableElement element) {

}
})
.compilationShouldSucceed()
.executeTest();
// put your unit test code
// ....

}
})
.thenExpectThat()
.compilationSucceeds()
.executeTest();

}

// Example 2 : Pass in annotation processor and element
// The processor will be instantiated and initialized
@Test
public void yourUnitTestWithPassedInElementAndProcessor() {
}

CompileTestBuilder
.unitTest()
.defineTestWithPassedInElement(YourProcessorToTest.class, PassedInElement.class, new UnitTestForTestingAnnotationProcessors<YourProcessorToTest, ExecutableElement>() {
@Override
public void unitTest(YourProcessorToTest unit, ProcessingEnvironment processingEnvironment, ExecutableElement element) {
// Example 2 : Pass in annotation processor and element
// The processor will be instantiated and initialized
@Test
public void yourUnitTestWithPassedInElementAndProcessor() {

Cute
.unitTest()
.when()
.passInProcessor(YourProcessorToTest.class)
.andPassInElement().<ExecutableElement>fromClass(PassedInElement.class)
.intoUnitTest( new UnitTestForTestingAnnotationProcessors<YourProcessorToTest, ExecutableElement>() {
@Override
public void unitTest(YourProcessorToTest unit, ProcessingEnvironment processingEnvironment, ExecutableElement element) {

// put your unit test code
String result = unit.methodToTest(element);
MatcherAssert.assertThat(result, Matchers.is("EXPECTED RESULT"));
// put your unit test code
String result = unit.methodToTest(element);
MatcherAssert.assertThat(result, Matchers.is("EXPECTED RESULT"));

}
})
.compilationShouldSucceed()
.executeTest();
}
})
.thenExpectThat()
.compilationSucceeds()
.executeTest();


}
}
```

# Projects using this toolkit library
Expand Down
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>0.12.1</version>
<version>1.0.0_RC1</version>
</parent>

<name>coverage</name>
Expand Down
117 changes: 117 additions & 0 deletions coverage/pom.xml.releaseBackup
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>coverage</artifactId>
<packaging>pom</packaging>

<parent>
<groupId>io.toolisticon.cute</groupId>
<artifactId>cute-parent</artifactId>
<version>0.12.1-SNAPSHOT</version>
</parent>

<name>coverage</name>

<dependencies>

<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>cute</artifactId>
</dependency>
<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>extension-api</artifactId>
</dependency>
<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>extension-junit4</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>extension-junit4</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>extension-testng</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>integration-test-junit4</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>integration-test-testng</artifactId>
<version>${project.version}</version>
</dependency>
<!--
<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>extension-modulesupport</artifactId>
</dependency>
-->
<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>extension-plainjava</artifactId>
</dependency>

</dependencies>

<profiles>
<profile>
<id>use jdk8 sources</id>
<activation>
<jdk>[1.8,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>extension-junit5</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.toolisticon.cute</groupId>
<artifactId>integration-test-junit5</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</profile>
</profiles>


<build>

<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>


<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>report-aggregate</id>
<phase>verify</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>

</build>

</project>
Loading

0 comments on commit 00862fb

Please sign in to comment.