KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > perjoinpoint > PerJoinpointTester


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 /**
31  * Tests an annotated introduction
32  *
33  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
34  * @version $Revision: 45977 $
35  */

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 JavaDoc name)
46    {
47       super(name);
48    }
49
50    private class StaticTask extends Thread JavaDoc
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 JavaDoc e)
76             {
77                failed = true;
78                throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
79
}
80          }
81       }
82    }
83
84    private class Task extends Thread JavaDoc
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 JavaDoc e)
111             {
112                failed = true;
113                throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
114
}
115          }
116       }
117    }
118
119    public void testStaticThreadbased() throws Exception JavaDoc
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 JavaDoc("task failed");
130    }
131
132    public void testSharedThreadbased() throws Exception JavaDoc
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 JavaDoc("task failed");
145    }
146
147    public void testUnsharedThreadbased() throws Exception JavaDoc
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 JavaDoc("task failed");
159    }
160
161
162 }
163
164
Popular Tags