KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > 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.entity.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.EjbEntityDescriptor;
28 import java.lang.ClassLoader JavaDoc;
29 import com.sun.enterprise.tools.verifier.tests.*;
30 import java.util.*;
31 import java.util.logging.Level JavaDoc;
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  * create<Method> method tests
39  * Entity beans home interface method exceptions match test.
40  *
41  * The following are the requirements for the enterprise Bean's home interface
42  * signature:
43  *
44  * An Entity Bean's home interface defines one or more create(...) methods.
45  *
46  * All the exceptions defined in the throws clause of an ejbCreate method of
47  * the enterprise Bean class must be defined in the throws clause of the
48  * matching create method of the home interface.
49  *
50  */

51 public class HomeInterfaceCreateMethodExceptionMatch extends EjbTest implements EjbCheck {
52
53     Result result = null;
54     ComponentNameConstructor compName = null;
55     boolean foundAtLeastOneCreate = false;
56     /**
57      * Entity 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      * An Entity 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      boolean oneFailed = false;
77      if (descriptor instanceof EjbEntityDescriptor) {
78          oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(),descriptor.getLocalHomeClassName(),descriptor);
79          if (!foundAtLeastOneCreate) {
80          result.addNaDetails(smh.getLocalString
81                      ("tests.componentNameConstructor",
82                       "For [ {0} ]",
83                       new Object JavaDoc[] {compName.toString()}));
84          result.addNaDetails(smh.getLocalString
85                      (getClass().getName() + ".debug4",
86                       "In Home Interface ",
87                       new Object JavaDoc[] {}));
88          result.addNaDetails(smh.getLocalString
89                      (getClass().getName() + ".notApplicable1",
90                       "No create method was found, test not applicable." ));
91          result.setStatus(result.NOT_APPLICABLE);
92          } else {
93          if (oneFailed) {
94              result.setStatus(result.FAILED);
95          } else {
96              result.setStatus(result.PASSED);
97          }
98          }
99         return result;
100         
101     } else {
102         result.addNaDetails(smh.getLocalString
103                           ("tests.componentNameConstructor",
104                            "For [ {0} ]",
105                            new Object JavaDoc[] {compName.toString()}));
106         result.notApplicable(smh.getLocalString
107                  (getClass().getName() + ".notApplicable",
108                   "[ {0} ] expected {1} bean, but called with {2} bean.",
109                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
110         return result;
111     }
112     }
113
114     /**
115      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
116      * @param descriptor the Enterprise Java Bean deployment descriptor
117      * @param home for the Home interface of the Ejb.
118      * @param interfaceType remote/local
119      * @return boolean the results for this assertion i.e if a test has failed or not
120      */

121
122     private boolean commonToBothInterfaces(String JavaDoc component, String JavaDoc local,EjbDescriptor descriptor) {
123       boolean oneFailed = false;
124       int ejbCreateMethodLoopCounter = 0;
125       // RULE: entity home interface are only allowed to have create
126
// methods which match ejbCreate, and exceptions match Bean's
127
try {
128       Class JavaDoc methodReturnType;
129       Class JavaDoc [] methodParameterTypes;
130       Class JavaDoc [] methodExceptionTypes;
131       Class JavaDoc [] ejbCreateMethodExceptionTypes;
132       Class JavaDoc [] ejbCreateMethodParameterTypes;
133       boolean ejbCreateFound = false;
134       boolean exceptionsMatch = false;
135       Vector<Method> createMethodSuffix = new Vector<Method>();
136
137       if (component != null) {
138           Class JavaDoc home = Class.forName(component, false, getVerifierContext().getClassLoader());
139           Method [] homeMethods = home.getDeclaredMethods();
140           
141           for (int i = 0; i < homeMethods.length; i++) {
142           // The method name must start with create.
143
if (homeMethods[i].getName().startsWith("create")) {
144               createMethodSuffix.addElement( (Method)homeMethods[i]);
145               foundAtLeastOneCreate = true;
146           }
147           }
148       }
149       if (local != null) {
150           Class JavaDoc home = Class.forName(local, false, getVerifierContext().getClassLoader());
151           Method [] homeMethods = home.getDeclaredMethods();
152           
153           for (int i = 0; i < homeMethods.length; i++) {
154           // The method name must start with create.
155
if (homeMethods[i].getName().startsWith("create")) {
156               createMethodSuffix.addElement( (Method)homeMethods[i]);
157               foundAtLeastOneCreate = true;
158           }
159           }
160       }
161       if (foundAtLeastOneCreate == false)
162           return false;
163       Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
164       
165       do {
166           Method [] methods = EJBClass.getDeclaredMethods();
167           // find matching "ejbCreate" in bean class
168
for (int i = 0; i < methods.length; i++) {
169           ejbCreateFound = false;
170           exceptionsMatch = false;
171           if (methods[i].getName().startsWith("ejbCreate")) {
172               String JavaDoc matchSuffix = methods[i].getName().substring(9);
173               for (int k = 0; k < createMethodSuffix.size(); k++) {
174               if (matchSuffix.equals(((Method)(createMethodSuffix.elementAt(k))).getName().substring(6))) {
175                   // clear these from last time thru loop
176
// retrieve the EJB Class Methods
177
methodParameterTypes = ((Method)(createMethodSuffix.elementAt(k))).getParameterTypes();
178                   ejbCreateMethodParameterTypes = methods[i].getParameterTypes();
179                   if (Arrays.equals(methodParameterTypes,ejbCreateMethodParameterTypes)) {
180                   ejbCreateFound = true;
181                   methodExceptionTypes = ((Method)(createMethodSuffix.elementAt(k))).getExceptionTypes();
182                   ejbCreateMethodExceptionTypes = methods[i].getExceptionTypes();
183                   // methodExceptionTypes needs to be
184
// a superset of all the possible exceptions thrown in
185
// ejbCreateMethodExceptionTypes
186
// All the exceptions defined in the throws clause of the
187
// matching ejbCreate and ejbPostCreate methods of the
188
// enterprise Bean class must be included in the throws
189
// clause of the matching create method of the home interface
190
// (i.e the set of exceptions defined for the create method
191
// must be a superset of the union of exceptions defined for
192
// the ejbCreate and ejbPostCreate methods)
193
if (RmiIIOPUtils.isEjbFindMethodExceptionsSubsetOfFindMethodExceptions(ejbCreateMethodExceptionTypes,methodExceptionTypes)) {
194                       exceptionsMatch = true;
195                       // used to display output below
196
ejbCreateMethodLoopCounter = k;
197                       break;
198                   }
199                   } // method params match
200
}
201               }
202               //report for this particular create method found in home interface
203
//if we know that ejbCreateFound got set to true in the above
204
// loop, check other booleans, otherwise skip test, set status
205
// to FAILED below
206

207               // now display the appropriate results for this particular create
208
// method
209
if (ejbCreateFound && exceptionsMatch) {
210               result.addGoodDetails(smh.getLocalString
211                           ("tests.componentNameConstructor",
212                            "For [ {0} ]",
213                            new Object JavaDoc[] {compName.toString()}));
214               result.addGoodDetails(smh.getLocalString
215                         (getClass().getName() + ".debug1",
216                          "For Home Interface Method [ {0} ]",
217                          new Object JavaDoc[] {((Method)(createMethodSuffix.elementAt(ejbCreateMethodLoopCounter))).getName()}));
218               result.addGoodDetails(smh.getLocalString
219                         (getClass().getName() + ".passed",
220                          "The corresponding [ {0} ] method with matching exceptions was found.",
221                          new Object JavaDoc[] {methods[i].getName()}));
222               } else if (ejbCreateFound && !exceptionsMatch) {
223               oneFailed = true;
224               result.addErrorDetails(smh.getLocalString
225                           ("tests.componentNameConstructor",
226                            "For [ {0} ]",
227                            new Object JavaDoc[] {compName.toString()}));
228               result.addErrorDetails(smh.getLocalString
229                          (getClass().getName() + ".debug1",
230                           "For Home Interface Method [ {0} ]",
231                           new Object JavaDoc[] {((Method)(createMethodSuffix.elementAt(ejbCreateMethodLoopCounter))).getName()}));
232               result.addErrorDetails(smh.getLocalString
233                          (getClass().getName() + ".failed",
234                           "Error: No corresponding ejbCreate() method was found, where the exceptions defined by method ejbCreate() are defined within matching create() method."));
235               logger.log(Level.FINE, getClass().getName() + ".debug1",
236                       new Object JavaDoc[] {((Method)(createMethodSuffix.elementAt(ejbCreateMethodLoopCounter))).getName()});
237               logger.log(Level.FINE, getClass().getName() + ".debug3",
238                       new Object JavaDoc[] {methods[i].getName(),((Method)(createMethodSuffix.elementAt(ejbCreateMethodLoopCounter))).getName()});
239               break;
240               } // end of reporting for this particular 'create' method
241
}
242           if (oneFailed == true)
243               break;
244           }
245       } while (((EJBClass = EJBClass.getSuperclass()) != null) && (!(ejbCreateFound && exceptionsMatch)));
246      
247       return oneFailed;
248       } catch (ClassNotFoundException JavaDoc e) {
249       Verifier.debug(e);
250       result.addErrorDetails(smh.getLocalString
251                  ("tests.componentNameConstructor",
252                   "For [ {0} ]",
253                   new Object JavaDoc[] {compName.toString()}));
254       result.failed(smh.getLocalString
255             (getClass().getName() + ".failedException",
256              "Error: Home (Local/Remote) interface or bean class [ {0} ] does not exist or is not loadable within bean [ {1} ]",
257              new Object JavaDoc[] {descriptor.getEjbClassName(),descriptor.getName()}));
258       return oneFailed;
259       }
260     }
261 }
262
Popular Tags