From 05352087723755351c3fb30c595aca4b92acd07e Mon Sep 17 00:00:00 2001 From: lprimak Date: Tue, 2 Jul 2024 17:38:58 -0500 Subject: [PATCH] enh(integration-tests): added integration tests for lifecycle methods --- junit5/integration-test/pom.xml | 237 ++++++++++++++++++ .../junit5/lifecycle/FileWriterExtension.java | 122 +++++++++ .../arquillian/junit5/lifecycle/Greeter.java | 34 +++ .../lifecycle/LifecycleMethodsTestCase.java | 98 ++++++++ junit5/pom.xml | 12 + 5 files changed, 503 insertions(+) create mode 100644 junit5/integration-test/pom.xml create mode 100644 junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/FileWriterExtension.java create mode 100644 junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/Greeter.java create mode 100644 junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/LifecycleMethodsTestCase.java diff --git a/junit5/integration-test/pom.xml b/junit5/integration-test/pom.xml new file mode 100644 index 000000000..aaa624984 --- /dev/null +++ b/junit5/integration-test/pom.xml @@ -0,0 +1,237 @@ + + + 4.0.0 + + org.jboss.arquillian.junit5 + arquillian-junit5-integration-test + + + org.jboss.arquillian + 1.8.2.Final-SNAPSHOT + arquillian-build + ../../build + + + + UTF-8 + 11 + ${jdk.version} + ${jdk.version} + ${jdk.version} + ${skipITs} + 6.2024.6 + 1.8.1.Final + + + Arquillian JUnit 5 Container Integration Test + + + + org.junit.jupiter + junit-jupiter-api + test + + + + org.jboss.arquillian.junit5 + arquillian-junit5-container + ${project.version} + test + + + + jakarta.inject + jakarta.inject-api + 2.0.1 + provided + + + jakarta.enterprise + jakarta.enterprise.cdi-api + 4.0.1 + provided + + + jakarta.activation + jakarta.activation-api + 2.1.0 + provided + + + + + fish.payara.arquillian + arquillian-payara-server-remote + 3.0 + test + + + org.jboss.arquillian.testenricher + * + + + org.jboss.arquillian.protocol + * + + + org.jboss.arquillian.container + * + + + + + org.jboss.arquillian.container + arquillian-container-spi + ${project.version} + test + + + org.jboss.arquillian.testenricher + arquillian-testenricher-cdi-jakarta + ${jakartaArquillianDependencies} + test + + + org.jboss.arquillian.testenricher + arquillian-testenricher-ejb-jakarta + ${jakartaArquillianDependencies} + test + + + org.jboss.arquillian.testenricher + arquillian-testenricher-initialcontext + ${project.version} + test + + + org.jboss.arquillian.testenricher + arquillian-testenricher-resource-jakarta + ${jakartaArquillianDependencies} + test + + + org.jboss.arquillian.protocol + arquillian-protocol-servlet-jakarta + ${jakartaArquillianDependencies} + test + + + + + + + + maven-surefire-plugin + + + default-test + none + + + + + maven-failsafe-plugin + + + + + maven-dependency-plugin + 3.7.1 + + + pre-integration-test + + unpack + + + + + false + ${basedir}/target + ${basedir}/target/dependency-maven-plugin-markers + + + fish.payara.distributions + payara + ${payara.version} + zip + + + ${payara.start.skip} + + + + + + org.codehaus.mojo + exec-maven-plugin + + + start-domain + pre-integration-test + + + start-domain + + + + exec + + + + stop-domain + post-integration-test + + + stop-domain + + + + exec + + + + + + org.apache.maven.plugins + maven-jar-plugin + + true + + + + + + + + maven-failsafe-plugin + 3.3.0 + + + integration-test + + integration-test + verify + + + + + + **/*TestCase.java + + + + + org.codehaus.mojo + exec-maven-plugin + 3.3.0 + + ${basedir}/target/payara6/bin/asadmin + ${payara.start.skip} + + + + + + diff --git a/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/FileWriterExtension.java b/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/FileWriterExtension.java new file mode 100644 index 000000000..cabccb5fb --- /dev/null +++ b/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/FileWriterExtension.java @@ -0,0 +1,122 @@ +/* + * Copyright 2021 JBoss by Red Hat. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.arquillian.junit5.lifecycle; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; + +/** + * + * @author lprimak + */ +public class FileWriterExtension implements BeforeAllCallback, ExtensionContext.Store.CloseableResource { + private static Path TMP_FILE_PATH; + static final String TMP_FILE_ASSET_NAME = "temporaryFileAsset"; + + enum RunsWhere { + CLIENT, + SERVER, + } + + @Override + public void beforeAll(ExtensionContext extensionContext) throws Exception { + if (!isRunningOnServer()) { + Path tempDir = Files.createTempDirectory("arquillianLifecycleTest"); + initTmpFileName(tempDir); + assertTrue(tempDir.toFile().delete(), "Cleanup Failed"); + createTmpFile(); + } + extensionContext.getRoot().getStore(ExtensionContext.Namespace.GLOBAL).put(this.getClass().getName(), this); + } + + @Override + public void close() throws Throwable { + if (isRunningOnServer()) { + return; + } + String contents; + try (FileReader fileReader = new FileReader(getTmpFilePath().toFile())) { + char[] buf = new char[1000]; + int length = fileReader.read(buf); + contents = new String(buf, 0, length - 1); + } + Path tempDir = getTmpFilePath().getParent(); + Files.walk(tempDir).map(Path::toFile).forEach(File::delete); + assertTrue(tempDir.toFile().delete(), "Cleanup Failed"); + assertEquals("before_all,before_each,test_one,after_each,before_each,test_two,after_each,after_all", contents); + } + + static void initTmpFileName(Path tmpDirBase) { + TMP_FILE_PATH = tmpDirBase.getParent().resolve( + tmpDirBase.getFileName() + "-arquillianLifecycleTest") + .resolve("lifecycleOutput"); + } + + void createTmpFile() throws IOException { + TMP_FILE_PATH.getParent().toFile().mkdir(); + File tmpFile = TMP_FILE_PATH.toFile(); + tmpFile.delete(); + assertTrue(tmpFile.createNewFile(), "cannot create results file"); + } + + static Path getTmpFilePath() { + if (isRunningOnServer()) { + try (InputStream istrm = Thread.currentThread().getContextClassLoader() + .getResourceAsStream(TMP_FILE_ASSET_NAME)) { + return Paths.get(new BufferedReader(new InputStreamReader(istrm)).readLine()); + } catch (IOException ioe) { + throw new RuntimeException(ioe); + } + } else { + return TMP_FILE_PATH; + } + } + + static void appendToFile(String str) { + try (FileWriter fw = new FileWriter(getTmpFilePath().toFile(), true)) { + fw.append(str + ","); + } catch (IOException ex) { + throw new RuntimeException(ex); + } + } + + static void checkRunsWhere(RunsWhere expected) { + assertEquals(expected, isRunningOnServer() ? RunsWhere.SERVER : RunsWhere.CLIENT); + } + + static boolean isRunningOnServer() { + try { + new InitialContext().lookup("java:comp/env"); + return true; + } catch (NamingException ex) { + return false; + } + } +} diff --git a/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/Greeter.java b/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/Greeter.java new file mode 100644 index 000000000..92d231059 --- /dev/null +++ b/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/Greeter.java @@ -0,0 +1,34 @@ +/* + * Copyright 2021 JBoss by Red Hat. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.arquillian.junit5.lifecycle; + +import jakarta.enterprise.context.Dependent; +import java.io.PrintStream; + +/** + * + * @author lprimak + */ +@Dependent +public class Greeter { + public void greet(PrintStream to, String name) { + to.println(createGreeting(name)); + } + + public String createGreeting(String name) { + return "Hello, " + name + "!"; + } +} diff --git a/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/LifecycleMethodsTestCase.java b/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/LifecycleMethodsTestCase.java new file mode 100644 index 000000000..1e3f8cdf4 --- /dev/null +++ b/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/LifecycleMethodsTestCase.java @@ -0,0 +1,98 @@ +/* + * Copyright 2021 JBoss by Red Hat. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.arquillian.junit5.lifecycle; + +import jakarta.inject.Inject; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit5.ArquillianExtension; +import static org.jboss.arquillian.junit5.lifecycle.FileWriterExtension.RunsWhere.CLIENT; +import static org.jboss.arquillian.junit5.lifecycle.FileWriterExtension.RunsWhere.SERVER; +import static org.jboss.arquillian.junit5.lifecycle.FileWriterExtension.TMP_FILE_ASSET_NAME; +import static org.jboss.arquillian.junit5.lifecycle.FileWriterExtension.appendToFile; +import static org.jboss.arquillian.junit5.lifecycle.FileWriterExtension.checkRunsWhere; +import static org.jboss.arquillian.junit5.lifecycle.FileWriterExtension.getTmpFilePath; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.StringAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** + * + * @author lprimak + */ +@ExtendWith(FileWriterExtension.class) +@ExtendWith(ArquillianExtension.class) +public class LifecycleMethodsTestCase { + @Inject + Greeter greeter; + + @Test + @Order(1) + void one() { + assertEquals("one", "one"); + appendToFile("test_one"); + checkRunsWhere(SERVER); + } + + @Test + @Order(2) + void should_create_greeting() { + assertEquals("Hello, Earthling!", greeter.createGreeting("Earthling")); + greeter.greet(System.out, "Earthling"); + appendToFile("test_two"); + checkRunsWhere(SERVER); + } + + @BeforeAll + static void beforeAll() { + appendToFile("before_all"); + checkRunsWhere(CLIENT); + } + + @BeforeEach + void beforeEach() { + appendToFile("before_each"); + checkRunsWhere(SERVER); + } + + @AfterEach + void afterEeach() { + appendToFile("after_each"); + checkRunsWhere(SERVER); + } + + @AfterAll + static void afterAll() { + appendToFile("after_all"); + checkRunsWhere(CLIENT); + } + + + @Deployment + static JavaArchive createDeployment() { + JavaArchive jar = ShrinkWrap.create(JavaArchive.class) + .addClass(FileWriterExtension.class) + .addAsResource(new StringAsset(getTmpFilePath().toString()), TMP_FILE_ASSET_NAME); + return jar; + } +} diff --git a/junit5/pom.xml b/junit5/pom.xml index 59798d7cb..970adb4d3 100644 --- a/junit5/pom.xml +++ b/junit5/pom.xml @@ -29,4 +29,16 @@ container + + + activate-integration-tests + + [11,) + + + integration-test + + + +