/
AbstractContainerEntityManagerFactoryIntegrationTests.java
254 lines (217 loc) · 8.79 KB
/
AbstractContainerEntityManagerFactoryIntegrationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jpa;
import java.lang.reflect.Proxy;
import java.util.List;
import jakarta.persistence.EntityManager;
import jakarta.persistence.FlushModeType;
import jakarta.persistence.NoResultException;
import jakarta.persistence.Query;
import org.junit.jupiter.api.Test;
import org.springframework.core.testfixture.io.SerializationTestUtils;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.orm.jpa.domain.DriversLicense;
import org.springframework.orm.jpa.domain.Person;
import org.springframework.transaction.TransactionDefinition;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatRuntimeException;
/**
* Integration tests for LocalContainerEntityManagerFactoryBean.
* Uses an in-memory database.
*
* @author Rod Johnson
* @author Juergen Hoeller
*/
public abstract class AbstractContainerEntityManagerFactoryIntegrationTests
extends AbstractEntityManagerFactoryIntegrationTests {
@Test
protected void testEntityManagerFactoryImplementsEntityManagerFactoryInfo() {
boolean condition = entityManagerFactory instanceof EntityManagerFactoryInfo;
assertThat(condition).as("Must have introduced config interface").isTrue();
EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory;
assertThat(emfi.getPersistenceUnitName()).isEqualTo("Person");
assertThat(emfi.getPersistenceUnitInfo()).as("PersistenceUnitInfo must be available").isNotNull();
assertThat(emfi.getNativeEntityManagerFactory()).as("Raw EntityManagerFactory must be available").isNotNull();
}
@Test
void testStateClean() {
assertThat(countRowsInTable("person")).as("Should be no people from previous transactions").isEqualTo(0);
}
@Test
void testJdbcTx1_1() {
testJdbcTx2();
}
@Test
void testJdbcTx1_2() {
testJdbcTx2();
}
@Test
void testJdbcTx1_3() {
testJdbcTx2();
}
@Test
void testJdbcTx2() {
assertThat(countRowsInTable("person")).as("Any previous tx must have been rolled back").isEqualTo(0);
executeSqlScript("/org/springframework/orm/jpa/insertPerson.sql");
}
@Test
void testEntityManagerProxyIsProxy() {
assertThat(Proxy.isProxyClass(sharedEntityManager.getClass())).isTrue();
Query q = sharedEntityManager.createQuery("select p from Person as p");
q.getResultList();
assertThat(sharedEntityManager.isOpen()).as("Should be open to start with").isTrue();
sharedEntityManager.close();
assertThat(sharedEntityManager.isOpen()).as("Close should have been silently ignored").isTrue();
}
@Test
void testBogusQuery() {
assertThatRuntimeException().isThrownBy(() -> {
Query query = sharedEntityManager.createQuery("It's raining toads");
// required in OpenJPA case
query.executeUpdate();
});
}
@Test
void testGetReferenceWhenNoRow() {
assertThatException().isThrownBy(() -> {
Person notThere = sharedEntityManager.getReference(Person.class, 666);
// We may get here (as with Hibernate). Either behaviour is valid:
// throw exception on first access or on getReference itself.
notThere.getFirstName();
})
.matches(ex -> ex.getClass().getName().endsWith("NotFoundException"));
}
@Test
void testLazyLoading() throws Exception {
try {
Person tony = new Person();
tony.setFirstName("Tony");
tony.setLastName("Blair");
tony.setDriversLicense(new DriversLicense("8439DK"));
sharedEntityManager.persist(tony);
assertThat(DataSourceUtils.getConnection(jdbcTemplate.getDataSource()).getTransactionIsolation())
.isEqualTo(TransactionDefinition.ISOLATION_READ_COMMITTED);
setComplete();
endTransaction();
transactionDefinition.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
startNewTransaction();
assertThat(DataSourceUtils.getConnection(jdbcTemplate.getDataSource()).getTransactionIsolation())
.isEqualTo(TransactionDefinition.ISOLATION_SERIALIZABLE);
sharedEntityManager.clear();
Person newTony = entityManagerFactory.createEntityManager().getReference(Person.class, tony.getId());
assertThat(tony).isNotSameAs(newTony);
endTransaction();
transactionDefinition.setIsolationLevel(TransactionDefinition.ISOLATION_DEFAULT);
startNewTransaction();
assertThat(DataSourceUtils.getConnection(jdbcTemplate.getDataSource()).getTransactionIsolation())
.isEqualTo(TransactionDefinition.ISOLATION_READ_COMMITTED);
endTransaction();
assertThat(newTony.getDriversLicense()).isNotNull();
newTony.getDriversLicense().getSerialNumber();
}
finally {
deleteFromTables("person", "drivers_license");
}
}
@Test
@SuppressWarnings("unchecked")
public void testMultipleResults() {
// Add with JDBC
String firstName = "Tony";
insertPerson(firstName);
assertThat(Proxy.isProxyClass(sharedEntityManager.getClass())).isTrue();
Query q = sharedEntityManager.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertThat(people).hasSize(1);
assertThat(people.get(0).getFirstName()).isEqualTo(firstName);
}
protected void insertPerson(String firstName) {
String INSERT_PERSON = "INSERT INTO PERSON (ID, FIRST_NAME, LAST_NAME) VALUES (?, ?, ?)";
jdbcTemplate.update(INSERT_PERSON, 1, firstName, "Blair");
}
@Test
void testEntityManagerProxyRejectsProgrammaticTxManagement() {
assertThatIllegalStateException().as("Should not be able to create transactions on container managed EntityManager").isThrownBy(
sharedEntityManager::getTransaction);
}
@Test
void testInstantiateAndSaveWithSharedEmProxy() {
testInstantiateAndSave(sharedEntityManager);
}
protected void testInstantiateAndSave(EntityManager em) {
assertThat(countRowsInTable("person")).as("Should be no people from previous transactions").isEqualTo(0);
Person p = new Person();
p.setFirstName("Tony");
p.setLastName("Blair");
em.persist(p);
em.flush();
assertThat(countRowsInTable("person")).as("1 row must have been inserted").isEqualTo(1);
}
@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersons() {
EntityManager em = entityManagerFactory.createEntityManager();
Query q = em.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertThat(people).isEmpty();
assertThatExceptionOfType(NoResultException.class).isThrownBy(q::getSingleResult);
}
@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersonsNotTransactional() {
endTransaction();
EntityManager em = entityManagerFactory.createEntityManager();
Query q = em.createQuery("select p from Person as p");
List<Person> people = q.getResultList();
assertThat(people).isEmpty();
assertThatExceptionOfType(NoResultException.class).isThrownBy(q::getSingleResult);
}
@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersonsShared() {
Query q = this.sharedEntityManager.createQuery("select p from Person as p");
q.setFlushMode(FlushModeType.AUTO);
List<Person> people = q.getResultList();
assertThat(people).isEmpty();
assertThatExceptionOfType(NoResultException.class).isThrownBy(q::getSingleResult);
}
@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersonsSharedNotTransactional() {
endTransaction();
EntityManager em = this.sharedEntityManager;
Query q = em.createQuery("select p from Person as p");
q.setFlushMode(FlushModeType.AUTO);
List<Person> people = q.getResultList();
assertThat(people).isEmpty();
assertThatException()
.isThrownBy(q::getSingleResult)
.withMessageContaining("closed");
// We would typically expect an IllegalStateException, but Hibernate throws a
// PersistenceException. So we assert the contents of the exception message instead.
Query q2 = em.createQuery("select p from Person as p");
q2.setFlushMode(FlushModeType.AUTO);
assertThatExceptionOfType(NoResultException.class).isThrownBy(q2::getSingleResult);
}
@Test
void testCanSerializeProxies() throws Exception {
assertThat(SerializationTestUtils.serializeAndDeserialize(entityManagerFactory)).isNotNull();
assertThat(SerializationTestUtils.serializeAndDeserialize(sharedEntityManager)).isNotNull();
}
}