1 15 package org.apache.hivemind.order; 16 17 import hivemind.test.FrameworkTestCase; 18 19 import java.util.List ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.ErrorHandler; 25 import org.apache.hivemind.impl.DefaultErrorHandler; 26 import org.apache.hivemind.test.AggregateArgumentsMatcher; 27 import org.apache.hivemind.test.ArgumentMatcher; 28 import org.apache.hivemind.test.TypeMatcher; 29 import org.easymock.MockControl; 30 31 36 public class TestOrderer extends FrameworkTestCase 37 { 38 private static final Log LOG = LogFactory.getLog(TestOrderer.class); 39 40 private ErrorHandler getErrorHandler() 41 { 42 return (ErrorHandler) newMock(ErrorHandler.class); 43 } 44 45 public void testNoDependencies() throws Exception 46 { 47 Orderer o = new Orderer(getErrorHandler(), "cartoon character"); 48 49 o.add("FRED", "fred", null, null); 50 o.add("BARNEY", "barney", null, null); 51 o.add("WILMA", "wilma", null, null); 52 o.add("BETTY", "betty", null, null); 53 54 replayControls(); 55 56 List l = o.getOrderedObjects(); 57 58 assertListsEqual(new Object [] 59 { "FRED", "BARNEY", "WILMA", "BETTY" }, l); 60 61 verifyControls(); 62 } 63 64 public void testPrereq() throws Exception 65 { 66 Orderer o = new Orderer(getErrorHandler(), "cartoon character"); 67 68 o.add("FRED", "fred", "wilma", null); 69 o.add("BARNEY", "barney", "betty", null); 70 o.add("BETTY", "betty", null, null); 71 o.add("WILMA", "wilma", null, null); 72 73 replayControls(); 74 75 List l = o.getOrderedObjects(); 76 77 assertListsEqual(new Object [] 78 { "WILMA", "FRED", "BETTY", "BARNEY" }, l); 79 80 verifyControls(); 81 } 82 83 public void testPostreq() throws Exception 84 { 85 Orderer o = new Orderer(getErrorHandler(), "cartoon character"); 86 87 o.add("FRED", "fred", null, "barney,wilma"); 88 o.add("BARNEY", "barney", null, "betty"); 89 o.add("BETTY", "betty", null, null); 90 o.add("WILMA", "wilma", null, null); 91 92 replayControls(); 93 94 List l = o.getOrderedObjects(); 95 96 assertListsEqual(new Object [] 97 { "FRED", "BARNEY", "BETTY", "WILMA" }, l); 98 99 verifyControls(); 100 } 101 102 public void testPrePostreq() throws Exception 103 { 104 Orderer o = new Orderer(getErrorHandler(), "cartoon character"); 105 106 o.add("FRED", "fred", null, "barney,wilma"); 107 o.add("BARNEY", "barney", "wilma", "betty"); 108 o.add("BETTY", "betty", null, null); 109 o.add("WILMA", "wilma", null, null); 110 111 replayControls(); 112 113 List l = o.getOrderedObjects(); 114 115 assertListsEqual(new Object [] 116 { "FRED", "WILMA", "BARNEY", "BETTY" }, l); 117 118 verifyControls(); 119 } 120 121 public void testUnknownPrereq() throws Exception 122 { 123 ErrorHandler eh = (ErrorHandler) newMock(ErrorHandler.class); 124 125 eh.error(LOG, "Unknown cartoon character dependency 'charlie' (for 'fred').", null, null); 126 127 replayControls(); 128 129 Orderer o = new Orderer(LOG, eh, "cartoon character"); 130 131 o.add("FRED", "fred", "charlie", "barney,wilma"); 132 o.add("BARNEY", "barney", "wilma", "betty"); 133 o.add("BETTY", "betty", null, null); 134 o.add("WILMA", "wilma", null, null); 135 136 List l = o.getOrderedObjects(); 137 138 assertListsEqual(new Object [] 139 { "FRED", "WILMA", "BARNEY", "BETTY" }, l); 140 141 verifyControls(); 142 } 143 144 public void testUnknownPostreq() throws Exception 145 { 146 ErrorHandler eh = (ErrorHandler) newMock(ErrorHandler.class); 147 148 eh.error(LOG, "Unknown cartoon character dependency 'dino' (for 'betty').", null, null); 149 150 replayControls(); 151 152 Orderer o = new Orderer(LOG, eh, "cartoon character"); 153 154 o.add("FRED", "fred", null, "barney,wilma"); 155 o.add("BARNEY", "barney", "wilma", "betty"); 156 o.add("BETTY", "betty", null, "dino"); 157 o.add("WILMA", "wilma", null, null); 158 159 List l = o.getOrderedObjects(); 160 161 assertListsEqual(new Object [] 162 { "FRED", "WILMA", "BARNEY", "BETTY" }, l); 163 164 verifyControls(); 165 } 166 167 public void testCyclePre() throws Exception 168 { 169 MockControl c = newControl(ErrorHandler.class); 170 ErrorHandler eh = (ErrorHandler) c.getMock(); 171 172 eh.error( 173 LOG, 174 "Unable to order cartoon character 'wilma' due to dependency cycle:" 175 + " A cycle has been detected from the initial object [wilma]", 176 null, 177 new ApplicationRuntimeException("")); 178 179 c.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[] 180 { null, null, null, new TypeMatcher() })); 181 182 replayControls(); 183 184 Orderer o = new Orderer(LOG, eh, "cartoon character"); 185 186 o.add("FRED", "fred", "wilma", null); 187 o.add("BARNEY", "barney", "betty", null); 188 o.add("BETTY", "betty", "fred", null); 189 o.add("WILMA", "wilma", "barney", null); 190 191 List l = o.getOrderedObjects(); 192 193 assertListsEqual(new Object [] 194 { "WILMA", "FRED", "BETTY", "BARNEY" }, l); 195 196 verifyControls(); 197 } 198 199 public void testCyclePost() throws Exception 200 { 201 MockControl c = newControl(ErrorHandler.class); 202 ErrorHandler eh = (ErrorHandler) c.getMock(); 203 204 eh 205 .error( 206 LOG, 207 "Unable to order cartoon character 'betty' due to dependency cycle: A cycle has been detected from the initial object [fred]", 208 null, 209 new ApplicationRuntimeException("")); 210 211 c.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[] 212 { null, null, null, new TypeMatcher() })); 213 214 replayControls(); 215 216 Orderer o = new Orderer(LOG, eh, "cartoon character"); 217 218 o.add("WILMA", "wilma", null, "betty"); 219 o.add("FRED", "fred", null, "barney"); 220 o.add("BARNEY", "barney", null, "wilma"); 221 o.add("BETTY", "betty", null, "fred"); 222 223 List l = o.getOrderedObjects(); 224 assertListsEqual(new Object [] 225 { "FRED", "BARNEY", "WILMA", "BETTY" }, l); 226 227 verifyControls(); 228 } 229 230 public void testDupe() throws Exception 231 { 232 Orderer o = new Orderer(new DefaultErrorHandler(), "cartoon character"); 233 234 o.add("FRED", "flintstone", null, null); 235 o.add("BARNEY", "rubble", null, null); 236 237 interceptLogging(); 238 239 o.add("WILMA", "flintstone", null, null); 240 241 assertLoggedMessage("Cartoon character 'flintstone' duplicates previous value (at unknown location) and is being ignored."); 242 243 List l = o.getOrderedObjects(); 244 245 assertListsEqual(new Object [] 246 { "FRED", "BARNEY" }, l); 247 } 248 249 public void testPreStar() throws Exception 250 { 251 Orderer o = new Orderer(getErrorHandler(), "cartoon character"); 252 253 o.add("FRED", "fred", "*", null); 254 o.add("BARNEY", "barney", "betty", null); 255 o.add("WILMA", "wilma", "betty", null); 256 o.add("BETTY", "betty", null, null); 257 258 replayControls(); 259 260 List l = o.getOrderedObjects(); 261 262 assertListsEqual(new Object [] 263 { "BETTY", "BARNEY", "WILMA", "FRED" }, l); 264 265 verifyControls(); 266 } 267 268 public void testPreStartDupe() throws Exception 269 { 270 Orderer o = new Orderer(new DefaultErrorHandler(), "cartoon character"); 271 272 o.add("FRED", "fred", "*", null); 273 o.add("BARNEY", "barney", "*", null); 274 o.add("WILMA", "wilma", "betty", null); 275 o.add("BETTY", "betty", null, null); 276 277 interceptLogging(); 278 279 List l = o.getOrderedObjects(); 280 281 assertListsEqual(new Object [] 282 { "BARNEY", "BETTY", "WILMA", "FRED" }, l); 283 284 assertLoggedMessage("Cartoon character 'barney' has been ordered " 285 + "last, conflicting with 'fred' (at unknown location)."); 286 } 287 288 public void testPostStar() throws Exception 289 { 290 Orderer o = new Orderer(getErrorHandler(), "cartoon character"); 291 292 o.add("FRED", "fred", null, "wilma"); 293 o.add("BARNEY", "barney", null, "*"); 294 o.add("WILMA", "wilma", null, "betty"); 295 o.add("BETTY", "betty", null, null); 296 297 replayControls(); 298 299 List l = o.getOrderedObjects(); 300 301 assertListsEqual(new Object [] 302 { "BARNEY", "FRED", "WILMA", "BETTY" }, l); 303 304 verifyControls(); 305 } 306 307 public void testPostStarDupe() throws Exception 308 { 309 Orderer o = new Orderer(new DefaultErrorHandler(), "cartoon character"); 310 311 o.add("FRED", "fred", null, "wilma"); 312 o.add("BARNEY", "barney", null, "*"); 313 o.add("WILMA", "wilma", null, "*"); 314 o.add("BETTY", "betty", null, null); 315 316 interceptLogging(); 317 318 List l = o.getOrderedObjects(); 319 320 assertListsEqual(new Object [] 321 { "BARNEY", "FRED", "WILMA", "BETTY" }, l); 322 323 assertLoggedMessage("Cartoon character 'wilma' has been ordered " 324 + "first, conflicting with 'barney' (at unknown location)."); 325 } 326 327 public void testNoObjects() throws Exception 328 { 329 Orderer o = new Orderer(new DefaultErrorHandler(), "cartoon character"); 330 331 List l = o.getOrderedObjects(); 332 333 assertEquals(0, l.size()); 334 } 335 336 public void testException() throws Exception 337 { 338 String msg = OrdererMessages.exception("cartoon character", new NullPointerException ( 339 "Unknown character exception")); 340 assertEquals( 341 "Improper message returned from exception orderer", 342 "Unable to order cartoon characters: Unknown character exception", 343 msg); 344 } 345 } | Popular Tags |