KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > controller > RequestSynchronizerTest


1 package com.tonbeller.wcf.controller;
2
3 import junit.framework.TestCase;
4
5 public class RequestSynchronizerTest extends TestCase {
6
7   RequestSynchronizer rsync;
8
9   /**
10    * Constructor for RequestSynchronizerTest.
11    * @param arg0
12    */

13   public RequestSynchronizerTest(String JavaDoc arg0) {
14     super(arg0);
15   }
16
17   abstract class MyRequest implements RequestSynchronizer.Handler, Runnable JavaDoc {
18     String JavaDoc error = "nothing called";
19
20     public void normalRequest() {
21       error = "normalRequest";
22     }
23
24     public void recursiveRequest() throws Exception JavaDoc {
25       error = "recursiveRequest";
26     }
27
28     public void showBusyPage(boolean redirect) throws Exception JavaDoc {
29       error = "showBusyPage";
30     }
31
32     public void run() {
33       try {
34         rsync.handleRequest(this);
35       } catch (Exception JavaDoc e) {
36         fail(e.toString());
37       }
38     }
39
40     public void validate() {
41       assertNull(error);
42     }
43
44     public String JavaDoc getResultURI() {
45       return null;
46     }
47
48     public boolean isBusyPage() {
49       return false;
50     }
51   }
52
53   class NormalRequest extends MyRequest {
54     public void normalRequest() {
55       try {
56         Thread.sleep(1000);
57       } catch (InterruptedException JavaDoc e) {
58       }
59       error = null;
60     }
61   }
62
63   class Previous extends MyRequest {
64     public void showBusyPage(boolean redirect) throws Exception JavaDoc {
65       error = null;
66     }
67   }
68
69   class Recursive extends MyRequest {
70     public void normalRequest() {
71       error ="normalRequest";
72       try {
73         rsync.handleRequest(this);
74       } catch (Exception JavaDoc e) {
75         fail(e.toString());
76       }
77     }
78     public void recursiveRequest() throws Exception JavaDoc {
79       error = null;
80     }
81   }
82
83   public void test1() throws Exception JavaDoc {
84     NormalRequest n = new NormalRequest();
85     Thread JavaDoc t = new Thread JavaDoc(n);
86     t.start();
87     t.join();
88     n.validate();
89   }
90
91   public void testRecursive() throws Exception JavaDoc {
92     Recursive n = new Recursive();
93     Thread JavaDoc t = new Thread JavaDoc(n);
94     t.start();
95     t.join();
96     n.validate();
97   }
98   
99   public void test2() throws Exception JavaDoc {
100     NormalRequest n = new NormalRequest();
101     Thread JavaDoc t1 = new Thread JavaDoc(n);
102     t1.start();
103
104     // sicherstellen, dass t1 wirklich loslaeuft
105
try {
106       Thread.sleep(50);
107     } catch (InterruptedException JavaDoc e) {
108     }
109
110     Previous p = new Previous();
111     Thread JavaDoc t2 = new Thread JavaDoc(p);
112     t2.start();
113
114     t1.join();
115     t2.join();
116     n.validate();
117     p.validate();
118   }
119
120   public void setUp() {
121     rsync = new RequestSynchronizer();
122   }
123 }
124
Popular Tags