View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.wsat.simple;
18  
19  import com.arjuna.mw.wst11.UserTransaction;
20  import com.arjuna.mw.wst11.UserTransactionFactory;
21  
22  import org.jboss.arquillian.container.test.api.Deployment;
23  import org.jboss.arquillian.junit.Arquillian;
24  import org.jboss.as.quickstarts.wsat.simple.jaxws.RestaurantServiceAT;
25  import org.jboss.shrinkwrap.api.ArchivePaths;
26  import org.jboss.shrinkwrap.api.ShrinkWrap;
27  import org.jboss.shrinkwrap.api.asset.EmptyAsset;
28  import org.jboss.shrinkwrap.api.asset.StringAsset;
29  import org.jboss.shrinkwrap.api.spec.WebArchive;
30  import org.junit.Assert;
31  import org.junit.Test;
32  import org.junit.runner.RunWith;
33  
34  import javax.inject.Inject;
35  
36  /**
37   * Simple set of tests for the RestaurantServiceAT
38   * 
39   * @author paul.robinson@redhat.com, 2012-01-04
40   */
41  @RunWith(Arquillian.class)
42  public class ClientTest {
43  
44      private static final String ManifestMF = "Manifest-Version: 1.0\n"
45              + "Dependencies: org.jboss.xts,org.jboss.modules,org.jboss.msc\n";
46  
47      @Inject
48      @ClientStub
49      private RestaurantServiceAT client;
50  
51      /**
52       * Create the deployment archive to be deployed by Arquillian.
53       * 
54       * @return a WebArchive representing the required deployment
55       */
56      @Deployment
57      public static WebArchive createTestArchive() {
58  
59          return ShrinkWrap.create(WebArchive.class, "wsat-simple.war")
60                  .addPackages(true, RestaurantServiceATImpl.class.getPackage()).addAsResource("context-handlers.xml")
61                  .addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
62                  .setManifest(new StringAsset(ManifestMF));
63      }
64  
65      /**
66       * Test the simple scenario where a booking is made and then committed.
67       * 
68       * @throws Exception if something goes wrong.
69       */
70      @Test
71      public void testCommit() throws Exception {
72  
73          System.out
74                  .println("\n\nStarting 'testCommit'. This test invokes a WS within an AT. The AT is later committed, which causes the back-end resource(s) to be committed.");
75          System.out.println("[CLIENT] Creating a new WS-AT User Transaction");
76          UserTransaction ut = UserTransactionFactory.userTransaction();
77          try {
78              System.out
79                      .println("[CLIENT] Beginning Atomic Transaction (All calls to Web services that support WS-AT wil be included in this transaction)");
80              ut.begin();
81              System.out.println("[CLIENT] invoking makeBooking() on WS");
82              client.makeBooking();
83              System.out.println("[CLIENT] committing Atomic Transaction (This will cause the AT to complete successfully)");
84              ut.commit();
85  
86              // Check the booking is visible after the transaction has committed.
87              Assert.assertEquals(1, client.getBookingCount());
88  
89          } finally {
90              rollbackIfActive(ut);
91              client.reset();
92          }
93      }
94  
95      /**
96       * Tests the scenario where a booking is made and the transaction is later rolledback.
97       * 
98       * @throws Exception if something goes wrong
99       */
100     @Test
101     public void testRollback() throws Exception {
102 
103         System.out
104                 .println("\n\nStarting 'testRollback'. This test invokes a WS within an AT. The AT is later rolled back, which causes the back-end resource(s) to be rolled back.");
105         System.out.println("[CLIENT] Creating a new WS-AT User Transaction");
106         UserTransaction ut = UserTransactionFactory.userTransaction();
107         try {
108             System.out
109                     .println("[CLIENT] Beginning Atomic Transaction (All calls to Web services that support WS-AT wil be included in this transaction)");
110             ut.begin();
111             System.out.println("[CLIENT] invoking makeBooking() on WS");
112             client.makeBooking();
113             System.out
114                     .println("[CLIENT] rolling back Atomic Transaction (This will cause the AT and thus the enlisted back-end resources to rollback)");
115             ut.rollback();
116 
117             // Check the booking is visible after the transaction has committed.
118             Assert.assertEquals(0, client.getBookingCount());
119 
120         } finally {
121             rollbackIfActive(ut);
122             client.reset();
123         }
124     }
125 
126     /**
127      * Utility method for rolling back a transaction if it is currently active.
128      * 
129      * @param ut The User Business Activity to cancel.
130      */
131     private void rollbackIfActive(UserTransaction ut) {
132         try {
133             ut.rollback();
134         } catch (Throwable th2) {
135             // do nothing, not active
136         }
137     }
138 }