KickJava   Java API By Example, From Geeks To Geeks.

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


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.ejbcreatemethod;
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  * Entity beans home interface create method match bean class test.
36  *
37  * The following are the requirements for the Entity Bean's home interface
38  * signature:
39  *
40  * A Entity 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 EjbCreateMatchesCreate extends EjbTest implements EjbCheck {
48     Result result = null;
49     ComponentNameConstructor compName = null;
50     boolean foundAtLeastOneCreate = false;
51
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      * A Entity 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 EjbEntityDescriptor) {
74         boolean oneFailed = false;
75         // RULE: entity home interface are only allowed to have create
76
// methods which match ejbCreate,
77
oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(),descriptor.getLocalHomeClassName(),descriptor);
78         if (oneFailed) {
79         result.setStatus(result.FAILED);
80         } else {
81                 if(foundAtLeastOneCreate == false)
82                 {
83                     result.setStatus(Result.NOT_APPLICABLE);
84                 }
85                 else
86             result.setStatus(result.PASSED);
87         }
88  
89         return result;
90           
91     } else {
92         result.addNaDetails(smh.getLocalString
93                        ("tests.componentNameConstructor",
94                     "For [ {0} ]",
95                     new Object JavaDoc[] {compName.toString()}));
96         result.notApplicable(smh.getLocalString
97                  (getClass().getName() + ".notApplicable",
98                   "[ {0} ] expected {1} bean, but called with {2} bean.",
99                   new Object JavaDoc[] {getClass(),"Session","Entity"}));
100         return result;
101     }
102     }
103
104     /**
105      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
106      * @param descriptor the Enterprise Java Bean deployment descriptor
107      * @param home for the Home interface of the Ejb.
108      * @return boolean the results for this assertion i.e if a test has failed or not
109      */

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