Skip to content

Commit

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

* More unit tests migrated

* Migrate more tests to junit5
  • Loading branch information
pibizza authored and rgdoliveira committed Nov 18, 2024
1 parent 38e06e1 commit 84c1710
Show file tree
Hide file tree
Showing 96 changed files with 3,937 additions and 3,727 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package org.drools.mvel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

import org.drools.compiler.builder.impl.EvaluatorRegistry;
import org.drools.core.base.ClassFieldAccessorCache;
Expand All @@ -37,23 +37,18 @@
import org.drools.model.index.BetaIndexImpl;
import org.drools.modelcompiler.util.EvaluationUtil;
import org.drools.mvel.accessors.ClassFieldAccessorStore;
import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

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

@RunWith(Parameterized.class)
public abstract class AbstractTupleIndexHashTableIteratorTest {

public static EvaluatorRegistry registry = new EvaluatorRegistry();

protected boolean useLambdaConstraint;

private IndexMemory.EqualityMemoryType originalMemoryImpl;

@Before
@BeforeEach
public void before() {
try {
originalMemoryImpl = IndexMemory.getEqualityMemoryType();
Expand All @@ -64,7 +59,7 @@ public void before() {
}


@After
@AfterEach
public void after() {
try {
IndexMemory.setEqualityMemoryType(originalMemoryImpl);
Expand All @@ -73,12 +68,8 @@ public void after() {
}
}

@Parameterized.Parameters(name = "useLambdaConstraint={0}")
public static Collection<Object[]> getParameters() {
Collection<Object[]> parameters = new ArrayList<>();
parameters.add(new Object[]{false});
parameters.add(new Object[]{true});
return parameters;
public static Stream<Boolean> parameters() {
return Stream.of(false, true);
}

protected static BetaConstraint createFooThisEqualsDBetaConstraint(boolean useLambdaConstraint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
package org.drools.mvel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

import org.drools.core.RuleBaseConfiguration;
import org.drools.core.base.ClassFieldAccessorCache;
Expand All @@ -47,27 +47,19 @@
import org.drools.model.functions.Predicate1;
import org.drools.modelcompiler.util.EvaluationUtil;
import org.drools.mvel.model.Cheese;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

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

@RunWith(Parameterized.class)
public abstract class BaseBetaConstraintsTest {

public static EvaluatorRegistry registry = new EvaluatorRegistry();

protected boolean useLambdaConstraint;

@Parameterized.Parameters(name = "useLambdaConstraint={0}")
public static Collection<Object[]> getParameters() {
Collection<Object[]> parameters = new ArrayList<>();
parameters.add(new Object[]{false});
parameters.add(new Object[]{true});
return parameters;
public static Stream<Boolean> parameters() {
return Stream.of(false, true);
}

protected BetaConstraint getCheeseTypeConstraint(final String identifier,
protected BetaConstraint getCheeseTypeConstraint(boolean useLambdaConstraint, final String identifier,
Operator operator) {
if (useLambdaConstraint) {
Pattern pattern = new Pattern(0, new ClassObjectType(Cheese.class));
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,53 @@
import org.drools.core.common.DoubleBetaConstraints;
import org.drools.base.reteoo.NodeTypeEnums;
import org.drools.base.rule.constraint.BetaConstraint;
import org.junit.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

public class DoubleBetaConstraintsTest extends BaseBetaConstraintsTest {

public DoubleBetaConstraintsTest(boolean useLambdaConstraint) {
this.useLambdaConstraint = useLambdaConstraint;
}

@Test
public void testAllNoneIndexed() {
BetaConstraint constraint0 = getCheeseTypeConstraint("cheeseType0", Operator.BuiltInOperator.NOT_EQUAL.getOperator());
BetaConstraint constraint1 = getCheeseTypeConstraint("cheeseType1", Operator.BuiltInOperator.NOT_EQUAL.getOperator());
@ParameterizedTest(name = "useLambdaConstraint={0}")
@MethodSource("parameters")
public void testAllNoneIndexed(boolean useLambdaConstraint) {
BetaConstraint constraint0 = getCheeseTypeConstraint(useLambdaConstraint, "cheeseType0", Operator.BuiltInOperator.NOT_EQUAL.getOperator());
BetaConstraint constraint1 = getCheeseTypeConstraint(useLambdaConstraint, "cheeseType1", Operator.BuiltInOperator.NOT_EQUAL.getOperator());
BetaConstraint[] constraints = new BetaConstraint[] {constraint0, constraint1 };
checkBetaConstraints( constraints, DoubleBetaConstraints.class );
}

@Test
public void testOneIndexed() {
BetaConstraint constraint0 = getCheeseTypeConstraint("cheeseType0", Operator.BuiltInOperator.EQUAL.getOperator());
BetaConstraint constraint1 = getCheeseTypeConstraint("cheeseType1", Operator.BuiltInOperator.NOT_EQUAL.getOperator());
@ParameterizedTest(name = "useLambdaConstraint={0}")
@MethodSource("parameters")
public void testOneIndexed(boolean useLambdaConstraint) {
BetaConstraint constraint0 = getCheeseTypeConstraint(useLambdaConstraint, "cheeseType0", Operator.BuiltInOperator.EQUAL.getOperator());
BetaConstraint constraint1 = getCheeseTypeConstraint(useLambdaConstraint, "cheeseType1", Operator.BuiltInOperator.NOT_EQUAL.getOperator());
BetaConstraint[] constraints = new BetaConstraint[] {constraint0, constraint1 };
checkBetaConstraints( constraints, DoubleBetaConstraints.class );

constraint0 = getCheeseTypeConstraint( "cheeseType0", Operator.BuiltInOperator.NOT_EQUAL.getOperator() );
constraint1 = getCheeseTypeConstraint( "cheeseType1", Operator.BuiltInOperator.EQUAL.getOperator() );
constraint0 = getCheeseTypeConstraint(useLambdaConstraint, "cheeseType0", Operator.BuiltInOperator.NOT_EQUAL.getOperator() );
constraint1 = getCheeseTypeConstraint(useLambdaConstraint, "cheeseType1", Operator.BuiltInOperator.EQUAL.getOperator() );
constraints = new BetaConstraint[] {constraint0, constraint1 };
checkBetaConstraints( constraints, DoubleBetaConstraints.class );
}

@Test
public void testOneIndexedForComparison() {
BetaConstraint constraint0 = getCheeseTypeConstraint("cheeseType0", Operator.BuiltInOperator.GREATER.getOperator());
BetaConstraint constraint1 = getCheeseTypeConstraint("cheeseType1", Operator.BuiltInOperator.NOT_EQUAL.getOperator());
@ParameterizedTest(name = "useLambdaConstraint={0}")
@MethodSource("parameters")
public void testOneIndexedForComparison(boolean useLambdaConstraint) {
BetaConstraint constraint0 = getCheeseTypeConstraint(useLambdaConstraint, "cheeseType0", Operator.BuiltInOperator.GREATER.getOperator());
BetaConstraint constraint1 = getCheeseTypeConstraint(useLambdaConstraint, "cheeseType1", Operator.BuiltInOperator.NOT_EQUAL.getOperator());
BetaConstraint[] constraints = new BetaConstraint[] {constraint0, constraint1 };
checkBetaConstraints( constraints, DoubleBetaConstraints.class, NodeTypeEnums.ExistsNode );

constraint0 = getCheeseTypeConstraint( "cheeseType0", Operator.BuiltInOperator.NOT_EQUAL.getOperator() );
constraint1 = getCheeseTypeConstraint( "cheeseType1", Operator.BuiltInOperator.GREATER.getOperator() );
constraint0 = getCheeseTypeConstraint(useLambdaConstraint, "cheeseType0", Operator.BuiltInOperator.NOT_EQUAL.getOperator() );
constraint1 = getCheeseTypeConstraint(useLambdaConstraint, "cheeseType1", Operator.BuiltInOperator.GREATER.getOperator() );
constraints = new BetaConstraint[] {constraint0, constraint1 };
checkBetaConstraints( constraints, DoubleBetaConstraints.class, NodeTypeEnums.ExistsNode );
}

@Test
public void testTwoIndexed() {
BetaConstraint constraint0 = getCheeseTypeConstraint("cheeseType0", Operator.BuiltInOperator.EQUAL.getOperator());
BetaConstraint constraint1 = getCheeseTypeConstraint("cheeseType1", Operator.BuiltInOperator.EQUAL.getOperator());
@ParameterizedTest(name = "useLambdaConstraint={0}")
@MethodSource("parameters")
public void testTwoIndexed(boolean useLambdaConstraint) {
BetaConstraint constraint0 = getCheeseTypeConstraint(useLambdaConstraint, "cheeseType0", Operator.BuiltInOperator.EQUAL.getOperator());
BetaConstraint constraint1 = getCheeseTypeConstraint(useLambdaConstraint, "cheeseType1", Operator.BuiltInOperator.EQUAL.getOperator());
BetaConstraint[] constraints = new BetaConstraint[] {constraint0, constraint1 };
checkBetaConstraints( constraints, DoubleBetaConstraints.class );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
*/
package org.drools.mvel;

import org.drools.base.rule.Declaration;
import org.drools.base.rule.Pattern;
import org.drools.base.rule.accessor.ReadAccessor;
import org.drools.base.rule.constraint.AlphaNodeFieldConstraint;
import org.drools.core.base.ClassFieldAccessorCache;
import org.drools.core.common.InternalFactHandle;
Expand All @@ -30,37 +27,23 @@
import org.drools.mvel.accessors.ClassFieldAccessorStore;
import org.drools.mvel.accessors.ClassFieldReader;
import org.drools.mvel.model.Cheese;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.drools.base.base.AccessorKey.AccessorType.ClassObjectType;

@RunWith(Parameterized.class)
public class FieldConstraintTest {

ClassFieldAccessorStore store = new ClassFieldAccessorStore();

private final boolean useLambdaConstraint;

public FieldConstraintTest(boolean useLambdaConstraint) {
this.useLambdaConstraint = useLambdaConstraint;
}

@Parameterized.Parameters(name = "useLambdaConstraint={0}")
public static Collection<Object[]> getParameters() {
Collection<Object[]> parameters = new ArrayList<>();
parameters.add(new Object[]{false});
parameters.add(new Object[]{true});
return parameters;
public static Stream<Boolean> parameters() {
return Stream.of(false, true);
}

@Before
@BeforeEach
public void setUp() throws Exception {
store.setClassFieldAccessorCache( new ClassFieldAccessorCache( Thread.currentThread().getContextClassLoader() ) );
store.setEagerWire( true );
Expand All @@ -78,8 +61,9 @@ public void setUp() throws Exception {
* This is currently the same as using a ReturnValueConstraint just that it
* doesn't need any requiredDeclarations
*/
@Test
public void testLiteralConstraint() {
@ParameterizedTest(name = "useLambdaConstraint={0}")
@MethodSource("parameters")
public void testLiteralConstraint(boolean useLambdaConstraint) {
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();;
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newKieSession();

Expand Down Expand Up @@ -116,8 +100,9 @@ public void testLiteralConstraint() {
*
* </pre>
*/
@Test
public void testPrimitiveLiteralConstraint() {
@ParameterizedTest(name = "useLambdaConstraint={0}")
@MethodSource("parameters")
public void testPrimitiveLiteralConstraint(boolean useLambdaConstraint) {
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();;
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl)kBase.newKieSession();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Stream;

import org.drools.base.base.ValueResolver;
import org.drools.core.base.ClassFieldAccessorCache;
Expand All @@ -38,10 +37,8 @@
import org.drools.core.test.model.Cheese;
import org.drools.kiesession.rulebase.InternalKnowledgeBase;
import org.drools.kiesession.rulebase.KnowledgeBaseFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.kie.api.event.kiebase.AfterFunctionRemovedEvent;
import org.kie.api.event.kiebase.AfterKieBaseLockedEvent;
import org.kie.api.event.kiebase.AfterKieBaseUnlockedEvent;
Expand All @@ -64,33 +61,18 @@

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

@RunWith(Parameterized.class)
public class InternalRuleBaseEventSupportTest {

private InternalKnowledgeBase kBase;
private TestRuleBaseListener listener1;
private TestRuleBaseListener listener2;
private InternalKnowledgePackage pkg;

private final boolean useLambdaConstraint;

public InternalRuleBaseEventSupportTest(boolean useLambdaConstraint) {
this.useLambdaConstraint = useLambdaConstraint;
}

@Parameterized.Parameters(name = "useLambdaConstraint={0}")
public static Collection<Object[]> getParameters() {
Collection<Object[]> parameters = new ArrayList<>();
parameters.add(new Object[]{false});
parameters.add(new Object[]{true});
return parameters;
public static Stream<Boolean> parameters() {
return Stream.of(false, true);
}

/* (non-Javadoc)
* @see junit.framework.TestCase#setUp()
*/
@Before
public void setUp() throws Exception {
public void setUp(boolean useLambdaConstraint) throws Exception {
kBase = KnowledgeBaseFactory.newKnowledgeBase();;
listener1 = new TestRuleBaseListener( "(listener-1) " );
listener2 = new TestRuleBaseListener( "(listener-2) " );
Expand Down Expand Up @@ -169,8 +151,10 @@ public String getName() {

}

@Test
public void testAddPackageEvents() throws Exception {
@ParameterizedTest(name = "useLambdaConstraint={0}")
@MethodSource("parameters")
public void testAddPackageEvents(boolean useLambdaConstraint) throws Exception {
setUp(useLambdaConstraint);
assertThat(listener1.getBeforePackageAdded()).isEqualTo(0);
assertThat(listener1.getAfterPackageAdded()).isEqualTo(0);
assertThat(listener2.getBeforePackageAdded()).isEqualTo(0);
Expand All @@ -192,8 +176,10 @@ public void testAddPackageEvents() throws Exception {
assertThat(listener2.getAfterRuleAdded()).isEqualTo(2);
}

@Test
public void testRemovePackageEvents() throws Exception {
@ParameterizedTest(name = "useLambdaConstraint={0}")
@MethodSource("parameters")
public void testRemovePackageEvents(boolean useLambdaConstraint) throws Exception {
setUp(useLambdaConstraint);
this.kBase.addPackage( pkg );

assertThat(listener1.getBeforePackageRemoved()).isEqualTo(0);
Expand Down Expand Up @@ -371,25 +357,25 @@ public void beforeKieBaseUnlocked(BeforeKieBaseUnlockedEvent event) {

}

public void beforeProcessAdded(BeforeProcessAddedEvent event) {
// TODO Auto-generated method stub
}

public void afterProcessAdded(AfterProcessAddedEvent event) {
// TODO Auto-generated method stub
}

public void beforeProcessRemoved(BeforeProcessRemovedEvent event) {
// TODO Auto-generated method stub
}

public void afterProcessRemoved(AfterProcessRemovedEvent event) {
// TODO Auto-generated method stub
}
public void beforeProcessAdded(BeforeProcessAddedEvent event) {
// TODO Auto-generated method stub
}

public void afterProcessAdded(AfterProcessAddedEvent event) {
// TODO Auto-generated method stub
}

public void beforeProcessRemoved(BeforeProcessRemovedEvent event) {
// TODO Auto-generated method stub
}

public void afterProcessRemoved(AfterProcessRemovedEvent event) {
// TODO Auto-generated method stub
}

}

Expand Down
Loading

0 comments on commit 84c1710

Please sign in to comment.