1 package org.jbpm.graph.exe; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 import junit.framework.TestCase; 7 8 import org.jbpm.graph.def.ActionHandler; 9 import org.jbpm.graph.def.DelegationException; 10 import org.jbpm.graph.def.ProcessDefinition; 11 12 public class ExceptionHandlingTest extends TestCase { 13 14 static List executedActions = null; 15 16 public void setUp() { 17 executedActions = new ArrayList (); 18 } 19 20 public static class BatterException extends Exception { 21 private static final long serialVersionUID = 1L; 22 } 23 24 public static class Batter implements ActionHandler { 25 private static final long serialVersionUID = 1L; 26 public void execute(ExecutionContext executionContext) throws Exception { 27 throw new BatterException(); 28 } 29 } 30 31 public static class Pitcher implements ActionHandler { 32 private static final long serialVersionUID = 1L; 33 Throwable exception = null; 34 public void execute(ExecutionContext executionContext) throws DelegationException { 35 this.exception = executionContext.getException(); 36 executedActions.add(this); 37 } 38 } 39 40 public void testUncaughtException() { 41 ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 42 "<process-definition>" + 43 " <start-state>" + 44 " <transition to='play ball' />" + 45 " </start-state>" + 46 " <state name='play ball'>" + 47 " <event type='node-enter'>" + 48 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Batter' />" + 49 " </event>" + 50 " </state>" + 51 "</process-definition>"); 52 53 try { 54 new ProcessInstance(processDefinition).signal(); 55 fail("expected exception"); 56 } catch (DelegationException e) { 57 e.printStackTrace(); 59 assertSame(BatterException.class, e.getCause().getClass()); 60 } 61 } 62 63 public static class RuntimeBatter implements ActionHandler { 64 private static final long serialVersionUID = 1L; 65 public void execute(ExecutionContext executionContext) throws Exception { 66 throw new RuntimeException ("here i come"); 67 } 68 } 69 70 public void testUncaughtRuntimeException() { 71 ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 72 "<process-definition>" + 73 " <start-state>" + 74 " <transition to='play ball' />" + 75 " </start-state>" + 76 " <state name='play ball'>" + 77 " <event type='node-enter'>" + 78 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$RuntimeBatter' />" + 79 " </event>" + 80 " </state>" + 81 "</process-definition>"); 82 83 try { 84 new ProcessInstance(processDefinition).signal(); 85 fail("expected exception"); 86 } catch (DelegationException e) { 87 e.printStackTrace(); 89 assertSame(RuntimeException .class, e.getCause().getClass()); 90 } 91 } 92 93 public static class ErrorBatter implements ActionHandler { 94 private static final long serialVersionUID = 1L; 95 public void execute(ExecutionContext executionContext) throws Exception { 96 throw new Error ("jvm trouble coming your way"); 97 } 98 } 99 100 public void testUncaughtError() { 101 ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 102 "<process-definition>" + 103 " <start-state>" + 104 " <transition to='play ball' />" + 105 " </start-state>" + 106 " <state name='play ball'>" + 107 " <event type='node-enter'>" + 108 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$ErrorBatter' />" + 109 " </event>" + 110 " </state>" + 111 "</process-definition>"); 112 113 try { 114 new ProcessInstance(processDefinition).signal(); 115 fail("expected exception"); 116 } catch (DelegationException e) { 117 e.printStackTrace(); 119 assertSame(Error .class, e.getCause().getClass()); 120 } 121 } 122 123 public void testSimpleCatchAll() { 124 ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 125 "<process-definition>" + 126 " <start-state>" + 127 " <transition to='play ball' />" + 128 " </start-state>" + 129 " <state name='play ball'>" + 130 " <event type='node-enter'>" + 131 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Batter' />" + 132 " </event>" + 133 " <exception-handler>" + 134 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Pitcher'/>" + 135 " </exception-handler>" + 136 " </state>" + 137 "</process-definition>"); 138 139 new ProcessInstance(processDefinition).signal(); 140 assertEquals(1, executedActions.size()); 141 Pitcher executedPitcher = (Pitcher) executedActions.get(0); 142 assertSame(BatterException.class, executedPitcher.exception.getClass()); 143 } 144 145 public void testCatchOnlyTheSpecifiedException() { 146 ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 147 "<process-definition>" + 148 " <start-state>" + 149 " <transition to='play ball' />" + 150 " </start-state>" + 151 " <state name='play ball'>" + 152 " <event type='node-enter'>" + 153 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Batter' />" + 154 " </event>" + 155 " <exception-handler exception-class='org.jbpm.graph.exe.ExceptionHandlingTest$BatterException'>" + 156 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Pitcher'/>" + 157 " </exception-handler>" + 158 " </state>" + 159 "</process-definition>"); 160 161 new ProcessInstance(processDefinition).signal(); 162 } 163 164 public void testDontCatchTheNonSpecifiedException() { 165 ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 166 "<process-definition>" + 167 " <start-state>" + 168 " <transition to='play ball' />" + 169 " </start-state>" + 170 " <state name='play ball'>" + 171 " <event type='node-enter'>" + 172 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Batter' />" + 173 " </event>" + 174 " <exception-handler exception-class='java.lang.RuntimeException'>" + 175 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Pitcher'/>" + 176 " </exception-handler>" + 177 " </state>" + 178 "</process-definition>"); 179 180 try { 181 new ProcessInstance(processDefinition).signal(); 182 } catch (DelegationException e) { 183 assertSame(BatterException.class, e.getCause().getClass()); 184 } 185 } 186 187 public static class SecondExceptionHandler implements ActionHandler { 188 private static final long serialVersionUID = 1L; 189 public void execute(ExecutionContext executionContext) throws DelegationException { 190 executedActions.add(this); 191 } 192 } 193 194 public void testCatchWithTheSecondSpecifiedExceptionHandler() { 195 ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 196 "<process-definition>" + 197 " <start-state>" + 198 " <transition to='play ball' />" + 199 " </start-state>" + 200 " <state name='play ball'>" + 201 " <event type='node-enter'>" + 202 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Batter' />" + 203 " </event>" + 204 " <exception-handler exception-class='java.lang.RuntimeException'>" + 206 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Pitcher'/>" + 207 " </exception-handler>" + 208 " <exception-handler>" + 210 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$SecondExceptionHandler'/>" + 211 " </exception-handler>" + 212 " </state>" + 213 "</process-definition>"); 214 215 new ProcessInstance(processDefinition).signal(); 216 assertEquals(1, executedActions.size()); 217 assertSame(SecondExceptionHandler.class, executedActions.get(0).getClass()); 218 } 219 220 public void testTwoActionsInOneExceptionHandler() { 221 ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 222 "<process-definition>" + 223 " <start-state>" + 224 " <transition to='play ball' />" + 225 " </start-state>" + 226 " <state name='play ball'>" + 227 " <event type='node-enter'>" + 228 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Batter' />" + 229 " </event>" + 230 " <exception-handler>" + 231 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Pitcher'/>" + 232 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$SecondExceptionHandler'/>" + 233 " </exception-handler>" + 234 " </state>" + 235 "</process-definition>"); 236 237 new ProcessInstance(processDefinition).signal(); 238 assertEquals(2, executedActions.size()); 239 assertSame(Pitcher.class, executedActions.get(0).getClass()); 240 assertSame(SecondExceptionHandler.class, executedActions.get(1).getClass()); 241 } 242 243 public void testProcessDefinitionExceptionHandling() { 244 ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 245 "<process-definition>" + 246 " <start-state>" + 247 " <transition to='play ball' />" + 248 " </start-state>" + 249 " <state name='play ball'>" + 250 " <event type='node-enter'>" + 251 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Batter' />" + 252 " </event>" + 253 " </state>" + 254 " <exception-handler>" + 255 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Pitcher'/>" + 256 " </exception-handler>" + 257 "</process-definition>"); 258 259 new ProcessInstance(processDefinition).signal(); 260 assertEquals(1, executedActions.size()); 261 } 262 263 public void testSuperStateExceptionHandling() { 264 ProcessDefinition processDefinition = ProcessDefinition.parseXmlString( 265 "<process-definition>" + 266 " <start-state>" + 267 " <transition to='superstate/play ball' />" + 268 " </start-state>" + 269 " <super-state name='superstate'>" + 270 " <state name='play ball'>" + 271 " <event type='node-enter'>" + 272 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Batter' />" + 273 " </event>" + 274 " </state>" + 275 " <exception-handler>" + 276 " <action class='org.jbpm.graph.exe.ExceptionHandlingTest$Pitcher'/>" + 277 " </exception-handler>" + 278 " </super-state>" + 279 "</process-definition>"); 280 281 new ProcessInstance(processDefinition).signal(); 282 assertEquals(1, executedActions.size()); 283 } 284 } 285 | Popular Tags |