View Javadoc
1   /**
2    * Copyright 2005-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.data.jpa.eclipselink;
17  
18  import org.eclipse.persistence.exceptions.TransactionException;
19  import org.junit.Test;
20  import org.junit.runner.RunWith;
21  import org.kuali.rice.core.framework.persistence.jta.Jta;
22  import org.kuali.rice.krad.data.jpa.eclipselink.JtaTransactionController;
23  import org.mockito.Mock;
24  import org.mockito.runners.MockitoJUnitRunner;
25  
26  import javax.transaction.TransactionManager;
27  import javax.transaction.UserTransaction;
28  
29  import static org.junit.Assert.*;
30  
31  /**
32   * Tests the {@link JtaTransactionController}.
33   *
34   * @author Kuali Rice Team (rice.collab@kuali.org)
35   */
36  @RunWith(MockitoJUnitRunner.class)
37  public class JtaTransactionControllerTest {
38  
39      @Mock private TransactionManager transactionManager;
40      @Mock private UserTransaction userTransaction;
41  
42      /**
43       * When JTA is disabled, attempts to invoke this method will trigger an IllegalStateException which the superclass
44       * will wrap in a {@link TransactionException}.
45       * @throws Exception
46       */
47      @Test
48      public void testAcquireTransactionManager_JtaDisabled() throws Exception {
49          assertFalse(Jta.isEnabled());
50          try {
51              new JtaTransactionController();
52              fail("A TransactionException should have been thrown");
53          } catch (TransactionException e) {
54              assertEquals(TransactionException.ERROR_OBTAINING_TRANSACTION_MANAGER, e.getErrorCode());
55              assertEquals(IllegalStateException.class, e.getInternalException().getClass());
56          }
57      }
58  
59      @Test
60      public void testAcquireTransactionManager() throws Exception {
61          Jta.configure(transactionManager, userTransaction);
62          try {
63              assertTrue(Jta.isEnabled());
64              JtaTransactionController controller = new JtaTransactionController();
65              assertEquals(transactionManager, controller.acquireTransactionManager());
66          } finally {
67              Jta.reset();
68          }
69      }
70  
71  
72  }