KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > session > createmethod > HomeInterfaceCreateMethodExceptionMatch


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.createmethod;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import com.sun.enterprise.deployment.EjbDescriptor;
27 import com.sun.enterprise.deployment.EjbSessionDescriptor;
28 import com.sun.enterprise.deployment.MethodDescriptor;
29 import java.lang.ClassLoader JavaDoc;
30 import com.sun.enterprise.tools.verifier.tests.*;
31 import java.util.*;
32 import java.lang.reflect.*;
33 import com.sun.enterprise.tools.verifier.*;
34 import com.sun.enterprise.tools.verifier.tests.ejb.RmiIIOPUtils;
35 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
36
37 /**
38  * Session beans home interface method exceptions match test.
39  *
40  * The following are the requirements for the enterprise Bean's home interface
41  * signature:
42  *
43  * A Session Bean's home interface defines one or more create(...) methods.
44  *
45  * All the exceptions defined in the throws clause of an ejbCreate method of
46  * the enterprise Bean class must be defined in the throws clause of the
47  * matching create method of the home interface.
48  *
49  */

50 public class HomeInterfaceCreateMethodExceptionMatch extends EjbTest implements EjbCheck {
51     Result result = null;
52     ComponentNameConstructor compName = null;
53     boolean foundAtLeastOneCreate = false;
54
55
56     /**
57      * Session beans home interface method exceptions match test.
58      *
59      * The following are the requirements for the enterprise Bean's home interface
60      * signature:
61      *
62      * A Session Bean's home interface defines one or more create(...) methods.
63      *
64      * All the exceptions defined in the throws clause of an ejbCreate method of
65      * the enterprise Bean class must be defined in the throws clause of the
66      * matching create method of the home interface.
67      *
68      * @param descriptor the Enterprise Java Bean deployment descriptor
69      *
70      * @return <code>Result</code> the results for this assertion
71      */

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

140
141     private boolean commonToBothInterfaces(String JavaDoc home, String JavaDoc local,EjbSessionDescriptor descriptor) {
142     boolean oneFailed = false;
143     int ejbCreateMethodLoopCounter = 0;
144     // RULE: session home interface are only allowed to have create
145
// methods which match ejbCreate, and exceptions match Bean's
146
try {
147         Context context = getVerifierContext();
148         ClassLoader JavaDoc jcl = context.getClassLoader();
149         Class JavaDoc methodReturnType;
150         Class JavaDoc [] methodParameterTypes;
151         Class JavaDoc [] methodExceptionTypes;
152         Class JavaDoc [] ejbCreateMethodExceptionTypes;
153         Class JavaDoc [] ejbCreateMethodParameterTypes;
154         boolean ejbCreateFound = false;
155         boolean exceptionsMatch = false;
156         Vector<Method> createMethodSuffix = new Vector<Method>();
157
158         if (home != null) {
159         Class JavaDoc c = Class.forName(home, false, getVerifierContext().getClassLoader());
160         Method [] homeMethods = c.getDeclaredMethods();
161         for (int i = 0; i < homeMethods.length; i++) {
162             // The method name must start with create.
163
if (homeMethods[i].getName().startsWith("create")) {
164             createMethodSuffix.addElement( (Method)homeMethods[i]);
165             foundAtLeastOneCreate = true;
166             }
167         }
168         }
169         if (local != null) {
170         Class JavaDoc c = Class.forName(local, false, getVerifierContext().getClassLoader());
171         Method [] homeMethods = c.getDeclaredMethods();
172         for (int i = 0; i < homeMethods.length; i++) {
173             // The method name must start with create.
174
if (homeMethods[i].getName().startsWith("create")) {
175             createMethodSuffix.addElement( (Method)homeMethods[i]);
176             foundAtLeastOneCreate = true;
177             }
178         }
179         }
180         if (foundAtLeastOneCreate == false) {
181         result.addErrorDetails(smh.getLocalString
182                        ("tests.componentNameConstructor",
183                     "For [ {0} ]",
184                     new Object JavaDoc[] {compName.toString()}));
185         result.failed(smh.getLocalString
186                   (getClass().getName() + ".failedException2",
187                    "Error: no create<Method> method exists!",
188                    new Object JavaDoc[] {}));
189         return true;
190         }
191         Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
192         
193         do {
194         Method [] methods = EJBClass.getDeclaredMethods();
195         // find matching "ejbCreate" in bean class
196
for (int i = 0; i < methods.length; i++) {
197             ejbCreateFound = false;
198             exceptionsMatch = false;
199             if (methods[i].getName().startsWith("ejbCreate")) {
200             String JavaDoc matchSuffix = methods[i].getName().substring(9);
201             for (int k = 0; k < createMethodSuffix.size(); k++) {
202                 if (matchSuffix.equals(((Method)(createMethodSuffix.elementAt(k))).getName().substring(6))) {
203                 // clear these from last time thru loop
204
// retrieve the EJB Class Methods
205
methodParameterTypes = ((Method)(createMethodSuffix.elementAt(k))).getParameterTypes();
206                 ejbCreateMethodParameterTypes = methods[i].getParameterTypes();
207                 if (Arrays.equals(methodParameterTypes,ejbCreateMethodParameterTypes)) {
208                     ejbCreateFound = true;
209                     methodExceptionTypes = ((Method)(createMethodSuffix.elementAt(k))).getExceptionTypes();
210                     ejbCreateMethodExceptionTypes = methods[i].getExceptionTypes();
211                     if (RmiIIOPUtils.isEjbFindMethodExceptionsSubsetOfFindMethodExceptions(ejbCreateMethodExceptionTypes,methodExceptionTypes)) {
212                     exceptionsMatch = true;
213                     // used to display output below
214
ejbCreateMethodLoopCounter = k;
215                     break;
216                     }
217                 } // method params match
218
} // found ejbCreate
219
}
220             
221             //report for this particular create method found in home interface
222
//if we know that ejbCreateFound got set to true in the above
223
// loop, check other booleans, otherwise skip test, set status
224
// to FAILED below
225

226             // now display the appropriate results for this particular create
227
// method
228
if (ejbCreateFound && exceptionsMatch) {
229                 result.addGoodDetails(smh.getLocalString
230                        ("tests.componentNameConstructor",
231                     "For [ {0} ]",
232                     new Object JavaDoc[] {compName.toString()}));
233                 result.addGoodDetails(smh.getLocalString
234                           (getClass().getName() + ".debug1",
235                            "For Home Interface Method [ {0} ]",
236                            new Object JavaDoc[] {((Method)(createMethodSuffix.elementAt(ejbCreateMethodLoopCounter))).getName()}));
237                 result.addGoodDetails(smh.getLocalString
238                           (getClass().getName() + ".passed",
239                            "The corresponding [ {0} ] method with matching exceptions was found.",
240                            new Object JavaDoc[] {methods[i].getName()}));
241             }
242             
243             if (ejbCreateFound && !exceptionsMatch) {
244                 oneFailed = true;
245                 result.addErrorDetails(smh.getLocalString
246                        ("tests.componentNameConstructor",
247                     "For [ {0} ]",
248                     new Object JavaDoc[] {compName.toString()}));
249                 result.addErrorDetails(smh.getLocalString
250                            (getClass().getName() + ".debug1",
251                             "For Home Interface Method [ {0} ]",
252                             new Object JavaDoc[] {((Method)(createMethodSuffix.elementAt(ejbCreateMethodLoopCounter))).getName()}));
253                 result.addErrorDetails(smh.getLocalString
254                            (getClass().getName() + ".failed",
255                             "Error: A corresponding [ {0} ] method was found, but the exceptions defined by method [ {1} ] are not defined within matching create() method.",
256                             new Object JavaDoc[] {"ejb"+methods[i].getName().toUpperCase().substring(0,1)+methods[i].getName().substring(1),"ejb"+methods[i].getName().toUpperCase().substring(0,1)+methods[i].getName().substring(1)}));
257             } else if (!ejbCreateFound) {
258                 // set status to FAILED, 'cause there is not even an
259
// ejbCreate method to begin with, regardless of its parameters
260
oneFailed = true;
261                 result.addErrorDetails(smh.getLocalString
262                        ("tests.componentNameConstructor",
263                     "For [ {0} ]",
264                     new Object JavaDoc[] {compName.toString()}));
265                 result.addErrorDetails(smh.getLocalString
266                            (getClass().getName() + ".debug1",
267                             "For Home Interface Method [ {0} ]",
268                             new Object JavaDoc[] {((Method)(createMethodSuffix.elementAt(ejbCreateMethodLoopCounter))).getName()}));
269                 result.addErrorDetails(smh.getLocalString
270                            (getClass().getName() + ".failed1",
271                             "Error: No corresponding ejbCreate method was found." ));
272                 break;
273             } // end of reporting for this particular 'create' method
274
} // if the home interface found a "create" method
275
} // for all the methods within the home interface class, loop
276
if (oneFailed == true)
277             break;
278         } while (((EJBClass = EJBClass.getSuperclass()) != null) && (!(ejbCreateFound && exceptionsMatch)));
279         return oneFailed;
280     } catch (ClassNotFoundException JavaDoc e) {
281         Verifier.debug(e);
282         result.addErrorDetails(smh.getLocalString
283                    ("tests.componentNameConstructor",
284                     "For [ {0} ]",
285                     new Object JavaDoc[] {compName.toString()}));
286         result.failed(smh.getLocalString
287               (getClass().getName() + ".failedException",
288                "Error: Home (Remote/Local) interface or Bean class [ {0} ] does not exist or is not loadable within bean [ {1} ]",
289                new Object JavaDoc[] {descriptor.getEjbClassName(),descriptor.getName()}));
290         return oneFailed;
291     }
292     }
293 }
294
Popular Tags