001: // Copyright 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.examples.panorama.startup.impl;
016:
017: import java.util.ArrayList;
018: import java.util.Collections;
019: import java.util.List;
020: import java.util.Locale;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.examples.panorama.startup.Executable;
024: import org.apache.examples.panorama.startup.impl.Task;
025: import org.apache.examples.panorama.startup.impl.TaskExecutor;
026: import org.apache.hivemind.ApplicationRuntimeException;
027: import org.apache.hivemind.ErrorLog;
028: import org.apache.hivemind.Messages;
029: import org.apache.hivemind.Resource;
030: import org.apache.hivemind.impl.MessageFinderImpl;
031: import org.apache.hivemind.impl.ModuleMessages;
032: import org.apache.hivemind.internal.MessageFinder;
033: import org.apache.hivemind.service.ThreadLocale;
034: import org.apache.hivemind.service.impl.ThreadLocaleImpl;
035: import org.apache.hivemind.test.AggregateArgumentsMatcher;
036: import org.apache.hivemind.test.ArgumentMatcher;
037: import org.apache.hivemind.test.HiveMindTestCase;
038: import org.apache.hivemind.test.RegexpMatcher;
039: import org.apache.hivemind.test.TypeMatcher;
040: import org.apache.hivemind.util.FileResource;
041: import org.easymock.MockControl;
042:
043: /**
044: * Tests for the {@link org.apache.examples.panorama.startup.impl.TaskExecutor} service.
045: *
046: * @author Howard Lewis Ship
047: */
048: public class TestTaskExecutor extends HiveMindTestCase {
049: private static List _tokens = new ArrayList();
050:
051: protected void setUp() throws Exception {
052: super .setUp();
053:
054: _tokens.clear();
055: }
056:
057: protected void tearDown() throws Exception {
058: super .tearDown();
059:
060: _tokens.clear();
061: }
062:
063: public static void addToken(String token) {
064: _tokens.add(token);
065: }
066:
067: public Messages getMessages() {
068: String projectRoot = System.getProperty("PROJECT_ROOT", ".");
069: String path = projectRoot
070: + "/examples/src/descriptor/META-INF/panorama.startup.xml";
071:
072: Resource r = new FileResource(path);
073: MessageFinder mf = new MessageFinderImpl(r);
074: ThreadLocale tl = new ThreadLocaleImpl(Locale.getDefault());
075:
076: return new ModuleMessages(mf, tl);
077: }
078:
079: public void testSuccess() {
080: ExecutableFixture f1 = new ExecutableFixture("f1");
081:
082: Task t1 = new Task();
083:
084: t1.setExecutable(f1);
085: t1.setId("first");
086: t1.setAfter("second");
087: t1.setTitle("Fixture #1");
088:
089: ExecutableFixture f2 = new ExecutableFixture("f2");
090:
091: Task t2 = new Task();
092: t2.setExecutable(f2);
093: t2.setId("second");
094: t2.setTitle("Fixture #2");
095:
096: List tasks = new ArrayList();
097: tasks.add(t1);
098: tasks.add(t2);
099:
100: MockControl logControl = newControl(Log.class);
101: Log log = (Log) logControl.getMock();
102:
103: TaskExecutor e = new TaskExecutor();
104:
105: ErrorLog errorLog = (ErrorLog) newMock(ErrorLog.class);
106:
107: e.setErrorLog(errorLog);
108: e.setLog(log);
109: e.setMessages(getMessages());
110: e.setTasks(tasks);
111:
112: // Note the ordering; explicitly set, to check that ordering does
113: // take place.
114: log.info("Executing task Fixture #2.");
115: log.info("Executing task Fixture #1.");
116: log.info("Executed 2 tasks \\(in \\d+ milliseconds\\)\\.");
117: logControl.setMatcher(new RegexpMatcher());
118:
119: replayControls();
120:
121: e.run();
122:
123: assertListsEqual(new String[] { "f2", "f1" }, _tokens);
124:
125: verifyControls();
126: }
127:
128: public void testFailure() {
129: Executable f = new Executable() {
130: public void execute() throws Exception {
131: throw new ApplicationRuntimeException("Failure!");
132: }
133: };
134:
135: Task t = new Task();
136:
137: t.setExecutable(f);
138: t.setId("failure");
139: t.setTitle("Failure");
140:
141: List tasks = Collections.singletonList(t);
142:
143: MockControl logControl = newControl(Log.class);
144: Log log = (Log) logControl.getMock();
145:
146: MockControl errorLogControl = newControl(ErrorLog.class);
147: ErrorLog errorLog = (ErrorLog) errorLogControl.getMock();
148:
149: log.info("Executing task Failure.");
150:
151: errorLog.error(
152: "Exception while executing task Failure: Failure!",
153: null, new ApplicationRuntimeException(""));
154: errorLogControl
155: .setMatcher(new AggregateArgumentsMatcher(
156: new ArgumentMatcher[] { null, null,
157: new TypeMatcher() }));
158:
159: log
160: .info("Executed one task with one failure \\(in \\d+ milliseconds\\)\\.");
161: logControl.setMatcher(new AggregateArgumentsMatcher(
162: new RegexpMatcher()));
163:
164: replayControls();
165:
166: TaskExecutor e = new TaskExecutor();
167:
168: e.setErrorLog(errorLog);
169: e.setLog(log);
170: e.setMessages(getMessages());
171: e.setTasks(tasks);
172:
173: e.run();
174:
175: verifyControls();
176: }
177: }
|