KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > 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.entity.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.EjbEntityDescriptor;
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  * create<Method> method tests
37  * Entity beans home interface create method return type test.
38  *
39  * The following are the requirements for the enterprise Bean's home interface
40  * signature:
41  *
42  * An Entity Bean's home interface defines zero or more create(...) methods.
43  *
44  * The return type for a create method must be the enterprise Bean's remote
45  * interface type.
46  *
47  */

48 public class HomeInterfaceCreateMethodReturn extends EjbTest implements EjbCheck {
49     boolean foundAtLeastOneCreate = false;
50     Result result = null;
51     ComponentNameConstructor compName = null;
52     boolean remote_exists = false;
53     boolean local_exists = false;
54     /**
55      * Entity 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      * An Entity Bean's home interface defines zero 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     if (descriptor instanceof EjbEntityDescriptor) {
78         boolean oneFailed = false;
79      
80         // RULE: Entity home interface are only allowed to have create
81
// methods which returns the entity Bean's
82
// remote interface.
83

84         if (descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName()) &&
85         descriptor.getRemoteClassName() != null && !"".equals(descriptor.getRemoteClassName()) ) {
86         remote_exists = true;
87         home = descriptor.getHomeClassName();
88         remote = descriptor.getRemoteClassName();
89         }
90         if (descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName())&&
91         descriptor.getLocalClassName() != null && !"".equals(descriptor.getLocalClassName())) {
92         local_exists = true;
93         localHome = descriptor.getLocalHomeClassName();
94         local = descriptor.getLocalClassName();
95         }
96         oneFailed = commonToBothInterfaces(remote,home,local,localHome,(EjbEntityDescriptor)descriptor);
97         
98         if (!foundAtLeastOneCreate) {
99         result.addNaDetails(smh.getLocalString
100                           ("tests.componentNameConstructor",
101                            "For [ {0} ]",
102                            new Object JavaDoc[] {compName.toString()}));
103         result.addNaDetails(smh.getLocalString
104                       (getClass().getName() + ".debug3",
105                        "In Home Interface ",
106                        new Object JavaDoc[] {}));
107         result.addNaDetails(smh.getLocalString
108                       (getClass().getName() + ".notApplicable1",
109                        "No create method was found, test not applicable." ));
110         result.setStatus(result.NOT_APPLICABLE);
111         } else {
112         if (oneFailed) {
113             result.setStatus(result.FAILED);
114         } else {
115             result.setStatus(result.PASSED);
116         }
117         }
118          
119         return result;
120         
121     } else {
122         result.addNaDetails(smh.getLocalString
123                           ("tests.componentNameConstructor",
124                            "For [ {0} ]",
125                            new Object JavaDoc[] {compName.toString()}));
126         result.notApplicable(smh.getLocalString
127                  (getClass().getName() + ".notApplicable",
128                   "[ {0} ] expected {1} bean, but called with {2} bean.",
129                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
130         return result;
131     }
132     }
133
134     /**
135      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
136      * @param descriptor the Enterprise Java Bean deployment descriptor
137      * @param home for the Home interface of the Ejb.
138      * @param remote Remote/Local interface
139      * @return boolean the results for this assertion i.e if a test has failed or not
140      */

141     
142       private boolean commonToBothInterfaces(String JavaDoc remote, String JavaDoc home, String JavaDoc local, String JavaDoc localHome, EjbEntityDescriptor 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         foundAtLeastOneCreate=true;
189         // return type must be the remote interface
190
methodReturnType = methods[i].getReturnType();
191         if (remote_exists) {
192             if (methodReturnType.getName().equals(remote)) {
193             // this is the right ejbCreate method
194
validReturn = true;
195             result.addGoodDetails(smh.getLocalString
196                           ("tests.componentNameConstructor",
197                            "For [ {0} ]",
198                            new Object JavaDoc[] {compName.toString()}));
199             result.addGoodDetails(smh.getLocalString
200                           (getClass().getName() + ".debug1",
201                            "For Home Interface [ {0} ] Method [ {1} ]",
202                            new Object JavaDoc[] {home ,methods[i].getName()}));
203             result.addGoodDetails(smh.getLocalString
204                           (getClass().getName() + ".passed",
205                            "The create method which returns [ {0} ] interface was found.",
206                            new Object JavaDoc[] {"remote"}));
207             }
208         }
209         if (local_exists) {
210             if (methodReturnType.getName().equals(local)) {
211             // this is the right ejbCreate method
212
validReturn = true;
213             result.addGoodDetails(smh.getLocalString
214                           ("tests.componentNameConstructor",
215                            "For [ {0} ]",
216                            new Object JavaDoc[] {compName.toString()}));
217             result.addGoodDetails(smh.getLocalString
218                           (getClass().getName() + ".debug1",
219                            "For Home Interface [ {0} ] Method [ {1} ]",
220                            new Object JavaDoc[] {home ,methods[i].getName()}));
221             result.addGoodDetails(smh.getLocalString
222                           (getClass().getName() + ".passed",
223                            "The create method which returns [ {0} ] interface was found.",
224                            new Object JavaDoc[] {"local"}));
225             }
226         }
227         
228         //report for this particular create method found in home interface
229
// now display the appropriate results for this particular create
230
// method
231
if (!validReturn) {
232             oneFailed = true;
233             result.addErrorDetails(smh.getLocalString
234                           ("tests.componentNameConstructor",
235                            "For [ {0} ]",
236                            new Object JavaDoc[] {compName.toString()}));
237             result.addErrorDetails(smh.getLocalString
238                        (getClass().getName() + ".debug1",
239                         "For Home Interface [ {0} ] Method [ {1} ]",
240                         new Object JavaDoc[] {home,methods[i].getName()}));
241             result.addErrorDetails(smh.getLocalString
242                        (getClass().getName() + ".failed",
243                         "Error: A Create method was found, but the return type [ {0} ] was not the Remote/Local interface" ,
244                         new Object JavaDoc[] {methodReturnType.getName()}));
245             return oneFailed;
246         } // end of reporting for this particular 'create' method
247
} // if the home interface found a "create" method
248
} // for all the methods within the home interface class, loop
249

250     return oneFailed;
251     
252     }
253 }
254
Popular Tags