Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved. |
| 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 | /* |
| 18 | * Client.java |
| 19 | * |
| 20 | * Created on February 21, 2003, 3:20 PM |
| 21 | */ |
| 22 | |
| 23 | import java.util.*; |
| 24 | |
| 25 | import javax.naming.*; |
Gaurav Gupta | ed5e731 | 2020-05-01 07:54:26 +0530 | [diff] [blame] | 26 | import jakarta.ejb.*; |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 27 | import javax.rmi.PortableRemoteObject; |
| 28 | |
| 29 | import com.sun.ejte.ccl.reporter.SimpleReporterAdapter; |
| 30 | |
| 31 | import java.rmi.*; |
| 32 | |
| 33 | import test.*; |
| 34 | |
| 35 | /** |
| 36 | * This class is used to test Read-Only CMP beans by accessing |
| 37 | * field 'shortName' before and after jdbc update of the same table. |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 38 | * It also tests that a non-DFG field 'description' is loaded |
| 39 | * correctly for Read-Only beans when bean is accessed after both |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 40 | * findByPrimaryKey and custom finders. |
| 41 | * The test is executed for CMP1.1 bean (A1RO) and CMP2.x bean (A2RO). |
| 42 | * |
| 43 | * @author mvatkina |
| 44 | */ |
| 45 | public class Client { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 46 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 47 | private static SimpleReporterAdapter stat = |
| 48 | new SimpleReporterAdapter("appserv-tests"); |
| 49 | |
| 50 | private static A1Home a1home = null; |
| 51 | private static A2Home a2home = null; |
| 52 | private static TestHome thome = null; |
| 53 | private static Test tbean = null; |
| 54 | |
| 55 | public static void main(String[] args) { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 56 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 57 | try { |
| 58 | System.out.println("START"); |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 59 | stat.addDescription("robeans"); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 60 | |
| 61 | lookupBeans(); |
| 62 | tbean = thome.create(); |
| 63 | |
| 64 | testA1(); |
| 65 | testA2(); |
| 66 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 67 | stat.addStatus("ejbclient robeans", stat.PASS); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 68 | System.out.println("FINISH"); |
| 69 | |
| 70 | } catch (Exception ex) { |
| 71 | System.err.println("Caught an exception:"); |
| 72 | ex.printStackTrace(); |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 73 | stat.addStatus("ejbclient robeans", stat.FAIL); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 74 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 75 | stat.printSummary("robeans"); |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 76 | } |
| 77 | |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 78 | /** Run CMP1.1 test. |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 79 | * getShortName() must return the same value. |
| 80 | * getDescription() must return non-null value. |
| 81 | */ |
| 82 | private static void testA1() throws FinderException, RemoteException { |
| 83 | tbean.insertValues("A1RO"); |
| 84 | A1 a1bean = a1home.findByPrimaryKey("A1RO"); |
| 85 | String name = a1bean.getShortName(); |
| 86 | |
| 87 | verifyDescription(a1bean.getDescription(), "A1RO", true); |
| 88 | |
| 89 | tbean.updateValues("A1RO"); |
| 90 | verifyShortName(name, a1bean.getShortName(), "A1RO"); |
| 91 | |
| 92 | // Find another bean. |
| 93 | Collection c = a1home.findByShortName("A1RO1"); |
| 94 | if (c.size() != 1) { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 95 | System.out.println("ERROR: 1.1 findByShortName returned wrong number of records: " |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 96 | + c.size()); |
| 97 | } |
| 98 | a1bean = (A1)c.iterator().next(); |
| 99 | |
| 100 | verifyDescription(a1bean.getDescription(), "A1RO", false); |
| 101 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 102 | |
| 103 | /** Run CMP2.x test. |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 104 | * getShortName() must return the same value. |
| 105 | * getDescription() must return non-null value. |
| 106 | */ |
| 107 | private static void testA2() throws FinderException, RemoteException { |
| 108 | tbean.insertValues("A2RO"); |
| 109 | A2 a2bean = a2home.findByPrimaryKey("A2RO"); |
| 110 | String name = a2bean.getShortName(); |
| 111 | |
| 112 | verifyDescription(a2bean.getDescription(), "A2RO", true); |
| 113 | |
| 114 | tbean.updateValues("A2RO"); |
| 115 | verifyShortName(name, a2bean.getShortName(), "A2RO"); |
| 116 | |
| 117 | // Find another bean. |
| 118 | Collection c = a2home.findByShortName("A2RO1"); |
| 119 | if (c.size() != 1) { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 120 | System.out.println("ERROR: 2.x findByShortName returned wrong number of records: " |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 121 | + c.size()); |
| 122 | } |
| 123 | a2bean = (A2)c.iterator().next(); |
| 124 | |
| 125 | verifyDescription(a2bean.getDescription(), "A2RO", false); |
| 126 | } |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 127 | |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 128 | private static void lookupBeans() throws NamingException { |
| 129 | Context initial = new InitialContext(); |
| 130 | Object objref = initial.lookup("java:comp/env/ejb/A1RO"); |
| 131 | a1home = (A1Home)PortableRemoteObject.narrow(objref, test.A1Home.class); |
| 132 | |
| 133 | objref = initial.lookup("java:comp/env/ejb/A2RO"); |
| 134 | a2home = (A2Home)PortableRemoteObject.narrow(objref, test.A2Home.class); |
| 135 | |
| 136 | objref = initial.lookup("java:comp/env/ejb/TestRO"); |
| 137 | thome = (TestHome)PortableRemoteObject.narrow(objref, test.TestHome.class); |
| 138 | |
| 139 | } |
| 140 | |
| 141 | /** Verifies that the value of the 'shortName' field didn't change. |
| 142 | * @param name1 the value of the first access of the field. |
| 143 | * @param name2 the value of the second access of the field. |
| 144 | * @param beanName the name of the bean to test. |
| 145 | */ |
| 146 | private static void verifyShortName(String name1, String name2, String beanName) { |
| 147 | if (name1.equals(name2)) { |
| 148 | System.out.println(beanName + " shortName OK: " + name1); |
| 149 | } else { |
| 150 | System.out.println(beanName + " FAILED: " + name1 + "-" + name2); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** Verifies that the value of the 'description' field had been loaded. |
| 155 | * @param description the value of the field. |
| 156 | * @param beanName the name of the bean to test. |
| 157 | * @param isFindByPK true if verification is done after findByPrimaryKey |
| 158 | * call. |
| 159 | */ |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 160 | private static void verifyDescription(String description, String beanName, |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 161 | boolean isFindByPK) { |
| 162 | |
| 163 | if (description != null) { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 164 | System.out.println(beanName + " non-DFG field OK" |
| 165 | + ((isFindByPK)? "" : " after custom finder") + ": " |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 166 | + description); |
| 167 | } else { |
David Matějček | f4dc06a | 2021-05-17 12:10:57 +0200 | [diff] [blame^] | 168 | System.out.println(beanName + " FAILED: non-DFG field is NULL" |
Vinay Vishal | 5717147 | 2018-09-18 20:22:00 +0530 | [diff] [blame] | 169 | + ((isFindByPK)? "" : " after custom finder")); |
| 170 | } |
| 171 | } |
| 172 | } |