1 22 package org.jboss.test.aop.perjoinpoint; 23 24 25 import org.jboss.test.aop.AOPTestWithSetup; 26 27 import junit.framework.Test; 28 import junit.framework.TestSuite; 29 30 36 public class PerJoinpointTester extends AOPTestWithSetup 37 { 38 public static Test suite() 39 { 40 TestSuite suite = new TestSuite("PerJoinpointTester"); 41 suite.addTestSuite(PerJoinpointTester.class); 42 return suite; 43 } 44 45 public PerJoinpointTester(String name) 46 { 47 super(name); 48 } 49 50 private class StaticTask extends Thread 51 { 52 public boolean failed = false; 53 54 public void run() 55 { 56 ThreadbasedTest.staticCounter2 = 10; 57 for (int i = 0; i < 10; i++) 58 { 59 if (ThreadbasedTest.staticCounter != i) 60 { 61 failed = true; 62 break; 63 } 64 if (ThreadbasedTest.staticCounter2 != i + 10) 65 { 66 failed = true; 67 break; 68 } 69 ThreadbasedTest.staticCounter++; 70 ThreadbasedTest.staticCounter2++; 71 try 72 { 73 Thread.sleep(500); 74 } 75 catch (InterruptedException e) 76 { 77 failed = true; 78 throw new RuntimeException (e); } 80 } 81 } 82 } 83 84 private class Task extends Thread 85 { 86 public boolean failed = false; 87 public ThreadbasedTest test; 88 89 public void run() 90 { 91 test.counter2 = 10; 92 for (int i = 0; i < 10; i++) 93 { 94 if (test.counter != i) 95 { 96 failed = true; 97 break; 98 } 99 if (test.counter2 != i + 10) 100 { 101 failed = true; 102 break; 103 } 104 test.counter++; 105 test.counter2++; 106 try 107 { 108 Thread.sleep(500); 109 } 110 catch (InterruptedException e) 111 { 112 failed = true; 113 throw new RuntimeException (e); } 115 } 116 } 117 } 118 119 public void testStaticThreadbased() throws Exception 120 { 121 System.out.println("testStaticThreadbased..."); 122 StaticTask task1 = new StaticTask(); 123 StaticTask task2 = new StaticTask(); 124 task1.start(); 125 Thread.sleep(1000); 126 task2.start(); 127 task1.join(); 128 task2.join(); 129 if (task1.failed || task2.failed) throw new RuntimeException ("task failed"); 130 } 131 132 public void testSharedThreadbased() throws Exception 133 { 134 ThreadbasedTest test = new ThreadbasedTest(); 135 Task task1 = new Task(); 136 task1.test = test; 137 Task task2 = new Task(); 138 task2.test = test; 139 task1.start(); 140 Thread.sleep(1000); 141 task2.start(); 142 task1.join(); 143 task2.join(); 144 if (task1.failed || task2.failed) throw new RuntimeException ("task failed"); 145 } 146 147 public void testUnsharedThreadbased() throws Exception 148 { 149 Task task1 = new Task(); 150 task1.test = new ThreadbasedTest(); 151 Task task2 = new Task(); 152 task2.test = new ThreadbasedTest(); 153 task1.start(); 154 Thread.sleep(1000); 155 task2.start(); 156 task1.join(); 157 task2.join(); 158 if (task1.failed || task2.failed) throw new RuntimeException ("task failed"); 159 } 160 161 162 } 163 164 | Popular Tags |