Skip to content

Commit

Permalink
[incubator-kie-drools-6136] Migrate drools test coverage to JUnit5 - a…
Browse files Browse the repository at this point in the history
…pache#5 (apache#6168)

* Migrated tests to JUnit5

* More tests migrated

* Migrated tests to Junit5

* Migrated more tests

* Migrated test suite to JUnit5

* More tests moved to JUnit5
  • Loading branch information
pibizza authored and rgdoliveira committed Nov 25, 2024
1 parent 72b4e69 commit 063308e
Show file tree
Hide file tree
Showing 91 changed files with 1,431 additions and 1,498 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import org.drools.testcoverage.domain.Drink;
import org.drools.testcoverage.domain.Order;
import org.drools.testcoverage.kieci.withdomain.util.KJarLoadUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kie.api.KieServices;
import org.kie.api.builder.ReleaseId;
import org.kie.api.runtime.KieContainer;
Expand All @@ -45,13 +45,13 @@ public class KJarLoadingTest {

private KieSession kieSession;

@Before
@BeforeEach
public void init() {
final KieContainer container = KS.newKieContainer(KJAR_RELEASE_ID, this.getClass().getClassLoader());
this.kieSession = container.newKieSession();
}

@After
@AfterEach
public void dispose() {
if (this.kieSession != null) {
this.kieSession.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
package org.drools.testcoverage.kieci.withoutdomain;

import org.drools.testcoverage.kieci.withoutdomain.util.KJarLoadUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kie.api.KieServices;
import org.kie.api.builder.ReleaseId;
import org.kie.api.runtime.KieContainer;
Expand All @@ -42,13 +42,13 @@ public class KJarLoadingTest {

private KieSession kieSession;

@Before
@BeforeEach
public void init() {
final KieContainer container = KS.newKieContainer(KJAR_RELEASE_ID);
this.kieSession = container.newKieSession();
}

@After
@AfterEach
public void dispose() {
if (this.kieSession != null) {
this.kieSession.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@
package org.drools.compiler.integrationtests;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import org.drools.testcoverage.common.util.KieBaseTestConfiguration;
import org.drools.testcoverage.common.util.KieBaseUtil;
import org.drools.testcoverage.common.util.KieSessionTestConfiguration;
import org.drools.testcoverage.common.util.TestParametersUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.drools.testcoverage.common.util.TestParametersUtil2;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.kie.api.KieBase;
import org.kie.api.event.rule.AfterMatchFiredEvent;
import org.kie.api.event.rule.DefaultAgendaEventListener;
Expand All @@ -48,29 +46,22 @@
* inserted through one or more entry points.
* BZ-978979
*/
@RunWith(Parameterized.class)
public class NegativePatternsTest {

private static final int LOOPS = 300;
private static final int SHORT_SLEEP_TIME = 20;
private static final int LONG_SLEEP_TIME = 30;

private final KieBaseTestConfiguration kieBaseTestConfiguration;

private KieSession ksession;
private TrackingAgendaEventListener firedRulesListener;

public NegativePatternsTest(final KieBaseTestConfiguration kieBaseTestConfiguration) {
this.kieBaseTestConfiguration = kieBaseTestConfiguration;
}

@Parameterized.Parameters(name = "KieBase type={0}")
public static Collection<Object[]> getParameters() {
return TestParametersUtil.getKieBaseStreamConfigurations(true);
public static Stream<KieBaseTestConfiguration> parameters() {
return TestParametersUtil2.getKieBaseStreamConfigurations(true).stream();
}

@Before
public void prepareKieSession() {

public void prepareKieSession(KieBaseTestConfiguration kieBaseTestConfiguration) {
final String drl = "package org.drools.compiler.integrationtests\n" +
"\n" +
"import " + NegativePatternsTest.TestEvent.class.getCanonicalName() + "\n" +
Expand Down Expand Up @@ -124,15 +115,17 @@ public void prepareKieSession() {
ksession.addEventListener(firedRulesListener);
}

@After
@AfterEach
public void cleanKieSession() {
if (ksession != null) {
ksession.dispose();
}
}

@Test
public void testSingleEvent() {
@ParameterizedTest(name = "KieBase type={0}")
@MethodSource("parameters")
public void testSingleEvent(KieBaseTestConfiguration kieBaseTestConfiguration) {
prepareKieSession(kieBaseTestConfiguration);
final EntryPoint entryPoint = ksession.getEntryPoint("EventStream");

int count = 0;
Expand Down Expand Up @@ -169,8 +162,10 @@ public void testSingleEvent() {
assertThat(firedRulesListener.ruleFiredCount("SingleAbsence")).isEqualTo(count);
}

@Test
public void testConstrainedAbsence() {
@ParameterizedTest(name = "KieBase type={0}")
@MethodSource("parameters")
public void testConstrainedAbsence(KieBaseTestConfiguration kieBaseTestConfiguration) {
prepareKieSession(kieBaseTestConfiguration);
final EntryPoint entryPoint = ksession.getEntryPoint("EventStream");

int count = 0;
Expand Down Expand Up @@ -198,8 +193,10 @@ public void testConstrainedAbsence() {
assertThat(firedRulesListener.ruleFiredCount("SingleConstrained")).isEqualTo(count);
}

@Test
public void testMultipleEvents() {
@ParameterizedTest(name = "KieBase type={0}")
@MethodSource("parameters")
public void testMultipleEvents(KieBaseTestConfiguration kieBaseTestConfiguration) {
prepareKieSession(kieBaseTestConfiguration);
final EntryPoint entryPoint = ksession.getEntryPoint("EventStream");

int count = 0;
Expand Down Expand Up @@ -237,8 +234,10 @@ public void testMultipleEvents() {
assertThat(firedRulesListener.ruleFiredCount("MultipleEvents")).isEqualTo(count);
}

@Test
public void testMultipleEntryPoints() {
@ParameterizedTest(name = "KieBase type={0}")
@MethodSource("parameters")
public void testMultipleEntryPoints(KieBaseTestConfiguration kieBaseTestConfiguration) {
prepareKieSession(kieBaseTestConfiguration);
final EntryPoint entryPoint = ksession.getEntryPoint("EventStream");

final EntryPoint otherStream = ksession.getEntryPoint("OtherStream");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,12 @@

public abstract class AbstractConcurrentTest {

protected final boolean enforcedJitting;
protected final boolean serializeKieBase;
protected final boolean sharedKieBase;
protected final boolean sharedKieSession;
protected final KieBaseTestConfiguration kieBaseTestConfiguration;

public AbstractConcurrentTest(final boolean enforcedJitting, final boolean serializeKieBase,
final boolean sharedKieBase, final boolean sharedKieSession, KieBaseTestConfiguration kieBaseTestConfiguration) {
this.enforcedJitting = enforcedJitting;
this.serializeKieBase = serializeKieBase;
this.sharedKieBase = sharedKieBase;
this.sharedKieSession = sharedKieSession;
this.kieBaseTestConfiguration = kieBaseTestConfiguration;
}
protected boolean enforcedJitting;
protected boolean isKieBaseSerialized;
protected boolean isKieBaseShared;
protected boolean isKieSessionShared;
protected KieBaseTestConfiguration kieBaseTestConfiguration;


interface TestWithSessionExecutor {
boolean execute(KieSession kieSession, int counter);
Expand All @@ -67,6 +59,15 @@ interface TestWithSessionExecutor {
interface TestExecutor {
boolean execute(int counter);
}

protected void initTest(boolean enforcedJitting, boolean isKieBaseSerialized,
boolean isKieBaseShared, boolean isKieSessionShared, KieBaseTestConfiguration kieBaseTestConfiguration) {
this.enforcedJitting = enforcedJitting;
this.isKieBaseSerialized = isKieBaseSerialized;
this.isKieBaseShared = isKieBaseShared;
this.isKieSessionShared = isKieSessionShared;
this.kieBaseTestConfiguration = kieBaseTestConfiguration;
}

protected void parallelTest(final int threadCount, final TestExecutor testExecutor) throws InterruptedException {
final List<Callable<Boolean>> tasks = new ArrayList<>();
Expand Down Expand Up @@ -96,8 +97,8 @@ protected void parallelTest(final int repetitions, final int threadCount, final
for (int i = 0; i < threadCount; i++) {
final int counter = i;
tasks.add(() -> {
final KieBase kieBaseForTest = this.sharedKieBase ? sharedKieBase : getKieBase(drls);
if (this.sharedKieSession) {
final KieBase kieBaseForTest = this.isKieBaseShared ? sharedKieBase : getKieBase(drls);
if (this.isKieSessionShared) {
return testExecutor.execute(sharedKieSession, counter);
} else {
return executeInSeparateSession(testExecutor, kieBaseForTest, globalName, global, counter);
Expand Down
Loading

0 comments on commit 063308e

Please sign in to comment.