Skip to content

Commit

Permalink
OpenRewrite applied for junit5 and assertj (apache#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas committed Oct 2, 2024
1 parent ce5806b commit 9e033dc
Show file tree
Hide file tree
Showing 3 changed files with 295 additions and 421 deletions.
18 changes: 12 additions & 6 deletions drools-drl/drools-drl10-parser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,24 @@
</dependency>

<!-- Tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${version.org.assertj}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${version.org.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${version.org.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,98 @@

import java.util.List;

import org.drools.drl.ast.descr.*;
import org.junit.Test;
import org.drools.drl.ast.descr.AnnotationDescr;
import org.drools.drl.ast.descr.AttributeDescr;
import org.drools.drl.ast.descr.BaseDescr;
import org.drools.drl.ast.descr.ExprConstraintDescr;
import org.drools.drl.ast.descr.GlobalDescr;
import org.drools.drl.ast.descr.PackageDescr;
import org.drools.drl.ast.descr.PatternDescr;
import org.drools.drl.ast.descr.RuleDescr;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.drools.parser.DRLParserHelper.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.drools.parser.DRLParserHelper.computeTokenIndex;
import static org.drools.parser.DRLParserHelper.createDrlParser;
import static org.drools.parser.DRLParserHelper.parse;

public class DRLParserTest {
class DRLParserTest {

private static final String drl =
"package org.test;\n" +
"import org.test.model.Person;\n" +
"global String result;\n" +
"rule TestRule @Test(true) no-loop salience 15 when \n" +
" $p:Person( age >= 18 )\n" +
"then\n" +
" int a = 4;\n" +
" System.out.println($p.getName());\n" +
"end\n";
"import org.test.model.Person;\n" +
"global String result;\n" +
"rule TestRule @Test(true) no-loop salience 15 when \n" +
" $p:Person( age >= 18 )\n" +
"then\n" +
" int a = 4;\n" +
" System.out.println($p.getName());\n" +
"end\n";

@Test
public void testParse() {
void testParse() {
PackageDescr packageDescr = parse(drl);
assertEquals("org.test", packageDescr.getName());
assertThat(packageDescr.getName()).isEqualTo("org.test");

assertEquals(1, packageDescr.getImports().size());
assertEquals("org.test.model.Person", packageDescr.getImports().get(0).getTarget());
assertThat(packageDescr.getImports().size()).isEqualTo(1);
assertThat(packageDescr.getImports().get(0).getTarget()).isEqualTo("org.test.model.Person");

assertEquals(1, packageDescr.getGlobals().size());
assertThat(packageDescr.getGlobals().size()).isEqualTo(1);
GlobalDescr globalDescr = packageDescr.getGlobals().get(0);
assertEquals("String", globalDescr.getType());
assertEquals("result", globalDescr.getIdentifier());
assertThat(globalDescr.getType()).isEqualTo("String");
assertThat(globalDescr.getIdentifier()).isEqualTo("result");

assertEquals(1, packageDescr.getRules().size());
assertThat(packageDescr.getRules().size()).isEqualTo(1);
RuleDescr ruleDescr = packageDescr.getRules().get(0);

AnnotationDescr annotationDescr = ruleDescr.getAnnotation("Test");
assertNotNull(annotationDescr);
assertEquals("true", annotationDescr.getValue());
assertThat(annotationDescr).isNotNull();
assertThat(annotationDescr.getValue()).isEqualTo("true");

assertEquals(2, ruleDescr.getAttributes().size());
assertNotNull(ruleDescr.getAttributes().get("no-loop"));
assertThat(ruleDescr.getAttributes().size()).isEqualTo(2);
assertThat(ruleDescr.getAttributes().get("no-loop")).isNotNull();
AttributeDescr salience = ruleDescr.getAttributes().get("salience");
assertNotNull(salience);
assertEquals("15", salience.getValue());
assertThat(salience).isNotNull();
assertThat(salience.getValue()).isEqualTo("15");

assertEquals("TestRule", ruleDescr.getName());
assertThat(ruleDescr.getName()).isEqualTo("TestRule");

assertEquals(1, ruleDescr.getLhs().getDescrs().size());
assertThat(ruleDescr.getLhs().getDescrs().size()).isEqualTo(1);
PatternDescr patternDescr = (PatternDescr) ruleDescr.getLhs().getDescrs().get(0);
assertEquals("$p", patternDescr.getIdentifier());
assertEquals("Person", patternDescr.getObjectType());
assertThat(patternDescr.getIdentifier()).isEqualTo("$p");
assertThat(patternDescr.getObjectType()).isEqualTo("Person");

List<? extends BaseDescr> constraints = patternDescr.getConstraint().getDescrs();
assertEquals(1, constraints.size());
assertThat(constraints.size()).isEqualTo(1);
ExprConstraintDescr expr = (ExprConstraintDescr) constraints.get(0);
assertEquals("age >= 18", expr.getExpression());
assertThat(expr.getExpression()).isEqualTo("age >= 18");

assertThat(ruleDescr.getConsequence().toString()).isEqualToIgnoringWhitespace("int a = 4; System.out.println($p.getName());");
}

@Test
public void testComputeTokenIndex() {
void testComputeTokenIndex() {
DRLParser parser = createDrlParser(drl);
parser.compilationUnit();

assertEquals(0, (int) computeTokenIndex(parser, 1, 0));
assertEquals(0, (int) computeTokenIndex(parser, 1, 1));
assertEquals(0, (int) computeTokenIndex(parser, 1, 7));
assertEquals(1, (int) computeTokenIndex(parser, 1, 8));
assertEquals(2, (int) computeTokenIndex(parser, 1, 9));
assertEquals(2, (int) computeTokenIndex(parser, 1, 9));
assertEquals(3, (int) computeTokenIndex(parser, 1, 12));
assertEquals(4, (int) computeTokenIndex(parser, 1, 13));
assertEquals(5, (int) computeTokenIndex(parser, 1, 17));
assertEquals(6, (int) computeTokenIndex(parser, 1, 18));
assertEquals(6, (int) computeTokenIndex(parser, 2, 0));
assertEquals(7, (int) computeTokenIndex(parser, 2, 1));
assertEquals(7, (int) computeTokenIndex(parser, 2, 6));
assertEquals(8, (int) computeTokenIndex(parser, 2, 7));
assertEquals(80, (int) computeTokenIndex(parser, 9, 0));
assertEquals(81, (int) computeTokenIndex(parser, 9, 1));
assertEquals(82, (int) computeTokenIndex(parser, 9, 4));
assertEquals(82, (int) computeTokenIndex(parser, 9, 5));
assertEquals(82, (int) computeTokenIndex(parser, 10, 0)); // EOF
assertThat((int) computeTokenIndex(parser, 1, 0)).isEqualTo(0);
assertThat((int) computeTokenIndex(parser, 1, 1)).isEqualTo(0);
assertThat((int) computeTokenIndex(parser, 1, 7)).isEqualTo(0);
assertThat((int) computeTokenIndex(parser, 1, 8)).isEqualTo(1);
assertThat((int) computeTokenIndex(parser, 1, 9)).isEqualTo(2);
assertThat((int) computeTokenIndex(parser, 1, 9)).isEqualTo(2);
assertThat((int) computeTokenIndex(parser, 1, 12)).isEqualTo(3);
assertThat((int) computeTokenIndex(parser, 1, 13)).isEqualTo(4);
assertThat((int) computeTokenIndex(parser, 1, 17)).isEqualTo(5);
assertThat((int) computeTokenIndex(parser, 1, 18)).isEqualTo(6);
assertThat((int) computeTokenIndex(parser, 2, 0)).isEqualTo(6);
assertThat((int) computeTokenIndex(parser, 2, 1)).isEqualTo(7);
assertThat((int) computeTokenIndex(parser, 2, 6)).isEqualTo(7);
assertThat((int) computeTokenIndex(parser, 2, 7)).isEqualTo(8);
assertThat((int) computeTokenIndex(parser, 9, 0)).isEqualTo(80);
assertThat((int) computeTokenIndex(parser, 9, 1)).isEqualTo(81);
assertThat((int) computeTokenIndex(parser, 9, 4)).isEqualTo(82);
assertThat((int) computeTokenIndex(parser, 9, 5)).isEqualTo(82);
assertThat((int) computeTokenIndex(parser, 10, 0)).isEqualTo(82); // EOF
}

}
Loading

0 comments on commit 9e033dc

Please sign in to comment.