blob: 3b3f5c6cda61d21f400d61969a11441046d89b71 [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
Gaurav Gupta36555072020-03-26 14:10:23 +05302 * Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved.
Vinay Vishal57171472018-09-18 20:22:00 +05303 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v. 2.0, which is available at
6 * http://www.eclipse.org/legal/epl-2.0.
7 *
8 * This Source Code may also be made available under the following Secondary
9 * Licenses when the conditions for such availability set forth in the
10 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
11 * version 2 with the GNU Classpath Exception, which is available at
12 * https://www.gnu.org/software/classpath/license.html.
13 *
14 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
15 */
16
17package com.sun.s1asdev.ejb.sfsb.passivateactivate.client;
18
19import java.util.ArrayList;
Vinay Vishal57171472018-09-18 20:22:00 +053020import javax.naming.*;
Gaurav Gupta36555072020-03-26 14:10:23 +053021import jakarta.jms.*;
Gaurav Guptaed5e7312020-05-01 07:54:26 +053022import jakarta.ejb.*;
Vinay Vishal57171472018-09-18 20:22:00 +053023import javax.rmi.PortableRemoteObject;
Vinay Vishal57171472018-09-18 20:22:00 +053024
25import com.sun.s1asdev.ejb.sfsb.passivateactivate.ejb.SFSBHome;
26import com.sun.s1asdev.ejb.sfsb.passivateactivate.ejb.SFSB;
27
28import com.sun.ejte.ccl.reporter.SimpleReporterAdapter;
29
30public class Client {
31
32 // consts
33 public static String kTestNotRun = "TEST NOT RUN";
34 public static String kTestPassed = "TEST PASSED";
35 public static String kTestFailed = "TEST FAILED";
David Matějčekf4dc06a2021-05-17 12:10:57 +020036
37 private static SimpleReporterAdapter stat =
Vinay Vishal57171472018-09-18 20:22:00 +053038 new SimpleReporterAdapter("appserv-tests");
39
40 private static final int MAX_SFSBS = 40;
41
42 private SFSBHome home;
43 private ArrayList sfsbList = new ArrayList();
44
45 private String _sfsbPrefix = "SFSB_" + System.currentTimeMillis() + "_";
46
47 public static void main (String[] args) {
48
49 stat.addDescription("passivateactivate");
50 Client client = new Client(args);
51 System.out.println("[passivateactivateClient] doTest()...");
52 client.doTest();
53 System.out.println("[passivateactivateClient] DONE doTest()...");
54 stat.printSummary("passivateactivate");
David Matějčekf4dc06a2021-05-17 12:10:57 +020055 }
56
Vinay Vishal57171472018-09-18 20:22:00 +053057 public Client (String[] args) {
58 }
David Matějčekf4dc06a2021-05-17 12:10:57 +020059
Vinay Vishal57171472018-09-18 20:22:00 +053060 public void doTest() {
David Matějčekf4dc06a2021-05-17 12:10:57 +020061 initSFSBList(); //create SFSBs
Vinay Vishal57171472018-09-18 20:22:00 +053062 accessSFSB(); //access the SFBS
David Matějčekf4dc06a2021-05-17 12:10:57 +020063 removeTest();
Vinay Vishal57171472018-09-18 20:22:00 +053064 }
65
66 private void initSFSBList() {
67 System.out.println("[passivateactivateClient] Inside init....");
68 try {
69 Context ic = new InitialContext();
70 Object objref = ic.lookup("java:comp/env/ejb/SFSB");
71 home = (SFSBHome)PortableRemoteObject.narrow
72 (objref, SFSBHome.class);
73 for (int i=0; i < MAX_SFSBS; i++) {
74
75 //Creating these many SFSBs will cause passivation
76 SFSB sfsb = (SFSB) home.create(_sfsbPrefix + i);
77 sfsb.createSFSBChild();
78 sfsbList.add(sfsb);
79 }
80 System.out.println("[passivateactivate] Initalization done");
81 stat.addStatus("ejbclient initSFSBList", stat.PASS);
82 } catch(Exception e) {
83 e.printStackTrace();
84 //stat.addStatus("ejbclient localEntityGetEJBObject(-)" , stat.PASS);
85 System.out.println("[passivateactivateClient] Exception in init....");
86 e.printStackTrace();
87 stat.addStatus("ejbclient initSFSBList", stat.FAIL);
88 }
89 }
90
91 public void accessSFSB() {
92 try {
David Matějčekf4dc06a2021-05-17 12:10:57 +020093 System.out.println("Waiting for 10 seconds before accessing...");
94 for (int i=0; i<10; i++) {
95 System.out.println("" + (10 - i) + " seconds left...");
96 try {
97 Thread.currentThread().sleep(1*1000);
98 } catch (Exception ex) {
99 }
100 }
Vinay Vishal57171472018-09-18 20:22:00 +0530101
102 boolean passed = true;
103 for (int i=0; i < MAX_SFSBS; i++) {
104 SFSB sfsb = (SFSB) sfsbList.get(i);
105 String sfsbName = _sfsbPrefix+i;
David Matějčekf4dc06a2021-05-17 12:10:57 +0200106 String retrievedName = sfsb.getName();
107
Vinay Vishal57171472018-09-18 20:22:00 +0530108 boolean sessionCtxTest = sfsb.checkSessionContext();
109 boolean initialCtxTest = sfsb.checkInitialContext();
110 boolean entityHomeTest = sfsb.checkEntityHome();
111 boolean entityLocalHomeTest = sfsb.checkEntityLocalHome();
112 boolean entityRemoteTest = sfsb.checkEntityRemoteRef();
113 boolean entityLocalTest = sfsb.checkEntityLocalRef();
114 boolean homeHandleTest = sfsb.checkHomeHandle();
115 boolean handleTest = sfsb.checkHandle();
116 boolean utTest = sfsb.checkUserTransaction();
David Matějčekf4dc06a2021-05-17 12:10:57 +0200117 boolean activationTest = (sfsb.getActivationCount() != 0);
118 boolean passivationTest = (sfsb.getPassivationCount() != 0);
Vinay Vishal57171472018-09-18 20:22:00 +0530119
David Matějčekf4dc06a2021-05-17 12:10:57 +0200120 int actCount = sfsb.getActivationCount();
121 int pasCount = sfsb.getPassivationCount();
Vinay Vishal57171472018-09-18 20:22:00 +0530122
123 System.out.println(sessionCtxTest + "; " + initialCtxTest
124 + "; " + entityHomeTest + "; " + entityLocalHomeTest
125 + "; " + entityRemoteTest + "; " + entityLocalTest
David Matějčekf4dc06a2021-05-17 12:10:57 +0200126 + "; " + homeHandleTest + "; " + handleTest
Vinay Vishal57171472018-09-18 20:22:00 +0530127 + "; " + utTest
David Matějčekf4dc06a2021-05-17 12:10:57 +0200128 + "; " + activationTest + " (" + actCount + ")"
129 + "; " + passivationTest + " (" + pasCount + ")"
130 );
Vinay Vishal57171472018-09-18 20:22:00 +0530131
132 passed = sessionCtxTest && initialCtxTest
133 && entityHomeTest && entityLocalHomeTest
134 && entityRemoteTest && entityLocalTest
135 && homeHandleTest && handleTest && utTest
David Matějčekf4dc06a2021-05-17 12:10:57 +0200136 && activationTest && passivationTest;
Vinay Vishal57171472018-09-18 20:22:00 +0530137
138 if (! passed) {
139 break;
140 }
141
David Matějčekf4dc06a2021-05-17 12:10:57 +0200142 sfsb.sleepForSeconds(2);
Vinay Vishal57171472018-09-18 20:22:00 +0530143 }
144
145 if (passed) {
146 stat.addStatus("ejbclient accessSFSB", stat.PASS);
147 } else {
148 stat.addStatus("ejbclient accessSFSB", stat.FAIL);
149 }
150
151 for (int i=0; i < MAX_SFSBS; i++) {
152 SFSB sfsb = (SFSB) sfsbList.get(i);
153 String sfsbName = _sfsbPrefix+i;
154
155 sfsb.makeStateNonSerializable();
David Matějčekf4dc06a2021-05-17 12:10:57 +0200156 }
Vinay Vishal57171472018-09-18 20:22:00 +0530157
David Matějčekf4dc06a2021-05-17 12:10:57 +0200158 //Creating these many SFSBs should force passivation of the above
159 // non-serializable beans
Vinay Vishal57171472018-09-18 20:22:00 +0530160 for (int i=0; i < MAX_SFSBS; i++) {
David Matějčekf4dc06a2021-05-17 12:10:57 +0200161 home.create(_sfsbPrefix + (i+1)*1000);
162 }
Vinay Vishal57171472018-09-18 20:22:00 +0530163
David Matějčekf4dc06a2021-05-17 12:10:57 +0200164 System.out.println("Waiting for 10 seconds for passivation to complete...");
Vinay Vishal57171472018-09-18 20:22:00 +0530165
David Matějčekf4dc06a2021-05-17 12:10:57 +0200166 for (int i=0; i<10; i++) {
167 System.out.println("" + (10 - i) + " seconds left...");
168 try {
169 Thread.currentThread().sleep(1*1000);
170 } catch (Exception ex) {
171 }
172 }
Vinay Vishal57171472018-09-18 20:22:00 +0530173
174 for (int i=0; i < MAX_SFSBS; i++) {
175 SFSB sfsb = (SFSB) sfsbList.get(i);
176 String sfsbName = _sfsbPrefix+i;
177
178 try {
David Matějčekf4dc06a2021-05-17 12:10:57 +0200179 System.out.print("Expecting exception for: " + sfsbName);
180 String nm = sfsb.getName();
181 System.out.println("ERROR. Didn't get expected exception. "
182 + "Got: " + nm);
183 passed = false;
184 break;
185 } catch (Exception ex) {
186 System.out.println("[**Got Exception**]");
187 }
188 }
189 if (passed) {
190 stat.addStatus("ejbclient non-serializable-state", stat.PASS);
191 } else {
192 stat.addStatus("ejbclient non-serializable-state", stat.FAIL);
193 }
Vinay Vishal57171472018-09-18 20:22:00 +0530194 } catch (Exception ex) {
195 stat.addStatus("ejbclient accessSFSB", stat.FAIL);
196
197 }
198 }
199
200
201 public void removeTest() {
David Matějčekf4dc06a2021-05-17 12:10:57 +0200202 SFSB sfsb = null;
203 try {
204 String myName = "_2_" + _sfsbPrefix + "_2_";
205 sfsb = (SFSB) home.create(myName);
206 String retrievedName = sfsb.getName();
207 boolean nameOK = myName.equalsIgnoreCase(retrievedName);
208 boolean gotException = false;
Vinay Vishal57171472018-09-18 20:22:00 +0530209
210 // Test BMT SFSB remove called in a transaction
David Matějčekf4dc06a2021-05-17 12:10:57 +0200211 jakarta.transaction.UserTransaction ut = (jakarta.transaction.UserTransaction)
Vinay Vishal57171472018-09-18 20:22:00 +0530212 (new InitialContext()).lookup("java:comp/UserTransaction");
213 ut.begin();
David Matějčekf4dc06a2021-05-17 12:10:57 +0200214 sfsb.remove();
Vinay Vishal57171472018-09-18 20:22:00 +0530215 ut.commit();
David Matějčekf4dc06a2021-05-17 12:10:57 +0200216 try {
217 sfsb.getName();
218 gotException = false; //Expecting an exception
219 } catch (Exception ex) {
220 gotException = true;
221 }
Vinay Vishal57171472018-09-18 20:22:00 +0530222
David Matějčekf4dc06a2021-05-17 12:10:57 +0200223 String resultStr = "(" + nameOK + " @@@ " + gotException + ")";
Vinay Vishal57171472018-09-18 20:22:00 +0530224 if (nameOK && gotException) {
225 stat.addStatus("ejbclient removeTest " + resultStr, stat.PASS);
226 } else {
227 System.err.println("======> FAIL because: nameOK: " + nameOK + " gotException: " + gotException);
David Matějčekf4dc06a2021-05-17 12:10:57 +0200228 stat.addStatus("ejbclient removeTest " + resultStr, stat.FAIL);
Vinay Vishal57171472018-09-18 20:22:00 +0530229 }
230
231 } catch (Exception ex) {
232 ex.printStackTrace();
233 stat.addStatus("ejbclient removeTest", stat.FAIL);
234 }
235 }
236
237} //Client{}