KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > panorama > startup > impl > TestTaskExecutor


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

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