KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > session > createmethod > HomeInterfaceCreateMethodReturn


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.EjbDescriptor;
27 import com.sun.enterprise.deployment.EjbSessionDescriptor;
28 import java.lang.ClassLoader JavaDoc;
29 import com.sun.enterprise.tools.verifier.tests.*;
30 import java.util.*;
31 import java.lang.reflect.*;
32 import com.sun.enterprise.tools.verifier.*;
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
34
35 /**
36  * Session beans home interface create method return type test.
37  *
38  * The following are the requirements for the enterprise Bean's home interface
39  * signature:
40  *
41  * A Session Bean's home interface defines one or more create(...) methods.
42  *
43  * The return type for a create method must be the enterprise Bean's remote
44  * interface type.
45  *
46  */

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

140
141
142     private boolean commonToBothInterfaces(String JavaDoc remote, String JavaDoc home, String JavaDoc local, String JavaDoc localHome, EjbSessionDescriptor descriptor) {
143     boolean oneFailed = false;
144     Class JavaDoc c,rc,lc,hc;
145     Method localMethods[],methods[];
146     try {
147         Context context = getVerifierContext();
148         ClassLoader JavaDoc jcl = context.getClassLoader();
149         if (remote_exists) {
150         c = Class.forName(home, false, getVerifierContext().getClassLoader());
151         rc = Class.forName(remote, false, getVerifierContext().getClassLoader());
152         methods = c.getDeclaredMethods();
153         oneFailed = findReturnType(methods,home,local,remote);
154         }
155         if (oneFailed == false) {
156         if (local_exists) {
157             hc = Class.forName(localHome, false, getVerifierContext().getClassLoader());
158             lc = Class.forName(local, false, getVerifierContext().getClassLoader());
159             localMethods = hc.getDeclaredMethods();
160             oneFailed = findReturnType(localMethods,localHome,local,remote);
161         }
162         }
163         return oneFailed;
164       
165     } catch (ClassNotFoundException JavaDoc e) {
166         Verifier.debug(e);
167         result.addErrorDetails(smh.getLocalString
168                    ("tests.componentNameConstructor",
169                     "For [ {0} ]",
170                     new Object JavaDoc[] {compName.toString()}));
171         result.failed(smh.getLocalString
172               (getClass().getName() + ".failedException",
173                "Error: Home interface [ {0} ] or [ {1} ]or Component interface [ {2} ] or [ {3} ] does not exist or is not loadable within bean [ {4} ]",
174                new Object JavaDoc[] {home, localHome, remote, local, descriptor.getName()}));
175         return false;
176     }
177     }
178
179
180     private boolean findReturnType(Method[] methods, String JavaDoc home, String JavaDoc local, String JavaDoc remote) {
181     Class JavaDoc methodReturnType;
182     boolean validReturn, oneFailed = false;
183     
184     for (int i=0; i< methods.length; i++) {
185         // clear these from last time thru loop
186
validReturn = false;
187         if (methods[i].getName().startsWith("create")) {
188         // return type must be the remote interface
189
methodReturnType = methods[i].getReturnType();
190         if (remote_exists) {
191             if (methodReturnType.getName().equals(remote)) {
192             // this is the right ejbCreate method
193
validReturn = true;
194             result.addGoodDetails(smh.getLocalString
195                        ("tests.componentNameConstructor",
196                     "For [ {0} ]",
197                     new Object JavaDoc[] {compName.toString()}));
198             result.addGoodDetails(smh.getLocalString
199                           (getClass().getName() + ".debug1",
200                            "For Home Interface [ {0} ] Method [ {1} ]",
201                            new Object JavaDoc[] {home ,methods[i].getName()}));
202             result.addGoodDetails(smh.getLocalString
203                           (getClass().getName() + ".passed",
204                            "The create method which returns [ {0} ] interface was found.",
205                            new Object JavaDoc[] {"remote"}));
206             }
207         }
208         if (local_exists) {
209             if (methodReturnType.getName().equals(local)) {
210             // this is the right ejbCreate method
211
validReturn = true;
212             result.addGoodDetails(smh.getLocalString
213                        ("tests.componentNameConstructor",
214                     "For [ {0} ]",
215                     new Object JavaDoc[] {compName.toString()}));
216             result.addGoodDetails(smh.getLocalString
217                           (getClass().getName() + ".debug1",
218                            "For Home Interface [ {0} ] Method [ {1} ]",
219                            new Object JavaDoc[] {home ,methods[i].getName()}));
220             result.addGoodDetails(smh.getLocalString
221                           (getClass().getName() + ".passed",
222                            "The create method which returns [ {0} ] interface was found.",
223                            new Object JavaDoc[] {"local"}));
224             }
225         }
226         
227         //report for this particular create method found in home interface
228
// now display the appropriate results for this particular create
229
// method
230
if (!validReturn) {
231             oneFailed = true;
232             result.addErrorDetails(smh.getLocalString
233                        ("tests.componentNameConstructor",
234                     "For [ {0} ]",
235                     new Object JavaDoc[] {compName.toString()}));
236             result.addErrorDetails(smh.getLocalString
237                        (getClass().getName() + ".debug1",
238                         "For Home Interface [ {0} ] Method [ {1} ]",
239                         new Object JavaDoc[] {home,methods[i].getName()}));
240             result.addErrorDetails(smh.getLocalString
241                        (getClass().getName() + ".failed",
242                         "Error: A Create method was found, but the return type [ {0} ] was not the Remote/Local interface" ,
243                         new Object JavaDoc[] {methodReturnType.getName()}));
244         } // end of reporting for this particular 'create' method
245
} // if the home interface found a "create" method
246
} // for all the methods within the home interface class, loop
247

248     return oneFailed;
249     
250     }
251     
252 }
253
Popular Tags