KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > test > common > ValveTestCase


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.test.common;
10
11 import java.io.PrintStream JavaDoc;
12
13 import junit.framework.TestCase;
14
15 import org.jboss.portal.common.concurrent.Valve;
16
17 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
18
19 /**
20  * @author <a HREF="mailto:julien@jboss.org">Julien Viet</a>
21  * @version $Revision: 1.2 $
22  */

23 public class ValveTestCase extends TestCase
24 {
25
26    public ValveTestCase(String JavaDoc key)
27    {
28       super(key);
29    }
30
31    public static final PrintStream JavaDoc out = System.out;
32
33    public void testTransitions()
34    {
35       Valve valve = new Valve();
36       assertEquals(Valve.CLOSED, valve.getState());
37       assertFalse(valve.beforeInvocation());
38       try
39       {
40          valve.closing(10);
41          fail("IllegalStateException expected");
42       }
43       catch (IllegalStateException JavaDoc expected)
44       {
45       }
46       try
47       {
48          valve.closing();
49          fail("IllegalStateException expected");
50       }
51       catch (IllegalStateException JavaDoc expected)
52       {
53       }
54       try
55       {
56          valve.closed();
57          fail("IllegalStateException expected");
58       }
59       catch (IllegalStateException JavaDoc expected)
60       {
61       }
62
63       // Open the valve
64
valve.open();
65       try
66       {
67          assertTrue(valve.beforeInvocation());
68       }
69       finally
70       {
71          valve.afterInvocation();
72       }
73       try
74       {
75          valve.open();
76          fail("IllegalStateException expected");
77       }
78       catch (IllegalStateException JavaDoc e)
79       {
80       }
81       try
82       {
83          valve.closed();
84          fail("IllegalStateException expected");
85       }
86       catch (IllegalStateException JavaDoc e)
87       {
88       }
89
90
91       // Closing the valve
92
valve.closing();
93       assertEquals(Valve.CLOSING, valve.getState());
94       assertFalse(valve.beforeInvocation());
95       try
96       {
97          valve.open();
98          fail("IllegalStateException expected");
99       }
100       catch (IllegalStateException JavaDoc e)
101       {
102       }
103       valve.closing();
104
105       // Close the valve
106
valve.closed();
107       assertEquals(Valve.CLOSED, valve.getState());
108    }
109
110    public void testTimeOut() throws Exception JavaDoc
111    {
112       final Valve valve = new Valve();
113       final CyclicBarrier barrier = new CyclicBarrier(2);
114
115       Thread JavaDoc thread = new Thread JavaDoc()
116       {
117          public void run()
118          {
119             try
120             {
121                assertTrue(valve.beforeInvocation());
122
123                // The valve is enterred
124
barrier.barrier();
125
126                // Wait
127
barrier.barrier();
128             }
129             catch (InterruptedException JavaDoc ignore)
130             {
131             }
132             finally
133             {
134                valve.afterInvocation();
135                try
136                {
137                   // Tell the other thread we have finished
138
barrier.barrier();
139                }
140                catch (InterruptedException JavaDoc ignore)
141                {
142                }
143             }
144          }
145       };
146
147       // Open valve and start thread
148
valve.open();
149       thread.start();
150
151       // Wait until the thread called beforeInvocation
152
barrier.barrier();
153
154       // Attemtp to close
155
assertFalse(valve.closing(100));
156
157       // Check it is in closing state
158
assertEquals(Valve.CLOSING, valve.getState());
159
160       // Ask the invoker thread to finish
161
barrier.barrier();
162
163       // Wait until invocation is finished
164
barrier.barrier();
165
166       // Close, no thread are inside
167
valve.closing();
168       assertEquals(Valve.CLOSING, valve.getState());
169
170       //
171
valve.closed();
172       assertEquals(Valve.CLOSED, valve.getState());
173    }
174
175 }
176
Popular Tags