1 /*
2 * File: ConditionalLoopStrategyTest.java
3 * Date: Nov 17, 2003
4 */
5 package com.rhi.architecture.parc;
6
7 import com.rhi.architecture.logging.*;
8 import com.rhi.architecture.resource.*;
9
10 import java.util.*;
11
12 import junit.framework.Test;
13 import junit.framework.TestCase;
14 import junit.framework.TestSuite;
15
16 /***
17 * ConditionalLoopStrategyTest
18 *
19 * @author <a href="mailto:pete_mckinstry@yahoo.com">Pete McKinstry</a>
20 * @copyright 2003, All rights reserved.
21 */
22 public class ConditionalLoopStrategyTest extends TestCase {
23
24 private static final Logger log = new DefaultLogger();
25
26 private ResourceContext ctx;
27
28 /***
29 * Constructor for ConditionalLoopStrategyTest.
30 * @param name
31 */
32 public ConditionalLoopStrategyTest(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, "25");
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 // reset
91 TestableConditionalLoopStrategy.remainingCount = 25;
92 TestableConditionalLoopStrategy strategy =
93 new TestableConditionalLoopStrategy();
94 long startTime = System.currentTimeMillis();
95 strategy.run();
96 long endTime = System.currentTimeMillis();
97 long actual = endTime-startTime;
98 System.out.println("actuals for non-sleep version = " + actual);
99 assertTrue(TestableConditionalLoopStrategy.remainingCount==0);
100 }
101
102 /***
103 * test run w/ sleep
104 * @throws Exception
105 */
106 public void testRunWithSleepTime() throws Exception {
107 Properties p = getProperties();
108 TestableConditionalLoopStrategy strategy =
109 new TestableConditionalLoopStrategy();
110 strategy.init(p);
111 // reset
112 TestableConditionalLoopStrategy.remainingCount = 25;
113 long startTime = System.currentTimeMillis();
114 strategy.run();
115 long endTime = System.currentTimeMillis();
116 long actual = endTime-startTime;
117 System.out.println("actuals for sleep version = " + actual);
118 assertTrue("timing wrong. actual = " + actual, actual>250);
119 }
120
121 /***
122 * return suite of all tests for this class.
123 * @return TestSuite
124 */
125 public static Test suite() {
126 return new TestSuite(ConditionalLoopStrategyTest.class);
127 }
128
129 /***
130 * main()
131 * @param args
132 */
133 public static void main(String[] args) {
134 junit.textui.TestRunner.run(suite());
135 }
136
137 }
This page was automatically generated by Maven