KickJava   Java API By Example, From Geeks To Geeks.

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


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.tests.ejb.EjbUtils;
33 import com.sun.enterprise.tools.verifier.*;
34 import java.lang.ClassLoader JavaDoc;
35 import com.sun.enterprise.tools.verifier.tests.*;
36
37 /**
38  * Single-object ejbFind<METHOD>(...) methods throws ObjectNotFoundException
39  * 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  * The ObjectNotFoundException is a subclass of FinderException. It is
50  * thrown by the ejbFind<METHOD>(...) methods to indicate that the
51  * requested entity object does not exist.
52  *
53  * Only single-object finders (see Subsection 9.1.8) may throw this
54  * exception. Multi-object finders must not throw this exception.
55  *
56  */

57 public class EjbFinderMethodObjectNotFoundException extends EjbTest implements EjbCheck {
58
59
60     /**
61      * Single-object ejbFind<METHOD>(...) methods throws ObjectNotFoundException
62      * test.
63      *
64      * EJB class contains all ejbFind<METHOD>(...) methods declared in the bean
65      * class.
66      *
67      * The signatures of the finder methods must follow the following rules:
68      *
69      * A finder method name must start with the prefix ``ejbFind''
70      * (e.g. ejbFindByPrimaryKey, ejbFindLargeAccounts, ejbFindLateShipments).
71      *
72      * The ObjectNotFoundException is a subclass of FinderException. It is
73      * thrown by the ejbFind<METHOD>(...) methods to indicate that the
74      * requested entity object does not exist.
75      *
76      * Only single-object finders (see Subsection 9.1.8) may throw this
77      * exception. Multi-object finders must not throw this exception.
78      *
79      * @param descriptor the Enterprise Java Bean deployment descriptor
80      *
81      * @return <code>Result</code> the results for this assertion
82      */

83     public Result check(EjbDescriptor descriptor) {
84
85     Result result = getInitializedResult();
86     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
87
88     if (descriptor instanceof EjbEntityDescriptor) {
89         String JavaDoc persistence =
90         ((EjbEntityDescriptor)descriptor).getPersistenceType();
91         if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistence)) {
92
93         boolean okayToThrowObjectNotFoundException = false;
94         boolean throwsObjectNotFoundException = false;
95         boolean oneFailed = false;
96         int findMethodModifiers = 0;
97         int foundAtLeastOne = 0;
98         try {
99             // retrieve the EJB Class Methods
100
Context context = getVerifierContext();
101         ClassLoader JavaDoc jcl = context.getClassLoader();
102             Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
103                     // start do while loop here....
104
do {
105             Method [] ejbFinderMethods = EJBClass.getDeclaredMethods();
106             String JavaDoc primaryKeyType = ((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName();
107           
108             for (int j = 0; j < ejbFinderMethods.length; ++j) {
109                 if (ejbFinderMethods[j].getName().startsWith("ejbFind")) {
110                 okayToThrowObjectNotFoundException = false;
111                 throwsObjectNotFoundException = false;
112                 Class JavaDoc returnByPrimaryKeyValue = ejbFinderMethods[j].getReturnType();
113                 foundAtLeastOne++;
114                 // Only single-object finders (see Subsection 9.1.8) may throw this
115
// exception. Multi-object finders must not throw this exception.
116
if (returnByPrimaryKeyValue.getName().equals(primaryKeyType)) {
117                     okayToThrowObjectNotFoundException = true;
118                 } else if ((returnByPrimaryKeyValue.getName().equals("java.util.Collection")) ||
119                        (returnByPrimaryKeyValue.getName().equals("java.util.Enumeration"))) {
120                     okayToThrowObjectNotFoundException = false;
121                 }
122   
123                 // now check exceptions, if it's not okay to throw
124
// ObjectNotFoundException, then flag this as an error, otherwise
125
// test passes or is not applicable
126
if (!okayToThrowObjectNotFoundException) {
127                 // get the exceptions and see if this method throw
128
// ObjectNotFoundException when it was not allowed to
129
Class JavaDoc [] exceptions = ejbFinderMethods[j].getExceptionTypes();
130                     if (EjbUtils.isValidObjectNotFoundExceptionException(exceptions)) {
131                     throwsObjectNotFoundException = true;
132                     }
133                 }
134   
135   
136                 if ((okayToThrowObjectNotFoundException) || ((!okayToThrowObjectNotFoundException) && (!throwsObjectNotFoundException))) {
137                     result.addGoodDetails(smh.getLocalString
138                               ("tests.componentNameConstructor",
139                                "For [ {0} ]",
140                                new Object JavaDoc[] {compName.toString()}));
141                     result.addGoodDetails(smh.getLocalString
142                               (getClass().getName() + ".debug1",
143                                "For EJB Class [ {0} ] Finder Method [ {1} ]",
144                                new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
145                     result.addGoodDetails(smh.getLocalString
146                               (getClass().getName() + ".passed",
147                                "An [ {0} ] method was found with valid exception types declared in throws clause.",
148                                new Object JavaDoc[] {ejbFinderMethods[j].getName()}));
149                 } else if ((!okayToThrowObjectNotFoundException) && throwsObjectNotFoundException) {
150                     oneFailed = true;
151                     result.addErrorDetails(smh.getLocalString
152                                ("tests.componentNameConstructor",
153                                 "For [ {0} ]",
154                                 new Object JavaDoc[] {compName.toString()}));
155                     result.addErrorDetails(smh.getLocalString
156                                (getClass().getName() + ".debug1",
157                                 "For EJB Class [ {0} ] Finder Method [ {1} ]",
158                                 new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
159                     result.addErrorDetails(smh.getLocalString
160                                (getClass().getName() + ".failed",
161                                 "Error: An [ {0} ] method was found, but [ {1} ] throws ObjectNotFoundException. Multi-object finder methods must not throw this exception.",
162                                 new Object JavaDoc[] {ejbFinderMethods[j].getName(),ejbFinderMethods[j].getName()}));
163                 }
164                 }
165             }
166                     } while (((EJBClass = EJBClass.getSuperclass()) != null) && (foundAtLeastOne == 0));
167   
168             if (foundAtLeastOne == 0) {
169             result.addNaDetails(smh.getLocalString
170                        ("tests.componentNameConstructor",
171                     "For [ {0} ]",
172                     new Object JavaDoc[] {compName.toString()}));
173             result.notApplicable(smh.getLocalString
174                          (getClass().getName() + ".notApplicable1",
175                           "[ {0} ] does not declare any ejbFind<METHOD>(...) methods.",
176                           new Object JavaDoc[] {descriptor.getEjbClassName()}));
177             }
178   
179         } catch (ClassNotFoundException JavaDoc e) {
180             Verifier.debug(e);
181             result.addErrorDetails(smh.getLocalString
182                        ("tests.componentNameConstructor",
183                         "For [ {0} ]",
184                         new Object JavaDoc[] {compName.toString()}));
185             result.failed(smh.getLocalString
186                   (getClass().getName() + ".failedException",
187                    "Error: EJB Class [ {1} ] does not exist or is not loadable.",
188                    new Object JavaDoc[] {descriptor.getEjbClassName()}));
189             oneFailed = true;
190         }
191     
192         if (oneFailed) {
193             result.setStatus(result.FAILED);
194                 } else if (foundAtLeastOne == 0) {
195                     result.setStatus(result.NOT_APPLICABLE);
196         } else {
197             result.setStatus(result.PASSED);
198         }
199     
200         return result;
201
202         } else { //if (CONTAINER_PERSISTENCE.equals(persistence))
203
result.addNaDetails(smh.getLocalString
204                     ("tests.componentNameConstructor",
205                      "For [ {0} ]",
206                      new Object JavaDoc[] {compName.toString()}));
207         result.notApplicable(smh.getLocalString
208                      (getClass().getName() + ".notApplicable2",
209                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
210                       new Object JavaDoc[] {EjbEntityDescriptor.BEAN_PERSISTENCE,descriptor.getName(),persistence}));
211         return result;
212         }
213
214
215     } else {
216         result.addNaDetails(smh.getLocalString
217                 ("tests.componentNameConstructor",
218                  "For [ {0} ]",
219                  new Object JavaDoc[] {compName.toString()}));
220         result.notApplicable(smh.getLocalString
221                  (getClass().getName() + ".notApplicable",
222                   "[ {0} ] expected {1} bean, but called with {2} bean.",
223                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
224         return result;
225     }
226     }
227 }
228
Popular Tags