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 13 public RequestSynchronizerTest(String arg0) { 14 super(arg0); 15 } 16 17 abstract class MyRequest implements RequestSynchronizer.Handler, Runnable { 18 String error = "nothing called"; 19 20 public void normalRequest() { 21 error = "normalRequest"; 22 } 23 24 public void recursiveRequest() throws Exception { 25 error = "recursiveRequest"; 26 } 27 28 public void showBusyPage(boolean redirect) throws Exception { 29 error = "showBusyPage"; 30 } 31 32 public void run() { 33 try { 34 rsync.handleRequest(this); 35 } catch (Exception e) { 36 fail(e.toString()); 37 } 38 } 39 40 public void validate() { 41 assertNull(error); 42 } 43 44 public String 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 e) { 58 } 59 error = null; 60 } 61 } 62 63 class Previous extends MyRequest { 64 public void showBusyPage(boolean redirect) throws Exception { 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 e) { 75 fail(e.toString()); 76 } 77 } 78 public void recursiveRequest() throws Exception { 79 error = null; 80 } 81 } 82 83 public void test1() throws Exception { 84 NormalRequest n = new NormalRequest(); 85 Thread t = new Thread (n); 86 t.start(); 87 t.join(); 88 n.validate(); 89 } 90 91 public void testRecursive() throws Exception { 92 Recursive n = new Recursive(); 93 Thread t = new Thread (n); 94 t.start(); 95 t.join(); 96 n.validate(); 97 } 98 99 public void test2() throws Exception { 100 NormalRequest n = new NormalRequest(); 101 Thread t1 = new Thread (n); 102 t1.start(); 103 104 try { 106 Thread.sleep(50); 107 } catch (InterruptedException e) { 108 } 109 110 Previous p = new Previous(); 111 Thread t2 = new Thread (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 |