From eb7e3083e854a7d9de630b701edd7b82543a017b Mon Sep 17 00:00:00 2001 From: lprimak Date: Sun, 20 Jun 2021 01:11:16 -0400 Subject: [PATCH] Added integration tests for lifecycle methods --- junit5/integration-test/pom.xml | 228 ++++++++++++++++++ .../junit5/lifecycle/CreateFileTestCase.java | 95 ++++++++ .../arquillian/junit5/lifecycle/Greeter.java | 32 +++ .../junit5/lifecycle/OneTestCase.java | 100 ++++++++ .../junit5/lifecycle/XCheckFileTestCase.java | 46 ++++ junit5/pom.xml | 1 + 6 files changed, 502 insertions(+) create mode 100644 junit5/integration-test/pom.xml create mode 100644 junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/CreateFileTestCase.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/OneTestCase.java create mode 100644 junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/XCheckFileTestCase.java diff --git a/junit5/integration-test/pom.xml b/junit5/integration-test/pom.xml new file mode 100644 index 000000000..7b1166f33 --- /dev/null +++ b/junit5/integration-test/pom.xml @@ -0,0 +1,228 @@ + + + 4.0.0 + + org.jboss.arquillian.junit5 + arquillian-junit5-integration-test + + + org.jboss.arquillian + 1.7.0.Final-SNAPSHOT + arquillian-build + ../../build + + + + UTF-8 + 1.8 + 1.8 + ${skipITs} + 5.2021.2 + + + Arquillian JUnit 5 Container Integration Test + + + + jakarta.inject + jakarta.inject-api + 1.0.3 + provided + + + jakarta.activation + jakarta.activation-api + 1.2.2 + provided + + + jakarta.xml.bind + jakarta.xml.bind-api + 2.3.3 + provided + + + + org.junit.jupiter + junit-jupiter-api + test + + + + org.jboss.arquillian.junit5 + arquillian-junit5-container + ${project.version} + test + + + + + fish.payara.arquillian + arquillian-payara-server-remote + 2.4.1 + 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 + ${project.version} + test + + + org.jboss.arquillian.testenricher + arquillian-testenricher-ejb + ${project.version} + test + + + org.jboss.arquillian.testenricher + arquillian-testenricher-initialcontext + ${project.version} + test + + + org.jboss.arquillian.testenricher + arquillian-testenricher-resource + ${project.version} + test + + + org.jboss.arquillian.protocol + arquillian-protocol-servlet + ${project.version} + test + + + + + + + + maven-surefire-plugin + + + default-test + none + + + + + maven-failsafe-plugin + + + + + maven-dependency-plugin + 3.1.2 + + + 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 + + + + + + + + + + maven-failsafe-plugin + 3.0.0-M5 + + + integration-test + + integration-test + verify + + + + + alphabetical + + **/*TestCase.java + + + + + org.codehaus.mojo + exec-maven-plugin + 3.0.0 + + ${basedir}/target/payara5/bin/asadmin + ${payara.start.skip} + + + + + + diff --git a/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/CreateFileTestCase.java b/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/CreateFileTestCase.java new file mode 100644 index 000000000..4b72c4af0 --- /dev/null +++ b/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/CreateFileTestCase.java @@ -0,0 +1,95 @@ +/* + * 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.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +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.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +/** + * + * @author lprimak + */ +public class CreateFileTestCase { + private static Path TMP_FILE_PATH; + static final String TMP_FILE_ASSET_NAME = "temporaryFileAsset"; + + enum RunsWhere { + CLIENT, + SERVER, + } + + @BeforeAll + static void initTmpFileName(@TempDir Path tmpDirBase) { + TMP_FILE_PATH = tmpDirBase.getParent().resolve( + tmpDirBase.getFileName() + "-arquillianLifecycleTest") + .resolve("lifecycleOutput"); + } + + @Test + 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..c746fd7ff --- /dev/null +++ b/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/Greeter.java @@ -0,0 +1,32 @@ +/* + * 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.PrintStream; + +/** + * + * @author lprimak + */ +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/OneTestCase.java b/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/OneTestCase.java new file mode 100644 index 000000000..a9e1a9ae5 --- /dev/null +++ b/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/OneTestCase.java @@ -0,0 +1,100 @@ +/* + * 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 javax.inject.Inject; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit5.ArquillianExtension; +import static org.jboss.arquillian.junit5.lifecycle.CreateFileTestCase.RunsWhere.CLIENT; +import static org.jboss.arquillian.junit5.lifecycle.CreateFileTestCase.RunsWhere.SERVER; +import static org.jboss.arquillian.junit5.lifecycle.CreateFileTestCase.TMP_FILE_ASSET_NAME; +import static org.jboss.arquillian.junit5.lifecycle.CreateFileTestCase.appendToFile; +import static org.jboss.arquillian.junit5.lifecycle.CreateFileTestCase.checkRunsWhere; +import static org.jboss.arquillian.junit5.lifecycle.CreateFileTestCase.getTmpFilePath; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +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(ArquillianExtension.class) +public class OneTestCase { + @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(Greeter.class) + .addClass(CreateFileTestCase.class) + .addAsResource(new StringAsset(getTmpFilePath().toString()), TMP_FILE_ASSET_NAME) + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + return jar; + } +} diff --git a/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/XCheckFileTestCase.java b/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/XCheckFileTestCase.java new file mode 100644 index 000000000..ff2fa7e82 --- /dev/null +++ b/junit5/integration-test/src/test/java/org/jboss/arquillian/junit5/lifecycle/XCheckFileTestCase.java @@ -0,0 +1,46 @@ +/* + * 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.File; +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; +import static org.jboss.arquillian.junit5.lifecycle.CreateFileTestCase.getTmpFilePath; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * + * @author lprimak + */ +// ensure this runs last +public class XCheckFileTestCase { + @Test + void check() throws IOException { + try (FileReader fileReader = new FileReader(getTmpFilePath().toFile())) { + char[] buf = new char[1000]; + int length = fileReader.read(buf); + String contents = new String(buf, 0, length - 1); + assertEquals("before_all,before_each,test_one,after_each,before_each,test_two,after_each,after_all", contents); + } + Path tempDir = getTmpFilePath().getParent(); + Files.walk(tempDir).map(Path::toFile).forEach(File::delete); + assertTrue(tempDir.toFile().delete(), "Cleanup Failed"); + } +} diff --git a/junit5/pom.xml b/junit5/pom.xml index 322beeda9..98601b3e0 100644 --- a/junit5/pom.xml +++ b/junit5/pom.xml @@ -27,6 +27,7 @@ core container + integration-test