Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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.testing.services.junit;

import org.junit.platform.engine.support.descriptor.MethodSource;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;

import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import static com.google.common.base.Throwables.getStackTraceAsString;
import static io.trino.testing.services.junit.Listeners.reportListenerFailure;
import static java.lang.String.format;

public class ReportOverriddenMethods
implements TestExecutionListener
{
@Override
public void testPlanExecutionStarted(TestPlan testPlan)
{
try {
testPlan.accept(new TestPlan.Visitor()
{
@Override
public void visit(TestIdentifier testIdentifier)
{
testIdentifier.getSource().ifPresent(source -> {
if (source instanceof MethodSource methodSource) {
if (!Modifier.isPublic(methodSource.getJavaMethod().getModifiers()) &&
!Modifier.isProtected(methodSource.getJavaMethod().getModifiers())) {
List<Class<?>> declaringClasses = Stream.<Class<?>>iterate(methodSource.getJavaClass(), clazz -> clazz.getSuperclass() != null, Class::getSuperclass)
.filter(clazz ->
Arrays.stream(clazz.getDeclaredMethods())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you extract these .filter body to a separate method for improved readability?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's just two-liner

.anyMatch(method -> method.getName().equals(methodSource.getJavaMethod().getName())))
.toList();
if (declaringClasses.size() > 1) {
throw new IllegalStateException(format(
"""
Method %s is not public. Similar methods are defined by %s. \
When tests are non-public, they do not @Override in Java sense, but they still interact with each other in JUnit, \
and only one of declared tests gets executed. \
This leads to tests being silently skipped without any source-level indication.""",
methodSource.getJavaMethod(),
declaringClasses));
}
}
}
});
}
});
}
catch (RuntimeException | Error e) {
reportListenerFailure(getClass(), "%s", getStackTraceAsString(e));
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
io.trino.testing.services.junit.LogTestDurationListener
io.trino.testing.services.junit.ReportOverriddenMethods