Skip to content

Commit

Permalink
[7.x][Drools 6875] Removed use of deprecated methods in assertions (a…
Browse files Browse the repository at this point in the history
…pache#4320)

* [DROOLS-6875] Removed use of deprecated methods in assertions

* Restored missing assertion
pibizza authored Apr 21, 2022

Unverified

The committer email address is not verified.
1 parent 68c4afa commit 3d10755
Showing 22 changed files with 168 additions and 172 deletions.
Original file line number Diff line number Diff line change
@@ -30,9 +30,8 @@
import java.util.List;
import java.util.Map;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNull;

public class ParserTest {
5 changes: 5 additions & 0 deletions drools-model/drools-mvel-compiler/pom.xml
Original file line number Diff line number Diff line change
@@ -43,6 +43,11 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Original file line number Diff line number Diff line change
@@ -22,35 +22,32 @@
import org.drools.mvelcompiler.context.MvelCompilerContext;
import org.junit.Test;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
import static org.assertj.core.api.Assertions.assertThat;


public class ModifyCompilerTest implements CompilerTest {

@Test
public void testUncompiledMethod() {
test("{modify( (List)$toEdit.get(0) ){ setEnabled( true ) }}",
"{ { ((List) $toEdit.get(0)).setEnabled(true); } }",
result -> assertThat(allUsedBindings(result), is(empty())));
result -> assertThat(allUsedBindings(result)).isEmpty());
}

@Test
public void testModify() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ modify($p) { setCanDrink(true); } }",
"{ { ($p).setCanDrink(true); } update($p); }",
result -> assertThat(allUsedBindings(result), containsInAnyOrder("$p")));
result -> assertThat(allUsedBindings(result)).containsExactlyInAnyOrder("$p"));
}

@Test
public void testModifyWithLambda() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ modify($p) { setCanDrinkLambda(() -> true); } }",
"{ { ($p).setCanDrinkLambda(() -> true); } update($p); }",
result -> assertThat(allUsedBindings(result), containsInAnyOrder("$p")));
result ->assertThat(allUsedBindings(result)).containsExactlyInAnyOrder("$p"));
}

@Test
@@ -72,7 +69,7 @@ public void testNestedModify() {
" update($fact); " +
"} " +
"} ",
result -> assertThat(allUsedBindings(result), containsInAnyOrder("$fact")));
result -> assertThat(allUsedBindings(result)).containsExactlyInAnyOrder("$fact"));
}

@Override
@@ -81,7 +78,7 @@ public void test(Consumer<MvelCompilerContext> testFunction,
String expectedResult,
Consumer<CompiledBlockResult> resultAssert) {
CompiledBlockResult compiled = new ModifyCompiler().compile(inputExpression);
assertThat(compiled.resultAsString(), equalToIgnoringWhiteSpace(expectedResult));
assertThat(compiled.resultAsString()).isEqualToIgnoringWhitespace(expectedResult);
resultAssert.accept(compiled);
}
}
Original file line number Diff line number Diff line change
@@ -24,10 +24,7 @@
import org.drools.core.util.MethodUtils;
import org.junit.Test;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;

public class MvelCompilerTest implements CompilerTest {

@@ -345,15 +342,15 @@ public void testSetterBigDecimalConstantModify() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ modify ( $p ) { salary = 50000 }; }",
"{ { $p.setSalary(new java.math.BigDecimal(50000)); } }",
result -> assertThat(allUsedBindings(result), containsInAnyOrder("$p")));
result -> assertThat(allUsedBindings(result)).containsExactlyInAnyOrder("$p"));
}

@Test
public void testSetterBigDecimalLiteralModify() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ modify ( $p ) { salary = 50000B }; }",
"{ { $p.setSalary(new java.math.BigDecimal(\"50000\")); } }",
result -> assertThat(allUsedBindings(result), containsInAnyOrder("$p")));
result -> assertThat(allUsedBindings(result)).containsExactlyInAnyOrder("$p"));
}

@Test
@@ -403,7 +400,7 @@ public void testDoNotConvertAdditionInStringConcatenation() {
" { $p.setSalary(new java.math.BigDecimal(50000)); }" +
" list.add(\"after \" + $p + \", money = \" + $p.getSalary()); " +
"}\n",
result -> assertThat(allUsedBindings(result), containsInAnyOrder("$p")));
result -> assertThat(allUsedBindings(result)).containsExactlyInAnyOrder("$p"));
}

@Test
@@ -730,7 +727,7 @@ public void testModify() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ modify ( $p ) { name = \"Luca\", age = 35 }; }",
"{\n {\n $p.setName(\"Luca\");\n $p.setAge(35);\n }\n }",
result -> assertThat(allUsedBindings(result), containsInAnyOrder("$p")));
result -> assertThat(allUsedBindings(result)).containsExactlyInAnyOrder("$p"));
}

@Test
@@ -741,46 +738,46 @@ public void testModifyMap() {
},
"{ modify ( $p ) { items = $p2.items }; }",
"{\n {\n $p.setItems($p2.getItems());\n }\n }",
result -> assertThat(allUsedBindings(result), containsInAnyOrder("$p")));
result -> assertThat(allUsedBindings(result)).containsExactlyInAnyOrder("$p"));
}

@Test
public void testModifySemiColon() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ modify($p) { setAge(1); }; }",
"{ { $p.setAge(1); } }",
result -> assertThat(allUsedBindings(result), containsInAnyOrder("$p")));
result -> assertThat(allUsedBindings(result)).containsExactlyInAnyOrder("$p"));
}

@Test
public void testModifyWithAssignment() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ modify($p) { age = $p.age+1 }; }",
"{ { $p.setAge($p.getAge() + 1); } }",
result -> assertThat(allUsedBindings(result), containsInAnyOrder("$p")));
result -> assertThat(allUsedBindings(result)).containsExactlyInAnyOrder("$p"));
}

@Test
public void testWithSemiColon() {
test("{ with( $l = new ArrayList()) { $l.add(2); }; }",
"{ java.util.ArrayList $l = new ArrayList(); $l.add(2); }",
result -> assertThat(allUsedBindings(result), is(empty())));
result -> assertThat(allUsedBindings(result)).isEmpty());
}

@Test
public void testWithWithAssignment() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ with($p = new Person()) { age = $p.age+1 }; }",
"{ org.drools.Person $p = new Person(); $p.setAge($p.getAge() + 1); }",
result -> assertThat(allUsedBindings(result), is(empty())));
result -> assertThat(allUsedBindings(result)).isEmpty());
}

@Test
public void testWithInIf() {
test(ctx -> ctx.addDeclaration("$p", Person.class),
"{ if (true) { with($p = new Person()) { age = $p.age+1 }; } }",
"{ if (true) { org.drools.Person $p = new Person(); $p.setAge($p.getAge() + 1); } }",
result -> assertThat(allUsedBindings(result), is(empty())));
result -> assertThat(allUsedBindings(result)).isEmpty());
}

@Test
@@ -827,7 +824,7 @@ public void testModifyInsideIfBlock() {
" }\n" +
" } " +
"}",
result -> assertThat(allUsedBindings(result), containsInAnyOrder("$p")));
result -> assertThat(allUsedBindings(result)).containsExactlyInAnyOrder("$p"));
}

@Test
5 changes: 5 additions & 0 deletions drools-model/drools-mvel-parser/pom.xml
Original file line number Diff line number Diff line change
@@ -32,6 +32,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Original file line number Diff line number Diff line change
@@ -57,12 +57,12 @@

import static org.drools.mvel.parser.DrlxParser.parseExpression;
import static org.drools.mvel.parser.printer.PrintUtil.printNode;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

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

public class DroolsMvelParserTest {

private static final Collection<String> operators = new HashSet<>();
@@ -258,7 +258,7 @@ public void testDotFreeExprWithThreeArgsInfinite() {
public void testDotFreeExprWithArgsNegated() {
String expr = "this not after[5,8] $a";
Expression expression = parseExpression( parser, expr ).getExpr();
assertThat(expression, instanceOf(PointFreeExpr.class));
assertThat(expression).isInstanceOf(PointFreeExpr.class);
assertTrue(((PointFreeExpr)expression).isNegated());
assertEquals("this not after[5ms,8ms] $a", printNode(expression)); // please note the parsed expression once normalized would take the time unit for milliseconds.
}
@@ -283,7 +283,7 @@ public void testDotFreeExprWithFourTemporalArgs() {
public void testHalfDotFreeExprWithFourTemporalArgs() {
String expr = "includes[1s,1m,1h,1d] $a";
Expression expression = parseExpression( parser, expr ).getExpr();
assertThat(expression, instanceOf(HalfPointFreeExpr.class));
assertThat(expression).isInstanceOf(HalfPointFreeExpr.class);
assertEquals(expr, printNode(expression));
}

@@ -705,7 +705,7 @@ public void testAndWithImplicitParameter3() {
public void dotFreeWithRegexp() {
String expr = "name matches \"[a-z]*\"";
Expression expression = parseExpression( parser, expr ).getExpr();
assertThat(expression, instanceOf(PointFreeExpr.class));
assertThat(expression).isInstanceOf(PointFreeExpr.class);
assertEquals("name matches \"[a-z]*\"", printNode(expression));
PointFreeExpr e = (PointFreeExpr)expression;
assertEquals("matches", e.getOperator().asString());
@@ -724,25 +724,25 @@ public void implicitOperatorWithRegexps() {
public void halfPointFreeExpr() {
String expr = "matches \"[A-Z]*\"";
Expression expression = parseExpression(parser, expr).getExpr();
assertThat(expression, instanceOf(HalfPointFreeExpr.class));
assertThat(expression).isInstanceOf(HalfPointFreeExpr.class);
assertEquals("matches \"[A-Z]*\"", printNode(expression));
}

@Test
public void halfPointFreeExprNegated() {
String expr = "not matches \"[A-Z]*\"";
Expression expression = parseExpression(parser, expr).getExpr();
assertThat(expression, instanceOf(HalfPointFreeExpr.class));
assertThat(expression).isInstanceOf(HalfPointFreeExpr.class);
assertEquals("not matches \"[A-Z]*\"", printNode(expression));
}

@Test
public void regressionTestHalfPointFree() {
assertThat(parseExpression(parser, "getAddress().getAddressName().length() == 5").getExpr(), instanceOf(BinaryExpr.class));
assertThat(parseExpression(parser, "isFortyYearsOld(this, true)").getExpr(), instanceOf(MethodCallExpr.class));
assertThat(parseExpression(parser, "getName().startsWith(\"M\")").getExpr(), instanceOf(MethodCallExpr.class));
assertThat(parseExpression(parser, "isPositive($i.intValue())").getExpr(), instanceOf(MethodCallExpr.class));
assertThat(parseExpression(parser, "someEntity.someString in (\"1.500\")").getExpr(), instanceOf(PointFreeExpr.class));
assertThat(parseExpression(parser, "getAddress().getAddressName().length() == 5").getExpr()).isInstanceOf(BinaryExpr.class);
assertThat(parseExpression(parser, "isFortyYearsOld(this, true)").getExpr()).isInstanceOf(MethodCallExpr.class);
assertThat(parseExpression(parser, "getName().startsWith(\"M\")").getExpr()).isInstanceOf(MethodCallExpr.class);
assertThat(parseExpression(parser, "isPositive($i.intValue())").getExpr()).isInstanceOf(MethodCallExpr.class);
assertThat(parseExpression(parser, "someEntity.someString in (\"1.500\")").getExpr()).isInstanceOf(PointFreeExpr.class);
}

@Test
@@ -760,7 +760,7 @@ public void halfPointFreeMVEL() {
assertEquals("this str[startsWith] \"M\" || str[startsWith] \"E\"", printNode(expression));

Expression expression2 = parseExpression(parser, "str[startsWith] \"E\"").getExpr();
assertThat(expression2, instanceOf(HalfPointFreeExpr.class));
assertThat(expression2).isInstanceOf(HalfPointFreeExpr.class);
assertEquals("str[startsWith] \"E\"", printNode(expression2));
}

@@ -1154,7 +1154,7 @@ public void testLineBreakAtTheEndOfStatementWithoutSemicolon() {
private void testMvelSquareOperator(String wholeExpression, String operator, String left, String right, boolean isNegated) {
String expr = wholeExpression;
Expression expression = parseExpression(parser, expr ).getExpr();
assertThat(expression, instanceOf(PointFreeExpr.class));
assertThat(expression).isInstanceOf(PointFreeExpr.class);
assertEquals(wholeExpression, printNode(expression));
PointFreeExpr e = (PointFreeExpr)expression;
assertEquals(operator, e.getOperator().asString());
Original file line number Diff line number Diff line change
@@ -39,7 +39,6 @@
import org.kie.api.runtime.KieSession;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@RunWith(Parameterized.class)
Original file line number Diff line number Diff line change
@@ -37,7 +37,6 @@
import org.drools.testcoverage.common.util.KieBaseUtil;
import org.drools.testcoverage.common.util.KieUtil;
import org.drools.testcoverage.common.util.TestParametersUtil;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@@ -55,14 +54,15 @@
import org.mockito.ArgumentCaptor;
import org.mvel2.MVEL;

import static org.hamcrest.CoreMatchers.is;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;


@RunWith(Parameterized.class)
public class JBRULESTest {

@@ -229,13 +229,13 @@ public void testJBRULES3111() {
verify(ael, times(2)).afterMatchFired(captor.capture());
final List<org.kie.api.event.rule.AfterMatchFiredEvent> aafe = captor.getAllValues();

Assert.assertThat(aafe.get(0).getMatch().getRule().getName(), is("kickOff"));
Assert.assertThat(aafe.get(1).getMatch().getRule().getName(), is("r1"));
assertThat(aafe.get(0).getMatch().getRule().getName()).isEqualTo("kickOff");
assertThat(aafe.get(1).getMatch().getRule().getName()).isEqualTo("r1");

final Object value = aafe.get(1).getMatch().getDeclarationValue("$t");
final String name = (String) MVEL.eval("$t.name", Collections.singletonMap("$t", value));

Assert.assertThat(name, is("one"));
assertThat(name).isEqualTo("one");
}

@Test
Original file line number Diff line number Diff line change
@@ -49,11 +49,11 @@
import org.kie.api.runtime.rule.Match;

import static java.util.Arrays.asList;
import static junit.framework.Assert.assertTrue;
import static junit.framework.TestCase.assertSame;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

@RunWith(Parameterized.class)
public class AddRuleTest {
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@
import org.kie.api.definition.KiePackage;
import org.kie.api.runtime.rule.Match;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.assertEquals;
import static junit.framework.TestCase.assertNotSame;
import static junit.framework.TestCase.assertSame;
import static org.junit.Assert.assertNotNull;
Loading

0 comments on commit 3d10755

Please sign in to comment.