KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.*;
27 import java.util.*;
28 import com.sun.enterprise.deployment.*;
29 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
30 import com.sun.enterprise.tools.verifier.*;
31 import java.lang.ClassLoader JavaDoc;
32 import com.sun.enterprise.tools.verifier.tests.*;
33
34 /**
35  * find<METHOD>(...) methods test.
36  *
37  * Home interface contains all find<METHOD>(...) methods declared in the home
38  * interface.
39  *
40  * Each finder method must be named ``find<METHOD>'' (e.g. findLargeAccounts),
41  * and it must match one of the ejbFind<METHOD> methods defined in the
42  * enterprise Bean class (e.g. ejbFindLargeAccounts). The matching
43  * ejbFind<METHOD> method must have the same number and types of arguments.
44  * (Note that the return type may be different.)
45  *
46  */

47 public class HomeInterfaceFindMethodMatch extends EjbTest implements EjbCheck {
48
49     Result result = null;
50     ComponentNameConstructor compName = null;
51
52     /**
53      * find<METHOD>(...) methods test.
54      *
55      * Home interface contains all find<METHOD>(...) methods declared in the home
56      * interface.
57      *
58      * Each finder method must be named ``find<METHOD>'' (e.g. findLargeAccounts),
59      * and it must match one of the ejbFind<METHOD> methods defined in the
60      * enterprise Bean class (e.g. ejbFindLargeAccounts). The matching
61      * ejbFind<METHOD> method must have the same number and types of arguments.
62      * (Note that the return type may be different.)
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         if (descriptor instanceof EjbEntityDescriptor &&
73                 ((EjbEntityDescriptor)descriptor).getPersistenceType().equals(EjbEntityDescriptor.BEAN_PERSISTENCE)) {
74             if(descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName()))
75                 commonToBothInterfaces(descriptor.getHomeClassName(), descriptor.getRemoteClassName(), descriptor);
76
77             if(descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName()))
78                 commonToBothInterfaces(descriptor.getLocalHomeClassName(), descriptor.getLocalClassName(), descriptor);
79
80         } else {
81             addNaDetails(result, compName);
82             result.notApplicable("This test is only applicable entity beans with bean managed persistence.");
83         }
84
85         return result;
86     }
87
88     /**
89      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
90      * @param descriptor the Enterprise Java Bean deployment descriptor
91      * @param home for the Home interface of the Ejb.
92      */

93
94     private void commonToBothInterfaces(String JavaDoc home, String JavaDoc remote, EjbDescriptor descriptor) {
95         Class JavaDoc [] methodParameterTypes;
96         Class JavaDoc [] ejbFinderMethodParameterTypes;
97         int ejbFinderMethodLoopCounter = 0;
98         try {
99             // retrieve the home interface methods
100
Context context = getVerifierContext();
101             ClassLoader JavaDoc jcl = context.getClassLoader();
102             Class JavaDoc homeInterfaceClass = Class.forName(home, false, getVerifierContext().getClassLoader());
103             Class JavaDoc remoteInterfaceClass = Class.forName(remote, false, getVerifierContext().getClassLoader());
104             Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
105             Method [] homeInterfaceMethods = homeInterfaceClass.getMethods();
106             Method [] ejbFinderMethods = EJBClass.getMethods();
107             int z;
108             // Note: this test will be done in the testing on the
109
// Home Interface class. i.e.
110
// also need to check that matching signatures and exceptions exist
111
// between home interface and EJB Class,
112
// i.e.
113
// Each finder method must be named ``find<METHOD>''
114
// (e.g. findLargeAccounts), and it must match one of the
115
// ejbFind<METHOD> methods defined in the enterprise Bean
116
// class (e.g. ejbFindLargeAccounts). The matching ejbFind<METHOD>
117
// method must have the same number and types of arguments.
118
// (Note that the return type may be different.)
119

120             for (int i=0; i< homeInterfaceMethods.length; i++) {
121                 if (homeInterfaceMethods[i].getName().startsWith("find")) {
122                     // clear these from last time thru loop
123
// find matching "ejbFind<METHOD>(...)" in bean class
124
for (z=0; z< ejbFinderMethods.length; z++) {
125                         if (ejbFinderMethods[z].getName().startsWith("ejbFind")) {
126                             // check rest of string to see if findAccount matches
127
// ejbFindAccount
128
if (homeInterfaceMethods[i].getName().toUpperCase().equals
129                                     (ejbFinderMethods[z].getName().toUpperCase().substring(3))) {
130                                 // found one, see if it matches same number and types
131
// of arguments,
132
methodParameterTypes = homeInterfaceMethods[i].getParameterTypes();
133                                 ejbFinderMethodParameterTypes = ejbFinderMethods[z].getParameterTypes();
134
135                                 boolean returnTypeMatch = checkReturnType(homeInterfaceMethods[i], ejbFinderMethods[z],
136                                         remoteInterfaceClass, descriptor);
137
138                                 if (!returnTypeMatch) {
139                                     addErrorDetails(result, compName);
140                                     result.failed(smh.getLocalString
141                                             (getClass().getName() + ".failReturnType",
142                                                     "For Home Interface [ {0} ] Method [ {1} ] return type [ {2} ] ",
143                                                     new Object JavaDoc[] {homeInterfaceClass.getName(), homeInterfaceMethods[i].getName(), homeInterfaceMethods[i].getReturnType().getName()}));
144                                     result.addErrorDetails(smh.getLocalString
145                                             (getClass().getName() + ".failReturnType1",
146                                                     "Error: does not match with return type [ {0} ] of corresponding ejbFind<METHOD>(...).",
147                                                     new Object JavaDoc[] {ejbFinderMethods[z].getReturnType().getName()}));
148
149                                 }
150                                 if (!Arrays.equals(methodParameterTypes,ejbFinderMethodParameterTypes)) {
151
152                                     addErrorDetails(result, compName);
153                                     result.failed(smh.getLocalString
154                                             (getClass().getName() + ".debug1",
155                                                     "For Home Interface [ {0} ] Method [ {1} ]",
156                                                     new Object JavaDoc[] {homeInterfaceClass.getName(),homeInterfaceMethods[i].getName()}));
157                                     result.addErrorDetails(smh.getLocalString
158                                             (getClass().getName() + ".failed",
159                                                     "Error: A corresponding [ {0} ] method was found, but the parameters did not match.",
160                                                     new Object JavaDoc[] {"ejb"+homeInterfaceMethods[i].getName().toUpperCase().substring(0,1)+homeInterfaceMethods[i].getName().substring(1)}));
161                                 }
162
163                                 // used to display output below
164
ejbFinderMethodLoopCounter = z;
165                                 break;
166                             }// if check to see if findAccount matches ejbFindAccount
167
} // if check to see if startsWith("ejbFind")
168
}// for all the business methods within the bean class, loop
169

170                     if (z==ejbFinderMethods.length) {
171                         // set status to FAILED, 'cause there is not even an
172
// find method to begin with, regardless of its parameters
173
addErrorDetails(result, compName);
174                         result.failed(smh.getLocalString
175                                 (getClass().getName() + ".debug1",
176                                         "For Home Interface [ {0} ] Method [ {1} ]",
177                                         new Object JavaDoc[] {homeInterfaceClass.getName(),homeInterfaceMethods[i].getName()}));
178                         result.addErrorDetails(smh.getLocalString
179                                 (getClass().getName() + ".failed1",
180                                         "Error: No corresponding ejbFind<METHOD>(...) method was found." ));
181                     }
182
183                     if(result.getStatus()!=Result.FAILED){
184                         addGoodDetails(result, compName);
185                         //result.passed()
186
result.passed(smh.getLocalString
187                                 (getClass().getName() + ".debug1",
188                                         "For Home Interface [ {0} ] Method [ {1} ]",
189                                         new Object JavaDoc[] {homeInterfaceClass.getName(),homeInterfaceMethods[i].getName()}));
190                         result.addGoodDetails(smh.getLocalString
191                                 (getClass().getName() + ".passed",
192                                         "The corresponding [ {0} ] method with matching parameters was found.",
193                                         new Object JavaDoc[] {ejbFinderMethods[ejbFinderMethodLoopCounter].getName()}));
194
195                     }
196
197                 } // if the home interface found a "find" method
198
}// for all the methods within the home interface class, loop
199

200         } catch (ClassNotFoundException JavaDoc e) {
201             Verifier.debug(e);
202             result.addErrorDetails(smh.getLocalString
203                     ("tests.componentNameConstructor",
204                             "For [ {0} ]",
205                             new Object JavaDoc[] {compName.toString()}));
206             result.failed(smh.getLocalString
207                     (getClass().getName() + ".failedException",
208                             "Error: Home interface [ {0} ] or EJB Class [ {1} ] does not exist or is not loadable.",
209                             new Object JavaDoc[] {descriptor.getHomeClassName(),descriptor.getEjbClassName()}));
210 // return false;
211
}
212     }
213
214     private boolean checkReturnType(Method homeFinderMethod, Method beanFinderMethod, Class JavaDoc remote, EjbDescriptor descriptor){
215         Class JavaDoc homeMethodtype = homeFinderMethod.getReturnType();
216         Class JavaDoc beanMethodType = beanFinderMethod.getReturnType();
217
218         if (homeMethodtype.getName().equals(remote.getName())) {
219             return beanMethodType.getName().equals(((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName());
220         } else return homeMethodtype.getName().equals(beanMethodType.getName());
221     }
222 }
223
Popular Tags