Skip to content

Commit

Permalink
Incubator kie drools 5792 cleanup tests (#5986)
Browse files Browse the repository at this point in the history
* [incubator-kie-drools-5792] cleanup drools-drl-parser-tests
- Apply open-rewrite JUnit5BestPractices

* - Apply open-rewrite SimplifyChainedAssertJAssertions

* - remove 'parse_' prefix
- remove unused 'throw Exception'

* - code format
  • Loading branch information
tkobayas committed Jun 11, 2024
1 parent 8658f7a commit aaef430
Show file tree
Hide file tree
Showing 6 changed files with 1,077 additions and 1,138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,28 @@
/**
* DRLExprTreeTest
*/
public class DRLExprParserTest {
class DRLExprParserTest {

DrlExprParser parser;

@BeforeEach
public void setUp() {
void setUp() {
this.parser = DrlExprParserFactory.getDrlExprParser(LanguageLevelOption.DRL6);
}

@AfterEach
public void tearDown() {
void tearDown() {
this.parser = null;
}

@Test
public void testSimpleExpression() {
void simpleExpression() {
String source = "a > b";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(1);
assertThat(result.getDescrs()).hasSize(1);

RelationalExprDescr expr = (RelationalExprDescr) result.getDescrs().get( 0 );
assertThat(expr.getOperator()).isEqualTo(">");
Expand All @@ -80,13 +80,13 @@ public void testSimpleExpression() {
}

@Test
public void testAndConnective() throws Exception {
void andConnective() {
String source = "a > b && 10 != 20";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(2);
assertThat(result.getDescrs()).hasSize(2);

RelationalExprDescr expr = (RelationalExprDescr) result.getDescrs().get( 0 );
assertThat(expr.getOperator()).isEqualTo(">");
Expand All @@ -104,17 +104,17 @@ public void testAndConnective() throws Exception {
}

@Test
public void testConnective2() throws Exception {
void connective2() {
String source = "(a > b || 10 != 20) && someMethod(10) == 20";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(2);
assertThat(result.getDescrs()).hasSize(2);

ConstraintConnectiveDescr or = (ConstraintConnectiveDescr) result.getDescrs().get( 0 );
assertThat(or.getConnective()).isEqualTo(ConnectiveType.OR);
assertThat(or.getDescrs().size()).isEqualTo(2);
assertThat(or.getDescrs()).hasSize(2);

RelationalExprDescr expr = (RelationalExprDescr) or.getDescrs().get( 0 );
assertThat(expr.getOperator()).isEqualTo(">");
Expand All @@ -140,27 +140,27 @@ public void testConnective2() throws Exception {
}

@Test
public void testBinding() throws Exception {
void binding() {
String source = "$x : property";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(1);
assertThat(result.getDescrs()).hasSize(1);

BindingDescr bind = (BindingDescr) result.getDescrs().get( 0 );
assertThat(bind.getVariable()).isEqualTo("$x");
assertThat(bind.getExpression()).isEqualTo("property");
}

@Test
public void testBindingConstraint() throws Exception {
void bindingConstraint() {
String source = "$x : property > value";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(1);
assertThat(result.getDescrs()).hasSize(1);

RelationalExprDescr rel = (RelationalExprDescr) result.getDescrs().get( 0 );
assertThat(rel.getOperator()).isEqualTo(">");
Expand All @@ -174,13 +174,13 @@ public void testBindingConstraint() throws Exception {
}

@Test
public void testBindingWithRestrictions() throws Exception {
void bindingWithRestrictions() {
String source = "$x : property > value && < 20";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(2);
assertThat(result.getDescrs()).hasSize(2);

RelationalExprDescr rel = (RelationalExprDescr) result.getDescrs().get( 0 );
assertThat(rel.getOperator()).isEqualTo(">");
Expand All @@ -203,13 +203,13 @@ public void testBindingWithRestrictions() throws Exception {
}

@Test
public void testDoubleBinding() throws Exception {
void doubleBinding() {
String source = "$x : x.m( 1, a ) && $y : y[z].foo";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(2);
assertThat(result.getDescrs()).hasSize(2);

BindingDescr bind = (BindingDescr) result.getDescrs().get( 0 );
assertThat(bind.getVariable()).isEqualTo("$x");
Expand All @@ -231,7 +231,7 @@ public void testDoubleBinding() throws Exception {

@ParameterizedTest
@EnumSource(Operator.BuiltInOperator.class)
public void testDrlKeywordMethodCall(Operator.BuiltInOperator operator) throws Exception {
void drlKeywordMethodCall(Operator.BuiltInOperator operator) {
// Skip operators that cannot be used as method names (==, !=, <, etc.).
assumeFalse(nonKeywordBuiltInOperators.contains(operator));

Expand All @@ -240,15 +240,15 @@ public void testDrlKeywordMethodCall(Operator.BuiltInOperator operator) throws E
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(1);
assertThat(result.getDescrs()).hasSize(1);

AtomicExprDescr descr = (AtomicExprDescr) result.getDescrs().get( 0 );
assertThat(descr.getExpression()).isEqualTo(source);
}

@ParameterizedTest
@EnumSource(Operator.BuiltInOperator.class)
public void testDrlKeywordInChainedMethodCallWithBinding(Operator.BuiltInOperator operator) throws Exception {
void drlKeywordInChainedMethodCallWithBinding(Operator.BuiltInOperator operator) {
// Skip operators that cannot be used as method names (==, !=, <, etc.).
assumeFalse(nonKeywordBuiltInOperators.contains(operator));

Expand All @@ -259,25 +259,25 @@ public void testDrlKeywordInChainedMethodCallWithBinding(Operator.BuiltInOperato
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(1);
assertThat(result.getDescrs()).hasSize(1);

BindingDescr bind = (BindingDescr) result.getDescrs().get( 0 );
assertThat(bind.getVariable()).isEqualTo(bindingVariableSource);
assertThat(bind.getExpression()).isEqualTo(expressionSource);
}

@Test
public void testDeepBinding() throws Exception {
void deepBinding() {
String source = "($a : a > $b : b[10].prop || 10 != 20) && $x : someMethod(10) == 20";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(2);
assertThat(result.getDescrs()).hasSize(2);

ConstraintConnectiveDescr or = (ConstraintConnectiveDescr) result.getDescrs().get( 0 );
assertThat(or.getConnective()).isEqualTo(ConnectiveType.OR);
assertThat(or.getDescrs().size()).isEqualTo(2);
assertThat(or.getDescrs()).hasSize(2);

RelationalExprDescr expr = (RelationalExprDescr) or.getDescrs().get( 0 );
assertThat(expr.getOperator()).isEqualTo(">");
Expand Down Expand Up @@ -307,14 +307,14 @@ public void testDeepBinding() throws Exception {

@Test
@Timeout(10000L)
public void testNestedExpression() throws Exception {
void nestedExpression() {
// DROOLS-982
String source = "(((((((((((((((((((((((((((((((((((((((((((((((((( a > b ))))))))))))))))))))))))))))))))))))))))))))))))))";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(1);
assertThat(result.getDescrs()).hasSize(1);

RelationalExprDescr expr = (RelationalExprDescr) result.getDescrs().get( 0 );
assertThat(expr.getOperator()).isEqualTo(">");
Expand Down Expand Up @@ -349,26 +349,26 @@ public void testNestedExpression() throws Exception {
"Object[][].class.getName()",
"new<Integer>ArrayList<Integer>()"
})
void testKeywords(String source) {
void keywords(String source) {
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(1);
assertThat(result.getDescrs()).hasSize(1);

AtomicExprDescr expr = (AtomicExprDescr) result.getDescrs().get( 0 );

assertThat(expr.getExpression()).isEqualTo(source);
}

@Test
public void testKeyword_instanceof() {
void keywordInstanceof() {
String source = "a instanceof A";
ConstraintConnectiveDescr result = parser.parse( source );
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(1);
assertThat(result.getDescrs()).hasSize(1);

// Unlike the other keywords, instanceof can only be used in a relational expression,
// so it needs to be tested differently.
Expand All @@ -383,7 +383,7 @@ public void testKeyword_instanceof() {
}

@Test
public void testMismatchedInput() {
void mismatchedInput() {
String source = "+";
parser.parse(source);
assertThat(parser.hasErrors()).isTrue();
Expand Down Expand Up @@ -412,7 +412,7 @@ public void testMismatchedInput() {
}

@Test
public void testExtraneousInput() {
void extraneousInput() {
String source = "a +; b";
parser.parse(source);
assertThat(parser.hasErrors()).isTrue();
Expand Down Expand Up @@ -441,7 +441,7 @@ public void testExtraneousInput() {
}

@Test
public void testNoViableAlt() {
void noViableAlt() {
String source = "a~a";
parser.parse(source);

Expand Down Expand Up @@ -471,10 +471,10 @@ void orWithMethodCall() {
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND); // root is AND
assertThat(result.getDescrs().size()).isEqualTo(1);
assertThat(result.getDescrs()).hasSize(1);
ConstraintConnectiveDescr or = (ConstraintConnectiveDescr) result.getDescrs().get(0);
assertThat(or.getConnective()).isEqualTo(ConnectiveType.OR);
assertThat(or.getDescrs().size()).isEqualTo(2);
assertThat(or.getDescrs()).hasSize(2);

RelationalExprDescr expr = (RelationalExprDescr) or.getDescrs().get(0);
assertThat(expr.getOperator()).isEqualTo("==");
Expand All @@ -498,10 +498,10 @@ void orWithMethodCallWithArg() {
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND); // root is AND
assertThat(result.getDescrs().size()).isEqualTo(1);
assertThat(result.getDescrs()).hasSize(1);
ConstraintConnectiveDescr or = (ConstraintConnectiveDescr) result.getDescrs().get(0);
assertThat(or.getConnective()).isEqualTo(ConnectiveType.OR);
assertThat(or.getDescrs().size()).isEqualTo(2);
assertThat(or.getDescrs()).hasSize(2);

RelationalExprDescr expr = (RelationalExprDescr) or.getDescrs().get(0);
assertThat(expr.getOperator()).isEqualTo("==");
Expand All @@ -525,7 +525,7 @@ void andWithMethodCall() {
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(2);
assertThat(result.getDescrs()).hasSize(2);

RelationalExprDescr expr = (RelationalExprDescr) result.getDescrs().get(0);
assertThat(expr.getOperator()).isEqualTo("==");
Expand All @@ -549,7 +549,7 @@ void andWithMethodCallWithArg() {
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getConnective()).isEqualTo(ConnectiveType.AND);
assertThat(result.getDescrs().size()).isEqualTo(2);
assertThat(result.getDescrs()).hasSize(2);

RelationalExprDescr expr = (RelationalExprDescr) result.getDescrs().get(0);
assertThat(expr.getOperator()).isEqualTo("==");
Expand All @@ -572,7 +572,7 @@ void newBigDecimal() {
ConstraintConnectiveDescr result = parser.parse(source);
assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse();

assertThat(result.getDescrs().size()).isEqualTo(1);
assertThat(result.getDescrs()).hasSize(1);

BindingDescr bind = (BindingDescr) result.getDescrs().get(0);
assertThat(bind.getVariable()).isEqualTo("$bd");
Expand Down
Loading

0 comments on commit aaef430

Please sign in to comment.