1   // 
2   // Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. 
3   // All rights reserved. 
4   // 
5   // Redistribution and use in source and binary forms, with or without modification,  
6   // are permitted provided that the following conditions are met: 
7   //  
8   // * Redistributions of source code must retain the above copyright notice,  
9   //	 this list of conditions and the following disclaimer. 
10  // * Redistributions in binary form must reproduce the above copyright notice,  
11  //	 this list of conditions and the following disclaimer in the documentation  
12  //	 and/or other materials provided with the distribution. 
13  // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.  
14  //	 nor the names of its contributors may be used to endorse or promote products  
15  //	 derived from this software without specific prior written permission. 
16  // 
17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  
18  // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED  
19  // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
20  // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,  
21  // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  
22  // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
23  // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
24  // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  
25  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  
26  // POSSIBILITY OF SUCH DAMAGE. 
27  // 
28  package org.objectledge.database;
29  
30  import javax.transaction.Status;
31  import javax.transaction.UserTransaction;
32  
33  import junit.framework.TestCase;
34  
35  import org.apache.log4j.BasicConfigurator;
36  import org.jcontainer.dna.Logger;
37  import org.jcontainer.dna.impl.Log4JLogger;
38  import org.objectledge.context.Context;
39  import org.picocontainer.Startable;
40  
41  /***
42   * 
43   * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
44   * @version $Id: JotmTransactionTest.java,v 1.8 2005/09/25 12:03:49 rafal Exp $
45   */
46  public class JotmTransactionTest extends TestCase
47  {
48      private Context context;
49      
50      private Logger log;
51      
52      private Transaction transaction;
53  
54      private Transaction.GuardValve valve;
55      
56      /***
57       * Constructor for JotmTransactionTest.
58       * @param arg0
59       */
60      public JotmTransactionTest(String arg0)
61      {
62          super(arg0);
63      }
64  
65      public void setUp()
66          throws Exception
67      {
68          BasicConfigurator.resetConfiguration();
69          BasicConfigurator.configure();
70          log = new Log4JLogger(org.apache.log4j.Logger.getLogger(JotmTransactionTest.class));
71          context = new Context();
72          context.clearAttributes();
73          transaction = new JotmTransaction(2, 120, context, log, null);
74          valve = new Transaction.GuardValve(transaction, log);
75      }
76      
77      public void tearDown()
78      {
79          ((Startable)transaction).stop(); 
80      }
81      
82      public void testGetUserTransaction()
83          throws Exception
84      {
85          UserTransaction ut = transaction.getUserTransaction();
86          assertEquals(Status.STATUS_NO_TRANSACTION, ut.getStatus());
87          ut.begin();
88          assertEquals(Status.STATUS_ACTIVE, ut.getStatus());
89          ut.commit();
90          assertEquals(Status.STATUS_NO_TRANSACTION, ut.getStatus());
91      }
92      
93      public void testCommit()
94          throws Exception
95      {
96          boolean controller = transaction.begin();
97          assertTrue(controller);
98          transaction.commit(controller);
99      }
100     
101     public void testRollback()
102         throws Exception
103     {
104         boolean controller = transaction.begin();
105         assertTrue(controller);
106         transaction.rollback(controller);
107     }
108     
109     public void testNestedCommit()
110         throws Exception
111     {
112         boolean controller1 = transaction.begin();
113         assertTrue(controller1);
114         boolean controller2 = transaction.begin();
115         assertFalse(controller2);
116         transaction.commit(controller2);
117         transaction.commit(controller1);
118         valve.process(context);
119     }
120 
121     public void testNestedRollback()
122         throws Exception
123     {
124         boolean controller1 = transaction.begin();
125         assertTrue(controller1);
126         boolean controller2 = transaction.begin();
127         assertFalse(controller2);
128         transaction.rollback(controller2);
129         transaction.rollback(controller1);
130     }
131 
132     public void testNestedMixed()
133         throws Exception
134     {
135         boolean controller1 = transaction.begin();
136         assertTrue(controller1);
137         boolean controller2 = transaction.begin();
138         assertFalse(controller2);
139         transaction.rollback(controller2);
140         try
141         {
142             transaction.commit(controller1);
143             fail("exception expected");
144         }
145         catch(Exception e)
146         {
147             assertEquals("commit failed", e.getMessage());
148         }
149     }
150     
151     public void testGuardValve()
152         throws Exception
153     {
154         boolean controller1 = transaction.begin();
155         assertTrue(controller1);
156         boolean controller2 = transaction.begin();
157         assertFalse(controller2);
158         transaction.commit(controller2);
159         valve.process(context);
160     }
161 
162     public void testGuardValveNoTracing()
163         throws Exception
164     {
165         transaction = new JotmTransaction(0, 120, context, log, null);
166         valve = new Transaction.GuardValve(transaction, log);
167         boolean controller1 = transaction.begin();
168         assertTrue(controller1);
169         boolean controller2 = transaction.begin();
170         assertFalse(controller2);
171         transaction.commit(controller2);
172         valve.process(context);
173     }
174 }