2 * Copyright © 2010 Sebastian Hoß <mail@shoss.de>
3 * This work is free. You can redistribute it and/or modify it under the
4 * terms of the Do What The Fuck You Want To Public License, Version 2,
5 * as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
7 package com
.github
.sebhoss
.reguloj
;
9 import java
.util
.HashSet
;
12 import com
.github
.sebhoss
.warnings
.CompilerWarnings
;
13 import com
.google
.common
.collect
.ImmutableList
;
15 import org
.junit
.Assert
;
16 import org
.junit
.Before
;
17 import org
.junit
.Test
;
18 import org
.junit
.rules
.ExpectedException
;
19 import org
.mockito
.BDDMockito
;
20 import org
.mockito
.Mockito
;
23 * Test cases for the ChainedRuleEngine.
25 @SuppressWarnings({ CompilerWarnings
.BOXING
, CompilerWarnings
.UNCHECKED
})
26 public class ChainedRuleEngineTest
{
28 /** Checks expected exception inside single test cases. */
30 public ExpectedException thrown
= ExpectedException
.none();
32 private RuleEngine
<Context
<Object
>> engine
;
33 private Context
<Object
> context
;
34 private Rule
<Context
<Object
>> rule
;
37 * Creates rule engine and context.
41 engine
= new ChainedRuleEngine
<>();
42 context
= Mockito
.mock(Context
.class);
43 rule
= Mockito
.mock(Rule
.class);
48 * Test method for ChainedRuleEngine#analyze(Context, java.util.Set)
51 * Ensures that <code>false</code> is returned when passing in an empty set.
55 public void shouldReturnFalseForEmptyRuleSet() {
56 final Set
<Rule
<Context
<Object
>>> rules
= new HashSet
<>();
58 final boolean fired
= engine
.analyze(rules
, context
);
60 Assert
.assertFalse(fired
);
65 * Test method for ChainedRuleEngine#analyze(Context, Set)
68 * Ensures that <code>true</code> is returned if any rule can fire.
72 public void shouldReturnTrueIfRuleFired() {
73 BDDMockito
.given(rule
.fires(context
)).willReturn(Boolean
.TRUE
);
75 final boolean fired
= engine
.analyze(ImmutableList
.of(rule
), context
);
77 Assert
.assertTrue(fired
);
82 * Test method for ChainedRuleEngine#analyze(Context, Set)
85 * Ensures that <code>false</code> is returned if no rule can fire.
89 public void shouldReturnFalseIfNoRuleFires() {
90 BDDMockito
.given(rule
.fires(context
)).willReturn(Boolean
.FALSE
);
92 final boolean fired
= engine
.analyze(ImmutableList
.of(rule
), context
);
94 Assert
.assertFalse(fired
);
99 * Test method for ChainedRuleEngine#infer(Context, Set)
102 * Ensures that the engine can handle an empty rule set.
107 public void shouldRunWithEmptyRuleSet() {
108 final Set
<Rule
<Context
<Object
>>> rules
= new HashSet
<>();
110 engine
.infer(rules
, context
);
115 * Test method for ChainedRuleEngine#infer(Context, Set)
118 * Ensures that the engine loops if any rule can fire.
122 public void shouldLoopWithFiringRule() {
123 BDDMockito
.given(rule
.run(context
)).willReturn(Boolean
.TRUE
).willReturn(Boolean
.FALSE
);
125 engine
.infer(ImmutableList
.of(rule
), context
);
127 Mockito
.verify(rule
, Mockito
.times(2)).run(context
);
132 * Test method for ChainedRuleEngine#infer(Context, Set)
135 * Ensures that the engine does not loop if no rule can fire.
139 public void shouldNotLoopWithNotFiringRule() {
140 BDDMockito
.given(rule
.run(context
)).willReturn(Boolean
.FALSE
);
142 engine
.infer(ImmutableList
.of(rule
), context
);
144 Mockito
.verify(rule
, Mockito
.times(1)).run(context
);