Lines 13-49
Link Here
|
13 |
package org.eclipse.persistence.testing.tests.jpa.sessionbean; |
13 |
package org.eclipse.persistence.testing.tests.jpa.sessionbean; |
14 |
|
14 |
|
15 |
import java.net.URL; |
15 |
import java.net.URL; |
16 |
import java.util.Iterator; |
16 |
import java.util.ArrayList; |
17 |
import java.util.List; |
17 |
import java.util.List; |
18 |
import java.util.Properties; |
18 |
import java.util.Properties; |
19 |
import javax.naming.Context; |
19 |
import javax.naming.Context; |
20 |
import javax.naming.InitialContext; |
20 |
import javax.naming.InitialContext; |
21 |
import javax.naming.NameNotFoundException; |
21 |
import javax.persistence.EntityManager; |
22 |
import javax.rmi.PortableRemoteObject; |
22 |
import javax.rmi.PortableRemoteObject; |
23 |
|
23 |
|
24 |
import junit.framework.*; |
24 |
import junit.framework.*; |
25 |
|
25 |
|
26 |
import org.eclipse.persistence.testing.framework.junit.JUnitTestCase; |
26 |
import org.eclipse.persistence.testing.framework.junit.JUnitTestCase; |
27 |
import org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee; |
27 |
import org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee; |
28 |
import org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Address; |
|
|
29 |
import org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.AdvancedTableCreator; |
28 |
import org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.AdvancedTableCreator; |
30 |
import org.eclipse.persistence.testing.models.jpa.fieldaccess.advanced.Employee.EmployeeStatus; |
|
|
31 |
import org.eclipse.persistence.testing.models.jpa.sessionbean.EmployeeService; |
29 |
import org.eclipse.persistence.testing.models.jpa.sessionbean.EmployeeService; |
32 |
|
30 |
|
33 |
/** |
31 |
/** |
34 |
* EJB 3 SessionBean tests. |
32 |
* JPA Cache Coordination SessionBean tests. |
35 |
* Testing using EclipseLink JPA in a JEE EJB 3 SessionBean environment. |
33 |
* Testing using EclipseLink JPA in a JEE EJB 3 SessionBean environment. |
36 |
* These tests can only be run with a server. |
34 |
* These tests can only be run with a server. |
37 |
*/ |
35 |
*/ |
38 |
public class SessionBeanTestsRCM extends JUnitTestCase { |
36 |
public class SessionBeanTestsRCM extends JUnitTestCase { |
|
|
37 |
public final int SLEEP = 2000; |
38 |
protected List<String> serverURLs; |
39 |
protected int server; |
39 |
protected EmployeeService service; |
40 |
protected EmployeeService service; |
40 |
private String wlsUserName; |
|
|
41 |
private String wlsPassword; |
42 |
private String server1Url; |
43 |
private String server2Url; |
44 |
private String server3Url; |
45 |
int empId = 0; |
46 |
private Employee employeeCached = null; |
47 |
|
41 |
|
48 |
public SessionBeanTestsRCM() { |
42 |
public SessionBeanTestsRCM() { |
49 |
super(); |
43 |
super(); |
Lines 56-237
public class SessionBeanTestsRCM extends JUnitTestCase {
Link Here
|
56 |
public SessionBeanTestsRCM(String name, boolean shouldRunTestOnServer) { |
50 |
public SessionBeanTestsRCM(String name, boolean shouldRunTestOnServer) { |
57 |
super(name); |
51 |
super(name); |
58 |
this.shouldRunTestOnServer = shouldRunTestOnServer; |
52 |
this.shouldRunTestOnServer = shouldRunTestOnServer; |
59 |
|
53 |
|
60 |
URL url = getClass().getResource("/weblogic.properties"); |
54 |
URL url = getClass().getResource("/weblogic.properties"); |
61 |
Properties properties = new Properties(); |
55 |
Properties properties = new Properties(); |
62 |
try { |
56 |
try { |
63 |
properties.load(url.openStream()); |
57 |
properties.load(url.openStream()); |
64 |
} catch (Exception error) { |
58 |
} catch (Exception error) { |
65 |
throw new RuntimeException(error); |
59 |
throw new RuntimeException(error); |
66 |
} |
60 |
} |
67 |
server1Url = properties.getProperty("rcm.wls.server1.url"); |
61 |
this.serverURLs = new ArrayList<String>(); |
68 |
server2Url = properties.getProperty("rcm.wls.server2.url"); |
62 |
this.serverURLs.add(properties.getProperty("rcm.wls.server1.url")); |
69 |
server3Url = properties.getProperty("rcm.wls.server3.url"); |
63 |
this.serverURLs.add(properties.getProperty("rcm.wls.server2.url")); |
|
|
64 |
this.serverURLs.add(properties.getProperty("rcm.wls.server3.url")); |
70 |
} |
65 |
} |
71 |
|
66 |
|
72 |
public static Test suite() { |
67 |
public static Test suite() { |
73 |
TestSuite suite = new TestSuite("SessionBeanTestsRCM"); |
68 |
TestSuite suite = new TestSuite("SessionBeanTestsRCM"); |
74 |
|
69 |
|
75 |
suite.addTest(new SessionBeanTestsRCM("testSetupRcmOnServer2", true)); |
70 |
suite.addTest(new SessionBeanTestsRCM("testSetup", true)); |
76 |
suite.addTest(new SessionBeanTestsRCM("testSetupForDeleteOnServer2", false)); |
71 |
suite.addTest(new SessionBeanTestsRCM("testLag", false)); |
77 |
|
72 |
|
78 |
suite.addTest(new SessionBeanTestsRCM("testSetupRcmOnServer1", false)); |
73 |
suite.addTest(new SessionBeanTestsRCM("testUpdates", false)); |
79 |
suite.addTest(new SessionBeanTestsRCM("testPerformDeleteOnServer1", false)); |
74 |
suite.addTest(new SessionBeanTestsRCM("testDelete", false)); |
80 |
|
75 |
|
81 |
suite.addTest(new SessionBeanTestsRCM("testConfirmUpdateOnServer2", false)); |
|
|
82 |
suite.addTest(new SessionBeanTestsRCM("testConfirmDeleteOnServer2", false)); |
83 |
return suite; |
76 |
return suite; |
84 |
} |
77 |
} |
85 |
|
78 |
|
86 |
/** |
79 |
/** |
87 |
* The setup is done as a test, both to record its failure, and to alow execution in the server2. |
80 |
* Return the next server index to use. |
|
|
81 |
* This cycles through the servers. |
88 |
*/ |
82 |
*/ |
89 |
public void testSetupRcmOnServer2() throws Exception { |
83 |
public synchronized int nextServer() { |
90 |
new AdvancedTableCreator().replaceTables(JUnitTestCase.getServerSession("sessionbean")); |
84 |
this.server++; |
|
|
85 |
if (this.server >= this.serverURLs.size()) { |
86 |
this.server = 0; |
87 |
} |
88 |
return this.server; |
91 |
} |
89 |
} |
92 |
|
90 |
|
93 |
public EmployeeService getEmployeeService(String url) throws Exception { |
91 |
public EmployeeService getEmployeeService() { |
|
|
92 |
if (this.service == null) { |
93 |
this.service = nextEmployeeService(); |
94 |
} |
95 |
return service; |
96 |
} |
97 |
|
98 |
public EmployeeService nextEmployeeService() { |
99 |
EmployeeService service = null; |
100 |
int server = nextServer(); |
94 |
Properties properties = new Properties(); |
101 |
Properties properties = new Properties(); |
|
|
102 |
String url = this.serverURLs.get(server); |
95 |
properties.put("java.naming.provider.url", url); |
103 |
properties.put("java.naming.provider.url", url); |
96 |
Context context = new InitialContext(properties); |
104 |
System.out.println(server + ":" + url); |
97 |
|
|
|
98 |
try { |
105 |
try { |
99 |
return (EmployeeService) PortableRemoteObject.narrow(context.lookup("java:comp/env/ejb/EmployeeService"), EmployeeService.class); |
106 |
Context context = new InitialContext(properties); |
100 |
} catch (NameNotFoundException notFoundException) { |
107 |
service = (EmployeeService)PortableRemoteObject.narrow(context.lookup("EmployeeService#org.eclipse.persistence.testing.models.jpa.sessionbean.EmployeeService"), EmployeeService.class); |
101 |
try { |
108 |
} catch (Exception notFoundException) { |
102 |
return (EmployeeService) PortableRemoteObject.narrow(context.lookup("ejb/EmployeeService"), EmployeeService.class); |
109 |
throw new Error("Lookup failed.", notFoundException); |
103 |
} catch (NameNotFoundException notFoundException2) { |
|
|
104 |
try { |
105 |
// WLS likes this one. |
106 |
return (EmployeeService) PortableRemoteObject.narrow(context.lookup("EmployeeService#org.eclipse.persistence.testing.models.jpa.sessionbean.EmployeeService"), EmployeeService.class); |
107 |
} catch (NameNotFoundException notFoundException3) { |
108 |
try { |
109 |
//jboss likes this one |
110 |
return (EmployeeService) PortableRemoteObject.narrow(context.lookup("EmployeeService/remote"), EmployeeService.class); |
111 |
} catch (NameNotFoundException notFoundException4) { |
112 |
throw new Error("All lookups failed.", notFoundException); |
113 |
} |
114 |
} |
115 |
} |
116 |
} |
110 |
} |
|
|
111 |
return service; |
117 |
} |
112 |
} |
118 |
|
113 |
|
119 |
|
114 |
/** |
120 |
/* CacheCoordination Delete Test Setup on Server2: |
115 |
* The setup is done as a test, both to record its failure, and to alow execution in the server2. |
121 |
* This test insert an employee record, which will be deleted later using Server1 |
|
|
122 |
*/ |
116 |
*/ |
123 |
public void testSetupForDeleteOnServer2() throws Exception { |
117 |
public void testSetup() throws Exception { |
124 |
// Ensure each session starts to enable cache synch. |
118 |
new AdvancedTableCreator().replaceTables(JUnitTestCase.getServerSession("sessionbean")); |
125 |
getEmployeeService(server1Url).findAll(); |
119 |
|
126 |
getEmployeeService(server2Url).findAll(); |
120 |
EntityManager em = createEntityManager("sessionbean"); |
127 |
Thread.sleep(5000); |
121 |
beginTransaction(em); |
128 |
/* Create an Employee record using Server2 */ |
122 |
try { |
129 |
Employee employee = new Employee(); |
123 |
Employee employee = new Employee(); |
130 |
employee.setFirstName("Jane2"); |
124 |
em.persist(employee); |
131 |
employee.setLastName("Doe2"); |
125 |
commitTransaction(em); |
132 |
employee.setAddress(new Address()); |
126 |
} finally { |
133 |
employee.getAddress().setCity("Ottawa2"); |
127 |
if (isTransactionActive(em)) { |
134 |
Employee manager = new Employee(); |
128 |
closeEntityManagerAndTransaction(em); |
135 |
manager.setFirstName("John2"); |
129 |
} |
136 |
manager.setLastName("Done2"); |
|
|
137 |
employee.setManager(manager); |
138 |
|
139 |
int empID = getEmployeeService(server2Url).insert(employee); |
140 |
|
141 |
employee = getEmployeeService(server2Url).findById(empID); |
142 |
if (employee == null) { |
143 |
fail("Server2 CacheCoordination Setup Failure: New employee added from Server2 is not found in cache or DB."); |
144 |
} |
130 |
} |
145 |
} |
131 |
} |
146 |
|
132 |
|
147 |
/** |
133 |
/** |
148 |
* CacheCoordination Update Test setup on Server1: |
134 |
* Build a test that validates cache coordination updates are working in the cluster. |
149 |
* This test insert an employee record, which is then updated using Server1. Later on, this update will be verified on Server2. |
|
|
150 |
* The setup is done as a test, both to record its failure, and to alow execution in the server1. |
151 |
*/ |
135 |
*/ |
152 |
public void testSetupRcmOnServer1() throws Exception { |
136 |
public void testUpdates() { |
153 |
Thread.sleep(1000); |
137 |
EmployeeService service = nextEmployeeService(); |
154 |
|
138 |
Employee employee = (Employee)service.findAll().get(0); |
155 |
/* Create an Employee record in Server1 */ |
139 |
|
156 |
Employee employee = new Employee(); |
140 |
for (int index = 0; index < 5; index++) { |
157 |
employee.setFirstName("Jane1"); |
141 |
service = nextEmployeeService(); |
158 |
employee.setLastName("Doe1"); |
142 |
employee = service.findById(employee.getId()); |
159 |
employee.setAddress(new Address()); |
|
|
160 |
employee.getAddress().setCity("Ottawa1"); |
161 |
Employee manager = new Employee(); |
162 |
manager.setFirstName("John1"); |
163 |
manager.setLastName("Done1"); |
164 |
employee.setManager(manager); |
165 |
employee.setStatus(EmployeeStatus.FULL_TIME); |
166 |
|
167 |
empId = getEmployeeService(server1Url).insert(employee); |
168 |
|
169 |
/* read Employee from cache and/or DB */ |
170 |
Employee jane1 = getEmployeeService(server1Url).findById(empId); |
171 |
/* update employee on Server1 */ |
172 |
jane1.setLastName("LastNameUpdatedOnServer1"); |
173 |
jane1.getAddress().setCity("newCity"); |
174 |
getEmployeeService(server1Url).update(jane1); |
175 |
|
176 |
if (!jane1.getLastName().equals("LastNameUpdatedOnServer1")) { |
177 |
fail("UpdateTest Setup on Server1 failed"); |
178 |
} |
143 |
} |
179 |
|
144 |
|
|
|
145 |
for (int index = 0; index < 5; index++) { |
146 |
service = nextEmployeeService(); |
147 |
int random = (int)(Math.random() * 1000000); |
148 |
employee = service.findById(employee.getId()); |
149 |
employee.setLastName(String.valueOf(random)); |
150 |
service.update(employee); |
151 |
try { |
152 |
Thread.sleep(SLEEP); |
153 |
} catch (Exception ignore) {} |
154 |
} |
180 |
} |
155 |
} |
181 |
|
|
|
182 |
/** |
156 |
/** |
183 |
* CacheCoordination Test setup for Delete on Server1: |
157 |
* Build a test that validates cache coordination deletes are working in the cluster. |
184 |
* Find employee created on Server2, then delete it using Server1. |
|
|
185 |
*/ |
158 |
*/ |
186 |
public void testPerformDeleteOnServer1() throws Exception { |
159 |
public void testDelete() { |
187 |
Thread.sleep(1000); |
160 |
EmployeeService service = nextEmployeeService(); |
188 |
List result = getEmployeeService(server1Url).findByFirstName("Jane2"); |
161 |
Employee employee = new Employee(); |
189 |
int count = 0; |
162 |
employee.setId(service.insert(employee)); |
190 |
for (Iterator i = result.iterator(); i.hasNext();) { |
163 |
|
191 |
employeeCached = (Employee) i.next(); |
164 |
for (int index = 0; index < 5; index++) { |
|
|
165 |
service = nextEmployeeService(); |
166 |
employee = service.findById(employee.getId()); |
192 |
} |
167 |
} |
193 |
|
168 |
|
194 |
if (employeeCached == null){ |
169 |
service = nextEmployeeService(); |
195 |
fail("Perform Delete Test failed: New employee was not found in distributed cache to delete"); |
170 |
employee = service.findById(employee.getId()); |
|
|
171 |
service.delete(employee); |
172 |
try { |
173 |
Thread.sleep(SLEEP); |
174 |
} catch (Exception ignore) {} |
175 |
|
176 |
for (int index = 0; index < 5; index++) { |
177 |
service = nextEmployeeService(); |
178 |
Employee result = service.findById(employee.getId()); |
179 |
if (result != null) { |
180 |
fail("Employee should be removed:" + result); |
181 |
} |
196 |
} |
182 |
} |
197 |
getEmployeeService(server1Url).delete(employeeCached); |
|
|
198 |
} |
199 |
|
183 |
|
200 |
/* CacheCoordination Test - Verify that Object Update done on Server1 is sync with Server2 thru cache: |
184 |
} |
201 |
* This test uses JPQL to read object on Server2. |
185 |
|
|
|
186 |
/** |
187 |
* Build a test that attempt to determine the coordination lag in a cluster. |
202 |
*/ |
188 |
*/ |
203 |
public void testConfirmUpdateOnServer2() throws Exception { |
189 |
public void testLag() { |
204 |
Thread.sleep(1000); |
190 |
EmployeeService service = nextEmployeeService(); |
205 |
/* verify updates are in sync: read Employee from using Server2 URL */ |
191 |
Employee employee = (Employee)service.findAll().get(0); |
206 |
List result = getEmployeeService(server2Url).findByFirstName("Jane1"); |
192 |
|
207 |
int count = 0; |
193 |
for (int index = 0; index < 5; index++) { |
208 |
for (Iterator i = result.iterator(); i.hasNext();){ |
194 |
service = nextEmployeeService(); |
209 |
employeeCached = (Employee) i.next(); |
195 |
employee = service.findById(employee.getId()); |
210 |
} |
|
|
211 |
if (employeeCached == null){ |
212 |
fail("Object Update Test verification failed: New employee was not found in distributed cache"); |
213 |
} |
196 |
} |
214 |
if (!employeeCached.getLastName().equals("LastNameUpdatedOnServer1")) { |
|
|
215 |
fail("Object Update Test verification failed: Changes from server1 is not seen by server2 from distributed cache"); |
216 |
} |
217 |
} |
218 |
|
219 |
/* CacheCoordination Test - Verify that Object Delete done on Server1 is sync with Server2 thru cache: |
220 |
* This test uses JPQL to read object on Server2. |
221 |
*/ |
222 |
public void testConfirmDeleteOnServer2() throws Exception { |
223 |
Thread.sleep(1000); |
224 |
|
197 |
|
225 |
/* verify deletes are in sync: read Employee from using Server2 URL */ |
198 |
// Sleep to let RCM to connect. |
226 |
List result = getEmployeeService(server2Url).findByFirstName("Jane2"); |
199 |
try { |
227 |
for (Iterator i = result.iterator(); i.hasNext();) { |
200 |
Thread.sleep(20000); |
228 |
employeeCached = (Employee) i.next(); |
201 |
} catch (Exception ignore) {} |
229 |
} |
202 |
|
230 |
if (employeeCached != null) { |
203 |
int sleeps[] = new int[]{1, 50, 100, 500, 1000, 2000, 3000, 4000, 5000, 10000}; |
231 |
fail("Object Delete Test verification failed: employee was not removed from cache as expected" ); |
204 |
int sleep = 0; |
|
|
205 |
boolean success = false; |
206 |
boolean failed = false; |
207 |
while (!success && (sleep < sleeps.length)) { |
208 |
for (int index = 0; index < 10; index++) { |
209 |
service = nextEmployeeService(); |
210 |
int random = (int)(Math.random() * 1000000); |
211 |
employee = service.findById(employee.getId()); |
212 |
employee.setLastName(String.valueOf(random)); |
213 |
try { |
214 |
service.update(employee); |
215 |
} catch (Exception lockError) { |
216 |
System.out.println("Failed at sleep of:" + sleeps[sleep] + " on attmept:" + index); |
217 |
sleep = sleep + 1; |
218 |
failed = true; |
219 |
break; |
220 |
} |
221 |
try { |
222 |
Thread.sleep(sleeps[sleep]); |
223 |
} catch (Exception ignore) { } |
224 |
} |
225 |
if (!failed) { |
226 |
success = true; |
227 |
System.out.println("Success at sleep of:" + sleeps[sleep]); |
228 |
break; |
229 |
} else { |
230 |
failed = false; |
231 |
} |
232 |
} |
232 |
} |
233 |
|
233 |
} |
234 |
} |
|
|
235 |
|
234 |
|
236 |
} |
235 |
} |
237 |
|
236 |
|