Added
Link Here
|
0 |
- |
1 |
/******************************************************************************* |
|
|
2 |
* Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. |
3 |
* This program and the accompanying materials are made available under the |
4 |
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 |
5 |
* which accompanies this distribution. |
6 |
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
7 |
* and the Eclipse Distribution License is available at |
8 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
9 |
* |
10 |
* Contributors: |
11 |
* jlamande - Initial API and implementation |
12 |
* Tomas Kraus - EclipseLink jUnit tests integration |
13 |
******************************************************************************/ |
14 |
package org.eclipse.persistence.testing.tests.jpa.advanced; |
15 |
|
16 |
import java.util.List; |
17 |
|
18 |
import javax.persistence.EntityManager; |
19 |
import javax.persistence.Query; |
20 |
import javax.persistence.TypedQuery; |
21 |
import javax.persistence.criteria.CriteriaBuilder; |
22 |
import javax.persistence.criteria.CriteriaQuery; |
23 |
import javax.persistence.criteria.JoinType; |
24 |
import javax.persistence.criteria.Root; |
25 |
|
26 |
import junit.framework.Test; |
27 |
import junit.framework.TestSuite; |
28 |
|
29 |
import org.eclipse.persistence.testing.framework.junit.JUnitTestCase; |
30 |
import org.eclipse.persistence.testing.models.jpa.advanced.AdvancedTableCreator; |
31 |
import org.eclipse.persistence.testing.models.jpa.advanced.customer.CustomerAddress; |
32 |
import org.eclipse.persistence.testing.models.jpa.advanced.customer.AddressType1; |
33 |
import org.eclipse.persistence.testing.models.jpa.advanced.customer.AddressType2; |
34 |
import org.eclipse.persistence.testing.models.jpa.advanced.customer.AdvancedCustomerTableCreator; |
35 |
import org.eclipse.persistence.testing.models.jpa.advanced.customer.RegisteredCustomer; |
36 |
|
37 |
/** |
38 |
* Runs tests on advanced customer model with Customer entity having two abstract Addresses. Each of those addresses |
39 |
* may contain instances of different offspring. Address entities are mapped into a single table using |
40 |
* {@code DiscriminatorColumn} to distinguish them. |
41 |
*/ |
42 |
public class AbstractEntityWithColumnDiscriminatorTest extends JUnitTestCase { |
43 |
|
44 |
/** {@link RegisteredCustomer} entity name attribute value. */ |
45 |
private static final String CUSTOMER_NAME = "JONES"; |
46 |
|
47 |
/** Value of {@code billingAddress} entity street attribute. */ |
48 |
private static final String CUSTOMER_ADDR_BILLING = "STREET1"; |
49 |
|
50 |
/** Value of {@code deliveryAddress} entity street attribute. */ |
51 |
private static final String CUSTOMER_ADDR_DELIVERY = "STREET2"; |
52 |
|
53 |
|
54 |
/** Persistence unit name. */ |
55 |
private static final String PU_NAME = "customer-pu"; |
56 |
|
57 |
/** |
58 |
* Build collection of test cases for this jUnit test instance. |
59 |
* @return {@link Test} class containing collection of test cases for this jUnit test instance. |
60 |
*/ |
61 |
public static Test suite() { |
62 |
final TestSuite suite = new TestSuite(); |
63 |
suite.addTest(new AbstractEntityWithColumnDiscriminatorTest("testSetup")); |
64 |
addNamedQueryTests(suite); |
65 |
suite.addTest(new AbstractEntityWithColumnDiscriminatorTest("testCleanup")); |
66 |
return suite; |
67 |
} |
68 |
|
69 |
/** |
70 |
* Adds test, similar to suite() but without adding a setup and cleanup. Used from {@code suite()} to add individual |
71 |
* tests. |
72 |
* @param suite Target {@link TestSuite} class where to store tests. |
73 |
* @return {@link Test} class containing collection of test cases for this jUnit test instance. |
74 |
*/ |
75 |
public static Test addNamedQueryTests(final TestSuite suite){ |
76 |
suite.setName("NamedQueryJUnitTest"); |
77 |
suite.addTest(new AbstractEntityWithColumnDiscriminatorTest("testJpqlNoFetch")); |
78 |
suite.addTest(new AbstractEntityWithColumnDiscriminatorTest("testJpqlFetch")); |
79 |
suite.addTest(new AbstractEntityWithColumnDiscriminatorTest("testCriteriaNoFetch")); |
80 |
suite.addTest(new AbstractEntityWithColumnDiscriminatorTest("testCriteriaFetch")); |
81 |
return suite; |
82 |
} |
83 |
|
84 |
/** |
85 |
* Create {@link RegisteredCustomer} entity instance with {@code billingAddress} set to {@link AddressType1} |
86 |
* instance and {@code deliveryAddress} set to {@link AddressType2} instance. Entity is persisted. |
87 |
* @param em An {@link EntityManager} instance used to persist entities. |
88 |
*/ |
89 |
private static void createCustomer(final EntityManager em) { |
90 |
em.getTransaction().begin(); |
91 |
try { |
92 |
final RegisteredCustomer cus = new RegisteredCustomer(CUSTOMER_NAME, new AddressType1( |
93 |
CUSTOMER_ADDR_BILLING), new AddressType2(CUSTOMER_ADDR_DELIVERY)); |
94 |
em.persist(cus); |
95 |
em.getTransaction().commit(); |
96 |
} catch (Throwable e) { |
97 |
em.getTransaction().rollback(); |
98 |
throw e; |
99 |
} |
100 |
} |
101 |
|
102 |
/** |
103 |
* Delete all {@link RegisteredCustomer} entities. |
104 |
* @param em An {@link EntityManager} instance used to persist entities. |
105 |
*/ |
106 |
private static void deleteAllCustomers(final EntityManager em) { |
107 |
List<RegisteredCustomer> customers = em.createNamedQuery( |
108 |
"RegisteredCustomer.selecAll", RegisteredCustomer.class).getResultList(); |
109 |
em.getTransaction().begin(); |
110 |
try { |
111 |
for (RegisteredCustomer cus : customers) { |
112 |
em.remove(cus); |
113 |
} |
114 |
em.getTransaction().commit(); |
115 |
} catch (Throwable e) { |
116 |
em.getTransaction().rollback(); |
117 |
throw e; |
118 |
} |
119 |
} |
120 |
|
121 |
/** |
122 |
* Validate {@link RegisteredCustomer} entity instance. |
123 |
* @param customer {@link RegisteredCustomer} entity instance to be validated. |
124 |
*/ |
125 |
private static void assertCustomer(final RegisteredCustomer customer) { |
126 |
assertEquals(CUSTOMER_NAME, customer.getName()); |
127 |
assertTrue(customer.getBillingAddress() instanceof AddressType1); |
128 |
assertEquals(CUSTOMER_ADDR_BILLING, ((AddressType1) customer.getBillingAddress()).getStreet()); |
129 |
assertTrue(customer.getDeliveryAddress() instanceof AddressType2); |
130 |
assertEquals(CUSTOMER_ADDR_DELIVERY, ((AddressType2) customer.getDeliveryAddress()).getStreet()); |
131 |
} |
132 |
|
133 |
/** |
134 |
* Constructs an instance of {@code AbstractEntityWithColumnDiscriminatorTest} class. |
135 |
*/ |
136 |
public AbstractEntityWithColumnDiscriminatorTest() { |
137 |
super(); |
138 |
} |
139 |
|
140 |
/** |
141 |
* Constructs an instance of {@code AbstractEntityWithColumnDiscriminatorTest} class with given test case name. |
142 |
* @param name Test case name. |
143 |
*/ |
144 |
public AbstractEntityWithColumnDiscriminatorTest(final String name) { |
145 |
super(name); |
146 |
setPuName(PU_NAME); |
147 |
} |
148 |
|
149 |
/** |
150 |
* Initial setup is done as first test in collection, both to record its failure, and to allow execution |
151 |
* in the server. |
152 |
*/ |
153 |
public void testSetup() { |
154 |
// Table creator shall not be used for this persistence unit. |
155 |
// new AdvancedCustomerTableCreator().replaceTables(getServerSession(PU_NAME)); |
156 |
clearCache(PU_NAME); |
157 |
final EntityManager em = createEntityManager(PU_NAME); |
158 |
try { |
159 |
createCustomer(em); |
160 |
} finally { |
161 |
em.close(); |
162 |
} |
163 |
} |
164 |
|
165 |
/** |
166 |
* Final cleanup is done as last test in collection, both to record its failure, and to allow execution |
167 |
* in the server. |
168 |
*/ |
169 |
public void testCleanup() { |
170 |
final EntityManager em = createEntityManager(PU_NAME); |
171 |
try { |
172 |
deleteAllCustomers(em); |
173 |
} finally { |
174 |
em.close(); |
175 |
} |
176 |
} |
177 |
|
178 |
/** |
179 |
* Test JPQL query on {@link RegisteredCustomer} without {@code LEFT JOIN FETCH} of {@link CustomerAddress} |
180 |
* entities. |
181 |
*/ |
182 |
public void testJpqlNoFetch() { |
183 |
final EntityManager em = createEntityManager(PU_NAME); |
184 |
try { |
185 |
final Query query = em.createNamedQuery( |
186 |
"RegisteredCustomer.selecByName", |
187 |
RegisteredCustomer.class).setParameter("name", CUSTOMER_NAME); |
188 |
final RegisteredCustomer customer = (RegisteredCustomer) query.getSingleResult(); |
189 |
assertCustomer(customer); |
190 |
} finally { |
191 |
em.close(); |
192 |
} |
193 |
} |
194 |
|
195 |
/** |
196 |
* Test JPQL query on {@link RegisteredCustomer} with {@code LEFT JOIN FETCH} of {@link CustomerAddress} entities. |
197 |
*/ |
198 |
public void testJpqlFetch() { |
199 |
final EntityManager em = createEntityManager(PU_NAME); |
200 |
try { |
201 |
final Query query = em.createNamedQuery( |
202 |
"RegisteredCustomer.selecByNameFetch", |
203 |
RegisteredCustomer.class).setParameter("name", CUSTOMER_NAME); |
204 |
final RegisteredCustomer customer = (RegisteredCustomer) query.getSingleResult(); |
205 |
assertCustomer(customer); |
206 |
} finally { |
207 |
em.close(); |
208 |
} |
209 |
} |
210 |
|
211 |
/** |
212 |
* Test criteria query on {@link RegisteredCustomer} without {@code LEFT JOIN FETCH} of {@link CustomerAddress} |
213 |
* entities. |
214 |
*/ |
215 |
public void testCriteriaNoFetch() { |
216 |
final EntityManager em = createEntityManager(PU_NAME); |
217 |
try { |
218 |
final CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); |
219 |
final CriteriaQuery<RegisteredCustomer> criteriaQuery |
220 |
= criteriaBuilder.createQuery(RegisteredCustomer.class); |
221 |
final Root<RegisteredCustomer> from = criteriaQuery.from(RegisteredCustomer.class); |
222 |
|
223 |
criteriaQuery.where(criteriaBuilder.equal(from.get("name"), CUSTOMER_NAME)); |
224 |
criteriaQuery.distinct(true); |
225 |
|
226 |
final TypedQuery<RegisteredCustomer> query = em.createQuery(criteriaQuery); |
227 |
final RegisteredCustomer customer = query.getSingleResult(); |
228 |
assertCustomer(customer); |
229 |
} finally { |
230 |
em.close(); |
231 |
} |
232 |
} |
233 |
|
234 |
/** |
235 |
* Test criteria query on {@link RegisteredCustomer} with {@code LEFT JOIN FETCH} of {@link CustomerAddress} |
236 |
* entities. |
237 |
*/ |
238 |
public void testCriteriaFetch() { |
239 |
final EntityManager em = createEntityManager(PU_NAME); |
240 |
try { |
241 |
final CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); |
242 |
final CriteriaQuery<RegisteredCustomer> criteriaQuery |
243 |
= criteriaBuilder.createQuery(RegisteredCustomer.class); |
244 |
final Root<RegisteredCustomer> from = criteriaQuery.from(RegisteredCustomer.class); |
245 |
|
246 |
from.fetch("billingAddress", JoinType.LEFT); |
247 |
from.fetch("deliveryAddress", JoinType.LEFT); |
248 |
|
249 |
criteriaQuery.where(criteriaBuilder.equal(from.get("name"), CUSTOMER_NAME)); |
250 |
criteriaQuery.distinct(true); |
251 |
|
252 |
final TypedQuery<RegisteredCustomer> query = em.createQuery(criteriaQuery); |
253 |
final RegisteredCustomer customer = query.getSingleResult(); |
254 |
assertCustomer(customer); |
255 |
} finally { |
256 |
em.close(); |
257 |
} |
258 |
} |
259 |
|
260 |
} |