KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > session > 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.session.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  * Session beans home interface create method match bean class test.
36  *
37  * The following are the requirements for the Session Bean's home interface
38  * signature:
39  *
40  * A Session Bean's home interface defines one or more create(...) methods.
41  *
42  * Each create method must be named ``create'', and it must match one of the
43  * ejbCreate methods defined in the enterprise Bean class. The matching
44  * ejbCreate method must have the same number and types of arguments.
45  *
46  */

47 public class HomeInterfaceCreateMethodMatchArgs extends EjbTest implements EjbCheck {
48     Result result = null;
49     ComponentNameConstructor compName = null;
50     boolean foundAtLeastOneCreate = false;
51
52     /**
53      * Session beans home interface create method match bean class test.
54      *
55      * The following are the requirements for the Session Bean's home interface
56      * signature:
57      *
58      * A Session Bean's home interface defines one 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 EjbSessionDescriptor) {
74
75             if (((descriptor.getHomeClassName() == null) || "".equals(descriptor.getHomeClassName())) &&
76                 ((descriptor.getLocalHomeClassName() == null) || "".equals(descriptor.getLocalHomeClassName()))) {
77
78                 if (implementsEndpoints(descriptor)) {
79                     result.addNaDetails(smh.getLocalString
80                         ("tests.componentNameConstructor",
81                         "For [ {0} ]",
82                          new Object JavaDoc[] {compName.toString()}));
83                     result.notApplicable(smh.getLocalString
84                        ("com.sun.enterprise.tools.verifier.tests.ejb.webservice.notapp",
85                        "Not Applicable because, EJB [ {0} ] implements a Service Endpoint Interface.",
86                        new Object JavaDoc[] {compName.toString()}));
87                     result.setStatus(result.NOT_APPLICABLE);
88                  }
89                  else {
90
91                    result.addErrorDetails(smh.getLocalString
92                                    ("tests.componentNameConstructor",
93                                     "For [ {0} ]",
94                                     new Object JavaDoc[] {compName.toString()}));
95                    result.addErrorDetails(smh.getLocalString
96                                     ("com.sun.enterprise.tools.verifier.tests.ejb.webservice.failedhome",
97                                     "Ejb [ {0} ] does not have local or remote Home interfaces",
98                                     new Object JavaDoc[] {descriptor.getEjbClassName()}));
99                    result.setStatus(result.FAILED);
100                  }
101               return result;
102             }
103
104         boolean oneFailed = false;
105         // RULE: session home interface are only allowed to have create
106
// methods which match ejbCreate,
107
oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(),descriptor.getLocalHomeClassName(),descriptor);
108         if (oneFailed) {
109         result.setStatus(result.FAILED);
110         } else {
111         result.setStatus(result.PASSED);
112         }
113  
114         return result;
115           
116     } else {
117         result.addNaDetails(smh.getLocalString
118                        ("tests.componentNameConstructor",
119                     "For [ {0} ]",
120                     new Object JavaDoc[] {compName.toString()}));
121         result.notApplicable(smh.getLocalString
122                  (getClass().getName() + ".notApplicable",
123                   "[ {0} ] expected {1} bean, but called with {2} bean.",
124                   new Object JavaDoc[] {getClass(),"Session","Entity"}));
125         return result;
126     }
127     }
128
129     /**
130      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
131      * @param descriptor the Enterprise Java Bean deployment descriptor
132      * @param home for the Home interface of the Ejb.
133      * @return boolean the results for this assertion i.e if a test has failed or not
134      */

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