1 15 package org.apache.hivemind.conditional; 16 17 import org.apache.hivemind.conditional.AndEvaluator; 18 import org.apache.hivemind.conditional.ClassNameEvaluator; 19 import org.apache.hivemind.conditional.EvaluationContext; 20 import org.apache.hivemind.conditional.Node; 21 import org.apache.hivemind.conditional.NodeImpl; 22 import org.apache.hivemind.conditional.NotEvaluator; 23 import org.apache.hivemind.conditional.OrEvaluator; 24 import org.apache.hivemind.conditional.PropertyEvaluator; 25 import org.apache.hivemind.test.HiveMindTestCase; 26 import org.easymock.MockControl; 27 28 34 public class TestEvaluators extends HiveMindTestCase 35 { 36 private EvaluationContext newContext() 37 { 38 return (EvaluationContext) newMock(EvaluationContext.class); 39 } 40 41 private Node newNode(EvaluationContext context, boolean value) 42 { 43 MockControl control = newControl(Node.class); 44 Node node = (Node) control.getMock(); 45 46 node.evaluate(context); 47 48 control.setReturnValue(value); 49 50 return node; 51 } 52 53 private Node newNode() 54 { 55 return (Node) newMock(Node.class); 56 } 57 58 public void testPropertyEvaluator() 59 { 60 MockControl control = newControl(EvaluationContext.class); 61 EvaluationContext context = (EvaluationContext) control.getMock(); 62 63 context.isPropertySet("foo.bar"); 64 control.setReturnValue(true); 65 66 replayControls(); 67 68 PropertyEvaluator pe = new PropertyEvaluator("foo.bar"); 69 70 assertEquals(true, pe.evaluate(context, null)); 71 72 verifyControls(); 73 } 74 75 public void testClassNameEvaluator() 76 { 77 MockControl control = newControl(EvaluationContext.class); 78 EvaluationContext context = (EvaluationContext) control.getMock(); 79 80 context.doesClassExist("foo.bar.Baz"); 81 control.setReturnValue(true); 82 83 replayControls(); 84 85 ClassNameEvaluator e = new ClassNameEvaluator("foo.bar.Baz"); 86 87 assertEquals(true, e.evaluate(context, null)); 88 89 verifyControls(); 90 } 91 92 public void testNotEvaluator() 93 { 94 EvaluationContext context = newContext(); 95 Node left = newNode(context, true); 96 97 Node node = new NodeImpl(left, null, new NotEvaluator()); 98 99 replayControls(); 100 101 assertEquals(false, node.evaluate(context)); 102 103 verifyControls(); 104 } 105 106 public void testAndEvaluatorTrue() 107 { 108 EvaluationContext context = newContext(); 109 Node left = newNode(context, true); 110 Node right = newNode(context, true); 111 112 Node node = new NodeImpl(left, right, new AndEvaluator()); 113 114 replayControls(); 115 116 assertEquals(true, node.evaluate(context)); 117 118 verifyControls(); 119 } 120 121 public void testAndEvaluatorShortcicuit() 122 { 123 EvaluationContext context = newContext(); 124 Node left = newNode(context, false); 125 Node right = newNode(); 126 127 Node node = new NodeImpl(left, right, new AndEvaluator()); 128 129 replayControls(); 130 131 assertEquals(false, node.evaluate(context)); 132 133 verifyControls(); 134 } 135 136 public void testAndEvaluatorFalse() 137 { 138 EvaluationContext context = newContext(); 139 Node left = newNode(context, true); 140 Node right = newNode(context, false); 141 142 Node node = new NodeImpl(left, right, new AndEvaluator()); 143 144 replayControls(); 145 146 assertEquals(false, node.evaluate(context)); 147 148 verifyControls(); 149 } 150 151 public void testOrEvaluatorTrue() 152 { 153 EvaluationContext context = newContext(); 154 Node left = newNode(context, false); 155 Node right = newNode(context, true); 156 157 Node node = new NodeImpl(left, right, new OrEvaluator()); 158 159 replayControls(); 160 161 assertEquals(true, node.evaluate(context)); 162 163 verifyControls(); 164 } 165 166 public void testOrEvaluatorShortcicuit() 167 { 168 EvaluationContext context = newContext(); 169 Node left = newNode(context, true); 170 Node right = newNode(); 171 172 Node node = new NodeImpl(left, right, new OrEvaluator()); 173 174 replayControls(); 175 176 assertEquals(true, node.evaluate(context)); 177 178 verifyControls(); 179 } 180 181 public void testOrEvaluatorFalse() 182 { 183 EvaluationContext context = newContext(); 184 Node left = newNode(context, false); 185 Node right = newNode(context, false); 186 187 Node node = new NodeImpl(left, right, new OrEvaluator()); 188 189 replayControls(); 190 191 assertEquals(false, node.evaluate(context)); 192 193 verifyControls(); 194 } 195 196 } | Popular Tags |