blob: bc19026f9595d827871329eab92b4fc080146f76 [file] [log] [blame]
Vinay Vishal57171472018-09-18 20:22:00 +05301/*
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
23import java.util.*;
24
25import javax.naming.*;
Gaurav Guptaed5e7312020-05-01 07:54:26 +053026import jakarta.ejb.*;
Vinay Vishal57171472018-09-18 20:22:00 +053027import javax.rmi.PortableRemoteObject;
28
29import com.sun.ejte.ccl.reporter.SimpleReporterAdapter;
30
31import java.rmi.*;
32
33import 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čekf4dc06a2021-05-17 12:10:57 +020038 * It also tests that a non-DFG field 'description' is loaded
39 * correctly for Read-Only beans when bean is accessed after both
Vinay Vishal57171472018-09-18 20:22:00 +053040 * findByPrimaryKey and custom finders.
41 * The test is executed for CMP1.1 bean (A1RO) and CMP2.x bean (A2RO).
42 *
43 * @author mvatkina
44 */
45public class Client {
David Matějčekf4dc06a2021-05-17 12:10:57 +020046
Vinay Vishal57171472018-09-18 20:22:00 +053047 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čekf4dc06a2021-05-17 12:10:57 +020056
Vinay Vishal57171472018-09-18 20:22:00 +053057 try {
58 System.out.println("START");
David Matějčekf4dc06a2021-05-17 12:10:57 +020059 stat.addDescription("robeans");
Vinay Vishal57171472018-09-18 20:22:00 +053060
61 lookupBeans();
62 tbean = thome.create();
63
64 testA1();
65 testA2();
66
David Matějčekf4dc06a2021-05-17 12:10:57 +020067 stat.addStatus("ejbclient robeans", stat.PASS);
Vinay Vishal57171472018-09-18 20:22:00 +053068 System.out.println("FINISH");
69
70 } catch (Exception ex) {
71 System.err.println("Caught an exception:");
72 ex.printStackTrace();
David Matějčekf4dc06a2021-05-17 12:10:57 +020073 stat.addStatus("ejbclient robeans", stat.FAIL);
Vinay Vishal57171472018-09-18 20:22:00 +053074 }
David Matějčekf4dc06a2021-05-17 12:10:57 +020075 stat.printSummary("robeans");
Vinay Vishal57171472018-09-18 20:22:00 +053076 }
77
David Matějčekf4dc06a2021-05-17 12:10:57 +020078 /** Run CMP1.1 test.
Vinay Vishal57171472018-09-18 20:22:00 +053079 * 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čekf4dc06a2021-05-17 12:10:57 +020095 System.out.println("ERROR: 1.1 findByShortName returned wrong number of records: "
Vinay Vishal57171472018-09-18 20:22:00 +053096 + c.size());
97 }
98 a1bean = (A1)c.iterator().next();
99
100 verifyDescription(a1bean.getDescription(), "A1RO", false);
101 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200102
103 /** Run CMP2.x test.
Vinay Vishal57171472018-09-18 20:22:00 +0530104 * 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čekf4dc06a2021-05-17 12:10:57 +0200120 System.out.println("ERROR: 2.x findByShortName returned wrong number of records: "
Vinay Vishal57171472018-09-18 20:22:00 +0530121 + c.size());
122 }
123 a2bean = (A2)c.iterator().next();
124
125 verifyDescription(a2bean.getDescription(), "A2RO", false);
126 }
David Matějčekf4dc06a2021-05-17 12:10:57 +0200127
Vinay Vishal57171472018-09-18 20:22:00 +0530128 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čekf4dc06a2021-05-17 12:10:57 +0200160 private static void verifyDescription(String description, String beanName,
Vinay Vishal57171472018-09-18 20:22:00 +0530161 boolean isFindByPK) {
162
163 if (description != null) {
David Matějčekf4dc06a2021-05-17 12:10:57 +0200164 System.out.println(beanName + " non-DFG field OK"
165 + ((isFindByPK)? "" : " after custom finder") + ": "
Vinay Vishal57171472018-09-18 20:22:00 +0530166 + description);
167 } else {
David Matějčekf4dc06a2021-05-17 12:10:57 +0200168 System.out.println(beanName + " FAILED: non-DFG field is NULL"
Vinay Vishal57171472018-09-18 20:22:00 +0530169 + ((isFindByPK)? "" : " after custom finder"));
170 }
171 }
172}