KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmessaging > 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.jbossmessaging.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.jbossmessaging.JMSTestCase;
37
38 /**
39  * XAResource tests
40  *
41  * @author <a HREF="mailto:richard.achmatowicz@jboss.com">Richard Achmatowicz</a>
42  * @author
43  * @version
44  */

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