KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > session > ejbcreatemethod > EjbCreateMethodReturn


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.ejbcreatemethod;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import java.lang.ClassLoader JavaDoc;
27 import com.sun.enterprise.tools.verifier.tests.*;
28 import javax.ejb.SessionBean JavaDoc;
29 import java.lang.reflect.*;
30 import com.sun.enterprise.deployment.EjbDescriptor;
31 import com.sun.enterprise.deployment.EjbSessionDescriptor;
32 import com.sun.enterprise.tools.verifier.*;
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
34
35 /**
36  * Session Bean's ejbCreate(...) methods return test.
37  * Each session Bean class must define one or more ejbCreate(...) methods.
38  * The number and signatures of a session Bean's create methods are specific
39  * to each EJB class. The method signatures must follow these rules:
40  *
41  * The method name must be ejbCreate.
42  *
43  * The return type must be void.
44  *
45  */

46 public class EjbCreateMethodReturn extends EjbTest implements EjbCheck {
47
48
49     /**
50      * Session Bean's ejbCreate(...) methods return test.
51      * Each session Bean class must define one or more ejbCreate(...) methods.
52      * The number and signature of a session Bean's create methods are specific
53      * to each EJB class. The method signatures must follow these rules:
54      *
55      * The method name must be ejbCreate.
56      *
57      * The return type must be void.
58      *
59      * @param descriptor the Enterprise Java Bean deployment descriptor
60      *
61      * @return <code>Result</code> the results for this assertion
62      */

63     public Result check(EjbDescriptor descriptor) {
64
65     Result result = getInitializedResult();
66     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
67
68     if (descriptor instanceof EjbSessionDescriptor) {
69         boolean oneFailed = false;
70         try {
71         Context context = getVerifierContext();
72         ClassLoader JavaDoc jcl = context.getClassLoader();
73         Class JavaDoc c = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
74
75         Class JavaDoc [] ejbCreateMethodParameterTypes;
76         int foundAtLeastOne = 0;
77         boolean ejbCreateFound = false;
78         boolean returnsVoid = false;
79                 // start do while loop here....
80
do {
81             Method [] methods = c.getDeclaredMethods();
82             for (int i = 0; i < methods.length; i++) {
83             // reset flags from last time thru loop
84
ejbCreateFound = false;
85             returnsVoid = false;
86
87             // The method name must be ejbCreate.
88
if (methods[i].getName().startsWith("ejbCreate")) {
89                 ejbCreateFound = true;
90
91                 // The return type must be void.
92
Class JavaDoc rt = methods[i].getReturnType();
93                 if (rt.getName().equals("void")) {
94                 returnsVoid = true;
95                 foundAtLeastOne++;
96                 }
97
98                 // now display the appropriate results for this particular ejbCreate
99
// method
100
if (ejbCreateFound && returnsVoid) {
101                 result.addGoodDetails(smh.getLocalString
102                               ("tests.componentNameConstructor",
103                                "For [ {0} ]",
104                                new Object JavaDoc[] {compName.toString()}));
105                 result.addGoodDetails(smh.getLocalString
106                               (getClass().getName() + ".debug1",
107                                "For EJB Class [ {0} ] method [ {1} ]",
108                                new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName()}));
109                 result.addGoodDetails(smh.getLocalString
110                               (getClass().getName() + ".passed",
111                                "[ {0} ] properly declares [ {1} ] method with valid 'void' return type.",
112                                new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName()}));
113                 } else if (ejbCreateFound && (!returnsVoid)) {
114                 result.addErrorDetails(smh.getLocalString
115                             ("tests.componentNameConstructor",
116                              "For [ {0} ]",
117                              new Object JavaDoc[] {compName.toString()}));
118                 result.addErrorDetails(smh.getLocalString
119                             (getClass().getName() + ".debug1",
120                              "For EJB Class [ {0} ] method [ {1} ]",
121                              new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName()}));
122                 result.addErrorDetails(smh.getLocalString
123                             (getClass().getName() + ".notApplicable2",
124                              "An [ {0} ] method was found, but [ {1} ] method has a non 'void' return type value. [ {2} ] methods return types must define return 'void' type for at least one ejbCreate method.",
125                              new Object JavaDoc[] {methods[i].getName(),methods[i].getName(),methods[i].getName()}));
126                 oneFailed = true;
127                 }
128             }
129             }
130                 } while (((c = c.getSuperclass()) != null) && (foundAtLeastOne == 0));
131         
132         if (foundAtLeastOne == 0) {
133             result.addErrorDetails(smh.getLocalString
134                        ("tests.componentNameConstructor",
135                         "For [ {0} ]",
136                         new Object JavaDoc[] {compName.toString()}));
137             result.failed(smh.getLocalString
138                   (getClass().getName() + ".failed",
139                    "Error: [ {0} ] does not properly declare at least one ejbCreate(...) method with valid 'void' return type. [ {1} ] is not a valid bean.",
140                    new Object JavaDoc[] {descriptor.getEjbClassName(),descriptor.getEjbClassName()}));
141             oneFailed = true;
142         }
143         } catch (ClassNotFoundException JavaDoc e) {
144         Verifier.debug(e);
145         result.addErrorDetails(smh.getLocalString
146                        ("tests.componentNameConstructor",
147                     "For [ {0} ]",
148                     new Object JavaDoc[] {compName.toString()}));
149         result.failed(smh.getLocalString
150                   (getClass().getName() + ".failedException",
151                    "Error: [ {0} ] class not found.",
152                    new Object JavaDoc[] {descriptor.getEjbClassName()}));
153         oneFailed = true;
154         }
155
156         if (oneFailed) {
157         result.setStatus(result.FAILED);
158         } else {
159         result.setStatus(result.PASSED);
160         }
161
162         return result;
163  
164     } else {
165         result.addNaDetails(smh.getLocalString
166                 ("tests.componentNameConstructor",
167                  "For [ {0} ]",
168                  new Object JavaDoc[] {compName.toString()}));
169         result.notApplicable(smh.getLocalString
170                  (getClass().getName() + ".notApplicable",
171                   "[ {0} ] expected {1} bean, but called with {2} bean.",
172                   new Object JavaDoc[] {getClass(),"Session","Entity"}));
173         return result;
174     }
175     }
176 }
177
Popular Tags