KickJava   Java API By Example, From Geeks To Geeks.

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


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.EjbCMPEntityDescriptor;
30 import com.sun.enterprise.deployment.EjbDescriptor;
31 import com.sun.enterprise.deployment.MethodDescriptor;
32 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbUtils;
34 import com.sun.enterprise.tools.verifier.*;
35 import java.lang.ClassLoader JavaDoc;
36 import com.sun.enterprise.tools.verifier.tests.*;
37
38 /**
39  * ejbFind<METHOD>(...) methods test.
40  *
41  * EJB class contains all ejbFind<METHOD>(...) methods declared in the bean
42  * class.
43  *
44  * The signatures of the finder methods must follow the following rules:
45  *
46  * A finder method name must start with the prefix ``ejbFind''
47  * (e.g. ejbFindByPrimaryKey, ejbFindLargeAccounts, ejbFindLateShipments).
48  *
49  * Compatibility Note: EJB 1.0 allowed the finder methods to throw the
50  * java.rmi.RemoteException to indicate a non-application exception. This
51  * practice is deprecated in EJB 1.1---an EJB 1.1 compliant enterprise
52  * bean should throw the javax.ejb.EJBException or another
53  * java.lang.RuntimeException to indicate non-application exceptions to
54  * the Container.
55  *
56  */

57 public class EjbFinderMethodException extends EjbTest implements EjbCheck {
58
59
60     /**
61      * ejbFind<METHOD>(...) methods test.
62      *
63      * EJB class contains all ejbFind<METHOD>(...) methods declared in the bean
64      * class.
65      *
66      * The signatures of the finder methods must follow the following rules:
67      *
68      * A finder method name must start with the prefix ``ejbFind''
69      * (e.g. ejbFindByPrimaryKey, ejbFindLargeAccounts, ejbFindLateShipments).
70      *
71      * Compatibility Note: EJB 1.0 allowed the finder methods to throw the
72      * java.rmi.RemoteException to indicate a non-application exception. This
73      * practice is deprecated in EJB 1.1---an EJB 1.1 compliant enterprise
74      * bean should throw the javax.ejb.EJBException or another
75      * java.lang.RuntimeException to indicate non-application exceptions to
76      * the Container.
77      *
78      * @param descriptor the Enterprise Java Bean deployment descriptor
79      *
80      * @return <code>Result</code> the results for this assertion
81      */

82     public Result check(EjbDescriptor descriptor) {
83
84     Result result = getInitializedResult();
85     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
86
87     if (descriptor instanceof EjbEntityDescriptor) {
88         String JavaDoc persistence =
89         ((EjbEntityDescriptor)descriptor).getPersistenceType();
90         if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistence)) {
91
92         boolean ejbFindMethodFound = false;
93         boolean throwsRemoteException = false;
94         boolean throwsFinderException = false;
95         boolean isValidFinderException = false;
96         boolean oneFailed = false;
97         int foundWarning = 0;
98         int foundAtLeastOne = 0;
99         try {
100             // retrieve the EJB Class Methods
101
Context context = getVerifierContext();
102             ClassLoader JavaDoc jcl = context.getClassLoader();
103             Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
104                     // start do while loop here....
105
do {
106             Method [] ejbFinderMethods = EJBClass.getDeclaredMethods();
107           
108             for (int j = 0; j < ejbFinderMethods.length; ++j) {
109                 throwsRemoteException = false;
110                 throwsFinderException = false;
111                 ejbFindMethodFound = false;
112   
113                 if (ejbFinderMethods[j].getName().startsWith("ejbFind")) {
114                 ejbFindMethodFound = true;
115                 foundAtLeastOne++;
116   
117                 // Compatibility Note: EJB 1.0 allowed the ejbFind<METHOD> to
118
// throw the java.rmi.RemoteException to indicate a non-application
119
// exception. This practice is deprecated in EJB 1.1---an EJB 1.1
120
// compliant enterprise bean should throw the javax.ejb.EJBException
121
// or another RuntimeException to indicate non-application
122
// exceptions to the Container (see Section 12.2.2).
123
// Note: Treat as a warning to user in this instance.
124
Class JavaDoc [] exceptions = ejbFinderMethods[j].getExceptionTypes();
125                 if (EjbUtils.isValidRemoteException(exceptions)) {
126                     throwsRemoteException = true;
127                 }
128
129                 if (EjbUtils.isValidFinderException(exceptions)) {
130                     throwsFinderException = true;
131                 }
132   
133                 if (ejbFindMethodFound && throwsRemoteException == false && throwsFinderException == true) {
134                     result.addGoodDetails(smh.getLocalString
135                               ("tests.componentNameConstructor",
136                                "For [ {0} ]",
137                                new Object JavaDoc[] {compName.toString()}));
138                     result.addGoodDetails(smh.getLocalString
139                               (getClass().getName() + ".debug1",
140                                "For EJB Class [ {0} ] Finder Method [ {1} ]",
141                                new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
142                     result.addGoodDetails(smh.getLocalString
143                               (getClass().getName() + ".passed",
144                                "[ {0} ] declares [ {1} ] method, which properly does not throw java.rmi.RemoteException, but throws FinderException",
145                                new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
146                 } else if (ejbFindMethodFound && throwsRemoteException == true) {
147                                     if (descriptor instanceof EjbCMPEntityDescriptor){
148                                         if(((EjbCMPEntityDescriptor) descriptor).getCMPVersion()==EjbCMPEntityDescriptor.CMP_2_x){
149                                         
150                                             result.addErrorDetails(smh.getLocalString
151                                    ("tests.componentNameConstructor",
152                                 "For [ {0} ]",
153                                 new Object JavaDoc[] {compName.toString()}));
154                                             result.addErrorDetails(smh.getLocalString
155                                    (getClass().getName() + ".debug1",
156                                 "For EJB Class [ {0} ] Finder method [ {1} ]",
157                                 new Object JavaDoc[] {descriptor.getEjbClassName(),ejbFinderMethods[j].getName()}));
158                                             result.addErrorDetails(smh.getLocalString
159                                    (getClass().getName() + ".error",
160                                 "Error: Compatibility Note:" +
161                                 "\n An [ {0} ] method was found, but" +
162                                 "\n EJB 1.0 allowed the ejbFind<METHOD> method to throw the " +
163                                 "\n java.rmi.RemoteException to indicate a non-application" +
164                                 "\n exception. This practice was deprecated in EJB 1.1" +
165                                 "\n ---an EJB 2.0 compliant enterprise bean must" +
166                                 "\n throw the javax.ejb.EJBException or another " +
167                                 "\n RuntimeException to indicate non-application exceptions" +
168                                 "\n to the Container. ",
169                                 new Object JavaDoc[] {ejbFinderMethods[j].getName()}));
170                                             oneFailed = true;
171                                         } else {
172                                             // warning for 1.1
173
foundWarning++;
174                                             result.addWarningDetails(smh.getLocalString
175                                  ("tests.componentNameConstructor",
176                                   "For [ {0} ]",
177                                   new Object JavaDoc[] {compName.toString()}));
178                                             result.addWarningDetails(smh.getLocalString
179                                  (getClass().getName() + ".debug1",
180                                   "For EJB Class [ {0} ] Finder method [ {1} ]",
181                                   new Object JavaDoc[] {descriptor.getEjbClassName(),ejbFinderMethods[j].getName()}));
182                                             result.addWarningDetails(smh.getLocalString
183                                  (getClass().getName() + ".warning",
184                                   "Error: Compatibility Note:" +
185                                   "\n An [ {0} ] method was found, but" +
186                                   "\n EJB 1.0 allowed the ejbFind<METHOD> method to throw the " +
187                                   "\n java.rmi.RemoteException to indicate a non-application" +
188                                   "\n exception. This practice is deprecated in EJB 1.1" +
189                                   "\n ---an EJB 1.1 compliant enterprise bean should" +
190                                   "\n throw the javax.ejb.EJBException or another " +
191                                   "\n RuntimeException to indicate non-application exceptions" +
192                                   "\n to the Container. ",
193                                   new Object JavaDoc[] {ejbFinderMethods[j].getName()}));
194                                         }
195                                     }else{
196                                         result.addNaDetails(smh.getLocalString
197                                             ("tests.componentNameConstructor",
198                         "For [ {0} ]",
199                         new Object JavaDoc[] {compName.toString()}));
200                             result.notApplicable(smh.getLocalString
201                         (getClass().getName() + ".notApplicable1",
202                         "[ {0} ] does not declare any ejbFind<METHOD>(...) methods.",
203                         new Object JavaDoc[] {descriptor.getEjbClassName()}));
204                                     }
205                 } else if (ejbFindMethodFound && throwsFinderException == false) {
206                     result.addErrorDetails(smh.getLocalString
207                                ("tests.componentNameConstructor",
208                                 "For [ {0} ]",
209                                 new Object JavaDoc[] {compName.toString()}));
210                     result.addErrorDetails(smh.getLocalString
211                                (getClass().getName() + ".debug1",
212                                 "For EJB Class [ {0} ] Finder method [ {1} ]",
213                                 new Object JavaDoc[] {descriptor.getEjbClassName(),ejbFinderMethods[j].getName()}));
214                     result.addErrorDetails(smh.getLocalString
215                                (getClass().getName() + ".error1",
216                                 "Error: ejbFind[Method] [ {0} ] does not throw FinderException",
217                                 new Object JavaDoc[] {ejbFinderMethods[j].getName()}));
218                     oneFailed = true;
219                 }
220                 }
221             }
222                     } while (((EJBClass = EJBClass.getSuperclass()) != null) && (foundAtLeastOne == 0));
223   
224             if (foundAtLeastOne == 0) {
225             result.addNaDetails(smh.getLocalString
226                        ("tests.componentNameConstructor",
227                     "For [ {0} ]",
228                     new Object JavaDoc[] {compName.toString()}));
229             result.notApplicable(smh.getLocalString
230                          (getClass().getName() + ".notApplicable1",
231                           "[ {0} ] does not declare any ejbFind<METHOD>(...) methods.",
232                           new Object JavaDoc[] {descriptor.getEjbClassName()}));
233             }
234   
235         } catch (ClassNotFoundException JavaDoc e) {
236             Verifier.debug(e);
237             result.addErrorDetails(smh.getLocalString
238                        ("tests.componentNameConstructor",
239                     "For [ {0} ]",
240                     new Object JavaDoc[] {compName.toString()}));
241             result.failed(smh.getLocalString
242                   (getClass().getName() + ".failedException",
243                    "Error: EJB Class [ {1} ] does not exist or is not loadable.",
244                    new Object JavaDoc[] {descriptor.getEjbClassName()}));
245             oneFailed = true;
246         }
247     
248         if (oneFailed) {
249             result.setStatus(result.FAILED);
250                 } else if (foundAtLeastOne == 0) {
251                     result.setStatus(result.NOT_APPLICABLE);
252         } else if (foundWarning > 0) {
253             result.setStatus(result.WARNING);
254         } else {
255             result.setStatus(result.PASSED);
256         }
257   
258         return result;
259
260         } else { //if (CONTAINER_PERSISTENCE.equals(persistence))
261
result.addNaDetails(smh.getLocalString
262                     ("tests.componentNameConstructor",
263                      "For [ {0} ]",
264                      new Object JavaDoc[] {compName.toString()}));
265         result.notApplicable(smh.getLocalString
266                      (getClass().getName() + ".notApplicable2",
267                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
268                       new Object JavaDoc[] {EjbEntityDescriptor.BEAN_PERSISTENCE,descriptor.getName(),persistence}));
269         return result;
270         }
271
272     } else {
273         result.addNaDetails(smh.getLocalString
274                        ("tests.componentNameConstructor",
275                     "For [ {0} ]",
276                     new Object JavaDoc[] {compName.toString()}));
277         result.notApplicable(smh.getLocalString
278                  (getClass().getName() + ".notApplicable",
279                   "[ {0} ] expected {1} bean, but called with {2} bean.",
280                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
281         return result;
282     }
283     }
284 }
285
Popular Tags