KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > order > TestOrderer


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.order;
16
17 import hivemind.test.FrameworkTestCase;
18
19 import java.util.List JavaDoc;
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 /**
32  * Tests for the {@link org.apache.hivemind.order.Orderer}.
33  *
34  * @author Howard Lewis Ship
35  */

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 JavaDoc
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 JavaDoc l = o.getOrderedObjects();
57
58         assertListsEqual(new Object JavaDoc[]
59         { "FRED", "BARNEY", "WILMA", "BETTY" }, l);
60
61         verifyControls();
62     }
63
64     public void testPrereq() throws Exception JavaDoc
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 JavaDoc l = o.getOrderedObjects();
76
77         assertListsEqual(new Object JavaDoc[]
78         { "WILMA", "FRED", "BETTY", "BARNEY" }, l);
79
80         verifyControls();
81     }
82
83     public void testPostreq() throws Exception JavaDoc
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 JavaDoc l = o.getOrderedObjects();
95
96         assertListsEqual(new Object JavaDoc[]
97         { "FRED", "BARNEY", "BETTY", "WILMA" }, l);
98
99         verifyControls();
100     }
101
102     public void testPrePostreq() throws Exception JavaDoc
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 JavaDoc l = o.getOrderedObjects();
114
115         assertListsEqual(new Object JavaDoc[]
116         { "FRED", "WILMA", "BARNEY", "BETTY" }, l);
117
118         verifyControls();
119     }
120
121     public void testUnknownPrereq() throws Exception JavaDoc
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 JavaDoc l = o.getOrderedObjects();
137
138         assertListsEqual(new Object JavaDoc[]
139         { "FRED", "WILMA", "BARNEY", "BETTY" }, l);
140
141         verifyControls();
142     }
143
144     public void testUnknownPostreq() throws Exception JavaDoc
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 JavaDoc l = o.getOrderedObjects();
160
161         assertListsEqual(new Object JavaDoc[]
162         { "FRED", "WILMA", "BARNEY", "BETTY" }, l);
163
164         verifyControls();
165     }
166
167     public void testCyclePre() throws Exception JavaDoc
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 JavaDoc l = o.getOrderedObjects();
192
193         assertListsEqual(new Object JavaDoc[]
194         { "WILMA", "FRED", "BETTY", "BARNEY" }, l);
195
196         verifyControls();
197     }
198
199     public void testCyclePost() throws Exception JavaDoc
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 JavaDoc l = o.getOrderedObjects();
224         assertListsEqual(new Object JavaDoc[]
225         { "FRED", "BARNEY", "WILMA", "BETTY" }, l);
226
227         verifyControls();
228     }
229
230     public void testDupe() throws Exception JavaDoc
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 JavaDoc l = o.getOrderedObjects();
244
245         assertListsEqual(new Object JavaDoc[]
246         { "FRED", "BARNEY" }, l);
247     }
248
249     public void testPreStar() throws Exception JavaDoc
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 JavaDoc l = o.getOrderedObjects();
261
262         assertListsEqual(new Object JavaDoc[]
263         { "BETTY", "BARNEY", "WILMA", "FRED" }, l);
264
265         verifyControls();
266     }
267
268     public void testPreStartDupe() throws Exception JavaDoc
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 JavaDoc l = o.getOrderedObjects();
280
281         assertListsEqual(new Object JavaDoc[]
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 JavaDoc
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 JavaDoc l = o.getOrderedObjects();
300
301         assertListsEqual(new Object JavaDoc[]
302         { "BARNEY", "FRED", "WILMA", "BETTY" }, l);
303
304         verifyControls();
305     }
306
307     public void testPostStarDupe() throws Exception JavaDoc
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 JavaDoc l = o.getOrderedObjects();
319
320         assertListsEqual(new Object JavaDoc[]
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 JavaDoc
328     {
329         Orderer o = new Orderer(new DefaultErrorHandler(), "cartoon character");
330
331         List JavaDoc l = o.getOrderedObjects();
332
333         assertEquals(0, l.size());
334     }
335
336     public void testException() throws Exception JavaDoc
337     {
338             String JavaDoc msg = OrdererMessages.exception("cartoon character", new NullPointerException JavaDoc(
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