KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > tm > resource > Operation


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.tm.resource;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.HashMap JavaDoc;
26
27 import javax.naming.InitialContext JavaDoc;
28 import javax.transaction.Status JavaDoc;
29 import javax.transaction.Transaction JavaDoc;
30 import javax.transaction.TransactionManager JavaDoc;
31
32 import org.jboss.logging.Logger;
33
34 /**
35  * Operations
36  * @author Adrian@jboss.org
37  * @version $Revision: 41193 $
38  */

39 public class Operation
40    implements Serializable JavaDoc
41 {
42    static final long serialVersionUID = 6263843332702708629L;
43    public static final int BEGIN = -1;
44    public static final int COMMIT = -2;
45    public static final int ROLLBACK = -3;
46    public static final int SUSPEND = -4;
47    public static final int RESUME = -5;
48    public static final int SETROLLBACK = -6;
49    public static final int STATUS = -7;
50    public static final int STATE = 0;
51    public static final int CREATE = 1;
52    public static final int ENLIST = 2;
53    public static final int DIFFRM = 3;
54    public static final int SETSTATUS = 4;
55    public static final int CREATE_LOCAL = 5;
56    public static final int FAIL_LOCAL = 6;
57
58    static HashMap JavaDoc resources = new HashMap JavaDoc();
59    static HashMap JavaDoc transactions = new HashMap JavaDoc();
60
61    static Logger log;
62
63    static TransactionManager JavaDoc tm = null;
64
65    Integer JavaDoc id;
66    int op;
67    int status;
68    Throwable JavaDoc throwable;
69
70    public Operation(int op, int id)
71    {
72       this(op, id, 0);
73    }
74
75    public Operation(int op, int id, int status)
76    {
77       this(op, id, status, null);
78    }
79
80    public Operation(int op, int id, int status, Throwable JavaDoc throwable)
81    {
82       this.id = new Integer JavaDoc(id);
83       this.op = op;
84       this.status = status;
85       this.throwable = throwable;
86    }
87
88    public void perform() throws Exception JavaDoc
89    {
90       Throwable JavaDoc caught = null;
91       try
92       {
93          switch (op)
94          {
95          case BEGIN:
96             begin();
97             break;
98          case COMMIT:
99             commit();
100             break;
101          case ROLLBACK:
102             rollback();
103             break;
104          case SUSPEND:
105             suspend();
106             break;
107          case RESUME:
108             resume();
109             break;
110          case SETROLLBACK:
111             setRollbackOnly();
112             break;
113          case STATUS:
114             checkStatus();
115             break;
116          case STATE:
117             checkState();
118             break;
119          case CREATE:
120             create();
121             break;
122          case CREATE_LOCAL:
123             createLocal();
124             break;
125          case FAIL_LOCAL:
126             failLocal();
127             break;
128          case ENLIST:
129             enlist();
130             break;
131          case DIFFRM:
132             differentRM();
133             break;
134          case SETSTATUS:
135             setStatus();
136             break;
137          default:
138             throw new IllegalArgumentException JavaDoc("Invalid operation " + op);
139          }
140       }
141       catch (Throwable JavaDoc t)
142       {
143          caught = t;
144       }
145       if (throwable != null && caught == null)
146          throw new Exception JavaDoc("Expected throwable " + throwable);
147       if (throwable != null && (throwable.getClass().isAssignableFrom(caught.getClass())) == false)
148       {
149          caught.printStackTrace();
150          throw new Exception JavaDoc("Expected throwable " + throwable + " was " + caught);
151       }
152       if (throwable == null && caught != null)
153       {
154          caught.printStackTrace();
155          throw new Exception JavaDoc("Unexpected throwable " + caught);
156       }
157    }
158
159    public void begin() throws Exception JavaDoc
160    {
161       log.info("BEGIN " + id);
162       getTM().begin();
163       Transaction JavaDoc tx = getTM().getTransaction();
164       transactions.put(id, tx);
165       log.info("BEGUN " + tx);
166    }
167
168    public void commit() throws Exception JavaDoc
169    {
170       log.info("COMMIT " + id);
171       assertTx(id);
172       getTM().commit();
173       int status = getTM().getStatus();
174       if (Status.STATUS_NO_TRANSACTION != status)
175          throw new Exception JavaDoc("Expected no thread association after commit status=" + status);
176       log.info("COMMITTED " + id);
177    }
178
179    public void rollback() throws Exception JavaDoc
180    {
181       log.info("ROLLBACK " + id);
182       assertTx(id);
183       getTM().rollback();
184       int status = getTM().getStatus();
185       if (Status.STATUS_NO_TRANSACTION != status)
186          throw new Exception JavaDoc("Expected no thread association after rollback status=" + status);
187       log.info("ROLLEDBACK " + id);
188    }
189
190    public void suspend() throws Exception JavaDoc
191    {
192       log.info("SUSPEND " + id);
193       assertTx(id);
194       getTM().suspend();
195       log.info("SUSPENDED " + id);
196    }
197
198    public void resume() throws Exception JavaDoc
199    {
200       log.info("RESUME " + id);
201       getTM().resume(getTx(id));
202       assertTx(id);
203       log.info("RESUMED " + id);
204    }
205
206    public void setRollbackOnly() throws Exception JavaDoc
207    {
208       log.info("SETROLLBACK " + id);
209       getTx(id).setRollbackOnly();
210       log.info("SETTEDROLLBACK " + id);
211    }
212
213    public void checkStatus() throws Exception JavaDoc
214    {
215       log.info("CHECKSTATUS " + id);
216       int actualStatus = getTx(id).getStatus();
217       log.info("CHECKINGSTATUS " + id + " Expected " + status + " was " + actualStatus);
218       if (actualStatus != status)
219          throw new Exception JavaDoc("Transaction " + id + " Expected status " + status + " was " + actualStatus);
220    }
221
222    public void checkState() throws Exception JavaDoc
223    {
224       log.info("CHECKSTATE " + id);
225       int actualStatus = getRes(id).getStatus();
226       log.info("CHECKINGSTATE " + id + " Expected " + status + " was " + actualStatus);
227       if (actualStatus != status)
228          throw new Exception JavaDoc("Resource " + id + " Expected state " + status + " was " + actualStatus);
229    }
230
231    public void create() throws Exception JavaDoc
232    {
233       log.info("CREATE " + id);
234       Resource res = new Resource(id);
235       resources.put(id, res);
236       log.info("CREATED " + res);
237    }
238
239    public void createLocal() throws Exception JavaDoc
240    {
241       log.info("CREATE_LOCAL " + id);
242       Resource res = new LocalResource(id);
243       resources.put(id, res);
244       log.info("CREATED_LOCAL " + res);
245    }
246
247    public void enlist() throws Exception JavaDoc
248    {
249       log.info("ENLIST " + id);
250       Transaction JavaDoc tx = getTM().getTransaction();
251       if (tx.enlistResource(getRes(id)) == false)
252          throw new Exception JavaDoc("Unable to enlist resource");
253       log.info("ENLISTED " + id + " " + tx);
254    }
255
256    public void differentRM() throws Exception JavaDoc
257    {
258       log.info("DIFFRM " + id);
259       getRes(id).newResourceManager();
260    }
261
262    public void failLocal() throws Exception JavaDoc
263    {
264       log.info("FAIL_LOCAL " + id);
265       LocalResource resource = (LocalResource) getRes(id);
266       resource.failLocal();
267    }
268
269    public void setStatus() throws Exception JavaDoc
270    {
271       log.info("SETSTATUS " + id + " " + status);
272       getRes(id).setStatus(status);
273       log.info("SETTEDSTATUS " + id + " " + status);
274    }
275
276    public static void start(Logger log)
277       throws Exception JavaDoc
278    {
279       Operation.log = log;
280       if (getTM().getTransaction() != null)
281          throw new IllegalStateException JavaDoc("Invalid thread association " + getTM().getTransaction());
282       reset();
283    }
284
285    public static void end()
286    {
287       reset();
288    }
289
290    public static void reset()
291    {
292       resources.clear();
293       transactions.clear();
294    }
295
296    public static void tidyUp()
297    {
298       try
299       {
300          if (getTM().getStatus() != Status.STATUS_NO_TRANSACTION)
301          {
302             log.warn("TIDYING UP AFTER BROKEN TEST!");
303             getTM().rollback();
304          }
305       }
306       catch (Exception JavaDoc ignored)
307       {
308       }
309    }
310    
311    public Resource getRes(Integer JavaDoc id)
312    {
313       Resource res = (Resource) resources.get(id);
314       if (res == null)
315          throw new IllegalStateException JavaDoc("No resource: " + id);
316       return res;
317    }
318    public Transaction JavaDoc getTx(Integer JavaDoc id)
319    {
320       Transaction JavaDoc tx = (Transaction JavaDoc) transactions.get(id);
321       if (tx == null)
322          throw new IllegalStateException JavaDoc("No transaction: " + id);
323       return tx;
324    }
325  
326    public void assertTx(Integer JavaDoc id)
327       throws Exception JavaDoc
328    {
329       Transaction JavaDoc tx = getTx(id);
330       Transaction JavaDoc current = getTM().getTransaction();
331       log.info("Asserting tx " + tx + " current " + current);
332       if (tx.equals(current) == false)
333          throw new IllegalStateException JavaDoc("Expected tx " + tx + " was " + current);
334    }
335
336    public static TransactionManager JavaDoc getTM()
337       throws Exception JavaDoc
338    {
339       if (tm == null)
340          tm = (TransactionManager JavaDoc) new InitialContext JavaDoc().lookup("java:/TransactionManager");
341       return tm;
342    }
343 }
344
Popular Tags