KickJava   Java API By Example, From Geeks To Geeks.

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


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;
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 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.RmiIIOPUtils;
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
34
35 /**
36  * Entity beans home interface method exceptions match test.
37  *
38  * The following are the requirements for the enterprise Bean's home interface
39  * signature:
40  *
41  * An Entity Bean's home interface defines one or more create(...) methods.
42  *
43  * All the exceptions defined in the throws clause of an ejbPostCreate method
44  * of the enterprise Bean class must be defined in the throws clause of the
45  * matching create method of the home interface.
46  *
47  */

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

69     public Result check(EjbDescriptor descriptor) {
70
71         result = getInitializedResult();
72     compName = getVerifierContext().getComponentNameConstructor();
73
74     if (descriptor instanceof EjbEntityDescriptor) {
75         boolean oneFailed = false;
76       
77         // RULE: entity home interface are only allowed to have create
78
// methods which match ejbPostCreate, and exceptions match Bean's
79

80         if(descriptor.getHomeClassName() != null) {
81         oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(),descriptor);
82         }
83         if(oneFailed == false) {
84         if(descriptor.getLocalHomeClassName() != null) {
85             oneFailed = commonToBothInterfaces(descriptor.getLocalHomeClassName(),descriptor);
86         }
87         }
88
89         if (!foundAtLeastOneCreate) {
90         result.addNaDetails(smh.getLocalString
91                        ("tests.componentNameConstructor",
92                     "For [ {0} ]",
93                     new Object JavaDoc[] {compName.toString()}));
94         result.addNaDetails(smh.getLocalString
95                     (getClass().getName() + ".debug3",
96                      "In Home Interface ",
97                      new Object JavaDoc[] {}));
98         result.addNaDetails(smh.getLocalString
99                     (getClass().getName() + ".notApplicable1",
100                      "No create method was found, test not applicable." ));
101         result.setStatus(result.NOT_APPLICABLE);
102         } else {
103         if (oneFailed) {
104             result.setStatus(result.FAILED);
105         } else {
106             result.setStatus(result.PASSED);
107         }
108         }
109         return result;
110         
111     } else {
112         result.addNaDetails(smh.getLocalString
113                        ("tests.componentNameConstructor",
114                     "For [ {0} ]",
115                     new Object JavaDoc[] {compName.toString()}));
116         result.notApplicable(smh.getLocalString
117                  (getClass().getName() + ".notApplicable",
118                   "[ {0} ] expected {1} bean, but called with {2} bean.",
119                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
120         return result;
121     }
122     }
123
124     /**
125      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
126      * @param descriptor the Enterprise Java Bean deployment descriptor
127      * @param home for the Home Interface of the Ejb
128      * @return boolean the results for this assertion i.e if a test has failed or not
129      */

130
131     private boolean commonToBothInterfaces(String JavaDoc home, EjbDescriptor descriptor) {
132     int ejbPostCreateMethodLoopCounter = 0;
133     boolean oneFailed = false;
134     foundAtLeastOneCreate = false;
135     try {
136         Class JavaDoc c = Class.forName(home, false, getVerifierContext().getClassLoader());
137         Class JavaDoc methodReturnType;
138         Class JavaDoc [] methodParameterTypes;
139         Class JavaDoc [] methodExceptionTypes;
140         Class JavaDoc [] ejbPostCreateMethodExceptionTypes;
141         Class JavaDoc [] ejbPostCreateMethodParameterTypes;
142         boolean ejbPostCreateFound = false;
143         boolean signaturesMatch = false;
144         boolean exceptionsMatch = false;
145         boolean createExists = false;
146
147         Method [] homeMethods = c.getDeclaredMethods();
148         Vector<Method> createMethodSuffix = new Vector<Method>();
149         for (int i = 0; i < homeMethods.length; i++) {
150         // The method name must start with create.
151
if (homeMethods[i].getName().startsWith("create")) {
152             foundAtLeastOneCreate = true;
153             createMethodSuffix.addElement( (Method)homeMethods[i]);
154         }
155         }
156         if(foundAtLeastOneCreate == false)
157         return false;
158         
159         Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
160         do {
161         Method [] methods = EJBClass.getDeclaredMethods();
162         // find matching "ejbCreate" in bean class
163
for (int i = 0; i < methods.length; i++) {
164             ejbPostCreateFound = false;
165             signaturesMatch = false;
166             exceptionsMatch = false;
167             Method [] ejbPostCreateMethods = EJBClass.getDeclaredMethods();
168             //ejbPostCreateMethods1 = null;
169
//ejbPostCreateMethods1 = ejbPostCreateMethods;
170
ejbPostCreateMethodLoopCounter = 0;
171             // Method [] ejbPostCreateMethods = EJBClass.getDeclaredMethods();
172
if (methods[i].getName().startsWith("ejbPostCreate")) {
173                 ejbPostCreateFound = true;
174             String JavaDoc matchSuffix = methods[i].getName().substring(13);
175             for (int k = 0; k < createMethodSuffix.size(); k++) {
176                 if (matchSuffix.equals(((Method)(createMethodSuffix.elementAt(k))).getName().substring(6))) {
177                 // clear these from last time thru loop
178
// retrieve the EJB Class Methods
179
methodParameterTypes = ((Method)(createMethodSuffix.elementAt(k))).getParameterTypes();
180                 ejbPostCreateMethodParameterTypes = methods[i].getParameterTypes();
181                 if (Arrays.equals(methodParameterTypes,ejbPostCreateMethodParameterTypes)) {
182                     signaturesMatch = true;
183                     methodExceptionTypes = ((Method)(createMethodSuffix.elementAt(k))).getExceptionTypes();
184                     ejbPostCreateMethodExceptionTypes = methods[i].getExceptionTypes();
185                     // methodExceptionTypes needs to be
186
// a superset of all the possible exceptions thrown in
187
// ejbPostCreateMethodExceptionTypes
188
// All the exceptions defined in the throws clause of the
189
// matching ejbCreate and ejbPostCreate methods of the
190
// enterprise Bean class must be included in the throws
191
// clause of the matching create method of the home interface
192
// (i.e the set of exceptions defined for the create method
193
// must be a superset of the union of exceptions defined for
194
// the ejbCreate and ejbPostCreate methods)
195
if (RmiIIOPUtils.isEjbFindMethodExceptionsSubsetOfFindMethodExceptions(ejbPostCreateMethodExceptionTypes,methodExceptionTypes)) {
196                     exceptionsMatch = true;
197                     // used to display output below
198
ejbPostCreateMethodLoopCounter = k;
199                     break;
200                     }
201                 } // method params match
202
} // found ejbPostCreate
203
} // for all the business methods within the bean class, loop
204

205             //report for this particular create method found in home interface
206
//if we know that ejbPostCreateFound got set to true in the above
207
// loop, check other booleans, otherwise skip test, set status
208
// to FAILED below
209

210             // now display the appropriate results for this particular create
211
// method
212
if (ejbPostCreateFound && signaturesMatch && exceptionsMatch) {
213                 result.addGoodDetails(smh.getLocalString
214                           ("tests.componentNameConstructor",
215                            "For [ {0} ]",
216                            new Object JavaDoc[] {compName.toString()}));
217                 result.addGoodDetails(smh.getLocalString
218                           (getClass().getName() + ".debug1",
219                            "For Home Interface [ {0} ] Method [ {1} ]",
220                            new Object JavaDoc[] {home,((Method)(createMethodSuffix.elementAt(ejbPostCreateMethodLoopCounter))).getName()}));
221                 result.addGoodDetails(smh.getLocalString
222                           (getClass().getName() + ".passed",
223                            "The corresponding [ {0} ] method with matching exceptions was found in [ {1} ].",
224                            new Object JavaDoc[] {methods[i].getName(), EJBClass.getName()}));
225             } else if (ejbPostCreateFound && signaturesMatch && !exceptionsMatch) {
226                 result.addErrorDetails(smh.getLocalString
227                         ("tests.componentNameConstructor",
228                          "For [ {0} ]",
229                          new Object JavaDoc[] {compName.toString()}));
230                 result.addErrorDetails(smh.getLocalString
231                      (getClass().getName() + ".debug1",
232                       "For Home Interface [ {0} ] Method [ {1} ]",
233                       new Object JavaDoc[] {home,((Method)(createMethodSuffix.elementAt(ejbPostCreateMethodLoopCounter))).getName()}));
234                 result.failed(smh.getLocalString
235                      (getClass().getName() + ".notApplicableDebug",
236                       "A corresponding [ {0} ] method was found in [ {1} ], but the exceptions did not match.",
237                       new Object JavaDoc[] {methods[i].getName(),EJBClass.getName()}));
238                 oneFailed = true;
239                 break;
240             } else if (ejbPostCreateFound && !signaturesMatch) {
241                 result.addErrorDetails(smh.getLocalString
242                            ("tests.componentNameConstructor",
243                             "For [ {0} ]",
244                             new Object JavaDoc[] {compName.toString()}));
245                 result.addErrorDetails(smh.getLocalString
246                      (getClass().getName() + ".debug3",
247                       "For Home Interface ",
248                       new Object JavaDoc[] {}));
249                 result.failed(smh.getLocalString
250                      (getClass().getName() + ".notApplicableDebug2",
251                       "A corresponding [ {0} ] method was found in [ {1} ], but the parameters did not match.",
252                       new Object JavaDoc[] {methods[i].getName(),EJBClass.getName()}));
253                 oneFailed = true;
254                 break;
255             }
256             }
257         }
258         if (oneFailed == true)
259             break;
260         } while (((EJBClass = EJBClass.getSuperclass()) != null) && (!(ejbPostCreateFound && signaturesMatch && exceptionsMatch)));
261         return oneFailed;
262     } catch (ClassNotFoundException JavaDoc e) {
263         Verifier.debug(e);
264         result.addErrorDetails(smh.getLocalString
265                    ("tests.componentNameConstructor",
266                     "For [ {0} ]",
267                     new Object JavaDoc[] {compName.toString()}));
268         result.failed(smh.getLocalString
269               (getClass().getName() + ".failedException",
270                "Error: Home interface [ {0} ] or EJB class [ {1} ] does not exist or is not loadable within bean [ {2} ]",
271                new Object JavaDoc[] {home, descriptor.getEjbClassName(), descriptor.getName()}));
272         return oneFailed;
273     }
274     }
275 }
276
Popular Tags