1 15 package org.apache.examples.panorama.startup.impl; 16 17 import java.io.File ; 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.List ; 21 import java.util.Locale ; 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 48 public class TestTaskExecutor extends HiveMindTestCase 49 { 50 private static List _tokens = new ArrayList (); 51 52 protected void setUp() throws Exception 53 { 54 super.setUp(); 55 56 _tokens.clear(); 57 } 58 59 protected void tearDown() throws Exception 60 { 61 super.tearDown(); 62 63 _tokens.clear(); 64 } 65 66 public static void addToken(String token) 67 { 68 _tokens.add(token); 69 } 70 71 public Messages getMessages() 72 { 73 File module = new File ("./examples/src/descriptor/META-INF/panorama.startup.xml"); 75 if (!module.exists()) { 76 module = new File ("./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 tasks = new ArrayList (); 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 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 [] 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 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 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 |