KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > findermethod > HomeInterfaceFindMethodExceptionMatch


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.findermethod;
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  * Entity beans home interface find<METHOD> method exceptions match test.
39  *
40  * The following are the requirements for the enterprise Bean's home interface
41  * find<METHOD> method signature:
42  *
43  * An Entity Bean's home interface defines one or more find<METHOD>(...)
44  * methods.
45  *
46  * All the exceptions defined in the throws clause of an ejbFind<METHOD>(...)
47  * method of the enterprise Bean class must be defined in the throws clause of
48  * the matching find<METHOD>(...) method of the home interface.
49  *
50  */

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

71     public Result check(EjbDescriptor descriptor) {
72
73     result = getInitializedResult();
74     compName = getVerifierContext().getComponentNameConstructor();
75     boolean oneFailed = false;
76     if (descriptor instanceof EjbEntityDescriptor) {
77         String JavaDoc persistence =
78         ((EjbEntityDescriptor)descriptor).getPersistenceType();
79         if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistence)) {
80         if(descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName())) {
81             oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(),descriptor);
82         }
83         if(oneFailed == false) {
84             if(descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName())) {
85             oneFailed = commonToBothInterfaces(descriptor.getLocalHomeClassName(),descriptor);
86             }
87         }
88
89         if (oneFailed) {
90             result.setStatus(result.FAILED);
91         } else {
92             result.setStatus(result.PASSED);
93         }
94    
95         return result;
96         } else { //if (CONTAINER_PERSISTENCE.equals(persistence))
97
result.addNaDetails(smh.getLocalString
98                    ("tests.componentNameConstructor",
99                     "For [ {0} ]",
100                     new Object JavaDoc[] {compName.toString()}));
101         result.notApplicable(smh.getLocalString
102                      (getClass().getName() + ".notApplicable2",
103                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
104                       new Object JavaDoc[] {EjbEntityDescriptor.BEAN_PERSISTENCE,descriptor.getName(),persistence}));
105         return result;
106         }
107         
108     } else {
109         result.addNaDetails(smh.getLocalString
110                    ("tests.componentNameConstructor",
111                     "For [ {0} ]",
112                     new Object JavaDoc[] {compName.toString()}));
113         result.notApplicable(smh.getLocalString
114                  (getClass().getName() + ".notApplicable",
115                   "[ {0} ] expected {1} bean, but called with {2} bean.",
116                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
117         return result;
118     }
119     }
120
121    /**
122      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
123      * @param descriptor the Enterprise Java Bean deployment descriptor
124      * @param home for the Home interface of the Ejb.
125      * @param interfaceType determines the type of interface (remote/local)
126      * @return boolean the results for this assertion i.e if a test has failed or not
127      */

128
129     private boolean commonToBothInterfaces(String JavaDoc home, EjbDescriptor descriptor) {
130     boolean oneFailed = false;
131     int ejbFinderMethodLoopCounter = 0;
132     // RULE: entity home interface are only allowed to have find<METHOD>
133
// methods which match ejbfind<METHOD>, and exceptions match Bean's
134
try {
135         Context context = getVerifierContext();
136         ClassLoader JavaDoc jcl = context.getClassLoader();
137         Class JavaDoc c = Class.forName(home, false, getVerifierContext().getClassLoader());
138         Method methods[] = c.getDeclaredMethods();
139         Class JavaDoc methodReturnType;
140         Class JavaDoc [] methodParameterTypes;
141         Class JavaDoc [] methodExceptionTypes;
142         Class JavaDoc [] ejbFinderMethodExceptionTypes;
143         Class JavaDoc [] ejbFinderMethodParameterTypes;
144         boolean ejbFinderFound = false;
145         boolean signaturesMatch = false;
146         boolean exceptionsMatch = false;
147         
148         
149         for (int i=0; i< methods.length; i++) {
150         if (methods[i].getName().startsWith("find")) {
151             // clear these from last time thru loop
152
ejbFinderFound = false;
153             signaturesMatch = false;
154             exceptionsMatch = false;
155             // retrieve the EJB Class Methods
156
Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
157             // start do while loop here....
158
do {
159             Method [] ejbFinderMethods = EJBClass.getDeclaredMethods();
160             // find matching "ejbFind<METHOD>" in bean class
161
for (int z=0; z< ejbFinderMethods.length; z++) {
162                 if (ejbFinderMethods[z].getName().startsWith("ejbFind")) {
163                 // check rest of string to see if findAccount matches
164
// ejbFindAccount
165
if (methods[i].getName().toUpperCase().equals
166                     (ejbFinderMethods[z].getName().toUpperCase().substring(3))) {
167                     // found one, see if it matches same number and types
168
// of arguments, exceptions too,
169

170                     ejbFinderFound = true;
171                     methodParameterTypes = methods[i].getParameterTypes();
172                     ejbFinderMethodParameterTypes = ejbFinderMethods[z].getParameterTypes();
173                     if (Arrays.equals(methodParameterTypes,ejbFinderMethodParameterTypes)) {
174                     signaturesMatch = true;
175                     
176                     methodExceptionTypes = methods[i].getExceptionTypes();
177                     ejbFinderMethodExceptionTypes = ejbFinderMethods[z].getExceptionTypes();
178                     
179                     // All the exceptions defined in the throws clause of the
180
// matching ejbFind method of the
181
// enterprise Bean class must be included in the throws
182
// clause of the matching find method of the home interface
183
// including findByPrimaryKey, this home interface
184
// find method must define a superset of all the
185
// exceptions thrown in the ejbFind method of the bean class
186
// so there may not be a 1-1 mapping of exceptions
187
// also, for all ejbFind/find combo's any unchecked
188
// exceptions thrown by the ejbFind<METHOD> in the bean
189
// class doesn't need to be thrown in the corresponding
190
// find<METHOD> of the home interface , these unchecked
191
// exceptions "subclass of RuntimeException" i.e
192
// out of memory exception are handled by the container,
193
// who throws a Runtime exception to the appropriate
194
// instance/object
195

196                     if (RmiIIOPUtils.isEjbFindMethodExceptionsSubsetOfFindMethodExceptions(ejbFinderMethodExceptionTypes,methodExceptionTypes)) {
197                         exceptionsMatch = true;
198                         // used to display output below
199
ejbFinderMethodLoopCounter = z;
200                         break;
201                     }
202                     } // method params match
203
} // check rest of string to see if findAccount
204
// matches ejbFindAccount
205
} // found ejbFind<METHOD>
206
} // for all the business methods within the bean class, loop
207

208             //report for this particular find method found in home interface
209
//if we know that ejbFinderFound got set to true in the above
210
// loop, check other booleans, otherwise skip test, set status
211
// to FAILED below
212

213             // now display the appropriate results for this particular find
214
// method
215
if (ejbFinderFound && signaturesMatch && exceptionsMatch) {
216                 result.addGoodDetails(smh.getLocalString
217                    ("tests.componentNameConstructor",
218                     "For [ {0} ]",
219                     new Object JavaDoc[] {compName.toString()}));
220                 result.addGoodDetails(smh.getLocalString
221                           (getClass().getName() + ".debug1",
222                            "For Home Interface [ {0} ] Method [ {1} ]",
223                            new Object JavaDoc[] {c.getName(),methods[i].getName()}));
224                 result.addGoodDetails(smh.getLocalString
225                           (getClass().getName() + ".passed",
226                            "The corresponding [ {0} ] method with matching exceptions was found.",
227                            new Object JavaDoc[] {ejbFinderMethods[ejbFinderMethodLoopCounter].getName()}));
228             } else if (ejbFinderFound && signaturesMatch && !exceptionsMatch) {
229                 logger.log(Level.FINE, getClass().getName() + ".debug1",
230                         new Object JavaDoc[] {c.getName(),methods[i].getName()});
231                 logger.log(Level.FINE, getClass().getName() + ".debug3",
232                         new Object JavaDoc[] {"ejb"+methods[i].getName().toUpperCase().substring(0,1)+methods[i].getName().substring(1)});
233                 logger.log(Level.FINE, getClass().getName() + ".debug2");
234
235             } else if (ejbFinderFound && !signaturesMatch) {
236                 logger.log(Level.FINE, getClass().getName() + ".debug1",
237                         new Object JavaDoc[] {c.getName(),methods[i].getName()});
238                 logger.log(Level.FINE, getClass().getName() + ".debug4",
239                         new Object JavaDoc[] {"ejb"+methods[i].getName().toUpperCase().substring(0,1)+methods[i].getName().substring(1)});
240                 logger.log(Level.FINE, getClass().getName() + ".debug2");
241
242             }
243             
244             } while (((EJBClass = EJBClass.getSuperclass()) != null) && (!(ejbFinderFound && signaturesMatch && exceptionsMatch)));
245             
246             
247             if (!ejbFinderFound && !signaturesMatch && !exceptionsMatch) {
248             oneFailed = true;
249             result.addErrorDetails(smh.getLocalString
250                    ("tests.componentNameConstructor",
251                     "For [ {0} ]",
252                     new Object JavaDoc[] {compName.toString()}));
253             result.addErrorDetails(smh.getLocalString
254                            (getClass().getName() + ".debug1",
255                         "For Home Interface [ {0} ] Method [ {1} ]",
256                         new Object JavaDoc[] {c.getName(),methods[i].getName()}));
257             result.addErrorDetails(smh.getLocalString
258                            (getClass().getName() + ".failed",
259                         "Error: No corresponding [ {0} ] method with matching signatures was found." ,
260                         new Object JavaDoc[] {"ejb"+methods[i].getName().toUpperCase().substring(0,1)+methods[i].getName().substring(1)}));
261             } // end of reporting for this particular 'find' method
262
} // if the home interface found a "find" method
263
} // for all the methods within the home interface class, loop
264
return oneFailed;
265         
266     } catch (ClassNotFoundException JavaDoc e) {
267         Verifier.debug(e);
268         result.addErrorDetails(smh.getLocalString
269                    ("tests.componentNameConstructor",
270                     "For [ {0} ]",
271                     new Object JavaDoc[] {compName.toString()}));
272         result.failed(smh.getLocalString
273               (getClass().getName() + ".failedException",
274                "Error: Home interface [ {0} ] or EJB class [ {1} ] does not exist or is not loadable within bean [ {2} ]",
275                new Object JavaDoc[] {home, descriptor.getEjbClassName(),descriptor.getName()}));
276         return oneFailed;
277     }
278     }
279 }
280
Popular Tags