1 18 package org.apache.activemq; 19 20 import junit.framework.TestCase; 21 22 import java.util.concurrent.atomic.AtomicBoolean ; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 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; private Thread autoFailThread; 40 41 private boolean verbose = true; 42 private boolean useAutoFail = false; private AtomicBoolean isTestSuccess; 44 45 protected void setUp() throws Exception { 46 if (isAutoFail()) { 48 startAutoFailThread(); 49 } 50 super.setUp(); 51 } 52 53 protected void tearDown() throws Exception { 54 super.tearDown(); 55 56 stopAutoFailThread(); 58 } 59 60 65 public void startAutoFailThread() { 66 setAutoFail(true); 67 isTestSuccess = new AtomicBoolean (false); 68 autoFailThread = new Thread (new Runnable () { 69 public void run() { 70 try { 71 Thread.sleep(getMaxTestTime()); 73 } catch (InterruptedException e) { 74 } finally { 76 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 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 116 public void setAutoFail(boolean val) { 117 this.useAutoFail = val; 118 } 119 120 public boolean isAutoFail() { 121 return this.useAutoFail; 122 } 123 124 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 |