KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > session > ejbcreatemethod > EjbCreateMatchesCreate


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.verifier.tests.ejb.session.ejbcreatemethod;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import com.sun.enterprise.deployment.*;
27 import java.lang.ClassLoader JavaDoc;
28 import com.sun.enterprise.tools.verifier.tests.*;
29 import java.util.*;
30 import java.lang.reflect.*;
31 import com.sun.enterprise.tools.verifier.*;
32 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
33
34 /**
35  * Session beans home interface create method match bean class test.
36  *
37  * The following are the requirements for the Session Bean's home interface
38  * signature:
39  *
40  * A Session Bean's home interface defines one or more create(...) methods.
41  *
42  * Each create method must be named ``create'', and it must match one of the
43  * ejbCreate methods defined in the enterprise Bean class. The matching
44  * ejbCreate method must have the same number and types of arguments.
45  *
46  */

47 public class EjbCreateMatchesCreate extends EjbTest implements EjbCheck {
48     Result result = null;
49     ComponentNameConstructor compName = null;
50     boolean foundAtLeastOneCreate = false;
51
52     /**
53      * Session beans home interface create method match bean class test.
54      *
55      * The following are the requirements for the Session Bean's home interface
56      * signature:
57      *
58      * A Session Bean's home interface defines one or more create(...) methods.
59      *
60      * Each create method must be named ``create'', and it must match one of the
61      * ejbCreate methods defined in the enterprise Bean class. The matching
62      * ejbCreate method must have the same number and types of arguments.
63      *
64      * @param descriptor the Enterprise Java Bean deployment descriptor
65      *
66      * @return <code>Result</code> the results for this assertion
67      */

68     public Result check(EjbDescriptor descriptor) {
69
70     result = getInitializedResult();
71     compName = getVerifierContext().getComponentNameConstructor();
72
73     if (descriptor instanceof EjbSessionDescriptor) {
74             
75             if (((descriptor.getHomeClassName() == null) || "".equals(descriptor.getHomeClassName())) &&
76                 ((descriptor.getLocalHomeClassName() == null) || "".equals(descriptor.getLocalHomeClassName()))) {
77
78                 if (implementsEndpoints(descriptor)) {
79                     result.addNaDetails(smh.getLocalString
80                         ("tests.componentNameConstructor",
81                         "For [ {0} ]",
82                          new Object JavaDoc[] {compName.toString()}));
83                     result.notApplicable(smh.getLocalString
84                        ("com.sun.enterprise.tools.verifier.tests.ejb.webservice.notapp",
85                        "Not Applicable because, EJB [ {0} ] implements a Service Endpoint Interface.",
86                        new Object JavaDoc[] {compName.toString()}));
87                     result.setStatus(result.NOT_APPLICABLE);
88                  return result;
89                  }
90             }
91
92         boolean oneFailed = false;
93         // RULE: session home interface are only allowed to have create
94
// methods which match ejbCreate,
95
oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(),descriptor.getLocalHomeClassName(),descriptor);
96         if (oneFailed) {
97         result.setStatus(result.FAILED);
98         } else {
99         result.setStatus(result.PASSED);
100         }
101  
102         return result;
103           
104     } else {
105         result.addNaDetails(smh.getLocalString
106                        ("tests.componentNameConstructor",
107                     "For [ {0} ]",
108                     new Object JavaDoc[] {compName.toString()}));
109         result.notApplicable(smh.getLocalString
110                  (getClass().getName() + ".notApplicable",
111                   "[ {0} ] expected {1} bean, but called with {2} bean.",
112                   new Object JavaDoc[] {getClass(),"Session","Entity"}));
113         return result;
114     }
115     }
116
117     /**
118      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
119      * @param descriptor the Enterprise Java Bean deployment descriptor
120      * @param home for the Home interface of the Ejb.
121      * @return boolean the results for this assertion i.e if a test has failed or not
122      */

123
124
125     private boolean commonToBothInterfaces(String JavaDoc remote, String JavaDoc local,EjbDescriptor descriptor) {
126     boolean oneFailed = false;
127     try {
128         Context context = getVerifierContext();
129         ClassLoader JavaDoc jcl = context.getClassLoader();
130         Class JavaDoc [] methodParameterTypes;
131         Class JavaDoc [] businessMethodParameterTypes;
132         boolean signaturesMatch = false;
133     
134         boolean found = false;
135         Vector<Method> createMethodSuffix = new Vector<Method>();
136         
137         if (local != null) {
138         Class JavaDoc localhome = Class.forName(local, false, getVerifierContext().getClassLoader());
139         Method [] localhomeMethods = localhome.getDeclaredMethods();
140         for (int i = 0; i < localhomeMethods.length; i++) {
141             // The method name must start with create.
142
if (localhomeMethods[i].getName().startsWith("create")) {
143             createMethodSuffix.addElement( (Method)localhomeMethods[i]);
144             foundAtLeastOneCreate = true;
145             
146             }
147         }
148         }
149         if (remote != null) {
150         Class JavaDoc home = Class.forName(remote, false, getVerifierContext().getClassLoader());
151         Method [] homeMethods = home.getDeclaredMethods();
152         for (int i = 0; i < homeMethods.length; i++) {
153             // The method name must start with create.
154
if (homeMethods[i].getName().startsWith("create")) {
155             createMethodSuffix.addElement( (Method)homeMethods[i]);
156             foundAtLeastOneCreate = true;
157             
158             }
159         }
160         }
161         if (foundAtLeastOneCreate == false) {
162         result.addErrorDetails(smh.getLocalString
163                        ("tests.componentNameConstructor",
164                     "For [ {0} ]",
165                     new Object JavaDoc[] {compName.toString()}));
166         result.failed(smh.getLocalString
167                   (getClass().getName() + ".failedException2",
168                    "Error: no create<Method> method exists!",
169                    new Object JavaDoc[] {}));
170         return true;
171         }
172         
173         Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
174         // start do while loop here....
175
Method [] methods = EJBClass.getMethods();
176             // find matching "ejbCreate" in bean class
177
for (int k = 0; k < createMethodSuffix.size(); k++) {
178                 for (int j = 0; j < methods.length; j++) {
179                     found = false;
180                     if (methods[j].getName().startsWith("ejbCreate")) {
181                         found = true;
182                         String JavaDoc matchSuffix = methods[j].getName().substring(9);
183                         signaturesMatch = false;
184                         if (matchSuffix.equals(((Method)(createMethodSuffix.elementAt(k))).getName().substring(6))) {
185                             methodParameterTypes = ((Method)(createMethodSuffix.elementAt(k))).getParameterTypes();
186                             businessMethodParameterTypes = methods[j].getParameterTypes();
187                             if (Arrays.equals(methodParameterTypes,businessMethodParameterTypes)) {
188                                 signaturesMatch = true;
189                                 // now display the appropriate results for this particular ejbCreate
190
// method
191
result.addGoodDetails(smh.getLocalString
192                                 ("tests.componentNameConstructor",
193                                 "For [ {0} ]",
194                                 new Object JavaDoc[] {compName.toString()}));
195                                 result.addGoodDetails(smh.getLocalString
196                                 (getClass().getName() + ".debug1",
197                                 "For Home Interface Method [ {0} ]",
198                                 new Object JavaDoc[] {((Method)(createMethodSuffix.elementAt(k))).getName()}));
199                                 result.addGoodDetails(smh.getLocalString
200                                 (getClass().getName() + ".passed",
201                                 "The corresponding ejbCreate method with matching parameters was found."));
202                                 break;
203                             }
204                         }
205                     }
206                 }
207                 if (signaturesMatch == false) {
208                     oneFailed = true;
209                     result.addErrorDetails(smh.getLocalString
210                     ("tests.componentNameConstructor",
211                     "For [ {0} ]",
212                     new Object JavaDoc[] {compName.toString()}));
213                     result.addErrorDetails(smh.getLocalString
214                     (getClass().getName() + ".debug3",
215                     "For Home Interface",
216                     new Object JavaDoc[] {}));
217                     result.addErrorDetails(smh.getLocalString
218                     (getClass().getName() + ".failed",
219                     "Error: No corresponding ejbCreate<Method> method with matching parameters was found." ));
220                     
221                 }
222             }
223         if (found == false && foundAtLeastOneCreate == true){
224         result.addErrorDetails(smh.getLocalString
225                        ("tests.componentNameConstructor",
226                     "For [ {0} ]",
227                     new Object JavaDoc[] {compName.toString()}));
228         result.failed(smh.getLocalString
229                   (getClass().getName() + ".failedException1",
230                    "Error: ejbCreate<Method> method corresponding to the create<Method> method does not exist!",
231                    new Object JavaDoc[] {}));
232         
233         }
234         if (found == false && foundAtLeastOneCreate == false){
235             result.addErrorDetails(smh.getLocalString
236                        ("tests.componentNameConstructor",
237                     "For [ {0} ]",
238                     new Object JavaDoc[] {compName.toString()}));
239         result.failed(smh.getLocalString
240                      (getClass().getName() + ".failedException2",
241                       "Error: no create<Method> method exists!",
242                    new Object JavaDoc[] {}));
243         }
244         return oneFailed;
245     } catch (ClassNotFoundException JavaDoc e) {
246         Verifier.debug(e);
247         result.addErrorDetails(smh.getLocalString
248                    ("tests.componentNameConstructor",
249                     "For [ {0} ]",
250                     new Object JavaDoc[] {compName.toString()}));
251         result.failed(smh.getLocalString
252               (getClass().getName() + ".failedException",
253                "Error: Home (Local/Remote) interface or bean class [ {0} ] does not exist or is not loadable within bean [ {1} ]",
254                new Object JavaDoc[] {descriptor.getEjbClassName(),descriptor.getName()}));
255         oneFailed = true;
256         return oneFailed;
257     }
258
259     }
260 }
261
Popular Tags