Line 0
Link Here
|
|
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 1998, 2008 Oracle. 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 |
* Oracle - initial API and implementation from Oracle TopLink |
12 |
******************************************************************************/ |
13 |
package org.eclipse.persistence.testing.tests.jpa.sessionbean; |
14 |
|
15 |
import java.util.Iterator; |
16 |
import java.util.List; |
17 |
import java.util.Properties; |
18 |
import javax.naming.Context; |
19 |
import javax.naming.InitialContext; |
20 |
import javax.naming.NameNotFoundException; |
21 |
import javax.rmi.PortableRemoteObject; |
22 |
|
23 |
import junit.framework.*; |
24 |
|
25 |
import org.eclipse.persistence.exceptions.ValidationException; |
26 |
import org.eclipse.persistence.testing.framework.junit.JUnitTestCase; |
27 |
import org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Department; |
28 |
import org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee; |
29 |
import org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address; |
30 |
import org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.AdvancedTableCreator; |
31 |
import org.eclipse.persistence.testing.models.jpa.sessionbean.EmployeeService; |
32 |
|
33 |
import org.eclipse.persistence.expressions.Expression; |
34 |
import org.eclipse.persistence.expressions.ExpressionBuilder; |
35 |
import org.eclipse.persistence.queries.ReadObjectQuery; |
36 |
|
37 |
|
38 |
/** |
39 |
* EJB 3 SessionBean tests. |
40 |
* Testing using EclipseLink JPA in a JEE EJB 3 SessionBean environment. |
41 |
* These tests can only be run with a server. |
42 |
*/ |
43 |
public class SessionBeanTestsRCM extends JUnitTestCase { |
44 |
protected EmployeeService service; |
45 |
private String wlsUserName="weblogic"; |
46 |
private String wlsPassword="weblogic"; |
47 |
private String server1Url = "t3://localhost:7001"; |
48 |
private String server2Url = "t3://localhost:7021"; |
49 |
private String server3Url = "t3://localhost:7031"; |
50 |
int empId = 0; |
51 |
private Employee employeeCached = null; |
52 |
|
53 |
public SessionBeanTestsRCM() { |
54 |
super(); |
55 |
} |
56 |
|
57 |
public SessionBeanTestsRCM(String name) { |
58 |
super(name); |
59 |
} |
60 |
|
61 |
public SessionBeanTestsRCM(String name, boolean shouldRunTestOnServer) { |
62 |
super(name); |
63 |
this.shouldRunTestOnServer = shouldRunTestOnServer; |
64 |
} |
65 |
|
66 |
public static Test suite() { |
67 |
TestSuite suite = new TestSuite("SessionBeanTestsRCM"); |
68 |
|
69 |
suite.addTest(new SessionBeanTestsRCM("testSetupRcmOnServer2", true)); |
70 |
suite.addTest(new SessionBeanTestsRCM("testSetupForDeleteOnServer2", true)); |
71 |
|
72 |
suite.addTest(new SessionBeanTestsRCM("testSetupRcmOnServer1", true)); |
73 |
suite.addTest(new SessionBeanTestsRCM("testPerformDeleteOnServer1", true)); |
74 |
|
75 |
//suite.addTest(new SessionBeanTestsRCM("testReadFromServer2AfterUpdateNonJPQL", true)); |
76 |
suite.addTest(new SessionBeanTestsRCM("testConfirmUpdateOnServer2", true)); |
77 |
suite.addTest(new SessionBeanTestsRCM("testConfirmDeleteOnServer2", true)); |
78 |
return suite; |
79 |
} |
80 |
|
81 |
/** |
82 |
* The setup is done as a test, both to record its failure, and to alow execution in the server2. |
83 |
*/ |
84 |
public void testSetupRcmOnServer2() throws Exception { |
85 |
new AdvancedTableCreator().replaceTables(JUnitTestCase.getServerSession("sessionbean")); |
86 |
} |
87 |
|
88 |
public EmployeeService getEmployeeService(String url) throws Exception { |
89 |
if (service == null) { |
90 |
|
91 |
Properties properties = new Properties(); |
92 |
//System.getProperty("server2.url"); |
93 |
|
94 |
if (url != null) { |
95 |
properties.put("java.naming.provider.url", url); |
96 |
} |
97 |
Context context = new InitialContext(properties); |
98 |
|
99 |
try { |
100 |
service = (EmployeeService) PortableRemoteObject.narrow(context.lookup("java:comp/env/ejb/EmployeeService"), EmployeeService.class); |
101 |
} catch (NameNotFoundException notFoundException) { |
102 |
try { |
103 |
service = (EmployeeService) PortableRemoteObject.narrow(context.lookup("ejb/EmployeeService"), EmployeeService.class); |
104 |
} catch (NameNotFoundException notFoundException2) { |
105 |
try { |
106 |
// WLS likes this one. |
107 |
service = (EmployeeService) PortableRemoteObject.narrow(context.lookup("EmployeeService#org.eclipse.persistence.testing.models.jpa.sessionbean.EmployeeService"), EmployeeService.class); |
108 |
} catch (NameNotFoundException notFoundException3) { |
109 |
try { |
110 |
//jboss likes this one |
111 |
service = (EmployeeService) PortableRemoteObject.narrow(context.lookup("EmployeeService/remote"), EmployeeService.class); |
112 |
} catch (NameNotFoundException notFoundException4) { |
113 |
throw new Error("All lookups failed.", notFoundException); |
114 |
} |
115 |
} |
116 |
} |
117 |
} |
118 |
} |
119 |
return service; |
120 |
} |
121 |
|
122 |
|
123 |
/* CacheCoordination Delete Test Setup on Server2: |
124 |
* This test insert an employee record, which will be deleted later using Server1 |
125 |
*/ |
126 |
public void testSetupForDeleteOnServer2() throws Exception { |
127 |
/* Create an Employee record using Server2 */ |
128 |
Employee employee = new Employee(); |
129 |
employee.setFirstName("Jane2"); |
130 |
employee.setLastName("Doe2"); |
131 |
employee.setAddress(new Address()); |
132 |
employee.getAddress().setCity("Ottawa2"); |
133 |
Employee manager = new Employee(); |
134 |
manager.setFirstName("John2"); |
135 |
manager.setLastName("Done2"); |
136 |
employee.setManager(manager); |
137 |
|
138 |
int empID = getEmployeeService(server2Url).insert(employee); |
139 |
|
140 |
try{ |
141 |
employee = getEmployeeService(server2Url).findById(empID); |
142 |
if(employee == null){ |
143 |
|
144 |
fail("Server2 CacheCoordination Setup Failure: New employee added from Server2 is not found in cache or DB."); |
145 |
} |
146 |
} catch(Exception e){ |
147 |
throw new Error("Following Emp ID not found: " + empID); |
148 |
} |
149 |
} |
150 |
|
151 |
/** |
152 |
* CacheCoordination Update Test setup on Server1: |
153 |
* This test insert an employee record, which is then updated using Server1. Later on, this update will be verified on Server2. |
154 |
* The setup is done as a test, both to record its failure, and to alow execution in the server1. |
155 |
*/ |
156 |
public void testSetupRcmOnServer1() throws Exception { |
157 |
|
158 |
/* Create an Employee record in Server1 */ |
159 |
Employee employee = new Employee(); |
160 |
employee.setFirstName("Jane1"); |
161 |
employee.setLastName("Doe1"); |
162 |
employee.setAddress(new Address()); |
163 |
employee.getAddress().setCity("Ottawa1"); |
164 |
Employee manager = new Employee(); |
165 |
manager.setFirstName("John1"); |
166 |
manager.setLastName("Done1"); |
167 |
employee.setManager(manager); |
168 |
|
169 |
empId = getEmployeeService(server1Url).insert(employee); |
170 |
|
171 |
/* read Employee from cache and/or DB */ |
172 |
Employee jane1 = (Employee) getEmployeeService(server1Url).findById(empId); |
173 |
/* update employee on Server1 */ |
174 |
jane1.setLastName("LastNameUpdatedOnServer1"); |
175 |
jane1.getAddress().setCity("newCity"); |
176 |
getEmployeeService(server1Url).update(jane1); |
177 |
|
178 |
if (!jane1.getLastName().equals("LastNameUpdatedOnServer1")) { |
179 |
fail("UpdateTest Setup on Server1 failed"); |
180 |
} |
181 |
|
182 |
} |
183 |
|
184 |
/** |
185 |
* CacheCoordination Test setup for Delete on Server1: |
186 |
* Find employee created on Server2, then delete it using Server1. |
187 |
*/ |
188 |
public void testPerformDeleteOnServer1() throws Exception { |
189 |
|
190 |
List result = getEmployeeService(server1Url).findByFirstName("Jane2"); |
191 |
int count = 0; |
192 |
for(Iterator i = result.iterator(); i.hasNext();){ |
193 |
employeeCached = (Employee) i.next(); |
194 |
} |
195 |
|
196 |
if(employeeCached == null){ |
197 |
|
198 |
fail("Perform Delete Test failed: New employee was not found in distributed cache to delete"); |
199 |
} |
200 |
|
201 |
getEmployeeService(server1Url).delete(employeeCached); |
202 |
} |
203 |
|
204 |
|
205 |
/* CacheCoordination Test - Verify that Object Update done on Server1 is sync with Server2 thru cache: |
206 |
* This test uses JPQL to read object on Server2. |
207 |
*/ |
208 |
public void testConfirmUpdateOnServer2() throws Exception { |
209 |
|
210 |
|
211 |
/* verify updates are in sync: read Employee from using Server2 URL */ |
212 |
|
213 |
List result = getEmployeeService(server2Url).findByFirstName("Jane1"); |
214 |
int count = 0; |
215 |
for(Iterator i = result.iterator(); i.hasNext();){ |
216 |
employeeCached = (Employee) i.next(); |
217 |
} |
218 |
if(employeeCached == null){ |
219 |
|
220 |
fail("Object Update Test verification failed: New employee was not found in distributed cache"); |
221 |
} |
222 |
if (!employeeCached.getLastName().equals("LastNameUpdatedOnServer1")) { |
223 |
fail("Object Update Test verification failed: Changes from server1 is not seen by server2 from distributed cache"); |
224 |
} |
225 |
|
226 |
|
227 |
} |
228 |
|
229 |
|
230 |
|
231 |
/* CacheCoordination Test - Verify that Object Delete done on Server1 is sync with Server2 thru cache: |
232 |
* This test uses JPQL to read object on Server2. |
233 |
*/ |
234 |
public void testConfirmDeleteOnServer2() throws Exception { |
235 |
|
236 |
/* verify deletes are in sync: read Employee from using Server2 URL */ |
237 |
|
238 |
List result = getEmployeeService(server2Url).findByFirstName("Jane2"); |
239 |
int count = 0; |
240 |
for(Iterator i = result.iterator(); i.hasNext();){ |
241 |
employeeCached = (Employee) i.next(); |
242 |
} |
243 |
if(!( employeeCached == null)){ |
244 |
|
245 |
fail("Object Delete Test verification failed: employee was not removed from cache as expected" ); |
246 |
} |
247 |
|
248 |
} |
249 |
|
250 |
/* CacheCoordination Test - Verify changes are on cache and read onto Server2: |
251 |
* |
252 |
*/ |
253 |
/* |
254 |
public void testReadFromServer2AfterUpdateNonJPQL() throws Exception { |
255 |
|
256 |
|
257 |
// read Employee from using Server2 URL |
258 |
ExpressionBuilder employees = new ExpressionBuilder(); |
259 |
Expression expression = employees.get("firstName").equal("Jane1"); |
260 |
ReadObjectQuery query = new ReadObjectQuery(Employee.class, expression); |
261 |
|
262 |
// verify updates are in sync |
263 |
|
264 |
query.checkCacheOnly(); |
265 |
//empID = getEmpID(); |
266 |
Employee employeeCached = (Employee) (this.getServerSession("sessionbean")).executeQuery(query); |
267 |
|
268 |
if(employeeCached == null){ |
269 |
|
270 |
fail("Object_Exist_Test: New employee was not added to distributed cache: (SERVER2_URL) "+ (this.getServerSession("sessionbean")).getName() ); |
271 |
} |
272 |
if (!employeeCached.getLastName().equals("LastNameUpdatedOnServer1")) { |
273 |
fail("Object_Update_Test: Changes from server1 is not seen by server2 from distributed cache"); |
274 |
} |
275 |
|
276 |
|
277 |
} |
278 |
*/ |
279 |
|
280 |
} |
281 |
|