1 22 package org.jboss.ejb3.test.jca.inflow; 23 24 import javax.naming.InitialContext ; 25 import javax.resource.spi.XATerminator ; 26 import javax.resource.spi.endpoint.MessageEndpoint ; 27 import javax.resource.spi.work.ExecutionContext ; 28 import javax.resource.spi.work.Work ; 29 import javax.resource.spi.work.WorkManager ; 30 import javax.transaction.Transaction ; 31 import javax.transaction.TransactionManager ; 32 import javax.transaction.xa.XAException ; 33 import javax.transaction.xa.XAResource ; 34 import javax.transaction.xa.Xid ; 35 36 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 51 { 52 TestResourceAdapterTxInflowResults results = new TestResourceAdapterTxInflowResults(); 53 try 54 { 55 basicTest(); 56 results.basicTest.pass(); 57 } 58 catch (Throwable t) 59 { 60 results.basicTest.fail(t); 61 } 62 63 return results; 64 } 65 66 public TransactionManager getTransactionManager() throws Exception 67 { 68 InitialContext ctx = new InitialContext (); 69 return (TransactionManager ) ctx.lookup("java:/TransactionManager"); 70 } 71 72 public void basicTest() throws Exception 73 { 74 XATerminator xt = adapter.ctx.getXATerminator(); 75 WorkManager wm = adapter.ctx.getWorkManager(); 76 TestWork work = new TestWork(); 77 ExecutionContext ec = new ExecutionContext (); 78 Xid 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 ("Work was not done"); 84 if (work.e != null) 85 throw work.e; 86 if (work.enlisted == false) 87 throw new Exception ("Not enlisted"); 88 if (work.delisted) 89 throw new Exception ("Should not be ended yet"); 90 if (work.committed) 91 throw new Exception ("Should not be committed yet"); 92 93 xt.commit(xid, true); 94 if (work.delisted == false) 95 throw new Exception ("Should be ended"); 96 if (work.committed == false) 97 throw new Exception ("Should be committed"); 98 } 99 100 public class MyXid implements Xid 101 { 102 int id; 103 104 public MyXid(int id) 105 { 106 this.id = id; 107 } 108 109 public byte[] getBranchQualifier() 110 { 111 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 , XAResource 127 { 128 public boolean complete = false; 129 public Exception 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 tm = getTransactionManager(); 141 Transaction tx = tm.getTransaction(); 142 tx.enlistResource(this); 143 } 144 catch (Exception e) 145 { 146 this.e = e; 147 } 148 } 149 150 public void release() 151 { 152 } 153 154 public void commit(Xid arg0, boolean arg1) throws XAException 155 { 156 committed = true; 157 } 158 159 public void end(Xid arg0, int arg1) throws XAException 160 { 161 delisted = true; 162 } 163 164 public void forget(Xid arg0) throws XAException 165 { 166 throw new XAException ("NYI"); 167 } 168 169 public int getTransactionTimeout() throws XAException 170 { 171 return 0; 173 } 174 175 public boolean isSameRM(XAResource arg0) throws XAException 176 { 177 return false; 179 } 180 181 public int prepare(Xid arg0) throws XAException 182 { 183 throw new XAException ("NYI"); 184 } 185 186 public Xid [] recover(int arg0) throws XAException 187 { 188 throw new XAException ("NYI"); 189 } 190 191 public void rollback(Xid arg0) throws XAException 192 { 193 throw new XAException ("NYI"); 194 } 195 196 public boolean setTransactionTimeout(int arg0) throws XAException 197 { 198 return false; 199 } 200 201 public void start(Xid arg0, int arg1) throws XAException 202 { 203 enlisted = true; 204 } 205 } 206 } 207 | Popular Tags |