KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > examples > panorama > startup > impl > TestTaskExecutor


1 // Copyright 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.examples.panorama.startup.impl;
16
17 import java.io.File JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Locale JavaDoc;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.examples.panorama.startup.Executable;
25 import org.apache.hivemind.ApplicationRuntimeException;
26 import org.apache.hivemind.ErrorLog;
27 import org.apache.hivemind.Messages;
28 import org.apache.hivemind.Resource;
29 import org.apache.hivemind.impl.MessageFinderImpl;
30 import org.apache.hivemind.impl.ModuleMessages;
31 import org.apache.hivemind.internal.MessageFinder;
32 import org.apache.hivemind.service.ThreadLocale;
33 import org.apache.hivemind.service.impl.ThreadLocaleImpl;
34 import org.apache.hivemind.test.AggregateArgumentsMatcher;
35 import org.apache.hivemind.test.ArgumentMatcher;
36 import org.apache.hivemind.test.HiveMindTestCase;
37 import org.apache.hivemind.test.RegexpMatcher;
38 import org.apache.hivemind.test.TypeMatcher;
39 import org.apache.hivemind.util.FileResource;
40 import org.easymock.MockControl;
41
42
43 /**
44  * Tests for the {@link org.apache.examples.panorama.startup.impl.TaskExecutor} service.
45  *
46  * @author Howard Lewis Ship
47  */

48 public class TestTaskExecutor extends HiveMindTestCase
49 {
50     private static List JavaDoc _tokens = new ArrayList JavaDoc();
51
52     protected void setUp() throws Exception JavaDoc
53     {
54         super.setUp();
55
56         _tokens.clear();
57     }
58
59     protected void tearDown() throws Exception JavaDoc
60     {
61         super.tearDown();
62
63         _tokens.clear();
64     }
65
66     public static void addToken(String JavaDoc token)
67     {
68         _tokens.add(token);
69     }
70
71     public Messages getMessages()
72     {
73         // Some magic to make this test run in eclipse and maven which use different root dirs
74
File JavaDoc module = new File JavaDoc("./examples/src/descriptor/META-INF/panorama.startup.xml");
75         if (!module.exists()) {
76             module = new File JavaDoc("./src/descriptor/META-INF/panorama.startup.xml");
77         }
78
79         Resource r = new FileResource(module.getAbsolutePath());
80         MessageFinder mf = new MessageFinderImpl(r);
81         ThreadLocale tl = new ThreadLocaleImpl(Locale.getDefault());
82
83         return new ModuleMessages(mf, tl);
84     }
85
86     public void testSuccess()
87     {
88         ExecutableFixture f1 = new ExecutableFixture("f1");
89
90         Task t1 = new Task();
91
92         t1.setExecutable(f1);
93         t1.setId("first");
94         t1.setAfter("second");
95         t1.setTitle("Fixture #1");
96
97         ExecutableFixture f2 = new ExecutableFixture("f2");
98
99         Task t2 = new Task();
100         t2.setExecutable(f2);
101         t2.setId("second");
102         t2.setTitle("Fixture #2");
103
104         List JavaDoc tasks = new ArrayList JavaDoc();
105         tasks.add(t1);
106         tasks.add(t2);
107
108         MockControl logControl = newControl(Log.class);
109         Log log = (Log) logControl.getMock();
110
111         TaskExecutor e = new TaskExecutor();
112
113         ErrorLog errorLog = (ErrorLog) newMock(ErrorLog.class);
114
115         e.setErrorLog(errorLog);
116         e.setLog(log);
117         e.setMessages(getMessages());
118         e.setTasks(tasks);
119
120         // Note the ordering; explicitly set, to check that ordering does
121
// take place.
122
log.info("Executing task Fixture #2.");
123         log.info("Executing task Fixture #1.");
124         log.info("Executed 2 tasks \\(in \\d+ milliseconds\\)\\.");
125         logControl.setMatcher(new RegexpMatcher());
126
127         replayControls();
128
129         e.run();
130
131         assertListsEqual(new String JavaDoc[]
132         { "f2", "f1" }, _tokens);
133
134         verifyControls();
135     }
136
137     public void testFailure()
138     {
139         Executable f = new Executable()
140         {
141             public void execute() throws Exception JavaDoc
142             {
143                 throw new ApplicationRuntimeException("Failure!");
144             }
145         };
146
147         Task t = new Task();
148
149         t.setExecutable(f);
150         t.setId("failure");
151         t.setTitle("Failure");
152
153         List JavaDoc tasks = Collections.singletonList(t);
154
155         MockControl logControl = newControl(Log.class);
156         Log log = (Log) logControl.getMock();
157
158         MockControl errorLogControl = newControl(ErrorLog.class);
159         ErrorLog errorLog = (ErrorLog) errorLogControl.getMock();
160
161         log.info("Executing task Failure.");
162
163         errorLog.error(
164                 "Exception while executing task Failure: Failure!",
165                 null,
166                 new ApplicationRuntimeException(""));
167         errorLogControl.setMatcher(new AggregateArgumentsMatcher(new ArgumentMatcher[]
168         { null, null, new TypeMatcher() }));
169
170         log.info("Executed one task with one failure \\(in \\d+ milliseconds\\)\\.");
171         logControl.setMatcher(new AggregateArgumentsMatcher(new RegexpMatcher()));
172
173         replayControls();
174
175         TaskExecutor e = new TaskExecutor();
176
177         e.setErrorLog(errorLog);
178         e.setLog(log);
179         e.setMessages(getMessages());
180         e.setTasks(tasks);
181
182         e.run();
183
184         verifyControls();
185     }
186 }
Popular Tags