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 20, 2024
2 parents 98f8b69 + 81dd3ce commit f72b820
Show file tree
Hide file tree
Showing 25 changed files with 52 additions and 65 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: [ 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, 18.0.x, 19.0.x, 20.0.x, 21.0.x ]

steps:
- uses: actions/checkout@v4
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This compile testing framework allows you to to do this and additionally support

- allows compile time tests supporting the most relevant test frameworks (junit4, junit5, testng, ...)
- simple, but powerful fluent api (immutable)
- additionally provides compilation outcome to do custom assertions with your favorite unit test frameworks.
- supports all Java versions >=8 (including support for java 9 modules)
- Enables you to debug annotation processors during compilation tests
- provides useful information for analysis of failing tests:
Expand Down Expand Up @@ -106,7 +107,12 @@ Black-Box-Compilation test allow you to define testcase source files and to appl
CuteApi.ExpectedFileObjectMatcherKind.BINARY,
JavaFileObjectUtils.readFromString("package your.test.package;\npublic class GeneratedFile{}")
)
.executeTest();
.executeTest()
.executeCustomAssertions(e -> {
// You can do all checks as custom assertions as well :)
MatcherAssert.assertThat("Compilation should be successful", e.compilationWasSuccessful());
// ...
});
```

Additionally, to the explicitly configured assertions it implicitly checks if your annotation processor has been applied and triggers an AssertionError if not.
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>1.2.0</version>
<version>1.3.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.2.0</version>
<version>1.3.0</version>
</parent>

<name>cute</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public interface CuteClassLoader {

<TYPE> Class<TYPE> getClass(String binaryClassName) throws ClassNotFoundException;
Class<?> getClass(String binaryClassName) throws ClassNotFoundException;

}
61 changes: 19 additions & 42 deletions cute/src/main/java/io/toolisticon/cute/CuteClassLoaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,49 @@ class CuteClassLoaderImpl extends ClassLoader implements CuteClassLoader {

final CompileTestFileManager compileTestFileManager;

final Map<String, Class<?>> classMap = new HashMap<>();
final Map<String, CompileTestFileManager.InMemoryOutputJavaFileObject> classMap = new HashMap<>();

public CuteClassLoaderImpl(CompileTestFileManager compileTestFileManager) {
this.compileTestFileManager = compileTestFileManager;

// get classes
List<JavaFileObject> javaFileObjectList = compileTestFileManager.getGeneratedJavaFileObjects().stream()
List<CompileTestFileManager.InMemoryOutputJavaFileObject> javaFileObjectList = compileTestFileManager.getGeneratedJavaFileObjects().stream()
.filter(e -> e.getKind() == JavaFileObject.Kind.CLASS)
.collect(Collectors.toList());

for (JavaFileObject javaFileObject : javaFileObjectList) {
try {
byte[] byteArray = readAllBytes(javaFileObject.openInputStream());
String className = convertJavaFileObjectNameToBinaryClassName(javaFileObject);
classMap.put(className, defineClass(className, byteArray, 0, byteArray.length));
} catch (IOException e) {
// Ignore for the moment
}
}
for (CompileTestFileManager.InMemoryOutputJavaFileObject javaFileObject : javaFileObjectList) {

}
String className = convertJavaFileObjectNameToBinaryClassName(javaFileObject);
classMap.put(className, javaFileObject);

private static byte[] readAllBytes(InputStream inputStream) throws IOException {
final int bufLen = 4 * 0x400; // 4KB
byte[] buf = new byte[bufLen];
int readLen;
IOException exception = null;

try {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
while ((readLen = inputStream.read(buf, 0, bufLen)) != -1)
outputStream.write(buf, 0, readLen);

return outputStream.toByteArray();
}
} catch (IOException e) {
exception = e;
throw e;
} finally {
if (exception == null) inputStream.close();
else try {
inputStream.close();
} catch (IOException e) {
exception.addSuppressed(e);
}
}

}


static String convertJavaFileObjectNameToBinaryClassName(JavaFileObject javaFileObject) {
Pattern pattern = Pattern.compile("^[/](.*)[.]class$");
Matcher matcher = pattern.matcher(javaFileObject.getName());
if (matcher.matches()) {
return matcher.group(1).replaceAll("[/]",".");
return matcher.group(1).replaceAll("[/]", ".");
}
throw new IllegalStateException("Got invalid name : " + javaFileObject.getName());
}

@Override
public <TYPE> Class<TYPE> getClass(String binaryClassName) throws ClassNotFoundException {
protected Class<?> findClass(String name) throws ClassNotFoundException {

if (!classMap.containsKey(name)) {
throw new ClassNotFoundException("Couldnt't find class : " + name);
}

Class<TYPE> clazz = (Class<TYPE>) this.classMap.get(binaryClassName);
CompileTestFileManager.InMemoryOutputJavaFileObject javaFileObject = classMap.get(name);

if(clazz == null){
throw new ClassNotFoundException(binaryClassName);
}
return clazz;
return defineClass(name, javaFileObject.getContent(), 0, javaFileObject.getContent().length);
}

@Override
public Class<?> getClass(String binaryClassName) throws ClassNotFoundException {
return this.loadClass(binaryClassName);
}
}
4 changes: 4 additions & 0 deletions cute/src/test/java/io/toolisticon/cute/CuteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ public void test_testGenerationOfClassAndCustomAssertions() {
Class<?> generatedClass = e.getClassLoader().getClass("io.toolisticon.cute.testhelper.compiletest.TestClassGeneratedClass");
MatcherAssert.assertThat("Class should exist", generatedClass != null);

Class<?> compiledClass = e.getClassLoader().getClass("io.toolisticon.cute.testhelper.compiletest.TestClass");
MatcherAssert.assertThat("Class should exist", compiledClass != null);


SimpleTestProcessor1Interface instance = (SimpleTestProcessor1Interface) generatedClass.getDeclaredConstructor().newInstance();
MatcherAssert.assertThat(instance.getOutput(), Matchers.is("WORKS!!!"));

Expand Down
2 changes: 1 addition & 1 deletion extension/api/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>extension-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>extension-api</name>
Expand Down
2 changes: 1 addition & 1 deletion extension/junit4/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>extension-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>extension-junit4</name>
Expand Down
2 changes: 1 addition & 1 deletion extension/junit5/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>extension-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>extension-junit5</name>
Expand Down
2 changes: 1 addition & 1 deletion extension/modulesupport/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>extension-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>extension-modulesupport</name>
Expand Down
2 changes: 1 addition & 1 deletion extension/plainjava/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>extension-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>extension-plainjava</name>
Expand Down
2 changes: 1 addition & 1 deletion extension/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.2.0</version>
<version>1.3.0</version>
</parent>

<name>extension-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion extension/testng/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>extension-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>extension-testng</name>
Expand Down
2 changes: 1 addition & 1 deletion integration-test/java9/namedAutomaticModule/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>integration-test-java9-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>integration-test-java9-namedAutomaticModule</name>
Expand Down
2 changes: 1 addition & 1 deletion integration-test/java9/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>integration-test-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>integration-test-java9-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion integration-test/java9/regularTestModule/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>integration-test-java9-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>integration-test-java9-regularModule</name>
Expand Down
2 changes: 1 addition & 1 deletion integration-test/java9/test/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>integration-test-java9-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>integration-test-java9-test</name>
Expand Down
2 changes: 1 addition & 1 deletion integration-test/java9/unnamedAutomaticModule/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>integration-test-java9-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>integration-test-java9-unnamedAutomaticModule</name>
Expand Down
2 changes: 1 addition & 1 deletion integration-test/junit4/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>integration-test-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>integration-test-junit4</name>
Expand Down
2 changes: 1 addition & 1 deletion integration-test/junit5/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>integration-test-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>integration-test-junit5</name>
Expand Down
2 changes: 1 addition & 1 deletion integration-test/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.2.0</version>
<version>1.3.0</version>
</parent>

<name>integration-test-parent</name>
Expand Down
2 changes: 1 addition & 1 deletion integration-test/testng/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>integration-test-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
</parent>

<name>integration-test-testng</name>
Expand Down
2 changes: 1 addition & 1 deletion legacy/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.2.0</version>
<version>1.3.0</version>
</parent>

<name>cute-legacy</name>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>io.toolisticon.cute</groupId>
<artifactId>cute-parent</artifactId>
<version>1.2.0</version>
<version>1.3.0</version>
<packaging>pom</packaging>

<name>cute-parent</name>
Expand Down

0 comments on commit f72b820

Please sign in to comment.