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

Add TestNG SQL Listener #175

Merged
merged 2 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion testng/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<module>testng-test-util</module>
<module>testng-sql-test</module>
<module>testng-jvm-test</module>
<module>testng-sql-listener</module>
</modules>

<build>
Expand All @@ -52,4 +53,4 @@
</plugins>
</build>

</project>
</project>
53 changes: 53 additions & 0 deletions testng/testng-sql-listener/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
~
~ Copyright 2019-2021 the original author or authors.
-->

<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">
<parent>
<artifactId>quick-perf-testng-parent</artifactId>
<groupId>org.quickperf</groupId>
<version>1.1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>quick-perf-testng-sql-listener</artifactId>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<dependencies.max.jdk.version>1.8</dependencies.max.jdk.version>
</properties>

<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.quickperf</groupId>
<artifactId>quick-perf-core</artifactId>
<version>1.1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.quickperf</groupId>
<artifactId>quick-perf-testng-test-util</artifactId>
<version>1.1.1-SNAPSHOT</version>
<classifier>tests</classifier>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.
*
* Copyright 2019-2021 the original author or authors.
*/

package org.quickperf.testng;

import org.quickperf.TestExecutionContext;
import org.quickperf.config.library.QuickPerfConfigs;
import org.quickperf.config.library.QuickPerfConfigsLoader;
import org.quickperf.config.library.SetOfAnnotationConfigs;
import org.quickperf.issue.JvmOrTestIssue;
import org.quickperf.issue.PerfIssuesEvaluator;
import org.quickperf.issue.PerfIssuesToFormat;
import org.quickperf.perfrecording.PerformanceRecording;
import org.quickperf.reporter.QuickPerfReporter;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;

import java.lang.reflect.Method;
import java.util.Collection;

public class QuickPerfSqlTestNGListener implements IInvokedMethodListener {

private final QuickPerfConfigs quickPerfConfigs = QuickPerfConfigsLoader.INSTANCE.loadQuickPerfConfigs();

private final PerfIssuesEvaluator perfIssuesEvaluator = PerfIssuesEvaluator.INSTANCE;

private final PerformanceRecording performanceRecording = PerformanceRecording.INSTANCE;

private final QuickPerfReporter quickPerfReporter = QuickPerfReporter.INSTANCE;

private TestExecutionContext testExecutionContext;

@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
TestExecutionContext testExecutionContext = buildTestExecutionContextFrom(method);
this.testExecutionContext = testExecutionContext;

if (!this.testExecutionContext.isQuickPerfDisabled()) {
performanceRecording.start(this.testExecutionContext);
}
}

private TestExecutionContext buildTestExecutionContextFrom(IInvokedMethod method) {
ITestNGMethod testNGMethod = method.getTestMethod();
Method testMethod = testNGMethod.getConstructorOrMethod().getMethod();
int noAllocationOffsetBecauseOnlyOneJvm = 0;
return TestExecutionContext.buildFrom(quickPerfConfigs, testMethod, noAllocationOffsetBecauseOnlyOneJvm);
}

@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {

if(!testExecutionContext.isQuickPerfDisabled()) {
performanceRecording.stop(testExecutionContext);
}

SetOfAnnotationConfigs testAnnotationConfigs = quickPerfConfigs.getTestAnnotationConfigs();

// Test issue should be taken into account
JvmOrTestIssue jvmOrTestIssue = JvmOrTestIssue.NONE;

Collection<PerfIssuesToFormat> groupOfPerfIssuesToFormat =
perfIssuesEvaluator.evaluatePerfIssuesIfNoJvmIssue( testAnnotationConfigs
, testExecutionContext
, jvmOrTestIssue);

testExecutionContext.cleanResources();

try {
quickPerfReporter.report(jvmOrTestIssue, groupOfPerfIssuesToFormat, testExecutionContext);
} catch (Throwable throwable) {
testResult.setThrowable(throwable);
testResult.setStatus(ITestResult.FAILURE);
}

}

}


Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.
*
* Copyright 2019-2021 the original author or authors.
*/

package org.quickperf.testng.sql;

import org.quickperf.testng.TestNGTests;
import org.testng.annotations.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class QuickPerfSqlTestNGListenerTest {

@Test
public void a_test_with_failing_business_code_should_fail() {

// GIVEN
Class<?> testClass = TestNGMethodFailing.class;
TestNGTests testNGTests = TestNGTests.createInstance(testClass);

// WHEN
TestNGTests.TestNGTestsResult testsResult = testNGTests.run();

// THEN
assertThat(testsResult.getNumberOfFailedTest()).isOne();

Throwable errorReport = testsResult.getThrowableOfFirstTest();
assertThat(errorReport).hasMessageContaining("expected [false] but found [true]");

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.
*
* Copyright 2019-2021 the original author or authors.
*/

package org.quickperf.testng.sql;

import org.testng.Assert;
import org.testng.annotations.Test;

public class TestNGMethodFailing {

@Test
public void a_failing_test() {
Assert.assertEquals(true, false);
}

}