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  
29  import static org.mockito.Mockito.doReturn;
30  import static org.mockito.Mockito.mock;
31  import static org.mockito.Mockito.never;
32  import static org.mockito.Mockito.verify;
33  
34  /**
35   * Test for {@link JtaTransactionFacadeFactory}.
36   */
37  @RunWith( HierarchicalContextRunner.class )
38  public class JtaTransactionFacadeProviderTest
39  {
40  
41      private JtaTransactionFacadeFactory sut;
42  
43      private UserTransactionFacade utFacade;
44  
45      private EntityManagerProvider emProvider;
46  
47      private EntityManager em;
48  
49      @Before
50      public void setUp()
51      {
52          // input
53          utFacade = mock( UserTransactionFacade.class );
54          emProvider = mock( EntityManagerProvider.class );
55  
56          // subject under test
57          sut = new JtaTransactionFacadeFactory( utFacade, emProvider );
58  
59          // environment
60          em = mock( EntityManager.class );
61          doReturn( em ).when( emProvider ).get();
62      }
63  
64      public class InnerTransactionTest
65      {
66  
67          private TransactionFacade sut;
68  
69          @Before
70          public void setUp()
71          {
72              doReturn( true ).when( utFacade ).isActive();
73              sut = JtaTransactionFacadeProviderTest.this.sut.createTransactionFacade();
74          }
75  
76          @Test
77          public void beginShouldDoNothing()
78          {
79              sut.begin();
80  
81              verify( utFacade, never() ).begin();
82              verify( em ).joinTransaction();
83          }
84  
85          @Test
86          public void commitShouldDoNothing()
87          {
88              sut.commit();
89  
90              verify( utFacade, never() ).commit();
91          }
92  
93          @Test
94          public void rollbackShouldSetRollbackOnlyFlag()
95          {
96              sut.rollback();
97  
98              verify( utFacade ).setRollbackOnly();
99          }
100     }
101 
102     public class OuterTransactionTest
103     {
104 
105         private TransactionFacade sut;
106 
107         @Before
108         public void setUp()
109         {
110             doReturn( false ).when( utFacade ).isActive();
111             sut = JtaTransactionFacadeProviderTest.this.sut.createTransactionFacade();
112         }
113 
114         @Test
115         public void beginShouldBeginTransaction()
116         {
117             sut.begin();
118 
119             verify( utFacade ).begin();
120             verify( em ).joinTransaction();
121         }
122 
123         @Test
124         public void commitShouldCommitTransaction()
125         {
126             sut.commit();
127 
128             verify( utFacade ).commit();
129         }
130 
131         @Test
132         public void commitShouldRollbackTransactionIfMarkedAsRollbackOnly()
133         {
134             doReturn( true ).when( utFacade ).getRollbackOnly();
135 
136             sut.commit();
137 
138             verify( utFacade ).rollback();
139         }
140 
141         @Test
142         public void rollbackShouldRollbackTransaction()
143         {
144             sut.rollback();
145 
146             verify( utFacade ).rollback();
147         }
148     }
149 
150 }