KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > AutoFailTestSupport


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

18 package org.apache.activemq;
19
20 import junit.framework.TestCase;
21
22 import java.util.concurrent.atomic.AtomicBoolean JavaDoc;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 /**
26  * Enforces a test case to run for only an allotted time to prevent them from hanging
27  * and breaking the whole testing.
28  *
29  * @version $Revision: 1.0 $
30  */

31
32 public abstract class AutoFailTestSupport extends TestCase {
33     protected static final Log log = LogFactory.getLog(AutoFailTestSupport.class);
34
35     public static final int EXIT_SUCCESS = 0;
36     public static final int EXIT_ERROR = 1;
37
38     private long maxTestTime = 5 * 60 * 1000; // 5 mins by default
39
private Thread JavaDoc autoFailThread;
40
41     private boolean verbose = true;
42     private boolean useAutoFail = false; // Disable auto fail by default
43
private AtomicBoolean JavaDoc isTestSuccess;
44
45     protected void setUp() throws Exception JavaDoc {
46         // Runs the auto fail thread before performing any setup
47
if (isAutoFail()) {
48             startAutoFailThread();
49         }
50         super.setUp();
51     }
52
53     protected void tearDown() throws Exception JavaDoc {
54         super.tearDown();
55
56         // Stops the auto fail thread only after performing any clean up
57
stopAutoFailThread();
58     }
59
60     /**
61      * Manually start the auto fail thread. To start it automatically, just set the auto fail to true before calling
62      * any setup methods. As a rule, this method is used only when you are not sure, if the setUp and tearDown method
63      * is propagated correctly.
64      */

65     public void startAutoFailThread() {
66         setAutoFail(true);
67         isTestSuccess = new AtomicBoolean JavaDoc(false);
68         autoFailThread = new Thread JavaDoc(new Runnable JavaDoc() {
69             public void run() {
70                 try {
71                     // Wait for test to finish succesfully
72
Thread.sleep(getMaxTestTime());
73                 } catch (InterruptedException JavaDoc e) {
74                     // This usually means the test was successful
75
} finally {
76                     // Check if the test was able to tear down succesfully, which usually means, it has finished its run.
77
if (!isTestSuccess.get()) {
78                         System.err.println("Test case has exceeded the maximum allotted time to run of: " + getMaxTestTime() + " ms.");
79                         log.fatal("Test case has exceeded the maximum allotted time to run of: " + getMaxTestTime() + " ms.");
80                         System.exit(EXIT_ERROR);
81                     }
82                 }
83             }
84         }, "AutoFailThread");
85
86         if (verbose) {
87             log.info("Starting auto fail thread...");
88         }
89
90         log.info("Starting auto fail thread...");
91         autoFailThread.start();
92     }
93
94     /**
95      * Manually stops the auto fail thread. As a rule, this method is used only when you are not sure, if the
96      * setUp and tearDown method is propagated correctly.
97      */

98     public void stopAutoFailThread() {
99         if (isAutoFail() && autoFailThread != null && autoFailThread.isAlive()) {
100             isTestSuccess.set(true);
101
102             if (verbose) {
103                 log.info("Stopping auto fail thread...");
104             }
105
106             log.info("Stopping auto fail thread...");
107             autoFailThread.interrupt();
108         }
109     }
110
111     /**
112      * Sets the auto fail value. As a rule, this should be used only before any setup methods is called to automatically
113      * enable the auto fail thread in the setup method of the test case.
114      * @param val
115      */

116     public void setAutoFail(boolean val) {
117         this.useAutoFail = val;
118     }
119
120     public boolean isAutoFail() {
121         return this.useAutoFail;
122     }
123
124     /**
125      * The assigned value will only be reflected when the auto fail thread has started its run. Value is in milliseconds.
126      * @param val
127      */

128     public void setMaxTestTime(long val) {
129         this.maxTestTime = val;
130     }
131
132     public long getMaxTestTime() {
133         return this.maxTestTime;
134     }
135 }
136
Popular Tags