KickJava   Java API By Example, From Geeks To Geeks.

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


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 final 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 method must not be declared as final.
44  *
45  */

46 public class EjbCreateMethodFinal extends EjbTest implements EjbCheck {
47
48
49     /**
50      * Session Bean's ejbCreate(...) methods final test.
51      * Each session Bean class must define one or more ejbCreate(...) methods.
52      * The number and signatures 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 method must not be declared as final.
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         int foundAtLeastOne = 0;
76         boolean ejbCreateFound = false;
77         boolean isFinal = false;
78                 // start do while loop here....
79
do {
80             Method [] methods = c.getDeclaredMethods();
81             for (int i = 0; i < methods.length; i++) {
82             // reset flags from last time thru loop
83
ejbCreateFound = false;
84             isFinal = false;
85
86             // The method name must be ejbCreate.
87
if (methods[i].getName().startsWith("ejbCreate")) {
88                 foundAtLeastOne++;
89                 ejbCreateFound = true;
90
91                 // The method must not be declared as final or static.
92
int modifiers = methods[i].getModifiers();
93                 if (Modifier.isFinal(modifiers)){
94                 isFinal = true;
95                 }
96
97                 // now display the appropriate results for this particular ejbCreate
98
// method
99
if (ejbCreateFound && (!isFinal)) {
100                 result.addGoodDetails(smh.getLocalString
101                               ("tests.componentNameConstructor",
102                                "For [ {0} ]",
103                                new Object JavaDoc[] {compName.toString()}));
104                 result.addGoodDetails(smh.getLocalString
105                               (getClass().getName() + ".debug1",
106                                "For EJB Class [ {0} ] method [ {1} ]",
107                                new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName()}));
108                 result.addGoodDetails(smh.getLocalString
109                               (getClass().getName() + ".passed",
110                                "[ {0} ] properly declares non-final [ {1} ] method.",
111                                new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName()}));
112                 } else if (ejbCreateFound && isFinal) {
113                 oneFailed = true;
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() + ".failed",
124                             "Error: A final [ {0} ] method was found, but [ {1} ] cannot be declared as final.",
125                             new Object JavaDoc[] {methods[i].getName(),methods[i].getName()}));
126                 }
127             }
128             }
129                 } while (((c = c.getSuperclass()) != null) && (foundAtLeastOne == 0));
130         
131         if (foundAtLeastOne == 0){
132             result.addErrorDetails(smh.getLocalString
133                        ("tests.componentNameConstructor",
134                         "For [ {0} ]",
135                         new Object JavaDoc[] {compName.toString()}));
136             result.failed(smh.getLocalString
137                   (getClass().getName() + ".failed1",
138                    "Error: [ {0} ] does not properly declare at least one ejbCreate(...) method. [ {1} ] is not a valid bean.",
139                    new Object JavaDoc[] {descriptor.getEjbClassName(),descriptor.getEjbClassName()}));
140             oneFailed = true;
141         }
142         } catch (ClassNotFoundException JavaDoc e) {
143         Verifier.debug(e);
144         result.addErrorDetails(smh.getLocalString
145                        ("tests.componentNameConstructor",
146                     "For [ {0} ]",
147                     new Object JavaDoc[] {compName.toString()}));
148         result.failed(smh.getLocalString
149                   (getClass().getName() + ".failedException",
150                    "Error: [ {0} ] class not found.",
151                    new Object JavaDoc[] {descriptor.getEjbClassName()}));
152         oneFailed = true;
153         }
154
155         if (oneFailed) {
156         result.setStatus(result.FAILED);
157         } else {
158         result.setStatus(result.PASSED);
159         }
160
161         return result;
162  
163     } else {
164         result.addNaDetails(smh.getLocalString
165                 ("tests.componentNameConstructor",
166                  "For [ {0} ]",
167                  new Object JavaDoc[] {compName.toString()}));
168         result.notApplicable(smh.getLocalString
169                  (getClass().getName() + ".notApplicable",
170                   "[ {0} ] expected {1} bean, but called with {2} bean.",
171                   new Object JavaDoc[] {getClass(),"Session","Entity"}));
172         return result;
173     }
174     }
175 }
176
Popular Tags