KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
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  * create<Method> method tests
36  * Entity beans home interface create method match bean class test.
37  *
38  * The following are the requirements for the Entity Bean's home interface
39  * signature:
40  *
41  * An Entity Bean's home interface defines zero or more create(...) methods.
42  *
43  * Each create method must be named ``create'', and it must match one of the
44  * ejbCreate methods defined in the enterprise Bean class. The matching
45  * ejbCreate method must have the same number and types of arguments.
46  *
47  */

48 public class HomeInterfaceCreateMethodMatchArgs extends EjbTest implements EjbCheck {
49     Result result = null;
50     ComponentNameConstructor compName = null;
51     boolean foundAtLeastOneCreate = false;
52     /**
53      * Entity beans home interface create method match bean class test.
54      *
55      * The following are the requirements for the Entity Bean's home interface
56      * signature:
57      *
58      * An Entity Bean's home interface defines zero 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 EjbEntityDescriptor) {
74         boolean oneFailed = false;
75       
76         // RULE: Entity home interface are only allowed to have create
77
// methods which match ejbCreate,
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() + ".debug3",
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           
100         return result;
101
102     } else {
103         result.addNaDetails(smh.getLocalString
104                           ("tests.componentNameConstructor",
105                            "For [ {0} ]",
106                            new Object JavaDoc[] {compName.toString()}));
107         result.notApplicable(smh.getLocalString
108                  (getClass().getName() + ".notApplicable",
109                   "[ {0} ] expected {1} bean, but called with {2} bean.",
110                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
111         return result;
112     }
113     }
114
115     /**
116      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
117      * @param descriptor the Enterprise Java Bean deployment descriptor
118      * @param home for the Home interface of the Ejb.
119      * @return boolean the results for this assertion i.e if a test has failed or not
120      */

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