KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > messageservice > ProcessMonitorTest


1 /*
2  * ProcessMonitorTest.java
3  * JUnit based test
4  *
5  * Created on February 1, 2007, 9:23 AM
6  */

7
8 package com.rift.coad.daemon.messageservice;
9
10 import java.util.concurrent.atomic.AtomicInteger JavaDoc;
11 import junit.framework.*;
12 import org.apache.log4j.Logger;
13 import org.apache.log4j.BasicConfigurator;
14
15
16 /**
17  * The class responsible for testing the ProcessMonitor.
18  *
19  * @author Brett Chaldecott
20  */

21 public class ProcessMonitorTest extends TestCase {
22     
23     
24     /**
25      * This class is responsible for testing the ProcessMonitor
26      */

27     public class TestThread extends Thread JavaDoc {
28         
29         // private member variables
30
private long delay = 0;
31         
32         /**
33          * The delay
34          */

35         public TestThread (long delay) {
36             this.delay = delay;
37         }
38         
39         /**
40          * This method tests the delay
41          */

42         public void run() {
43             callCount.incrementAndGet();
44             waitCount.incrementAndGet();
45             ProcessMonitor.getInstance().monitor(delay);
46             waitCount.decrementAndGet();
47         }
48     }
49     
50     
51     public ProcessMonitorTest(String JavaDoc testName) {
52         super(testName);
53         BasicConfigurator.configure();
54     }
55
56     protected void setUp() throws Exception JavaDoc {
57     }
58
59     protected void tearDown() throws Exception JavaDoc {
60     }
61     
62     // call count
63
private AtomicInteger JavaDoc callCount = new AtomicInteger JavaDoc(0);
64     private AtomicInteger JavaDoc waitCount = new AtomicInteger JavaDoc(0);
65     
66     /**
67      * Test of class com.rift.coad.daemon.messageservice.ProcessMonitor.
68      */

69     public void testProcessMonitor() throws Exception JavaDoc {
70         System.out.println("testProcessMonitor");
71         
72         ProcessMonitor expResult = ProcessMonitor.getInstance();
73         ProcessMonitor result = ProcessMonitor.getInstance();
74         assertEquals(expResult, result);
75         
76         TestThread testThread1 = new TestThread(400);
77         testThread1.start();
78         TestThread testThread2 = new TestThread(0);
79         testThread2.start();
80         TestThread testThread3 = new TestThread(400);
81         testThread3.start();
82         
83         Thread.sleep(100);
84         
85         assertEquals(3,callCount.get());
86         assertEquals(3,waitCount.get());
87         
88         Thread.sleep(500);
89         
90         assertEquals(3,callCount.get());
91         assertEquals(1,waitCount.get());
92         
93         ProcessMonitor.getInstance().notifyProcessor();
94         Thread.sleep(100);
95         
96         assertEquals(3,callCount.get());
97         assertEquals(0,waitCount.get());
98         
99         testThread1 = new TestThread(400);
100         testThread1.start();
101         
102         Thread.sleep(100);
103         
104         assertEquals(4,callCount.get());
105         assertEquals(1,waitCount.get());
106         
107         ProcessMonitor.getInstance().terminate();
108         
109         testThread2 = new TestThread(0);
110         testThread2.start();
111         
112         Thread.sleep(100);
113         
114         assertEquals(5,callCount.get());
115         assertEquals(0,waitCount.get());
116     }
117
118     
119     
120 }
121
Popular Tags