KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmq > test > XAResourceUnitTestCase


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.jbossmq.test;
23
24 import javax.jms.Message JavaDoc;
25 import javax.jms.Topic JavaDoc;
26 import javax.jms.TopicPublisher JavaDoc;
27 import javax.jms.TopicSession JavaDoc;
28 import javax.jms.TopicSubscriber JavaDoc;
29 import javax.jms.XATopicConnection JavaDoc;
30 import javax.jms.XATopicConnectionFactory JavaDoc;
31 import javax.jms.XATopicSession JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.transaction.xa.XAResource JavaDoc;
34 import javax.transaction.xa.Xid JavaDoc;
35
36 import org.jboss.test.JBossTestCase;
37
38 /**
39  * XAResource tests
40  *
41  * @author
42  * @version
43  */

44 public class XAResourceUnitTestCase extends JBossTestCase
45 {
46    static String JavaDoc XA_TOPIC_FACTORY = "XAConnectionFactory";
47
48    static String JavaDoc TEST_TOPIC = "topic/testTopic";
49
50    public XAResourceUnitTestCase(String JavaDoc name) throws Exception JavaDoc
51    {
52       super(name);
53    }
54
55    public void testXAResourceSuspendWorkCommit() throws Exception JavaDoc
56    {
57       InitialContext JavaDoc context = getInitialContext();
58       XATopicConnectionFactory JavaDoc factory = (XATopicConnectionFactory JavaDoc) context.lookup(XA_TOPIC_FACTORY);
59       Topic JavaDoc topic = (Topic JavaDoc) context.lookup(TEST_TOPIC);
60
61       XATopicConnection JavaDoc connection = factory.createXATopicConnection();
62       try
63       {
64          // Set up
65
XATopicSession JavaDoc xaSession = connection.createXATopicSession();
66          TopicSession JavaDoc session = xaSession.getTopicSession();
67          TopicPublisher JavaDoc publisher = session.createPublisher(topic);
68          Message JavaDoc message = session.createTextMessage();
69
70          // Add the xa resource to xid1
71
MyXid xid1 = new MyXid();
72          XAResource JavaDoc resource = xaSession.getXAResource();
73          resource.start(xid1, XAResource.TMNOFLAGS);
74
75          // Do some work
76
publisher.publish(message);
77
78          // Suspend the transaction
79
resource.end(xid1, XAResource.TMSUSPEND);
80
81          // Add the xa resource to xid2
82
MyXid xid2 = new MyXid();
83          resource.start(xid2, XAResource.TMNOFLAGS);
84
85          // Do some work in the new transaction
86
publisher.publish(message);
87
88          // Commit the first transaction and end the branch
89
resource.end(xid1, XAResource.TMSUCCESS);
90          resource.commit(xid1, true);
91
92          // Do some more work in the new transaction
93
publisher.publish(message);
94
95          // Commit the second transaction and end the branch
96
resource.end(xid2, XAResource.TMSUCCESS);
97          resource.commit(xid2, true);
98       }
99       finally
100       {
101          connection.close();
102       }
103    }
104
105    public void testXAResourceRollbackAfterPrepare() throws Exception JavaDoc
106    {
107       InitialContext JavaDoc context = getInitialContext();
108       XATopicConnectionFactory JavaDoc factory = (XATopicConnectionFactory JavaDoc) context.lookup(XA_TOPIC_FACTORY);
109       Topic JavaDoc topic = (Topic JavaDoc) context.lookup(TEST_TOPIC);
110
111       XATopicConnection JavaDoc connection = factory.createXATopicConnection();
112       try
113       {
114          // Set up
115
XATopicSession JavaDoc xaSession = connection.createXATopicSession();
116          TopicSession JavaDoc session = xaSession.getTopicSession();
117          TopicSubscriber JavaDoc subscriber = session.createSubscriber(topic);
118          connection.start();
119          TopicPublisher JavaDoc publisher = session.createPublisher(topic);
120          Message JavaDoc message = session.createTextMessage();
121
122          // Publish a message using "AutoAcknowledge"
123
publisher.publish(message);
124
125          // Add the xa resource to xid1
126
MyXid xid1 = new MyXid();
127          XAResource JavaDoc resource = xaSession.getXAResource();
128          resource.start(xid1, XAResource.TMNOFLAGS);
129
130          // Receive the message
131
message = subscriber.receiveNoWait();
132          if (message == null)
133             fail("No message?");
134
135          // Prepare the transaction
136
resource.end(xid1, XAResource.TMSUCCESS);
137          resource.prepare(xid1);
138          
139          // Rollback
140
resource.rollback(xid1);
141          
142          // Receive the message using "AutoAcknowledge"
143
message = subscriber.receiveNoWait();
144          if (message == null)
145             fail("No message after rollback?");
146       }
147       finally
148       {
149          connection.close();
150       }
151    }
152
153    public static junit.framework.Test suite() throws Exception JavaDoc
154    {
155        ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
156        return getDeploySetup(XAResourceUnitTestCase.class,
157                loader.getResource("messaging/test-destinations-service.xml").toString());
158    }
159
160    public static class MyXid
161       implements Xid JavaDoc
162    {
163       static byte next = 0;
164
165       byte[] xid;
166
167       public MyXid()
168       {
169          xid = new byte[] { ++next };
170       }
171  
172       public int getFormatId()
173       {
174          return 314;
175       }
176
177       public byte[] getGlobalTransactionId()
178       {
179          return xid;
180       }
181
182       public byte[] getBranchQualifier()
183       {
184          return null;
185       }
186    }
187 }
188
Popular Tags