Skip to content

Commit

Permalink
Forbid usage of inner test classes in testNG
Browse files Browse the repository at this point in the history
TestNG support for inner test classes is poor, see this issue:
#11185
  • Loading branch information
grantatspothero authored and hashhar committed Mar 16, 2022
1 parent 665e87c commit e0cf4a9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
6 changes: 6 additions & 0 deletions plugin/trino-kudu/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-testing-services</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.trino</groupId>
<artifactId>trino-tpch</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 io.trino.testng.services;

import org.testng.IClassListener;
import org.testng.ITestClass;

import java.util.Optional;

import static com.google.common.base.Throwables.getStackTraceAsString;
import static io.trino.testng.services.Listeners.reportListenerFailure;

/**
* Detects test classes which are defined as inner classes
* TestNG support is poor for these inner test classes: https://github.com/trinodb/trino/pull/11185
*/
public class ReportInnerTestClasses
implements IClassListener
{
@Override
public void onBeforeClass(ITestClass iTestClass)
{
try {
reportInnerTestClasses(iTestClass);
}
catch (RuntimeException | Error e) {
reportListenerFailure(
ReportInnerTestClasses.class,
"Failed to process %s: \n%s",
iTestClass,
getStackTraceAsString(e));
}
}

private void reportInnerTestClasses(ITestClass testClass)
{
Class<?> realClass = testClass.getRealClass();

if (realClass.getName().startsWith("io.trino.testng.services")) {
// ignore internal testcases
return;
}

Optional<Class<?>> maybeEnclosingClass = Optional.ofNullable(realClass.getEnclosingClass());
maybeEnclosingClass.ifPresent(enclosingClass ->
reportListenerFailure(ReportInnerTestClasses.class,
"Test class %s is defined as an inner class, has an enclosing class %s",
realClass.getName(),
enclosingClass.getName()));
}

@Override
public void onAfterClass(ITestClass iTestClass)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ io.trino.testng.services.LogTestDurationListener
io.trino.testng.services.RetryAnnotationTransformer
io.trino.testng.services.FlakyAnnotationVerifier
io.trino.testng.services.ProgressLoggingListener
io.trino.testng.services.ReportInnerTestClasses

0 comments on commit e0cf4a9

Please sign in to comment.