Skip to content

Commit

Permalink
feat(junit5): adjust junit5-extension
Browse files Browse the repository at this point in the history
Related to CAM-11955
  • Loading branch information
mboskamp committed Mar 22, 2022
1 parent 1ae3fa5 commit 391baae
Show file tree
Hide file tree
Showing 35 changed files with 706 additions and 476 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ public static void assertProcessEnded(ProcessEngine processEngine, String proces
ProcessEngineAssert.assertProcessEnded(processEngine, processInstanceId);
}

public static String annotationDeploymentSetUp(ProcessEngine processEngine, Class<?> testClass, String methodName, Deployment deploymentAnnotation) {
public static String annotationDeploymentSetUp(ProcessEngine processEngine, Class<?> testClass, String methodName,
Deployment deploymentAnnotation, Class<?>... parameterTypes) {
Method method = null;
boolean onMethod = true;

try {
method = getMethod(testClass, methodName);
method = getMethod(testClass, methodName, parameterTypes);
} catch (Exception e) {
if (deploymentAnnotation == null) {
// we have neither the annotation, nor can look it up from the method
Expand Down Expand Up @@ -140,7 +141,8 @@ public static String annotationDeploymentSetUp(ProcessEngine processEngine, Stri
return annotationDeploymentSetUp(processEngine, resources, testClass, true, methodName);
}

public static String annotationDeploymentSetUp(ProcessEngine processEngine, String[] resources, Class<?> testClass, boolean onMethod, String methodName) {
public static String annotationDeploymentSetUp(ProcessEngine processEngine, String[] resources, Class<?> testClass,
boolean onMethod, String methodName) {
if (resources != null) {
if (resources.length == 0 && methodName != null) {
String name = onMethod ? methodName : null;
Expand All @@ -162,8 +164,8 @@ public static String annotationDeploymentSetUp(ProcessEngine processEngine, Stri
return null;
}

public static String annotationDeploymentSetUp(ProcessEngine processEngine, Class<?> testClass, String methodName) {
return annotationDeploymentSetUp(processEngine, testClass, methodName, (Deployment) null);
public static String annotationDeploymentSetUp(ProcessEngine processEngine, Class<?> testClass, String methodName, Class<?>... parameterTypes) {
return annotationDeploymentSetUp(processEngine, testClass, methodName, (Deployment) null, parameterTypes);
}

public static void annotationDeploymentTearDown(ProcessEngine processEngine, String deploymentId, Class<?> testClass, String methodName) {
Expand Down Expand Up @@ -234,8 +236,8 @@ private static HistoryLevel getHistoryLevelForName(List<HistoryLevel> historyLev
throw new IllegalArgumentException("Unknown history level: " + name);
}

public static boolean annotationRequiredHistoryLevelCheck(ProcessEngine processEngine, Class<?> testClass, String methodName) {
RequiredHistoryLevel annotation = getAnnotation(processEngine, testClass, methodName, RequiredHistoryLevel.class);
public static boolean annotationRequiredHistoryLevelCheck(ProcessEngine processEngine, Class<?> testClass, String methodName, Class<?>... parameterTypes) {
RequiredHistoryLevel annotation = getAnnotation(processEngine, testClass, methodName, RequiredHistoryLevel.class, parameterTypes);

if (annotation != null) {
return historyLevelCheck(processEngine, annotation);
Expand All @@ -244,18 +246,18 @@ public static boolean annotationRequiredHistoryLevelCheck(ProcessEngine processE
}
}

public static boolean annotationRequiredDatabaseCheck(ProcessEngine processEngine, RequiredDatabase annotation, Class<?> testClass, String methodName) {
public static boolean annotationRequiredDatabaseCheck(ProcessEngine processEngine, RequiredDatabase annotation, Class<?> testClass, String methodName, Class<?>... parameterTypes) {

if (annotation != null) {
return databaseCheck(processEngine, annotation);

} else {
return annotationRequiredDatabaseCheck(processEngine, testClass, methodName);
return annotationRequiredDatabaseCheck(processEngine, testClass, methodName, parameterTypes);
}
}

public static boolean annotationRequiredDatabaseCheck(ProcessEngine processEngine, Class<?> testClass, String methodName) {
RequiredDatabase annotation = getAnnotation(processEngine, testClass, methodName, RequiredDatabase.class);
public static boolean annotationRequiredDatabaseCheck(ProcessEngine processEngine, Class<?> testClass, String methodName, Class<?>... parameterTypes) {
RequiredDatabase annotation = getAnnotation(processEngine, testClass, methodName, RequiredDatabase.class, parameterTypes);

if (annotation != null) {
return databaseCheck(processEngine, annotation);
Expand All @@ -278,16 +280,16 @@ private static boolean databaseCheck(ProcessEngine processEngine, RequiredDataba
}
}
}

String[] includes = annotation.includes();

if (includes != null && includes.length > 0) {
for (String include : includes) {
if (include.equals(actualDbType)) {
return true;
}
}

return false;
} else {
return true;
Expand All @@ -296,12 +298,13 @@ private static boolean databaseCheck(ProcessEngine processEngine, RequiredDataba
}


private static <T extends Annotation> T getAnnotation(ProcessEngine processEngine, Class<?> testClass, String methodName, Class<T> annotationClass) {
private static <T extends Annotation> T getAnnotation(ProcessEngine processEngine, Class<?> testClass,
String methodName, Class<T> annotationClass, Class<?>... parameterTypes) {
Method method = null;
T annotation = null;

try {
method = getMethod(testClass, methodName);
method = getMethod(testClass, methodName, parameterTypes);
annotation = method.getAnnotation(annotationClass);
} catch (Exception e) {
// - ignore if we cannot access the method
Expand All @@ -318,8 +321,8 @@ private static <T extends Annotation> T getAnnotation(ProcessEngine processEngin
return annotation;
}

protected static Method getMethod(Class<?> clazz, String methodName) throws SecurityException, NoSuchMethodException {
return clazz.getMethod(methodName, (Class<?>[]) null);
protected static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) throws SecurityException, NoSuchMethodException {
return clazz.getMethod(methodName, parameterTypes);
}

/**
Expand Down
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
<module>typed-values</module>
<module>engine-dmn</module>
<module>test-utils/testcontainers</module>
<module>test-utils/junit5-extension</module>

<module>engine</module>
<module>engine-cdi</module>
Expand Down Expand Up @@ -276,6 +277,8 @@

<module>clients/java</module>

<module>test-utils/junit5-extension</module>

<module>internal-dependencies</module>
<module>database</module>
<module>parent</module>
Expand Down
60 changes: 60 additions & 0 deletions test-utils/junit5-extension/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Camunda Platform JUnit 5

JUnit 5 extension that allows you to inject a process engine into your test.

## Usage

### Maven dependency
Add the dependency to your pom.xml

```xml
<dependency>
<groupId>org.camunda.bpm</groupId>
<artifactId>camunda-bpm-junit5</artifactId>
<version>7.17.0</version>
<scope>test</scope>
</dependency>
```

### Test code
Add the annotation to your test class:

```java
@ExtendWith(ProcessEngineExtension.class)
```

For further access provide a field where the process engine gets injected:

```java
public ProcessEngine processEngine;
```

Or register the extension from the builder:

```java
@RegisterExtension
ProcessEngineExtension extension = ProcessEngineExtension.builder()
.configurationResource("audithistory.camunda.cfg.xml")
.build();
```

and access the process engine from the extension object:

```java
RuntimeService runtimeService = extension.getProcessEngine().getRuntimeService();
```

If you don't want to create a configuration file, you can add a process engine, that you configure programmatically:

```java
public ProcessEngine myProcessEngine = ProcessEngineConfiguration
.createStandaloneInMemProcessEngineConfiguration()
.setJdbcUrl("jdbc:h2:mem:camunda;DB_CLOSE_DELAY=1000")
.buildProcessEngine();

@RegisterExtension
ProcessEngineExtension extension = ProcessEngineExtension
.builder()
.useProcessEngine(myProcessEngine)
.build();
```
36 changes: 8 additions & 28 deletions test-utils/junit5-extension/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@
<artifactId>camunda-bpm-junit5</artifactId>
<packaging>jar</packaging>

<name>Camunda BPM JUnit 5 Extension</name>
<name>Camunda Platform - JUnit 5 Extension</name>

<parent>
<groupId>org.camunda.bpm.extension</groupId>
<artifactId>camunda-bpm-junit5-parent</artifactId>
<version>1.1.1-SNAPSHOT</version>
<groupId>org.camunda.bpm</groupId>
<artifactId>camunda-database-settings</artifactId>
<relativePath>../../database</relativePath>
<version>7.17.0-SNAPSHOT</version>
</parent>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.camunda.bpm</groupId>
<artifactId>camunda-bom</artifactId>
<version>7.16.0</version>
<artifactId>camunda-core-internal-dependencies</artifactId>
<version>${project.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
Expand All @@ -26,7 +28,6 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.22.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -38,38 +39,17 @@
<dependency>
<groupId>org.camunda.bpm</groupId>
<artifactId>camunda-engine</artifactId>
<version>7.16.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.10</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<configuration>
<source>8</source>
</configuration>
</plugin>

</plugins>
</build>
</project>
Loading

0 comments on commit 391baae

Please sign in to comment.