1 package org.apache.onami.persist;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.junit.Before;
23 import org.junit.Test;
24
25 import javax.persistence.EntityManager;
26 import javax.persistence.EntityManagerFactory;
27 import java.util.Properties;
28
29 import static org.hamcrest.CoreMatchers.is;
30 import static org.hamcrest.CoreMatchers.sameInstance;
31 import static org.junit.Assert.assertThat;
32 import static org.junit.Assert.fail;
33 import static org.mockito.Mockito.doReturn;
34 import static org.mockito.Mockito.doThrow;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.times;
37 import static org.mockito.Mockito.verify;
38
39
40
41
42 public class EntityManagerProviderImplTest
43 {
44
45 private EntityManagerProviderImpl sut;
46
47 private EntityManagerFactoryProvider emfProvider;
48
49 private EntityManager em;
50
51 private Properties properties;
52
53 private EntityManagerFactory emf;
54
55 @Before
56 public void setUp()
57 {
58
59 emfProvider = mock( EntityManagerFactoryProvider.class );
60 properties = new Properties();
61
62
63 sut = new EntityManagerProviderImpl( emfProvider, properties );
64
65
66 emf = mock( EntityManagerFactory.class );
67 doReturn( emf ).when( emfProvider ).get();
68
69 em = mock( EntityManager.class );
70 doReturn( em ).when( emf ).createEntityManager( properties );
71 }
72
73 @Test
74 public void newInstanceShouldNotBeActive()
75 {
76 assertThat( sut.isActive(), is( false ) );
77 }
78
79 @Test
80 public void stoppingShouldDoNothingIfNotActive()
81 {
82 sut.end();
83
84 assertThat( sut.isActive(), is( false ) );
85 }
86
87 @Test
88 public void shouldBeActiveAfterStarting()
89 {
90 sut.begin();
91
92 verify( emf ).createEntityManager( properties );
93 assertThat( sut.isActive(), is( true ) );
94 }
95
96 @Test
97 public void shouldNotBeActiveAfterStartingAndStopping()
98 {
99 sut.begin();
100 sut.end();
101
102 verify( emf ).createEntityManager( properties );
103 verify( em ).close();
104 assertThat( sut.isActive(), is( false ) );
105 }
106
107 @Test
108 public void shouldNotBeActiveAfterStartingAndStoppingEvenWhenExceptionThrown()
109 {
110 doThrow( new RuntimeException() ).when( em ).close();
111
112 try
113 {
114 sut.begin();
115 sut.end();
116 }
117
118 catch ( RuntimeException e )
119 {
120 verify( emf ).createEntityManager( properties );
121 verify( em ).close();
122 assertThat( sut.isActive(), is( false ) );
123 return;
124 }
125 fail( "expected RuntimeException to be thrown" );
126 }
127
128 @Test
129 public void restartingShouldWork()
130 {
131 sut.begin();
132 sut.end();
133 sut.begin();
134
135 verify( emf, times( 2 ) ).createEntityManager( properties );
136 verify( em ).close();
137 assertThat( sut.isActive(), is( true ) );
138 }
139
140 @Test( expected = IllegalStateException.class )
141 public void startingWhenActiveShouldThrowException()
142 {
143 sut.begin();
144 sut.begin();
145 }
146
147 @Test
148 public void shouldReturnTheEntityManager()
149 {
150 sut.begin();
151 final EntityManager result = sut.get();
152
153 assertThat( result, sameInstance( em ) );
154 }
155
156 @Test( expected = IllegalStateException.class )
157 public void shouldThrowExceptionWhenGettingEntityManagerAndUnitOfWorkIsNotActive()
158 {
159 sut.get();
160 }
161
162 @Test( expected = NullPointerException.class )
163 public void entityManagerFactoryProviderIsMandatory()
164 {
165 new EntityManagerProviderImpl( null, properties );
166 }
167
168 @Test
169 public void propertiesAreOptional()
170 {
171 new EntityManagerProviderImpl( emfProvider, null );
172 }
173
174 @Test
175 public void shouldCreateEntityManagerWithoutPropertiesIfNull()
176 {
177 doReturn( em ).when( emf ).createEntityManager();
178 sut = new EntityManagerProviderImpl( emfProvider, null );
179
180 sut.begin();
181
182 verify( emf ).createEntityManager();
183 }
184
185 }