View Javadoc
1   package org.apache.onami.persist;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import de.bechte.junit.runners.context.HierarchicalContextRunner;
23  import org.junit.Before;
24  import org.junit.Test;
25  import org.junit.runner.RunWith;
26  
27  import javax.persistence.EntityManager;
28  import javax.persistence.EntityTransaction;
29  
30  import static org.mockito.Mockito.doReturn;
31  import static org.mockito.Mockito.mock;
32  import static org.mockito.Mockito.never;
33  import static org.mockito.Mockito.verify;
34  
35  /**
36   * Test for {@link ResourceLocalTransactionFacadeFactory}.
37   */
38  @RunWith( HierarchicalContextRunner.class )
39  public class ResourceLocalTransactionFacadeProviderTest
40  {
41  
42      private ResourceLocalTransactionFacadeFactory sut;
43  
44      private EntityManagerProvider emProvider;
45  
46      private EntityManager em;
47  
48      private EntityTransaction txn;
49  
50      @Before
51      public void setUp()
52      {
53          // input
54          emProvider = mock( EntityManagerProvider.class );
55  
56          // subject under test
57          sut = new ResourceLocalTransactionFacadeFactory( emProvider );
58  
59          // environment
60          em = mock( EntityManager.class );
61          doReturn( em ).when( emProvider ).get();
62  
63          txn = mock( EntityTransaction.class );
64          doReturn( txn ).when( em ).getTransaction();
65      }
66  
67      public class InnerTransactionTest
68      {
69  
70          private TransactionFacade sut;
71  
72          @Before
73          public void setUp()
74          {
75              doReturn( true ).when( txn ).isActive();
76              sut = ResourceLocalTransactionFacadeProviderTest.this.sut.createTransactionFacade();
77          }
78  
79          @Test
80          public void beginShouldDoNothing()
81          {
82              sut.begin();
83  
84              verify( txn, never() ).begin();
85          }
86  
87          @Test
88          public void commitShouldDoNothing()
89          {
90              sut.commit();
91  
92              verify( txn, never() ).commit();
93          }
94  
95          @Test
96          public void rollbackShouldSetRollbackOnlyFlag()
97          {
98              sut.rollback();
99  
100             verify( txn ).setRollbackOnly();
101         }
102     }
103 
104     public class OuterTransactionTest
105     {
106 
107         private TransactionFacade sut;
108 
109         @Before
110         public void setUp()
111         {
112             doReturn( false ).when( txn ).isActive();
113             sut = ResourceLocalTransactionFacadeProviderTest.this.sut.createTransactionFacade();
114         }
115 
116         @Test
117         public void beginShouldBeginTransaction()
118         {
119             sut.begin();
120 
121             verify( txn ).begin();
122         }
123 
124         @Test
125         public void commitShouldCommitTransaction()
126         {
127             sut.commit();
128 
129             verify( txn ).commit();
130         }
131 
132         @Test
133         public void commitShouldRollbackTransactionIfMarkedAsRollbackOnly()
134         {
135             doReturn( true ).when( txn ).getRollbackOnly();
136 
137             sut.commit();
138 
139             verify( txn ).rollback();
140         }
141 
142         @Test
143         public void rollbackShouldRollbackTransaction()
144         {
145             sut.rollback();
146 
147             verify( txn ).rollback();
148         }
149     }
150 
151 }