KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > ejbfindbyprimarykey > EjbFindByPrimaryKeyException


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.ejbfindbyprimarykey;
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  * Define ejbFindByPrimaryKey method exception test.
38  *
39  * Every entity enterprise Bean class must define the ejbFindByPrimaryKey
40  * method.
41  *
42  * Compatibility Note: EJB 1.0 allowed the finder methods to throw the
43  * java.rmi.RemoteException to indicate a non-application exception. This
44  * practice is deprecated in EJB 1.1---an EJB 1.1 compliant enterprise
45  * bean should throw the javax.ejb.EJBException or another
46  * java.lang.RuntimeException to indicate non-application exceptions to
47  * the Container.
48  *
49  */

50 public class EjbFindByPrimaryKeyException extends EjbTest implements EjbCheck {
51
52
53     /**
54      * Define ejbFindByPrimaryKey method exception test.
55      *
56      * Every entity enterprise Bean class must define the ejbFindByPrimaryKey
57      * method.
58      *
59      * Compatibility Note: EJB 1.0 allowed the finder methods to throw the
60      * java.rmi.RemoteException to indicate a non-application exception. This
61      * practice is deprecated in EJB 1.1---an EJB 1.1 compliant enterprise
62      * bean should throw the javax.ejb.EJBException or another
63      * java.lang.RuntimeException to indicate non-application exceptions to
64      * the Container.
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 persistentType =
77         ((EjbEntityDescriptor)descriptor).getPersistenceType();
78         if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistentType)) {
79         boolean ejbFindByPrimaryKeyMethodFound = false;
80         boolean throwsRemoteException = false;
81         boolean oneFailed = false;
82         int foundWarning = 0;
83         try {
84             // retrieve the EJB Class Methods
85
Context context = getVerifierContext();
86         ClassLoader JavaDoc jcl = context.getClassLoader();
87             Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
88                     // start do while loop here....
89
do {
90             Method [] ejbFinderMethods = EJBClass.getDeclaredMethods();
91           
92             for (int j = 0; j < ejbFinderMethods.length; ++j) {
93                 if (ejbFinderMethods[j].getName().equals("ejbFindByPrimaryKey")) {
94                 // Every entity enterprise Bean class must define the
95
// ejbFindByPrimaryKey method.
96
ejbFindByPrimaryKeyMethodFound = true;
97   
98                 // Compatibility Note: EJB 1.0 allowed the ejbFindByPrimaryKey to
99
// throw the java.rmi.RemoteException to indicate a non-application
100
// exception. This practice is deprecated in EJB 1.1---an EJB 1.1
101
// compliant enterprise bean should throw the javax.ejb.EJBException
102
// or another RuntimeException to indicate non-application
103
// exceptions to the Container (see Section 12.2.2).
104
// Note: Treat as a warning to user in this instance.
105
Class JavaDoc [] exceptions = ejbFinderMethods[j].getExceptionTypes();
106                 for (int z = 0; z < exceptions.length; ++z) {
107                     if (exceptions[z].getName().equals("java.rmi.RemoteException")) {
108                     throwsRemoteException = true;
109                     break;
110                     }
111                 }
112   
113                 if (ejbFindByPrimaryKeyMethodFound && (!throwsRemoteException)) {
114                     result.addGoodDetails(smh.getLocalString
115                        ("tests.componentNameConstructor",
116                     "For [ {0} ]",
117                     new Object JavaDoc[] {compName.toString()}));
118                     result.addGoodDetails(smh.getLocalString
119                               (getClass().getName() + ".debug1",
120                                "For EJB Class [ {0} ] Finder Method [ {1} ]",
121                                new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
122                     result.addGoodDetails(smh.getLocalString
123                               (getClass().getName() + ".passed",
124                                "[ {0} ] declares [ {1} ] method, which properly does not throw java.rmi.RemoteException",
125                                new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
126                 } else if (ejbFindByPrimaryKeyMethodFound && throwsRemoteException) {
127                     result.addWarningDetails(smh.getLocalString
128                        ("tests.componentNameConstructor",
129                     "For [ {0} ]",
130                     new Object JavaDoc[] {compName.toString()}));
131                     result.addWarningDetails(smh.getLocalString
132                                  (getClass().getName() + ".debug1",
133                                   "For EJB Class [ {0} ] Finder Method [ {1} ]",
134                                   new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
135                     result.addWarningDetails(smh.getLocalString
136                                  (getClass().getName() + ".warning",
137                                   "Error: Compatibility Note:" +
138                                   "\n An [ {0} ] method was found, but" +
139                                   "\n EJB 1.0 allowed the ejbFindByPrimaryKey method to throw " +
140                                   "\n the java.rmi.RemoteException to indicate a non-application" +
141                                   "\n exception. This practice is deprecated in EJB 1.1" +
142                                   "\n ---an EJB 1.1 compliant enterprise bean should" +
143                                   "\n throw the javax.ejb.EJBException or another " +
144                                   "\n RuntimeException to indicate non-application exceptions" +
145                                   "\n to the Container. ",
146                                   new Object JavaDoc[] {ejbFinderMethods[j].getName()}));
147                     foundWarning++;
148                 }
149                 // found one, and there should only be one, break out
150
break;
151                 }
152             }
153                     } while (((EJBClass = EJBClass.getSuperclass()) != null) && (!ejbFindByPrimaryKeyMethodFound));
154   
155             if (!ejbFindByPrimaryKeyMethodFound) {
156             oneFailed = true;
157             result.addErrorDetails(smh.getLocalString
158                            ("tests.componentNameConstructor",
159                         "For [ {0} ]",
160                         new Object JavaDoc[] {compName.toString()}));
161             result.addErrorDetails(smh.getLocalString
162                            (getClass().getName() + ".debug3",
163                         "For EJB Class [ {0} ]",
164                         new Object JavaDoc[] {descriptor.getEjbClassName()}));
165             result.addErrorDetails(smh.getLocalString
166                            (getClass().getName() + ".failed",
167                         "Error: No ejbFindByPrimaryKey method was found in bean class."));
168             }
169         } catch (ClassNotFoundException JavaDoc e) {
170             Verifier.debug(e);
171             result.addErrorDetails(smh.getLocalString
172                        ("tests.componentNameConstructor",
173                     "For [ {0} ]",
174                     new Object JavaDoc[] {compName.toString()}));
175             result.failed(smh.getLocalString
176                   (getClass().getName() + ".failedException",
177                    "Error: EJB Class [ {0} ] does not exist or is not loadable.",
178                    new Object JavaDoc[] {descriptor.getEjbClassName()}));
179             oneFailed = true;
180         }
181     
182         if (oneFailed) {
183             result.setStatus(result.FAILED);
184         } else if (foundWarning > 0) {
185             result.setStatus(result.WARNING);
186         } else {
187             result.setStatus(result.PASSED);
188         }
189   
190         } else { //(CONTAINER_PERSISTENCE.equals(persistentType))
191
result.addNaDetails(smh.getLocalString
192                        ("tests.componentNameConstructor",
193                     "For [ {0} ]",
194                     new Object JavaDoc[] {compName.toString()}));
195         result.notApplicable(smh.getLocalString
196                      (getClass().getName() + ".notApplicable2",
197                       "Expected persistence type [ {0} ], but bean [ {1} ] has persistence type [ {2} ]",
198                       new Object JavaDoc[] {EjbEntityDescriptor.BEAN_PERSISTENCE,descriptor.getName(),persistentType}));
199         }
200         return result;
201
202     } else {
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() + ".notApplicable",
209                   "[ {0} ] expected {1} bean, but called with {2} bean.",
210                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
211         return result;
212     }
213     }
214 }
215
Popular Tags