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.wsba.coordinatorcompletion.simple;
18  
19  import com.arjuna.mw.wst11.UserBusinessActivity;
20  import com.arjuna.mw.wst11.UserBusinessActivityFactory;
21  import org.junit.Assert;
22  import org.jboss.arquillian.container.test.api.Deployment;
23  import org.jboss.arquillian.junit.Arquillian;
24  import org.jboss.as.quickstarts.wsba.coordinatorcompletion.simple.jaxws.SetServiceBA;
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.Test;
31  import org.junit.runner.RunWith;
32  import javax.inject.Inject;
33  
34  @RunWith(Arquillian.class)
35  public class ClientTest {
36     
37      private static String ManifestMF = "Manifest-Version: 1.0\n"
38            + "Dependencies: org.jboss.xts\n";
39     
40      @Inject
41      @ClientStub
42      public SetServiceBA client;
43  
44      @Deployment
45      public static WebArchive createTestArchive() {
46          return ShrinkWrap.create(WebArchive.class, "test.war")
47                  .addPackages(true, SetServiceBAImpl.class.getPackage().getName())
48                  .addAsResource("context-handlers.xml")
49                  .addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
50                  .setManifest(new StringAsset(ManifestMF));
51      }
52  
53      /**
54       * Test the simple scenario where an item is added to the set within a Business Activity which is closed successfully.
55       * 
56       * @throws Exception if something goes wrong.
57       */
58      @Test
59      public void testSuccess() throws Exception {
60  
61          System.out
62                  .println("\n\nStarting 'testSuccess'. This test invokes a WS twice within a BA. The BA is later closes, which causes these WS calls to complete successfully.");
63          System.out.println("[CLIENT] Creating a new Business Activity");
64          UserBusinessActivity uba = UserBusinessActivityFactory.userBusinessActivity();
65          try {
66              String value1 = "1";
67              String value2 = "2";
68  
69              System.out
70                      .println("[CLIENT] Beginning Business Activity (All calls to Web services that support WS-BA wil be included in this activity)");
71              uba.begin();
72  
73              System.out.println("[CLIENT] invoking addValueToSet(1) on WS");
74              client.addValueToSet(value1);
75              System.out.println("[CLIENT] invoking addValueToSet(2) on WS");
76              client.addValueToSet(value2);
77  
78              System.out.println("[CLIENT] Closing Business Activity (This will cause the BA to complete successfully)");
79              uba.close();
80  
81              Assert.assertTrue("Expected value to be in the set, but it wasn't", client.isInSet(value1));
82              Assert.assertTrue("Expected value to be in the set, but it wasn't", client.isInSet(value2));
83  
84          } finally {
85              cancelIfActive(uba);
86              client.clear();
87          }
88      }
89  
90      /**
91       * Tests the scenario where an item is added to the set with in a business activity that is later cancelled. The test checks
92       * that the item is in the set after invoking addValueToSet on the Web service. After cancelling the Business Activity, the
93       * work should be compensated and thus the item should no longer be in the set.
94       * 
95       * @throws Exception if something goes wrong
96       */
97      @Test
98      public void testCancel() throws Exception {
99  
100         System.out
101                 .println("\n\nStarting 'testCancel'. This test invokes a WS twice within a BA. The BA is later cancelled, which causes these WS calls to be compensated.");
102         System.out.println("[CLIENT] Creating a new Business Activity");
103         UserBusinessActivity uba = UserBusinessActivityFactory.userBusinessActivity();
104         try {
105             String value1 = "1";
106             String value2 = "2";
107 
108             System.out
109                     .println("[CLIENT] Beginning Business Activity (All calls to Web services that support WS-BA will be included in this activity)");
110             uba.begin();
111 
112             System.out.println("[CLIENT] invoking addValueToSet(1) on WS");
113             client.addValueToSet(value1);
114             System.out.println("[CLIENT] invoking addValueToSet(2) on WS");
115             client.addValueToSet(value2);
116 
117             Assert.assertTrue("Expected value to be in the set, but it wasn't", client.isInSet(value1));
118             Assert.assertTrue("Expected value to be in the set, but it wasn't", client.isInSet(value2));
119 
120             System.out.println("[CLIENT] Cancelling Business Activity (This will cause the work to be compensated)");
121             uba.cancel();
122 
123             Assert.assertTrue("Expected value to not be in the set, but it was", !client.isInSet(value1));
124             Assert.assertTrue("Expected value to not be in the set, but it was", !client.isInSet(value2));
125 
126         } finally {
127             cancelIfActive(uba);
128             client.clear();
129         }
130 
131     }
132 
133     /**
134      * Utility method for cancelling a Business Activity if it is currently active.
135      * 
136      * @param uba The User Business Activity to cancel.
137      */
138     private void cancelIfActive(UserBusinessActivity uba) {
139         try {
140             uba.cancel();
141         } catch (Throwable th2) {
142             // do nothing, already closed
143         }
144     }
145 }