update tests

pull/3/head
Mahmoud Ben Hassine 11 years ago
parent f6afa72a19
commit 0fc49f9e61

@ -0,0 +1,81 @@
package io.github.benas.easyrules.core.test;
import io.github.benas.easyrules.api.RulesEngine;
import io.github.benas.easyrules.core.PriorityRulesEngine;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Test class for composite priority rule execution.
*
* @author Mahmoud Ben Hassine (md.benhassine@gmail.com)
*/
public class CompositePriorityRuleTest {
private SimplePriorityRule rule1, rule2;
private SimpleCompositePriorityRule compositeRule;
private RulesEngine rulesEngine;
@Before
public void setup(){
rule1 = new SimplePriorityRule("r1","d1",1);
rule2 = new SimplePriorityRule("r2","d2",2);
compositeRule = new SimpleCompositePriorityRule("cp", "crd");
rulesEngine = new PriorityRulesEngine();
}
@Test
public void testCompositeRule() {
compositeRule.addRule(rule1);
compositeRule.addRule(rule2);
rulesEngine.registerRule(compositeRule);
rulesEngine.fireRules();
//Rule 1 should be executed
assertEquals(true, rule1.isExecuted());
//Rule 2 should be executed
assertEquals(true, rule2.isExecuted());
//The composite Rule should be executed
assertEquals(true, compositeRule.isExecuted());
}
@Test
public void testCompositeRuleWithARuleThatEvaluateToFalse() {
compositeRule.addRule(rule1);
rule2 = new SimplePriorityRuleThatEvaluateToFalse("r2","d2",2);
compositeRule.addRule(rule2);
rulesEngine.registerRule(compositeRule);
rulesEngine.fireRules();
//The composite rule and composing rules should not be executed since not all rules conditions evaluate to TRUE
//Rule 1 should not be executed
assertEquals(false, rule1.isExecuted());
//Rule 2 should not be executed
assertEquals(false, rule2.isExecuted());
//The composite Rule not should be executed
assertEquals(false, compositeRule.isExecuted());
}
}

@ -1,6 +1,7 @@
package io.github.benas.easyrules.core.test; package io.github.benas.easyrules.core.test;
import io.github.benas.easyrules.api.RulesEngine; import io.github.benas.easyrules.api.RulesEngine;
import io.github.benas.easyrules.core.BasicRule;
import io.github.benas.easyrules.core.DefaultRulesEngine; import io.github.benas.easyrules.core.DefaultRulesEngine;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -23,10 +24,10 @@ public class CompositeRuleTest {
@Before @Before
public void setup(){ public void setup(){
rule1 = new SimpleRule("r1","d1",1); rule1 = new SimpleRule("r1","d1");
rule2 = new SimpleRule("r2","d2",2); rule2 = new SimpleRule("r2","d2");
compositeRule = new SimpleCompositeRule("cp", "crd", 0); compositeRule = new SimpleCompositeRule("cp", "crd");
rulesEngine = new DefaultRulesEngine(); rulesEngine = new DefaultRulesEngine();
} }
@ -57,7 +58,7 @@ public class CompositeRuleTest {
compositeRule.addRule(rule1); compositeRule.addRule(rule1);
rule2 = new SimpleRuleThatEvaluateToFalse("r2","d2",2); rule2 = new SimpleRuleThatEvaluateToFalse("r2","d2");
compositeRule.addRule(rule2); compositeRule.addRule(rule2);

@ -16,7 +16,8 @@ import org.junit.runners.Suite;
RulePriorityThresholdTest.class, RulePriorityThresholdTest.class,
SkipOnFirstAppliedRuleTest.class, SkipOnFirstAppliedRuleTest.class,
JmxRuleRegistrationTest.class, JmxRuleRegistrationTest.class,
CompositeRuleTest.class}) CompositeRuleTest.class,
CompositePriorityRuleTest.class})
public class EasyRulesTestSuite extends TestSuite { public class EasyRulesTestSuite extends TestSuite {
} }

@ -1,9 +1,12 @@
package io.github.benas.easyrules.core.test; package io.github.benas.easyrules.core.test;
import io.github.benas.easyrules.api.PriorityRule;
import io.github.benas.easyrules.api.Rule; import io.github.benas.easyrules.api.Rule;
import io.github.benas.easyrules.api.RulesEngine; import io.github.benas.easyrules.api.RulesEngine;
import io.github.benas.easyrules.core.BasicPriorityRule;
import io.github.benas.easyrules.core.BasicRule; import io.github.benas.easyrules.core.BasicRule;
import io.github.benas.easyrules.core.DefaultRulesEngine; import io.github.benas.easyrules.core.DefaultRulesEngine;
import io.github.benas.easyrules.core.PriorityRulesEngine;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -20,16 +23,16 @@ import static org.junit.Assert.assertNotNull;
*/ */
public class JmxRuleRegistrationTest { public class JmxRuleRegistrationTest {
private BasicRule rule; private PriorityRule rule;
private RulesEngine rulesEngine; private PriorityRulesEngine rulesEngine;
@Before @Before
public void setup(){ public void setup(){
rule = new BasicRule("rule","description",1); rule = new BasicPriorityRule("rule","description", 1);
rulesEngine = new DefaultRulesEngine(); rulesEngine = new PriorityRulesEngine();
} }
@Test @Test
@ -39,7 +42,7 @@ public class JmxRuleRegistrationTest {
//assert that the rule has been successfully registered within JMX registry //assert that the rule has been successfully registered within JMX registry
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("io.github.benas.easyrules.jmx:type=" + Rule.class.getSimpleName() + ",name=" + rule.getName()); ObjectName name = new ObjectName("io.github.benas.easyrules.jmx:type=" + rule.getClass().getSimpleName() + ",name=" + rule.getName());
MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(name); MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(name);
assertNotNull(mBeanInfo); assertNotNull(mBeanInfo);

@ -24,6 +24,8 @@
package io.github.benas.easyrules.core.test; package io.github.benas.easyrules.core.test;
import io.github.benas.easyrules.api.PriorityRule;
import io.github.benas.easyrules.core.BasicPriorityRule;
import io.github.benas.easyrules.core.BasicRule; import io.github.benas.easyrules.core.BasicRule;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -36,19 +38,24 @@ import static org.junit.Assert.*;
*/ */
public class RulePriorityComparisonTest { public class RulePriorityComparisonTest {
private BasicRule rule1, rule2; private BasicPriorityRule rule1, rule2;
@Before @Before
public void setup(){ public void setup(){
rule1 = new BasicRule("r1","d1",1); rule1 = new BasicPriorityRule("r1","d1",1);
rule2 = new BasicRule("r2","d2",2); rule2 = new BasicPriorityRule("r2","d2",2);
} }
@Test @Test
public void testDifferentRulePriorityComparison() { public void testLessThanRulePriorityComparison() {
assertEquals(-1, rule1.compareTo(rule2)); assertEquals(-1, rule1.compareTo(rule2));
} }
@Test
public void testGreaterThanRulePriorityComparison() {
assertEquals(1, rule2.compareTo(rule1));
}
@Test @Test
public void testSameRulePriorityComparison() { public void testSameRulePriorityComparison() {
rule1.setPriority(2); rule1.setPriority(2);

@ -25,36 +25,35 @@
package io.github.benas.easyrules.core.test; package io.github.benas.easyrules.core.test;
import io.github.benas.easyrules.api.RulesEngine; import io.github.benas.easyrules.api.RulesEngine;
import io.github.benas.easyrules.core.DefaultRulesEngine; import io.github.benas.easyrules.core.PriorityRulesEngine;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
/** /**
* Test class of "Rule Priority Threshold" parameter of Easy Rules default engine. * Test class of "Rule Priority Threshold" parameter of Easy Rules engine.
* *
* @author Mahmoud Ben Hassine (md.benhassine@gmail.com) * @author Mahmoud Ben Hassine (md.benhassine@gmail.com)
*/ */
public class RulePriorityThresholdTest { public class RulePriorityThresholdTest {
private SimpleRule rule1, rule2; private SimplePriorityRule rule1, rule2;
private RulesEngine rulesEngine; private RulesEngine rulesEngine;
@Before @Before
public void setup(){ public void setup(){
rule1 = new SimpleRule("r1","d1",1); rule1 = new SimplePriorityRule("r1","d1",1);
rule2 = new SimpleRule("r2","d2",2); rule2 = new SimplePriorityRule("r2","d2",2);
rulesEngine = new DefaultRulesEngine(); rulesEngine = new PriorityRulesEngine(1);
} }
@Test @Test
public void testRulePriorityThreshold() { public void testRulePriorityThreshold() {
rulesEngine.setRulePriorityThreshold(1);
rulesEngine.registerRule(rule1); rulesEngine.registerRule(rule1);
rulesEngine.registerRule(rule2); rulesEngine.registerRule(rule2);

@ -0,0 +1,28 @@
package io.github.benas.easyrules.core.test;
import io.github.benas.easyrules.core.CompositePriorityRule;
/**
* Simple composite rule class used for tests.
*
* @author Mahmoud Ben Hassine (md.benhassine@gmail.com)
*/
public class SimpleCompositePriorityRule extends CompositePriorityRule {
protected boolean executed;
public SimpleCompositePriorityRule(String name, String description) {
super(name, description);
}
@Override
public void performActions() throws Exception {
super.performActions();
executed = true;
}
public boolean isExecuted() {
return executed;
}
}

@ -11,8 +11,8 @@ public class SimpleCompositeRule extends CompositeRule {
protected boolean executed; protected boolean executed;
public SimpleCompositeRule(String name, String description, int priority) { public SimpleCompositeRule(String name, String description) {
super(name, description, priority); super(name, description);
} }
@Override @Override

@ -0,0 +1,35 @@
package io.github.benas.easyrules.core.test;
import io.github.benas.easyrules.core.BasicPriorityRule;
/**
* Simple priority rule class used for tests.
*
* @author Mahmoud Ben Hassine (md.benhassine@gmail.com)
*/
public class SimplePriorityRule extends BasicPriorityRule {
/**
* Has the rule been executed? .
*/
protected boolean executed;
public SimplePriorityRule(String name, String description, int priority) {
super(name, description, priority);
}
@Override
public boolean evaluateConditions() {
return true;
}
@Override
public void performActions() throws Exception {
executed = true;
}
public boolean isExecuted() {
return executed;
}
}

@ -0,0 +1,19 @@
package io.github.benas.easyrules.core.test;
/**
* Simple priority rule class used for tests.
*
* @author Mahmoud Ben Hassine (md.benhassine@gmail.com)
*/
public class SimplePriorityRuleThatEvaluateToFalse extends SimplePriorityRule {
public SimplePriorityRuleThatEvaluateToFalse(String name, String description, int priority) {
super(name, description, priority);
}
@Override
public boolean evaluateConditions() {
return false;
}
}

@ -0,0 +1,19 @@
package io.github.benas.easyrules.core.test;
/**
* Simple priority rule class used for tests.
*
* @author Mahmoud Ben Hassine (md.benhassine@gmail.com)
*/
public class SimplePriorityRuleThatThrowsException extends SimplePriorityRule {
public SimplePriorityRuleThatThrowsException(String name, String description, int priority) {
super(name, description, priority);
}
@Override
public void performActions() throws Exception {
throw new Exception("An exception occurred in SimplePriorityRuleThatThrowsException.performActions");
}
}

@ -1,5 +1,6 @@
package io.github.benas.easyrules.core.test; package io.github.benas.easyrules.core.test;
import io.github.benas.easyrules.core.BasicPriorityRule;
import io.github.benas.easyrules.core.BasicRule; import io.github.benas.easyrules.core.BasicRule;
/** /**
@ -14,8 +15,8 @@ public class SimpleRule extends BasicRule {
*/ */
protected boolean executed; protected boolean executed;
public SimpleRule(String name, String description, int priority) { public SimpleRule(String name, String description) {
super(name, description, priority); super(name, description);
} }
@Override @Override

@ -1,14 +1,14 @@
package io.github.benas.easyrules.core.test; package io.github.benas.easyrules.core.test;
/** /**
* Simple rule class used for tests. * Simple priority rule class used for tests.
* *
* @author Mahmoud Ben Hassine (md.benhassine@gmail.com) * @author Mahmoud Ben Hassine (md.benhassine@gmail.com)
*/ */
public class SimpleRuleThatEvaluateToFalse extends SimpleRule { public class SimpleRuleThatEvaluateToFalse extends SimpleRule {
public SimpleRuleThatEvaluateToFalse(String name, String description, int priority) { public SimpleRuleThatEvaluateToFalse(String name, String description) {
super(name, description, priority); super(name, description);
} }
@Override @Override

@ -7,8 +7,8 @@ package io.github.benas.easyrules.core.test;
*/ */
public class SimpleRuleThatThrowsException extends SimpleRule { public class SimpleRuleThatThrowsException extends SimpleRule {
public SimpleRuleThatThrowsException(String name, String description, int priority) { public SimpleRuleThatThrowsException(String name, String description) {
super(name, description, priority); super(name, description);
} }
@Override @Override

@ -25,7 +25,7 @@
package io.github.benas.easyrules.core.test; package io.github.benas.easyrules.core.test;
import io.github.benas.easyrules.api.RulesEngine; import io.github.benas.easyrules.api.RulesEngine;
import io.github.benas.easyrules.core.DefaultRulesEngine; import io.github.benas.easyrules.core.PriorityRulesEngine;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -38,27 +38,26 @@ import static org.junit.Assert.assertEquals;
*/ */
public class SkipOnFirstAppliedRuleTest { public class SkipOnFirstAppliedRuleTest {
private SimpleRule rule1, rule2; private SimplePriorityRule rule1, rule2;
private SimpleRuleThatThrowsException rule0; private SimplePriorityRuleThatThrowsException rule0;
private RulesEngine rulesEngine; private RulesEngine rulesEngine;
@Before @Before
public void setup(){ public void setup(){
rule1 = new SimpleRule("r1","d1",1); rule1 = new SimplePriorityRule("r1","d1",1);
rule2 = new SimpleRule("r2","d2",2); rule2 = new SimplePriorityRule("r2","d2",2);
rule0 = new SimpleRuleThatThrowsException("r0","d0",0); rule0 = new SimplePriorityRuleThatThrowsException("r0","d0",0);
rulesEngine = new DefaultRulesEngine(); rulesEngine = new PriorityRulesEngine(true);
} }
@Test @Test
public void testSkipOnFirstAppliedRule() { public void testSkipOnFirstAppliedRule() {
rulesEngine.setSkipOnFirstAppliedRule(true);
rulesEngine.registerRule(rule1); rulesEngine.registerRule(rule1);
rulesEngine.registerRule(rule2); rulesEngine.registerRule(rule2);
@ -75,7 +74,6 @@ public class SkipOnFirstAppliedRuleTest {
@Test @Test
public void testSkipOnFirstAppliedRuleWithException() { public void testSkipOnFirstAppliedRuleWithException() {
rulesEngine.setSkipOnFirstAppliedRule(true);
rulesEngine.registerRule(rule0); rulesEngine.registerRule(rule0);
rulesEngine.registerRule(rule1); rulesEngine.registerRule(rule1);

Loading…
Cancel
Save