KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > ejbfindermethod > EjbFinderMethodFinal


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.ejbfindermethod;
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.EjbEntityDescriptor;
29 import com.sun.enterprise.deployment.EjbDescriptor;
30 import com.sun.enterprise.deployment.MethodDescriptor;
31 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
32 import com.sun.enterprise.tools.verifier.*;
33 import java.lang.ClassLoader JavaDoc;
34 import com.sun.enterprise.tools.verifier.tests.*;
35
36 /**
37  * ejbFind<METHOD>(...) methods test.
38  *
39  * EJB class contains all ejbFind<METHOD>(...) methods declared in the bean
40  * class.
41  *
42  * The signatures of the finder methods must follow the following rules:
43  *
44  * A finder method name must start with the prefix ``ejbFind''
45  * (e.g. ejbFindByPrimaryKey, ejbFindLargeAccounts, ejbFindLateShipments).
46  *
47  * The method must not be declared as final.
48  *
49  */

50 public class EjbFinderMethodFinal extends EjbTest implements EjbCheck {
51
52
53     /**
54      * ejbFind<METHOD>(...) methods test.
55      *
56      * EJB class contains all ejbFind<METHOD>(...) methods declared in the bean
57      * class.
58      *
59      * The signatures of the finder methods must follow the following rules:
60      *
61      * A finder method name must start with the prefix ``ejbFind''
62      * (e.g. ejbFindByPrimaryKey, ejbFindLargeAccounts, ejbFindLateShipments).
63      *
64      * The method must not be declared as final.
65      *
66      * @param descriptor the Enterprise Java Bean deployment descriptor
67      *
68      * @return <code>Result</code> the results for this assertion
69      */

70     public Result check(EjbDescriptor descriptor) {
71
72     Result result = getInitializedResult();
73     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
74
75     if (descriptor instanceof EjbEntityDescriptor) {
76         String JavaDoc persistence =
77         ((EjbEntityDescriptor)descriptor).getPersistenceType();
78         if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistence)) {
79
80         boolean ejbFindMethodFound = false;
81         boolean isFinal = false;
82         boolean oneFailed = false;
83         int findMethodModifiers = 0;
84         int foundAtLeastOne = 0;
85         try {
86             // retrieve the EJB Class Methods
87
Context context = getVerifierContext();
88         ClassLoader JavaDoc jcl = context.getClassLoader();
89             Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
90                     // start do while loop here....
91
do {
92             Method [] ejbFinderMethods = EJBClass.getDeclaredMethods();
93           
94             for (int j = 0; j < ejbFinderMethods.length; ++j) {
95                 isFinal = false;
96                 ejbFindMethodFound = false;
97   
98                 if (ejbFinderMethods[j].getName().startsWith("ejbFind")) {
99                 foundAtLeastOne ++;
100                 ejbFindMethodFound = true;
101   
102                 // The method must not be declared as final.
103
findMethodModifiers = ejbFinderMethods[j].getModifiers();
104                 if (Modifier.isFinal(findMethodModifiers)) {
105                     isFinal = true;
106                 }
107     
108                 if (ejbFindMethodFound && !isFinal) {
109                     result.addGoodDetails(smh.getLocalString
110                                ("tests.componentNameConstructor",
111                                 "For [ {0} ]",
112                                 new Object JavaDoc[] {compName.toString()}));
113                     result.addGoodDetails(smh.getLocalString
114                               (getClass().getName() + ".debug1",
115                                "For EJB Class [ {0} ] Finder Method [ {1} ]",
116                                new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
117                     result.addGoodDetails(smh.getLocalString
118                               (getClass().getName() + ".passed",
119                                "A non-final [ {0} ] method was found.",
120                                new Object JavaDoc[] {ejbFinderMethods[j].getName()}));
121                 } else if (ejbFindMethodFound && isFinal) {
122                     oneFailed = true;
123                     result.addErrorDetails(smh.getLocalString
124                        ("tests.componentNameConstructor",
125                     "For [ {0} ]",
126                     new Object JavaDoc[] {compName.toString()}));
127                     result.addErrorDetails(smh.getLocalString
128                                (getClass().getName() + ".debug1",
129                                 "For EJB Class [ {0} ] Finder Method [ {1} ]",
130                                 new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
131                     result.addErrorDetails(smh.getLocalString
132                                (getClass().getName() + ".failed",
133                                 "Error: A final [ {0} ] method was found, but [ {1} ] cannot be declared as final.",
134                                 new Object JavaDoc[] {ejbFinderMethods[j].getName(),ejbFinderMethods[j].getName()}));
135                 }
136                 }
137             }
138                     } while (((EJBClass = EJBClass.getSuperclass()) != null) && (foundAtLeastOne == 0));
139   
140             if (foundAtLeastOne == 0){
141             result.addNaDetails(smh.getLocalString
142                        ("tests.componentNameConstructor",
143                     "For [ {0} ]",
144                     new Object JavaDoc[] {compName.toString()}));
145             result.notApplicable(smh.getLocalString
146                          (getClass().getName() + ".notApplicable1",
147                           "[ {0} ] does not declare any ejbFind<METHOD>(...) methods.",
148                           new Object JavaDoc[] {descriptor.getEjbClassName()}));
149             }
150   
151         } catch (ClassNotFoundException JavaDoc e) {
152             Verifier.debug(e);
153             result.addErrorDetails(smh.getLocalString
154                        ("tests.componentNameConstructor",
155                     "For [ {0} ]",
156                     new Object JavaDoc[] {compName.toString()}));
157              result.failed(smh.getLocalString
158                   (getClass().getName() + ".failedException",
159                    "Error: EJB Class [ {1} ] does not exist or is not loadable.",
160                    new Object JavaDoc[] {descriptor.getEjbClassName()}));
161             oneFailed = true;
162         }
163     
164         if (oneFailed) {
165             result.setStatus(result.FAILED);
166                 } else if (foundAtLeastOne == 0) {
167                     result.setStatus(result.NOT_APPLICABLE);
168         } else {
169             result.setStatus(result.PASSED);
170         }
171     
172         return result;
173
174         } else { //if (CONTAINER_PERSISTENCE.equals(persistence))
175
result.addNaDetails(smh.getLocalString
176                        ("tests.componentNameConstructor",
177                     "For [ {0} ]",
178                     new Object JavaDoc[] {compName.toString()}));
179         result.notApplicable(smh.getLocalString
180                      (getClass().getName() + ".notApplicable2",
181                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
182                       new Object JavaDoc[] {EjbEntityDescriptor.BEAN_PERSISTENCE,descriptor.getName(),persistence}));
183         return result;
184         }
185
186
187     } else {
188         result.addNaDetails(smh.getLocalString
189                        ("tests.componentNameConstructor",
190                     "For [ {0} ]",
191                     new Object JavaDoc[] {compName.toString()}));
192         result.notApplicable(smh.getLocalString
193                  (getClass().getName() + ".notApplicable",
194                   "[ {0} ] expected {1} bean, but called with {2} bean.",
195                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
196         return result;
197     }
198     }
199 }
200
Popular Tags