KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > session > createmethod > HomeInterfaceCreateMethodExceptionCreate


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.createmethod;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import com.sun.enterprise.deployment.EjbDescriptor;
27 import com.sun.enterprise.deployment.EjbSessionDescriptor;
28 import java.lang.ClassLoader JavaDoc;
29 import com.sun.enterprise.tools.verifier.tests.*;
30 import java.util.*;
31 import java.lang.reflect.*;
32 import com.sun.enterprise.tools.verifier.*;
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
34 import com.sun.enterprise.tools.verifier.tests.ejb.EjbUtils;
35
36 /**
37  * Session beans home interface create method throws CreateException test.
38  *
39  * The following are the requirements for the enterprise Bean's home interface
40  * signature:
41  *
42  * A Session Bean's home interface defines one or more create(...) methods.
43  *
44  * The throws clause must include javax.ejb.CreateException.
45  */

46 public class HomeInterfaceCreateMethodExceptionCreate extends EjbTest implements EjbCheck {
47     Result result = null;
48     ComponentNameConstructor compName = null;
49
50     /**
51      * Session beans home interface create method throws CreateException test.
52      *
53      * The following are the requirements for the enterprise Bean's home interface
54      * signature:
55      *
56      * A Session Bean's home interface defines one or more create(...) methods.
57      *
58      * The throws clause must include javax.ejb.CreateException.
59      *
60      * @param descriptor the Enterprise Java Bean deployment descriptor
61      *
62      * @return <code>Result</code> the results for this assertion
63      */

64     public Result check(EjbDescriptor descriptor) {
65
66     result = getInitializedResult();
67     compName = getVerifierContext().getComponentNameConstructor();
68
69     if (descriptor instanceof EjbSessionDescriptor) {
70         boolean oneFailed = false;
71         if (descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName()))
72         oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(),(EjbSessionDescriptor)descriptor);
73         if (oneFailed == false) {
74         if (descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName()))
75             oneFailed = commonToBothInterfaces(descriptor.getLocalHomeClassName(),(EjbSessionDescriptor)descriptor);
76         }
77             if ((oneFailed == false) && (implementsEndpoints(descriptor))) {
78                result.addNaDetails(smh.getLocalString
79                            ("tests.componentNameConstructor",
80                            "For [ {0} ]",
81                             new Object JavaDoc[] {compName.toString()}));
82                result.notApplicable(smh.getLocalString
83                   ("com.sun.enterprise.tools.verifier.tests.ejb.webservice.notapp",
84                    "Not Applicable because, EJB [ {0} ] implements a Service Endpoint Interface.",
85                     new Object JavaDoc[] {compName.toString()}));
86                result.setStatus(result.NOT_APPLICABLE);
87                return result;
88             }
89
90         if (oneFailed) {
91         result.setStatus(result.FAILED);
92         } else {
93         result.setStatus(result.PASSED);
94         }
95         return result;
96         
97     } else {
98         result.addNaDetails(smh.getLocalString
99                        ("tests.componentNameConstructor",
100                     "For [ {0} ]",
101                     new Object JavaDoc[] {compName.toString()}));
102         result.notApplicable(smh.getLocalString
103                  (getClass().getName() + ".notApplicable",
104                   "[ {0} ] expected {1} bean, but called with {2} bean.",
105                   new Object JavaDoc[] {getClass(),"Session","Entity"}));
106         return result;
107     }
108     }
109
110     /**
111      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
112      * @param descriptor the Enterprise Java Bean deployment descriptor
113      * @param home for the Home interface of the Ejb.
114      * @return boolean the results for this assertion i.e if a test has failed or not
115      */

116
117
118     private boolean commonToBothInterfaces(String JavaDoc home, EjbSessionDescriptor descriptor) {
119     boolean oneFailed = false;
120     // RULE: session home interface are only allowed to have create
121
// methods which match ejbCreate, and returns the session Bean's
122
// remote interface.
123
try {
124         Context context = getVerifierContext();
125         ClassLoader JavaDoc jcl = context.getClassLoader();
126         Class JavaDoc c = Class.forName(home, false, getVerifierContext().getClassLoader());
127         Method methods[] = c.getDeclaredMethods();
128         Class JavaDoc [] methodExceptionTypes;
129         boolean throwsCreateException = false;
130         
131         for (int i=0; i< methods.length; i++) {
132         // clear these from last time thru loop
133
throwsCreateException = false;
134         if (methods[i].getName().startsWith("create")) {
135             methodExceptionTypes = methods[i].getExceptionTypes();
136             
137             // methods must also throw javax.ejb.CreateException
138
if (EjbUtils.isValidCreateException(methodExceptionTypes)) {
139             throwsCreateException = true;
140             }
141             
142             //report for this particular create method found in home interface
143
// now display the appropriate results for this particular create
144
// method
145
if (throwsCreateException ) {
146             result.addGoodDetails(smh.getLocalString
147                        ("tests.componentNameConstructor",
148                     "For [ {0} ]",
149                     new Object JavaDoc[] {compName.toString()}));
150             result.addGoodDetails(smh.getLocalString
151                           (getClass().getName() + ".debug1",
152                            "For Home Interface [ {0} ] Method [ {1} ]",
153                            new Object JavaDoc[] {c.getName(),methods[i].getName()}));
154             result.addGoodDetails(smh.getLocalString
155                           (getClass().getName() + ".passed",
156                            "The create method which must throw javax.ejb.CreateException was found.."));
157             } else if (!throwsCreateException) {
158             oneFailed = true;
159             result.addErrorDetails(smh.getLocalString
160                        ("tests.componentNameConstructor",
161                     "For [ {0} ]",
162                     new Object JavaDoc[] {compName.toString()}));
163             result.addErrorDetails(smh.getLocalString
164                            (getClass().getName() + ".debug1",
165                         "For Home Interface [ {0} ] Method [ {1} ]",
166                         new Object JavaDoc[] {c.getName(),methods[i].getName()}));
167             result.addErrorDetails(smh.getLocalString
168                            (getClass().getName() + ".failed",
169                         "Error: A create method was found, but did not throw javax.ejb.CreateException." ));
170             } // end of reporting for this particular 'create' method
171
} // if the home interface found a "create" method
172
} // for all the methods within the home interface class, loop
173

174         return oneFailed;
175     } catch (ClassNotFoundException JavaDoc e) {
176         Verifier.debug(e);
177         result.addErrorDetails(smh.getLocalString
178                    ("tests.componentNameConstructor",
179                     "For [ {0} ]",
180                     new Object JavaDoc[] {compName.toString()}));
181         result.failed(smh.getLocalString
182               (getClass().getName() + ".failedException",
183                "Error: Home interface [ {0} ] does not exist or is not loadable within bean [ {1} ]",
184                new Object JavaDoc[] {home, descriptor.getName()}));
185         return oneFailed;
186         
187     }
188     }
189 }
190
Popular Tags