From 545629f2fd0f365cba5e78fca49654de889b6011 Mon Sep 17 00:00:00 2001 From: Tobias Stamann Date: Wed, 10 Apr 2024 16:33:27 +0200 Subject: [PATCH] [#91] Automaticylly add cute module for unittests including a module-info.java --- .../java/io/toolisticon/cute/CompileTest.java | 13 ++++++++++++- .../integrationtest/java9/IntegrationTest.java | 16 ++++++++++++++++ .../resources/testcases/unitTest/Test.java | 18 ++++++++++++++++++ .../testcases/unitTest/module-info.java | 5 +++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 integration-test/java9/test/src/test/resources/testcases/unitTest/Test.java create mode 100644 integration-test/java9/test/src/test/resources/testcases/unitTest/module-info.java diff --git a/cute/src/main/java/io/toolisticon/cute/CompileTest.java b/cute/src/main/java/io/toolisticon/cute/CompileTest.java index 88eca36..6e8844b 100644 --- a/cute/src/main/java/io/toolisticon/cute/CompileTest.java +++ b/cute/src/main/java/io/toolisticon/cute/CompileTest.java @@ -14,6 +14,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -261,7 +262,13 @@ public static CompilationResult compile(CuteApi.CompilerTestBB compileTestConfig if (!Java9SupportCheck.UNSUPPORTED_JAVA_VERSION) { ModuleSupportSpi moduleService = ModuleSupportSpiServiceLocator.locate(); if (moduleService != null) { - moduleService.applyModulePath(stdJavaFileManager, compilationTask, compileTestConfiguration.modules()); + + Set modulePath = new HashSet<>(compileTestConfiguration.modules()); + if (compileTestConfiguration.testType() == CuteApi.TestType.UNIT && hasModuleInfoSourceFile(compileTestConfiguration)) { + modulePath.add("cute"); + } + + moduleService.applyModulePath(stdJavaFileManager, compilationTask, modulePath); } } @@ -271,6 +278,10 @@ public static CompilationResult compile(CuteApi.CompilerTestBB compileTestConfig } + private static boolean hasModuleInfoSourceFile(CuteApi.CompilerTestBB compileTestConfiguration){ + return compileTestConfiguration.sourceFiles().stream().filter(sourceFile -> sourceFile.getName().endsWith("/module-info.java")).count() > 0; + } + /** * Allows checking if annotation processor has been applied during the compilation test. * diff --git a/integration-test/java9/test/src/test/java/io/toolisticon/cute/integrationtest/java9/IntegrationTest.java b/integration-test/java9/test/src/test/java/io/toolisticon/cute/integrationtest/java9/IntegrationTest.java index 69aab22..2d73980 100644 --- a/integration-test/java9/test/src/test/java/io/toolisticon/cute/integrationtest/java9/IntegrationTest.java +++ b/integration-test/java9/test/src/test/java/io/toolisticon/cute/integrationtest/java9/IntegrationTest.java @@ -5,6 +5,9 @@ import io.toolisticon.cute.integrationtest.javanine.namednonmodule.NamedAutomaticModuleTestClass; import org.junit.Test; +import javax.lang.model.element.TypeElement; +import javax.tools.Diagnostic; + /** * Java 9+ related integration tests. * Addresses mainly the jigsaw module system. @@ -80,5 +83,18 @@ public void testBindUnnamedAutomaticJavaModule() { } + @Test + public void testUnitTestWithModules() { + Cute.unitTest().given().useSourceFilesFromFolders("/testcases/unitTest") + .when() + .passInElement().fromGivenSourceFiles() + .intoUnitTest( ((processingEnvironment, element) -> { + processingEnvironment.getMessager().printMessage(Diagnostic.Kind.NOTE,"IT WORKED!!!"); + })) + .thenExpectThat().compilationSucceeds() + .andThat().compilerMessage().ofKindNote().equals("IT WORKED!!!" ) + .executeTest(); + } + } diff --git a/integration-test/java9/test/src/test/resources/testcases/unitTest/Test.java b/integration-test/java9/test/src/test/resources/testcases/unitTest/Test.java new file mode 100644 index 0000000..9636d4e --- /dev/null +++ b/integration-test/java9/test/src/test/resources/testcases/unitTest/Test.java @@ -0,0 +1,18 @@ +package io.toolisticon.cute.integrationtest.javanine; + +import io.toolisticon.cute.PassIn; + +@PassIn +public class Test { + + public static void testCall() { + + } + + public static void secondTestCall(String testClass) { + + } + + +} + diff --git a/integration-test/java9/test/src/test/resources/testcases/unitTest/module-info.java b/integration-test/java9/test/src/test/resources/testcases/unitTest/module-info.java new file mode 100644 index 0000000..43fb25c --- /dev/null +++ b/integration-test/java9/test/src/test/resources/testcases/unitTest/module-info.java @@ -0,0 +1,5 @@ +module io.toolisticon.cute.integrationtest.javanine { + exports io.toolisticon.cute.integrationtest.javanine; + requires cute; +} +