KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > inflow > TestResourceAdapterTxInflow


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.jca.inflow;
23
24 import javax.naming.InitialContext JavaDoc;
25 import javax.resource.spi.XATerminator JavaDoc;
26 import javax.resource.spi.endpoint.MessageEndpoint JavaDoc;
27 import javax.resource.spi.work.ExecutionContext JavaDoc;
28 import javax.resource.spi.work.Work JavaDoc;
29 import javax.resource.spi.work.WorkManager JavaDoc;
30 import javax.transaction.Transaction JavaDoc;
31 import javax.transaction.TransactionManager JavaDoc;
32 import javax.transaction.xa.XAException JavaDoc;
33 import javax.transaction.xa.XAResource JavaDoc;
34 import javax.transaction.xa.Xid JavaDoc;
35
36 /**
37  * Management interface of TestResourceAdapter.
38  *
39  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
40  * @version $Revision: 37406 $
41  */

42 public class TestResourceAdapterTxInflow
43 {
44    TestResourceAdapter adapter;
45    public TestResourceAdapterTxInflow(TestResourceAdapter adapter)
46    {
47       this.adapter = adapter;
48    }
49    
50    public TestResourceAdapterTxInflowResults run() throws Exception JavaDoc
51    {
52       TestResourceAdapterTxInflowResults results = new TestResourceAdapterTxInflowResults();
53       try
54       {
55          basicTest();
56          results.basicTest.pass();
57       }
58       catch (Throwable JavaDoc t)
59       {
60          results.basicTest.fail(t);
61       }
62
63       return results;
64    }
65    
66    public TransactionManager JavaDoc getTransactionManager() throws Exception JavaDoc
67    {
68       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
69       return (TransactionManager JavaDoc) ctx.lookup("java:/TransactionManager");
70    }
71    
72    public void basicTest() throws Exception JavaDoc
73    {
74       XATerminator JavaDoc xt = adapter.ctx.getXATerminator();
75       WorkManager JavaDoc wm = adapter.ctx.getWorkManager();
76       TestWork work = new TestWork();
77       ExecutionContext JavaDoc ec = new ExecutionContext JavaDoc();
78       Xid JavaDoc xid = new MyXid(1);
79       ec.setXid(xid);
80
81       wm.doWork(work, 0l, ec, null);
82       if (work.complete == false)
83          throw new Exception JavaDoc("Work was not done");
84       if (work.e != null)
85          throw work.e;
86       if (work.enlisted == false)
87          throw new Exception JavaDoc("Not enlisted");
88       if (work.delisted)
89          throw new Exception JavaDoc("Should not be ended yet");
90       if (work.committed)
91          throw new Exception JavaDoc("Should not be committed yet");
92
93       xt.commit(xid, true);
94       if (work.delisted == false)
95          throw new Exception JavaDoc("Should be ended");
96       if (work.committed == false)
97          throw new Exception JavaDoc("Should be committed");
98    }
99    
100    public class MyXid implements Xid JavaDoc
101    {
102       int id;
103       
104       public MyXid(int id)
105       {
106          this.id = id;
107       }
108       
109       public byte[] getBranchQualifier()
110       {
111          // TODO getBranchQualifier
112
return null;
113       }
114
115       public int getFormatId()
116       {
117          return 666;
118       }
119       
120       public byte[] getGlobalTransactionId()
121       {
122          return new byte[] { (byte) id };
123       }
124    }
125    
126    public class TestWork implements Work JavaDoc, XAResource JavaDoc
127    {
128       public boolean complete = false;
129       public Exception JavaDoc e;
130       
131       public boolean enlisted = false;
132       public boolean delisted = false;
133       public boolean committed = false;
134       
135       public void run()
136       {
137          try
138          {
139             complete = true;
140             TransactionManager JavaDoc tm = getTransactionManager();
141             Transaction JavaDoc tx = tm.getTransaction();
142             tx.enlistResource(this);
143          }
144          catch (Exception JavaDoc e)
145          {
146             this.e = e;
147          }
148       }
149
150       public void release()
151       {
152       }
153       
154       public void commit(Xid JavaDoc arg0, boolean arg1) throws XAException JavaDoc
155       {
156          committed = true;
157       }
158
159       public void end(Xid JavaDoc arg0, int arg1) throws XAException JavaDoc
160       {
161          delisted = true;
162       }
163       
164       public void forget(Xid JavaDoc arg0) throws XAException JavaDoc
165       {
166          throw new XAException JavaDoc("NYI");
167       }
168       
169       public int getTransactionTimeout() throws XAException JavaDoc
170       {
171          // TODO getTransactionTimeout
172
return 0;
173       }
174       
175       public boolean isSameRM(XAResource JavaDoc arg0) throws XAException JavaDoc
176       {
177          // TODO isSameRM
178
return false;
179       }
180       
181       public int prepare(Xid JavaDoc arg0) throws XAException JavaDoc
182       {
183          throw new XAException JavaDoc("NYI");
184       }
185
186       public Xid JavaDoc[] recover(int arg0) throws XAException JavaDoc
187       {
188          throw new XAException JavaDoc("NYI");
189       }
190
191       public void rollback(Xid JavaDoc arg0) throws XAException JavaDoc
192       {
193          throw new XAException JavaDoc("NYI");
194       }
195
196       public boolean setTransactionTimeout(int arg0) throws XAException JavaDoc
197       {
198          return false;
199       }
200
201       public void start(Xid JavaDoc arg0, int arg1) throws XAException JavaDoc
202       {
203          enlisted = true;
204       }
205    }
206 }
207
Popular Tags