Use new suppress warnings constants location
[reguloj.git] / src / test / java / com / github / sebhoss / reguloj / ChainedRuleEngineTest.java
blobf8e8f344b64385d7dbe357fb74d60c15bad4ad07
1 /*
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.
6 */
7 package com.github.sebhoss.reguloj;
9 import java.util.HashSet;
10 import java.util.Set;
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;
22 /**
23 * Test cases for the ChainedRuleEngine.
25 @SuppressWarnings({ CompilerWarnings.BOXING, CompilerWarnings.UNCHECKED })
26 public class ChainedRuleEngineTest {
28 /** Checks expected exception inside single test cases. */
29 @org.junit.Rule
30 public ExpectedException thrown = ExpectedException.none();
32 private RuleEngine<Context<Object>> engine;
33 private Context<Object> context;
34 private Rule<Context<Object>> rule;
36 /**
37 * Creates rule engine and context.
39 @Before
40 public void setup() {
41 engine = new ChainedRuleEngine<>();
42 context = Mockito.mock(Context.class);
43 rule = Mockito.mock(Rule.class);
46 /**
47 * <p>
48 * Test method for ChainedRuleEngine#analyze(Context, java.util.Set)
49 * </p>
50 * <p>
51 * Ensures that <code>false</code> is returned when passing in an empty set.
52 * </p>
54 @Test
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);
63 /**
64 * <p>
65 * Test method for ChainedRuleEngine#analyze(Context, Set)
66 * </p>
67 * <p>
68 * Ensures that <code>true</code> is returned if any rule can fire.
69 * </p>
71 @Test
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);
80 /**
81 * <p>
82 * Test method for ChainedRuleEngine#analyze(Context, Set)
83 * </p>
84 * <p>
85 * Ensures that <code>false</code> is returned if no rule can fire.
86 * </p>
88 @Test
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);
97 /**
98 * <p>
99 * Test method for ChainedRuleEngine#infer(Context, Set)
100 * </p>
101 * <p>
102 * Ensures that the engine can handle an empty rule set.
103 * </p>
106 @Test
107 public void shouldRunWithEmptyRuleSet() {
108 final Set<Rule<Context<Object>>> rules = new HashSet<>();
110 engine.infer(rules, context);
114 * <p>
115 * Test method for ChainedRuleEngine#infer(Context, Set)
116 * </p>
117 * <p>
118 * Ensures that the engine loops if any rule can fire.
119 * </p>
121 @Test
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);
131 * <p>
132 * Test method for ChainedRuleEngine#infer(Context, Set)
133 * </p>
134 * <p>
135 * Ensures that the engine does not loop if no rule can fire.
136 * </p>
138 @Test
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);