1 /***
2 * @(#)BasicTransactionTest.java
3 *
4 * JFoxSOAF, Service-Oriented Application Framework
5 *
6 * Copyright(c) JFoxSOAF Team
7 *
8 * Licensed under the GNU LGPL, Version 2.1 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.gnu.org/copyleft/lesser.html
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * For more information, please visit:
21 * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
22 * http://www.huihoo.org/jfox/jfoxsoaf
23 */
24
25 package org.huihoo.jfox.soaf.services.transaction;
26
27 import java.lang.reflect.Field;
28
29 import javax.transaction.RollbackException;
30 import javax.transaction.Status;
31 import javax.transaction.UserTransaction;
32
33 import junit.framework.TestCase;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.huihoo.jfox.soaf.container.ServiceFactory;
38 import org.huihoo.jfox.soaf.container.ServiceLoader;
39
40 /***
41 * JUnit test case for the TransactionService
42 *
43 * @author <a href="mailto:founder_chen@yahoo.com">Peter Cheng </a>
44 * @version $Revision:$ $Date:$
45 * @version Revision: 1.0
46 */
47
48 public class BasicTransactionTest extends TestCase {
49
50 private static final Log log = LogFactory
51 .getLog(BasicTransactionTest.class);
52
53 private ServiceLoader sl = ServiceLoader.getInstance();
54
55 private ServiceFactory sf = ServiceFactory.getInstance();
56
57 private JotmTransactionService jts;
58
59 /***
60 * Constructor for BasicTransactionTest.
61 *
62 * @param arg0
63 */
64 public BasicTransactionTest(String arg0) {
65 super(arg0);
66 }
67
68
69
70
71 protected void setUp() throws Exception {
72 super.setUp();
73 if (!sl.isServiceLoaded()) {
74 sl.initService("jfoxsoaf-config.xml");
75 }
76
77 jts = (JotmTransactionService) sf
78 .getService(JotmTransactionService.class);
79 }
80
81
82
83
84 protected void tearDown() throws Exception {
85 super.tearDown();
86 }
87
88 public void testSimpleTrans() {
89 UserTransaction utc = jts.getUserTransaction();
90 try {
91 log.info("a simple transaction which is committed:");
92 log.info("\t- initial status : " + getStatusName(utc.getStatus()));
93 utc.begin();
94 log.info("\t- after begin status : "
95 + getStatusName(utc.getStatus()));
96 utc.commit();
97 log.info("\t- after commit status : "
98 + getStatusName(utc.getStatus()));
99 } catch (Exception e) {
100 log.info("Exception of type :" + e.getClass().getName()
101 + " has been thrown");
102 log.info("Exception message :" + e.getMessage());
103 e.printStackTrace();
104 }
105 }
106
107 public void testRollback() {
108 UserTransaction utc = jts.getUserTransaction();
109 try {
110 log.info("a simple transaction which is rolled back.");
111 log.info("we set a transaction timeout to 1 second, begin the transaction, "
112 + "and wait 5 seconds before committing it:");
113 utc.setTransactionTimeout(1);
114 log.info("\t- initial status : " + getStatusName(utc.getStatus()));
115 utc.begin();
116 log.info("\t- after begin status : "
117 + getStatusName(utc.getStatus()));
118 log.info("\t- wait for 5 seconds");
119 Thread.sleep(5 * 1000);
120 utc.commit();
121 log.info("ERROR: the commit method should have failed due to timeout expiration");
122 } catch (RollbackException e) {
123 try {
124 log.info("\t- after rollback status : "
125 + getStatusName(utc.getStatus()));
126 } catch (Exception ex) {
127 log.info("Exception of type :" + e.getClass().getName()
128 + " has been thrown");
129 log.info("Exception message :" + e.getMessage());
130 e.printStackTrace();
131 }
132 } catch (Exception e) {
133 log.info("Exception of type :" + e.getClass().getName()
134 + " has been thrown");
135 log.info("Exception message :" + e.getMessage());
136 e.printStackTrace();
137 }
138
139 }
140
141 public static String getStatusName(int status) {
142 String statusName = null;
143 try {
144 Field[] flds = Status.class.getDeclaredFields();
145 for (int i = 0; i < flds.length; i++) {
146 if (flds[i].getInt(null) == status)
147 statusName = flds[i].getName();
148 }
149 } catch (Exception e) {
150 statusName = "invalid status value!";
151 }
152 return statusName;
153 }
154
155 }