Skip to content

Commit

Permalink
enh(integration-tests): added integration tests for lifecycle methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lprimak committed Jul 9, 2024
1 parent 513b086 commit 85c2c3a
Show file tree
Hide file tree
Showing 5 changed files with 503 additions and 0 deletions.
237 changes: 237 additions & 0 deletions junit5/integration-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>

<groupId>org.jboss.arquillian.junit5</groupId>
<artifactId>arquillian-junit5-integration-test</artifactId>

<parent>
<groupId>org.jboss.arquillian</groupId>
<version>1.8.2.Final-SNAPSHOT</version>
<artifactId>arquillian-build</artifactId>
<relativePath>../../build</relativePath>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>11</jdk.version>
<maven.compiler.source>${jdk.version}</maven.compiler.source>
<maven.compiler.target>${jdk.version}</maven.compiler.target>
<maven.compiler.release>${jdk.version}</maven.compiler.release>
<payara.start.skip>${skipITs}</payara.start.skip>
<payara.version>6.2024.6</payara.version>
<jakartaArquillianDependencies>1.8.1.Final</jakartaArquillianDependencies>
</properties>

<name>Arquillian JUnit 5 Container Integration Test</name>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jboss.arquillian.junit5</groupId>
<artifactId>arquillian-junit5-container</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>jakarta.inject</groupId>
<artifactId>jakarta.inject-api</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.0</version>
<scope>provided</scope>
</dependency>

<!-- Payara Java EE Container support (container) -->
<dependency>
<groupId>fish.payara.arquillian</groupId>
<artifactId>arquillian-payara-server-remote</artifactId>
<version>3.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.jboss.arquillian.testenricher</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-spi</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.testenricher</groupId>
<artifactId>arquillian-testenricher-cdi-jakarta</artifactId>
<version>${jakartaArquillianDependencies}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.testenricher</groupId>
<artifactId>arquillian-testenricher-ejb-jakarta</artifactId>
<version>${jakartaArquillianDependencies}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.testenricher</groupId>
<artifactId>arquillian-testenricher-initialcontext</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.testenricher</groupId>
<artifactId>arquillian-testenricher-resource-jakarta</artifactId>
<version>${jakartaArquillianDependencies}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.protocol</groupId>
<artifactId>arquillian-protocol-servlet-jakarta</artifactId>
<version>${jakartaArquillianDependencies}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Disable Surefire Plugin - Integration Only -->
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>default-test</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
</plugin>

<!-- Download / Unpack Payara -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.7.1</version>
<executions>
<execution>
<phase>pre-integration-test</phase>
<goals>
<goal>unpack</goal>
</goals>
</execution>
</executions>
<configuration>
<overWriteIfNewer>false</overWriteIfNewer>
<outputDirectory>${basedir}/target</outputDirectory>
<markersDirectory>${basedir}/target/dependency-maven-plugin-markers</markersDirectory>
<artifactItems>
<artifactItem>
<groupId>fish.payara.distributions</groupId>
<artifactId>payara</artifactId>
<version>${payara.version}</version>
<type>zip</type>
</artifactItem>
</artifactItems>
<skip>${payara.start.skip}</skip>
</configuration>
</plugin>

<!-- Start / Stop Payara Domain -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>start-domain</id>
<phase>pre-integration-test</phase>
<configuration>
<arguments>
<argument>start-domain</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
<execution>
<id>stop-domain</id>
<phase>post-integration-test</phase>
<configuration>
<arguments>
<argument>stop-domain</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<skipIfEmpty>true</skipIfEmpty>
</configuration>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>integration-test</id>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/*TestCase.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<executable>${basedir}/target/payara6/bin/asadmin</executable>
<skip>${payara.start.skip}</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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 + "!";
}
}
Loading

0 comments on commit 85c2c3a

Please sign in to comment.