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

Removed JUnit5 dependency for ClientTpcConfigTest #26265

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -60,6 +60,7 @@ public final class ArchUnitRules {
*/
public static final ArchRule NO_JUNIT_MIXING = classes()
.that().haveSimpleNameEndingWith("Test")
.or().haveSimpleNameEndingWith("IT")
.should(notMixJUnit4AndJUnit5Annotations());

private ArchUnitRules() {
Expand Down
Expand Up @@ -20,12 +20,12 @@
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.experimental.categories.Category;
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;

import org.junit.AfterClass;
import java.lang.annotation.Annotation;
import java.util.Set;

Expand All @@ -35,36 +35,79 @@ public class MixTestAnnotationsCondition extends ArchCondition<JavaClass> {
Before.class,
After.class,
BeforeClass.class,
AfterClass.class);
AfterClass.class
);

private static final Set<Class<? extends Annotation>> JUNIT_4_CLASS_ANNOTATION = Set.of(
Category.class
);

private static final Set<Class<? extends Annotation>> JUNIT_5_CLASS_ANNOTATION = Set.of(
org.junit.jupiter.api.Tag.class
);

private static final Set<Class<? extends Annotation>> JUNIT_5_ANNOTATION_CLASSES = Set.of(
org.junit.jupiter.api.Test.class,
org.junit.jupiter.api.BeforeEach.class,
org.junit.jupiter.api.AfterEach.class,
org.junit.jupiter.api.BeforeAll.class,
org.junit.jupiter.api.AfterAll.class
org.junit.jupiter.api.AfterAll.class,
org.junit.jupiter.api.Tag.class
);

private static final String JUNIT4_PACKAGE = "org.junit";
private static final String JUNIT5_PACKAGE = "org.junit.jupiter.api";

public MixTestAnnotationsCondition() {
super("Do not mix Junit4 and Junit5 annotations");
}


@Override
public void check(JavaClass item, ConditionEvents events) {
boolean hasJUnit4Annotation = item.getMethods().stream()
.anyMatch(method -> JUNIT_4_ANNOTATION_CLASSES.stream()
.anyMatch(method::isAnnotatedWith));
boolean hasJUnit4Elements = hasAnyJUnit4Annotations(item) || hasJUnit4Assertions(item);
boolean hasJUnit5Elements = hasAnyJUnit5Annotations(item) || hasJUnit5Assertions(item);

boolean hasJUnit5Annotation = item.getMethods().stream()
.anyMatch(method -> JUNIT_5_ANNOTATION_CLASSES.stream()
.anyMatch(method::isAnnotatedWith));

if (hasJUnit4Annotation && hasJUnit5Annotation) {
String message = String.format("Class %s mixes JUnit 4 and JUnit 5 annotations.", item.getName());
if (hasJUnit4Elements && hasJUnit5Elements) {
String message = String.format("Class %s mixes JUnit 4 and JUnit 5 elements.", item.getName());
events.add(SimpleConditionEvent.violated(item, message));
}
}

private boolean hasAnyJUnit4Annotations(JavaClass item) {
return hasClassAnnotation(item, JUNIT_4_CLASS_ANNOTATION)
|| hasMethodAnnotations(item, JUNIT_4_ANNOTATION_CLASSES);
}

private boolean hasJUnit4Assertions(JavaClass item) {
return isCallingMethod(item, JUNIT4_PACKAGE + "." + "Assert");
}

private boolean hasAnyJUnit5Annotations(JavaClass item) {
return hasClassAnnotation(item, JUNIT_5_CLASS_ANNOTATION)
|| hasMethodAnnotations(item, JUNIT_5_ANNOTATION_CLASSES);
}

private boolean hasJUnit5Assertions(JavaClass item) {
return isCallingMethod(item, JUNIT5_PACKAGE + "." + "Assertions");
}

private boolean isCallingMethod(JavaClass item, String fullMethodName) {
return item.getMethodCallsFromSelf().stream().
anyMatch(methodCall -> methodCall.getTarget().getFullName().contains(fullMethodName));
}

private boolean hasClassAnnotation(JavaClass item, Set<Class<? extends Annotation>> annotations) {
return item.getAnnotations().stream()
.anyMatch(annotation -> annotations.stream()
.anyMatch(classAnnotation -> classAnnotation.isAssignableFrom(annotation.getClass())));
}

private boolean hasMethodAnnotations(JavaClass item, Set<Class<? extends Annotation>> annotations) {
return item.getMethods().stream()
.anyMatch(method -> annotations.stream()
.anyMatch(method::isAnnotatedWith));
}

public static ArchCondition<JavaClass> notMixJUnit4AndJUnit5Annotations() {
return new MixTestAnnotationsCondition();
}
Expand Down
Expand Up @@ -53,9 +53,9 @@
import static com.hazelcast.jet.sql.impl.SqlEndToEndTestSupport.calculateExpectedPartitions;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@RunWith(HazelcastParametrizedRunner.class)
@Parameterized.UseParametersRunnerFactory(HazelcastSerialParametersRunnerFactory.class)
Expand Down
Expand Up @@ -24,10 +24,10 @@
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Needs to run serially because it messes with system properties.
Expand Down
Expand Up @@ -25,12 +25,12 @@
import static com.hazelcast.internal.json.Json.NULL;
import static com.hazelcast.internal.json.Json.TRUE;
import static com.hazelcast.test.TestJavaSerializationUtils.serializeAndDeserialize;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.Assert.assertNotSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand Down
Expand Up @@ -20,8 +20,8 @@
import com.hazelcast.test.HazelcastTestSupport;
import com.hazelcast.test.annotation.ParallelJVMTest;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.Test;
Copy link
Contributor

Choose a reason for hiding this comment

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

In this form it won't work:

  • Junit 4 test must be public
  • We use also ParameterizedTest in this class

Copy link
Author

Choose a reason for hiding this comment

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

You are right, I miss it. Do you suggest rewrite the test with Parameterized runner?

import org.junit.experimental.categories.Category;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.runner.RunWith;
Expand All @@ -32,12 +32,12 @@
import static com.hazelcast.internal.util.StringUtil.VERSION_PATTERN;
import static com.hazelcast.internal.util.StringUtil.isAllNullOrEmptyAfterTrim;
import static com.hazelcast.internal.util.StringUtil.isAnyNullOrEmptyAfterTrim;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;

@RunWith(HazelcastParallelClassRunner.class)
@Category({QuickTest.class, ParallelJVMTest.class})
Expand Down
Expand Up @@ -49,10 +49,10 @@

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
Expand Down
13 changes: 2 additions & 11 deletions hazelcast/src/test/java/com/hazelcast/osgi/HazelcastOSGiIT.java
Expand Up @@ -18,8 +18,8 @@

import com.hazelcast.instance.BuildInfoProvider;
import com.hazelcast.test.annotation.QuickTest;
import org.junit.After;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.api.AfterEach;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.Configuration;
import org.ops4j.pax.exam.junit.ExamReactorStrategy;
Expand All @@ -31,7 +31,6 @@
import org.ops4j.pax.url.mvn.ServiceConstants;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import javax.inject.Inject;

Expand Down Expand Up @@ -65,7 +64,7 @@ public Option[] config() {
return options(hzBundle, junitBundles);
}

@AfterEach
@After
public void tearDown() throws Exception {
for (Bundle bundle : bundleContext.getBundles()) {
if ("com.hazelcast".equals(bundle.getSymbolicName())) {
Expand All @@ -79,12 +78,4 @@ public void tearDown() throws Exception {
System.setProperty(MAVEN_REPOSITORIES_PROP, oldMavenRepoProperty);
}
}

private HazelcastOSGiService getService() {
ServiceReference serviceRef = bundleContext.getServiceReference(HazelcastOSGiService.class.getName());
if (serviceRef == null) {
return null;
}
return (HazelcastOSGiService) bundleContext.getService(serviceRef);
}
}
Expand Up @@ -34,9 +34,9 @@
import static com.hazelcast.spi.impl.operationservice.OperationAccessor.setCallTimeout;
import static com.hazelcast.spi.impl.operationservice.OperationAccessor.setCallerAddress;
import static com.hazelcast.spi.impl.operationservice.OperationAccessor.setInvocationTime;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@RunWith(MockitoJUnitRunner.class)
@Category({QuickTest.class, ParallelJVMTest.class})
Expand Down