add sanity checks

pull/94/head
Mahmoud Ben Hassine 8 years ago
parent 423c704644
commit 45aec6d238

@ -46,6 +46,7 @@ public class Facts implements Iterable<Map.Entry<String, Object>> {
*/
@Deprecated
public void add(String name, Object fact) {
Objects.requireNonNull(name);
facts.put(name, fact);
}
@ -57,6 +58,7 @@ public class Facts implements Iterable<Map.Entry<String, Object>> {
* @param fact object to put in the working memory
*/
public void put(String name, Object fact) {
Objects.requireNonNull(name);
facts.put(name, fact);
}
@ -66,6 +68,7 @@ public class Facts implements Iterable<Map.Entry<String, Object>> {
* @param name of fact to remove
*/
public void remove(String name) {
Objects.requireNonNull(name);
facts.remove(name);
}
@ -76,6 +79,7 @@ public class Facts implements Iterable<Map.Entry<String, Object>> {
* @return the fact having the given name, or null if there is no fact with the given name
*/
public Object get(String name) {
Objects.requireNonNull(name);
return facts.get(name);
}

@ -25,10 +25,7 @@ package org.jeasy.rules.api;
import org.jeasy.rules.core.RuleProxy;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.*;
/**
* This class encapsulates a set of rules and represents a rules namespace.
@ -75,6 +72,7 @@ public class Rules implements Iterable<Rule> {
* @param rule to register
*/
public void register(Object rule) {
Objects.requireNonNull(rule);
rules.add(RuleProxy.asRule(rule));
}
@ -84,6 +82,7 @@ public class Rules implements Iterable<Rule> {
* @param rule to unregister
*/
public void unregister(Object rule) {
Objects.requireNonNull(rule);
rules.remove(RuleProxy.asRule(rule));
}
@ -93,6 +92,7 @@ public class Rules implements Iterable<Rule> {
* @param ruleName the name of the rule to unregister
*/
public void unregister(final String ruleName){
Objects.requireNonNull(ruleName);
Rule rule = findRuleByName(ruleName);
if(rule != null) {
unregister(rule);

@ -54,4 +54,23 @@ public class FactsTest {
assertThat(facts.get("foo")).isEqualTo(1);
}
@Test(expected = NullPointerException.class)
public void addingNullFactsIsNotAllowed() throws Exception {
facts.add(null, "foo");
}
@Test(expected = NullPointerException.class)
public void whenPutNullFact_thenShouldThrowNullPointerException() throws Exception {
facts.put(null, "foo");
}
@Test(expected = NullPointerException.class)
public void whenRemoveNullFact_thenShouldThrowNullPointerException() throws Exception {
facts.remove(null);
}
@Test(expected = NullPointerException.class)
public void whenGetNullFact_thenShouldThrowNullPointerException() throws Exception {
facts.get(null);
}
}

@ -93,6 +93,16 @@ public class RulesTest {
assertThat(rules).isEmpty();
}
@Test(expected = NullPointerException.class)
public void whenRegisterNullRule_thenShouldThrowNullPointerException() throws Exception {
rules.register(null);
}
@Test(expected = NullPointerException.class)
public void whenUnregisterNullRule_thenShouldThrowNullPointerException() throws Exception {
rules.unregister(null);
}
@org.jeasy.rules.annotation.Rule
class DummyRule {
@Condition

Loading…
Cancel
Save