001: // Copyright 2004, 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.hivemind.order;
016:
017: import hivemind.test.FrameworkTestCase;
018:
019: import java.util.List;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.apache.hivemind.ApplicationRuntimeException;
024: import org.apache.hivemind.ErrorHandler;
025: import org.apache.hivemind.impl.DefaultErrorHandler;
026: import org.apache.hivemind.test.AggregateArgumentsMatcher;
027: import org.apache.hivemind.test.ArgumentMatcher;
028: import org.apache.hivemind.test.TypeMatcher;
029: import org.easymock.MockControl;
030:
031: /**
032: * Tests for the {@link org.apache.hivemind.order.Orderer}.
033: *
034: * @author Howard Lewis Ship
035: */
036: public class TestOrderer extends FrameworkTestCase {
037: private static final Log LOG = LogFactory.getLog(TestOrderer.class);
038:
039: private ErrorHandler getErrorHandler() {
040: return (ErrorHandler) newMock(ErrorHandler.class);
041: }
042:
043: public void testNoDependencies() throws Exception {
044: Orderer o = new Orderer(getErrorHandler(), "cartoon character");
045:
046: o.add("FRED", "fred", null, null);
047: o.add("BARNEY", "barney", null, null);
048: o.add("WILMA", "wilma", null, null);
049: o.add("BETTY", "betty", null, null);
050:
051: replayControls();
052:
053: List l = o.getOrderedObjects();
054:
055: assertListsEqual(new Object[] { "FRED", "BARNEY", "WILMA",
056: "BETTY" }, l);
057:
058: verifyControls();
059: }
060:
061: public void testPrereq() throws Exception {
062: Orderer o = new Orderer(getErrorHandler(), "cartoon character");
063:
064: o.add("FRED", "fred", "wilma", null);
065: o.add("BARNEY", "barney", "betty", null);
066: o.add("BETTY", "betty", null, null);
067: o.add("WILMA", "wilma", null, null);
068:
069: replayControls();
070:
071: List l = o.getOrderedObjects();
072:
073: assertListsEqual(new Object[] { "WILMA", "FRED", "BETTY",
074: "BARNEY" }, l);
075:
076: verifyControls();
077: }
078:
079: public void testPostreq() throws Exception {
080: Orderer o = new Orderer(getErrorHandler(), "cartoon character");
081:
082: o.add("FRED", "fred", null, "barney,wilma");
083: o.add("BARNEY", "barney", null, "betty");
084: o.add("BETTY", "betty", null, null);
085: o.add("WILMA", "wilma", null, null);
086:
087: replayControls();
088:
089: List l = o.getOrderedObjects();
090:
091: assertListsEqual(new Object[] { "FRED", "BARNEY", "BETTY",
092: "WILMA" }, l);
093:
094: verifyControls();
095: }
096:
097: public void testPrePostreq() throws Exception {
098: Orderer o = new Orderer(getErrorHandler(), "cartoon character");
099:
100: o.add("FRED", "fred", null, "barney,wilma");
101: o.add("BARNEY", "barney", "wilma", "betty");
102: o.add("BETTY", "betty", null, null);
103: o.add("WILMA", "wilma", null, null);
104:
105: replayControls();
106:
107: List l = o.getOrderedObjects();
108:
109: assertListsEqual(new Object[] { "FRED", "WILMA", "BARNEY",
110: "BETTY" }, l);
111:
112: verifyControls();
113: }
114:
115: public void testUnknownPrereq() throws Exception {
116: ErrorHandler eh = (ErrorHandler) newMock(ErrorHandler.class);
117:
118: eh
119: .error(
120: LOG,
121: "Unknown cartoon character dependency 'charlie' (for 'fred').",
122: null, null);
123:
124: replayControls();
125:
126: Orderer o = new Orderer(LOG, eh, "cartoon character");
127:
128: o.add("FRED", "fred", "charlie", "barney,wilma");
129: o.add("BARNEY", "barney", "wilma", "betty");
130: o.add("BETTY", "betty", null, null);
131: o.add("WILMA", "wilma", null, null);
132:
133: List l = o.getOrderedObjects();
134:
135: assertListsEqual(new Object[] { "FRED", "WILMA", "BARNEY",
136: "BETTY" }, l);
137:
138: verifyControls();
139: }
140:
141: public void testUnknownPostreq() throws Exception {
142: ErrorHandler eh = (ErrorHandler) newMock(ErrorHandler.class);
143:
144: eh
145: .error(
146: LOG,
147: "Unknown cartoon character dependency 'dino' (for 'betty').",
148: 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[] { "FRED", "WILMA", "BARNEY",
162: "BETTY" }, l);
163:
164: verifyControls();
165: }
166:
167: public void testCyclePre() throws Exception {
168: MockControl c = newControl(ErrorHandler.class);
169: ErrorHandler eh = (ErrorHandler) c.getMock();
170:
171: eh
172: .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, new ApplicationRuntimeException(""));
177:
178: c.setMatcher(new AggregateArgumentsMatcher(
179: new ArgumentMatcher[] { null, null, null,
180: 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[] { "WILMA", "FRED", "BETTY",
194: "BARNEY" }, l);
195:
196: verifyControls();
197: }
198:
199: public void testCyclePost() throws Exception {
200: MockControl c = newControl(ErrorHandler.class);
201: ErrorHandler eh = (ErrorHandler) c.getMock();
202:
203: eh
204: .error(
205: LOG,
206: "Unable to order cartoon character 'betty' due to dependency cycle: A cycle has been detected from the initial object [fred]",
207: null, new ApplicationRuntimeException(""));
208:
209: c.setMatcher(new AggregateArgumentsMatcher(
210: new ArgumentMatcher[] { null, null, null,
211: new TypeMatcher() }));
212:
213: replayControls();
214:
215: Orderer o = new Orderer(LOG, eh, "cartoon character");
216:
217: o.add("WILMA", "wilma", null, "betty");
218: o.add("FRED", "fred", null, "barney");
219: o.add("BARNEY", "barney", null, "wilma");
220: o.add("BETTY", "betty", null, "fred");
221:
222: List l = o.getOrderedObjects();
223: assertListsEqual(new Object[] { "FRED", "BARNEY", "WILMA",
224: "BETTY" }, l);
225:
226: verifyControls();
227: }
228:
229: public void testDupe() throws Exception {
230: Orderer o = new Orderer(new DefaultErrorHandler(),
231: "cartoon character");
232:
233: o.add("FRED", "flintstone", null, null);
234: o.add("BARNEY", "rubble", null, null);
235:
236: interceptLogging();
237:
238: o.add("WILMA", "flintstone", null, null);
239:
240: assertLoggedMessage("Cartoon character 'flintstone' duplicates previous value (at unknown location) and is being ignored.");
241:
242: List l = o.getOrderedObjects();
243:
244: assertListsEqual(new Object[] { "FRED", "BARNEY" }, l);
245: }
246:
247: public void testPreStar() throws Exception {
248: Orderer o = new Orderer(getErrorHandler(), "cartoon character");
249:
250: o.add("FRED", "fred", "*", null);
251: o.add("BARNEY", "barney", "betty", null);
252: o.add("WILMA", "wilma", "betty", null);
253: o.add("BETTY", "betty", null, null);
254:
255: replayControls();
256:
257: List l = o.getOrderedObjects();
258:
259: assertListsEqual(new Object[] { "BETTY", "BARNEY", "WILMA",
260: "FRED" }, l);
261:
262: verifyControls();
263: }
264:
265: public void testPreStartDupe() throws Exception {
266: Orderer o = new Orderer(new DefaultErrorHandler(),
267: "cartoon character");
268:
269: o.add("FRED", "fred", "*", null);
270: o.add("BARNEY", "barney", "*", null);
271: o.add("WILMA", "wilma", "betty", null);
272: o.add("BETTY", "betty", null, null);
273:
274: interceptLogging();
275:
276: List l = o.getOrderedObjects();
277:
278: assertListsEqual(new Object[] { "BARNEY", "BETTY", "WILMA",
279: "FRED" }, l);
280:
281: assertLoggedMessage("Cartoon character 'barney' has been ordered "
282: + "last, conflicting with 'fred' (at unknown location).");
283: }
284:
285: public void testPostStar() throws Exception {
286: Orderer o = new Orderer(getErrorHandler(), "cartoon character");
287:
288: o.add("FRED", "fred", null, "wilma");
289: o.add("BARNEY", "barney", null, "*");
290: o.add("WILMA", "wilma", null, "betty");
291: o.add("BETTY", "betty", null, null);
292:
293: replayControls();
294:
295: List l = o.getOrderedObjects();
296:
297: assertListsEqual(new Object[] { "BARNEY", "FRED", "WILMA",
298: "BETTY" }, l);
299:
300: verifyControls();
301: }
302:
303: public void testPostStarDupe() throws Exception {
304: Orderer o = new Orderer(new DefaultErrorHandler(),
305: "cartoon character");
306:
307: o.add("FRED", "fred", null, "wilma");
308: o.add("BARNEY", "barney", null, "*");
309: o.add("WILMA", "wilma", null, "*");
310: o.add("BETTY", "betty", null, null);
311:
312: interceptLogging();
313:
314: List l = o.getOrderedObjects();
315:
316: assertListsEqual(new Object[] { "BARNEY", "FRED", "WILMA",
317: "BETTY" }, l);
318:
319: assertLoggedMessage("Cartoon character 'wilma' has been ordered "
320: + "first, conflicting with 'barney' (at unknown location).");
321: }
322:
323: public void testNoObjects() throws Exception {
324: Orderer o = new Orderer(new DefaultErrorHandler(),
325: "cartoon character");
326:
327: List l = o.getOrderedObjects();
328:
329: assertEquals(0, l.size());
330: }
331:
332: public void testException() throws Exception {
333: String msg = OrdererMessages
334: .exception("cartoon character",
335: new NullPointerException(
336: "Unknown character exception"));
337: assertEquals(
338: "Improper message returned from exception orderer",
339: "Unable to order cartoon characters: Unknown character exception",
340: msg);
341: }
342: }
|