Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Junit5 support #22

Open
wants to merge 41 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
da7e2ba
Initial POC of Testing JUnit 5 Support in JMAQS
May 26, 2020
1d5c27a
Initial POC of Testing JUnit 5 Support in JMAQS
May 27, 2020
df847b4
Merge branch 'main' into junit5-support
jredingcsv May 6, 2022
f418a20
pull in main
jredingcsv May 6, 2022
29aa533
delete old base content, put junit content in the new maqs base
jredingcsv May 6, 2022
0a00439
Merge branch 'main' into junit5-support
jredingcsv Aug 11, 2022
ad12753
updated and set JUnit tests up
jredingcsv Aug 11, 2022
8d8971c
comments and copyright update
jredingcsv Aug 11, 2022
3f8d3dd
checkstyle updates
jredingcsv Aug 11, 2022
01a0ab2
checkstyle updates
jredingcsv Aug 11, 2022
8c8e3f3
checkstyle updates
jredingcsv Aug 11, 2022
a908d46
checkstyle updates
jredingcsv Aug 11, 2022
11c9b87
checkstyle updates
jredingcsv Aug 11, 2022
42ea7e9
checkstyle updates
jredingcsv Aug 11, 2022
4c70b59
code smells
jredingcsv Aug 11, 2022
8d55c69
code smells
jredingcsv Aug 11, 2022
9781d7e
fixing code smells and bugs
jredingcsv Aug 12, 2022
4a21f15
fixing code smells and bugs
jredingcsv Aug 12, 2022
607fb74
fixing code smells
jredingcsv Aug 12, 2022
01b372b
fixing code smells
jredingcsv Aug 12, 2022
38b308b
organizing code
jredingcsv Aug 15, 2022
c5c76e8
Merge branch 'main' into junit5-support
jredingcsv Sep 14, 2022
04a5587
Merge branch 'main' into junit5-support
jredingcsv Oct 24, 2022
7c07dcc
merge with main
jredingcsv Oct 24, 2022
40b6639
checkstyle changes
jredingcsv Oct 24, 2022
a880ca4
Merge branch 'main' into junit5-support
jredingcsv Nov 11, 2022
6f26aef
Merge branch 'main' into junit5-support
jonreding2010 Feb 4, 2023
67e0bd7
fix main merging issues
jonreding2010 Feb 4, 2023
ce32bf9
checkstyle updates
jonreding2010 Feb 4, 2023
d584144
checkstyle updates
jonreding2010 Feb 4, 2023
8bc2d3a
resolve junit unit tests issues
jonreding2010 Feb 7, 2023
b7c07cd
some more code coverage
jonreding2010 Feb 7, 2023
08539f2
Merge branch 'main' into junit5-support
jonreding2010 Feb 18, 2023
dae4d67
Merge branch 'main' into junit5-support
jonreding2010 Feb 27, 2023
56e30d6
Merge branch 'main' into junit5-support
jonreding2010 Mar 18, 2023
aa10ab0
Merge branch 'main' into junit5-support
jonreding2010 Apr 14, 2023
ca339d2
Merge branch 'main' into junit5-support
jonreding2010 Jul 24, 2023
e4e883a
update junit version
jonreding2010 Jul 24, 2023
663c84d
Merge branch 'main' into junit5-support
jonreding2010 Oct 31, 2023
04148b5
Merge branch 'main' into junit5-support
jonreding2010 Oct 31, 2023
0205564
Merge branch 'main' into junit5-support
jonreding2010 Jan 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions openmaqs-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<version>${revision}</version>
<packaging>jar</packaging>

<properties>
<junit.version>5.9.2</junit.version>
</properties>

<dependencies>
<dependency>
<groupId>io.github.openmaqs.utilities</groupId>
Expand All @@ -24,5 +28,15 @@
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ protected void beforeLoggingTeardown(ITestResult resultType) {
@Override
protected void createNewTestObject() {
this.setTestObject(
new BaseTestObject(this.createLogger(), this.getFullyQualifiedTestClassName()));
new BaseTestObject(this.createLogger(), this.getFullyQualifiedTestClassName()));
}
}
102 changes: 96 additions & 6 deletions openmaqs-base/src/main/java/io/github/openmaqs/base/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static java.lang.System.out;

import io.github.openmaqs.base.exceptions.MAQSRuntimeException;
import io.github.openmaqs.base.interfaces.TestResult;
import io.github.openmaqs.base.watcher.JunitTestWatcher;
import io.github.openmaqs.utilities.helper.StringProcessor;
import io.github.openmaqs.utilities.logging.ConsoleLogger;
import io.github.openmaqs.utilities.logging.FileLogger;
Expand All @@ -16,6 +18,7 @@
import io.github.openmaqs.utilities.logging.LoggingConfig;
import io.github.openmaqs.utilities.logging.LoggingEnabled;
import io.github.openmaqs.utilities.logging.MessageType;
import io.github.openmaqs.utilities.logging.TestResultType;
import io.github.openmaqs.utilities.performance.IPerfTimerCollection;
import io.github.openmaqs.utilities.performance.PerfTimerCollection;
import java.lang.reflect.Method;
Expand All @@ -27,15 +30,19 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

/**
* The Base test class.
* The Base Test class.
*/
public abstract class BaseTest {

Expand All @@ -54,6 +61,11 @@ public abstract class BaseTest {
*/
private ITestResult testResult;

/**
* The JUnit test result object.
*/
private TestResult junitTestResult;

/**
* The Collection of Base Test Objects to use.
*/
Expand All @@ -74,12 +86,17 @@ public abstract class BaseTest {
*/
ThreadLocal<String> fullyQualifiedTestClassName = new ThreadLocal<>();


@RegisterExtension
public JunitTestWatcher testWatcher;

/**
* Initializes a new instance of the BaseTest class.
*/
protected BaseTest() {
this.loggedExceptions = new ConcurrentHashMap<>();
this.baseTestObjects = new ConcurrentManagerHashMap();
this.testWatcher = new JunitTestWatcher(this);
}

/**
Expand Down Expand Up @@ -215,7 +232,36 @@ public void setTestObject(BaseTestObject baseTestObject) {
}

/**
* Setup before a test.
* Setup before a JUnit test.
*
* @param info the test info of the unit test being run
*/
@BeforeEach
public void setup(TestInfo info) {
String testClassName = null;
String testMethodName = null;

// Get the Fully Qualified Test Class Name and set it in the object
if (info.getTestClass().isPresent()) {
Optional<Class<?>> optional = info.getTestClass();
if (optional.isPresent()) {
testClassName = optional.get().getName();
}
}

if (info.getTestMethod().isPresent()) {
Optional<Method> optional = info.getTestMethod();
if (optional.isPresent()) {
testMethodName = optional.get().getName();
}
}

String testName = testClassName + "." + testMethodName;
customSetup(testName);
}

/**
* Setup before a testng unit test.
*
* @param method The initial executing Method object
* @param testContext The initial executing Test Context object
Expand All @@ -237,22 +283,25 @@ public void setup(Method method, ITestContext testContext) {
*/
public void customSetup(String testName, ITestContext testContext) {
this.testContextInstance = testContext;
customSetup(testName);
}

private void customSetup(String testName) {
testName = testName.replaceFirst("class ", "");
this.fullyQualifiedTestClassName.set(testName);

this.createNewTestObject();
}

/**
* Cleanup after a test.
* Cleanup after a TestNG test.
*/
@AfterMethod(alwaysRun = true)
public void teardown() {
try {
this.beforeLoggingTeardown(testResult);
} catch (Exception e) {
this.tryToLog(MessageType.WARNING, "Failed before logging teardown because: %s", e.getMessage());
this.tryToLog(MessageType.WARNING, "Failed before logging teardown because: %s",
e.getMessage());
}

// Log the test result
Expand Down Expand Up @@ -280,6 +329,42 @@ public void teardown() {
this.tryToLog(MessageType.WARNING, "Failed to cleanup log files because: %s", e.getMessage());
}

cleanUpTest();
}

/**
* Cleanup after a JUnit test.
*/
public void teardownJunit() {
// Log the test result
if (junitTestResult.isSuccess()) {
this.tryToLog(MessageType.SUCCESS, "Test Passed");
} else if (junitTestResult.getStatus() == TestResultType.FAIL) {
this.tryToLog(MessageType.ERROR, "Test Failed");
} else if (junitTestResult.getStatus() == TestResultType.SKIP) {
this.tryToLog(MessageType.INFORMATION, "Test was skipped");
} else {
this.tryToLog(MessageType.WARNING, "Test had an unexpected result.");
}

// Cleanup log files we don't want
try {
if ((this.getLogger() instanceof FileLogger)
&& junitTestResult.getStatus() == TestResultType.PASS
&& this.loggingEnabledSetting == LoggingEnabled.ONFAIL) {
Files.delete(Paths.get(((FileLogger) this.getLogger()).getFilePath()));
}
} catch (Exception e) {
this.tryToLog(MessageType.WARNING, "Failed to cleanup log files because: %s", e.getMessage());
}

cleanUpTest();
}

/**
* Cleans up logs and files after the test.
*/
private void cleanUpTest() {
// Get the Fully Qualified Test Name
String fullyQualifiedTestName = this.fullyQualifiedTestClassName.get();

Expand Down Expand Up @@ -310,6 +395,10 @@ public void setTestResult(ITestResult testResult) {
this.testResult = testResult;
}

public void setTestResult(TestResult testResult) {
this.junitTestResult = testResult;
}

/**
* Steps to take before logging teardown results.
*
Expand All @@ -330,7 +419,8 @@ protected ILogger createLogger() {
if (this.loggingEnabledSetting != LoggingEnabled.NO) {
return LoggerFactory.getLogger(
StringProcessor.safeFormatter("%s - %s", this.fullyQualifiedTestClassName.get(),
DateTimeFormatter.ofPattern(Logger.DEFAULT_DATE_TIME_FORMAT,
DateTimeFormatter.ofPattern(
Logger.DEFAULT_DATE_TIME_FORMAT,
Locale.getDefault()).format(LocalDateTime.now(Clock.systemUTC()))));
} else {
return LoggerFactory.getConsoleLogger();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2023 (C) OpenMAQS, All rights Reserved
*/

package io.github.openmaqs.base.interfaces;

import io.github.openmaqs.utilities.logging.TestResultType;

public class JunitTestResult implements TestResult {
private final TestResultType currentStatus;

public JunitTestResult(TestResultType status) {
this.currentStatus = status;
}

@Override
public TestResultType getStatus() {
return this.currentStatus;
}

@Override
public boolean isSuccess() {
return currentStatus == TestResultType.PASS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright 2023 (C) OpenMAQS, All rights Reserved
*/

package io.github.openmaqs.base.interfaces;

import io.github.openmaqs.utilities.logging.TestResultType;

public interface TestResult {
TestResultType getStatus();

boolean isSuccess();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2023 (C) OpenMAQS, All rights Reserved
*/

package io.github.openmaqs.base.watcher;

import io.github.openmaqs.base.BaseTest;
import io.github.openmaqs.base.interfaces.JunitTestResult;
import io.github.openmaqs.utilities.logging.TestResultType;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestWatcher;

/**
* The JUnit Test Watcher class.
*/
public class JunitTestWatcher implements TestWatcher {

// Test instance of the test being executed per each thread.
BaseTest testInstance;

public JunitTestWatcher(BaseTest testInstance) {
this.testInstance = testInstance;
}

@Override
public void testSuccessful(ExtensionContext context) {
this.testInstance.setTestResult(new JunitTestResult(TestResultType.PASS));
this.testInstance.teardownJunit();
}

@Override
public void testFailed(ExtensionContext context, Throwable cause) {
this.testInstance.setTestResult(new JunitTestResult(TestResultType.FAIL));
this.testInstance.teardownJunit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright 2023 (C) OpenMAQS, All rights Reserved
*/

package io.github.openmaqs.base.junit;

import io.github.openmaqs.base.BaseGenericTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class JUnitBaseGenericTestUnitTest extends BaseGenericTest {

@Test
void testCreateTestObject() {
Assertions.assertNotNull(this.getTestObject());
}
}
Loading
Loading