1 /* 2 * File: InfiniteLoopStrategyTest.java 3 * Date: Nov 17, 2003 4 */ 5 package com.rhi.architecture.parc; 6 7 import com.rhi.architecture.resource.ResourceContext; 8 import com.rhi.architecture.logging.*; 9 10 import java.util.*; 11 12 import junit.framework.Test; 13 import junit.framework.TestCase; 14 import junit.framework.TestSuite; 15 16 /*** 17 * InfiniteLoopStrategyTest 18 * 19 * @author <a href="mailto:pete_mckinstry@yahoo.com">Pete McKinstry</a> 20 * @copyright 2003, All rights reserved. 21 */ 22 public class InfiniteLoopStrategyTest extends TestCase { 23 24 private static final Logger log = new DefaultLogger(); 25 26 private ResourceContext ctx; 27 28 /*** 29 * Constructor for InfiniteLoopStrategyTest. 30 * @param name 31 */ 32 public InfiniteLoopStrategyTest(String name) { 33 super(name); 34 } 35 36 /* (non-Javadoc) 37 * @see junit.framework.TestCase#setUp() 38 */ 39 protected void setUp() throws Exception { 40 super.setUp(); 41 log.debug("START: " + this.getName()); 42 try { 43 ctx = new ResourceContext(); 44 Properties p = getProperties(); 45 //p.list(System.err); 46 log.debug("calling ResourceContext.init()"); 47 ctx.init( p ); 48 } 49 catch (Exception e) { 50 log.debug(e.toString()); 51 e.printStackTrace(); 52 throw new java.lang.RuntimeException( 53 "ResourceContext failed to initialize. " + 54 " e = " + e.toString() ); 55 } 56 } 57 58 /*** 59 * @return Properties 60 */ 61 public Properties getProperties() { 62 Properties p = new Properties(); 63 p.setProperty("Resource.LogFactory", 64 "com.rhi.architecture.logging.TestLogFactory"); 65 p.setProperty("Resource.ConfigFacility", 66 "com.rhi.architecture.config.ConfigFacility"); 67 p.setProperty("interface","UnitTest"); 68 p.setProperty("application","UnitTest"); 69 p.setProperty("environment","UT_"); 70 // for this test. 71 p.setProperty(InfiniteLoopStrategy.SLEEP_TIME, "50"); 72 73 return p; 74 } 75 76 /* (non-Javadoc) 77 * @see junit.framework.TestCase#tearDown() 78 */ 79 protected void tearDown() throws Exception { 80 super.tearDown(); 81 ctx.cleanup(); 82 log.debug("END: " + this.getName()); 83 } 84 85 /*** 86 * test run 87 * @throws Exception 88 */ 89 public void testRun() throws Exception { 90 Thread testThread = new StrategyThread(); 91 testThread.setDaemon(true); // in case of unclean shutdown. 92 // reset loop count 93 TestableInfiniteLoopStrategy.loopCount = 0; 94 testThread.start(); 95 Thread.sleep(100); 96 testThread.interrupt(); 97 testThread.stop(); 98 assertTrue(TestableInfiniteLoopStrategy.loopCount>1); 99 if (testThread.isAlive()) { 100 System.out.println("waiting for thread..."); 101 Thread.sleep(1000); 102 } 103 } 104 105 /*** 106 * test run w/ sleep 107 * @throws Exception 108 */ 109 public void testRunWithSleepTime() throws Exception { 110 StrategyThread testThread = new StrategyThread(); 111 Properties p = getProperties(); 112 TestableInfiniteLoopStrategy strategy = 113 new TestableInfiniteLoopStrategy(); 114 strategy.init(p); 115 testThread.setExecutionStrategy(strategy); 116 testThread.setDaemon(true); // in case of unclean shutdown. 117 // reset loop count 118 TestableInfiniteLoopStrategy.loopCount = 0; 119 testThread.start(); 120 Thread.sleep(100); 121 testThread.interrupt(); 122 testThread.stop(); 123 // there should have been time for 2 cycles. given test sleeps 124 // for 100 millis & strategy sleeps for 50 millis between cycles. 125 assertTrue((TestableInfiniteLoopStrategy.loopCount>0) && 126 (TestableInfiniteLoopStrategy.loopCount<10)); 127 if (testThread.isAlive()) { 128 System.out.println("waiting for thread..."); 129 Thread.sleep(500); 130 } 131 } 132 133 /*** 134 * return suite of all tests for this class. 135 * @return TestSuite 136 */ 137 public static Test suite() { 138 TestSuite suite = new TestSuite(); 139 suite.addTest(new InfiniteLoopStrategyTest("testRun")); 140 suite.addTest(new InfiniteLoopStrategyTest("testRunWithSleepTime")); 141 return suite; 142 } 143 144 /*** 145 * main() 146 * @param args 147 */ 148 public static void main(String[] args) { 149 junit.textui.TestRunner.run(suite()); 150 } 151 152 /*** 153 * StrategyThread 154 */ 155 public class StrategyThread extends Thread { 156 private ExecutionStrategy strategy = new TestableInfiniteLoopStrategy(); 157 158 /*** 159 * set exec strategy 160 * @param strat 161 */ 162 public void setExecutionStrategy(ExecutionStrategy strat) { 163 this.strategy = strat; 164 } 165 /*** 166 * run() 167 * 168 * @see java.lang.Runnable#run() 169 */ 170 public void run() { 171 try { 172 strategy.run(); 173 } 174 catch (ProcessingException e) { 175 System.err.println("ProcessingException: " + e); 176 e.printStackTrace(System.err); 177 TestableInfiniteLoopStrategy.loopCount = -1; // force error 178 } 179 } 180 } 181 182 }

This page was automatically generated by Maven