KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > beanflow > util > ActivityTestSupport


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.beanflow.util;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.servicemix.beanflow.Activity;
22 import org.apache.servicemix.beanflow.Activity.Transitions;
23
24 import java.util.Timer JavaDoc;
25
26 import junit.framework.TestCase;
27
28 /**
29  *
30  * @version $Revision: $
31  */

32 public abstract class ActivityTestSupport extends TestCase {
33
34     private static final Log log = LogFactory.getLog(ActivityTestSupport.class);
35
36     protected Timer JavaDoc timer = new Timer JavaDoc();
37     protected long timeout = 500L;
38
39     protected void assertStarted(Activity activity) throws Exception JavaDoc {
40         assertNotFailed(activity);
41         assertEquals("Transition", Transitions.Started, activity.getState().get());
42     }
43
44     protected void assertStopped(Activity activity) throws Exception JavaDoc {
45         assertNotFailed(activity);
46         assertEquals("Transition", Transitions.Stopped, activity.getState().get());
47
48         assertTrue("Flow should be stopped but is: " + activity.getState().get(), activity.isStopped());
49         assertTrue("Flow should not have failed", !activity.isFailed());
50     }
51
52     protected void assertNotFailed(Activity activity) throws Exception JavaDoc {
53         assertTrue("Should not have failed: " + activity.getFailedReason() + " exception: "
54                 + activity.getFailedException(), !activity.isFailed());
55         
56         Throwable JavaDoc failedException = activity.getFailedException();
57         if (failedException != null) {
58             if (failedException instanceof Exception JavaDoc) {
59                 throw (Exception JavaDoc) failedException;
60             }
61             else {
62                 throw new RuntimeException JavaDoc(failedException);
63             }
64         }
65     }
66
67     protected void assertFailed(Activity activity) {
68         assertEquals("Transition", Transitions.Failed, activity.getState().get());
69
70         assertTrue("Flow should be stopped but is: " + activity.getState().get(), activity.isStopped());
71         assertTrue("Flow should have failed", activity.isFailed());
72
73         log.info("The activity failed due to: " + activity.getFailedReason());
74     }
75
76     protected void startActivity(Activity activity, long timeout) throws Exception JavaDoc {
77         assertTrue("activity should not be stopped", !activity.isStopped());
78         assertTrue("activity should not have failed", !activity.isFailed());
79         assertEquals("Transition", Transitions.Initialised, activity.getState().get());
80
81         activity.startWithTimeout(timer, timeout);
82         assertStarted(activity);
83     }
84
85 }
86
Popular Tags