Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 1 | /* |
Gaurav Gupta | 3655507 | 2020-03-26 14:10:23 +0530 | [diff] [blame] | 2 | * Copyright (c) 2017, 2020 Oracle and/or its affiliates. All rights reserved. |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package com.sun.s1asdev.ejb.sfsb.passivateactivate.client; |
| 18 | |
| 19 | import java.util.ArrayList; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 20 | import javax.naming.*; |
Gaurav Gupta | 3655507 | 2020-03-26 14:10:23 +0530 | [diff] [blame] | 21 | import jakarta.jms.*; |
Gaurav Gupta | ed5e731 | 2020-05-01 07:54:26 +0530 | [diff] [blame] | 22 | import jakarta.ejb.*; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 23 | import javax.rmi.PortableRemoteObject; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 24 | |
| 25 | import com.sun.s1asdev.ejb.sfsb.passivateactivate.ejb.SFSBHome; |
| 26 | import com.sun.s1asdev.ejb.sfsb.passivateactivate.ejb.SFSB; |
| 27 | |
| 28 | import com.sun.ejte.ccl.reporter.SimpleReporterAdapter; |
| 29 | |
| 30 | public 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 36 | |
| 37 | private static SimpleReporterAdapter stat = |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 38 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 55 | } |
| 56 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 57 | public Client (String[] args) { |
| 58 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 59 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 60 | public void doTest() { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 61 | initSFSBList(); //create SFSBs |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 62 | accessSFSB(); //access the SFBS |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 63 | removeTest(); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 64 | } |
| 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 93 | 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 Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 101 | |
| 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 106 | String retrievedName = sfsb.getName(); |
| 107 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 108 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 117 | boolean activationTest = (sfsb.getActivationCount() != 0); |
| 118 | boolean passivationTest = (sfsb.getPassivationCount() != 0); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 119 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 120 | int actCount = sfsb.getActivationCount(); |
| 121 | int pasCount = sfsb.getPassivationCount(); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 122 | |
| 123 | System.out.println(sessionCtxTest + "; " + initialCtxTest |
| 124 | + "; " + entityHomeTest + "; " + entityLocalHomeTest |
| 125 | + "; " + entityRemoteTest + "; " + entityLocalTest |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 126 | + "; " + homeHandleTest + "; " + handleTest |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 127 | + "; " + utTest |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 128 | + "; " + activationTest + " (" + actCount + ")" |
| 129 | + "; " + passivationTest + " (" + pasCount + ")" |
| 130 | ); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 131 | |
| 132 | passed = sessionCtxTest && initialCtxTest |
| 133 | && entityHomeTest && entityLocalHomeTest |
| 134 | && entityRemoteTest && entityLocalTest |
| 135 | && homeHandleTest && handleTest && utTest |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 136 | && activationTest && passivationTest; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 137 | |
| 138 | if (! passed) { |
| 139 | break; |
| 140 | } |
| 141 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 142 | sfsb.sleepForSeconds(2); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 143 | } |
| 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 156 | } |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 157 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 158 | //Creating these many SFSBs should force passivation of the above |
| 159 | // non-serializable beans |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 160 | for (int i=0; i < MAX_SFSBS; i++) { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 161 | home.create(_sfsbPrefix + (i+1)*1000); |
| 162 | } |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 163 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 164 | System.out.println("Waiting for 10 seconds for passivation to complete..."); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 165 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 166 | 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 Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 173 | |
| 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 179 | 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 Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 194 | } catch (Exception ex) { |
| 195 | stat.addStatus("ejbclient accessSFSB", stat.FAIL); |
| 196 | |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | |
| 201 | public void removeTest() { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 202 | 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 Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 209 | |
| 210 | // Test BMT SFSB remove called in a transaction |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 211 | jakarta.transaction.UserTransaction ut = (jakarta.transaction.UserTransaction) |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 212 | (new InitialContext()).lookup("java:comp/UserTransaction"); |
| 213 | ut.begin(); |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 214 | sfsb.remove(); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 215 | ut.commit(); |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 216 | try { |
| 217 | sfsb.getName(); |
| 218 | gotException = false; //Expecting an exception |
| 219 | } catch (Exception ex) { |
| 220 | gotException = true; |
| 221 | } |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 222 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 223 | String resultStr = "(" + nameOK + " @@@ " + gotException + ")"; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 224 | 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ček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 228 | stat.addStatus("ejbclient removeTest " + resultStr, stat.FAIL); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | } catch (Exception ex) { |
| 232 | ex.printStackTrace(); |
| 233 | stat.addStatus("ejbclient removeTest", stat.FAIL); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | } //Client{} |