KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > 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.entity.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.EjbEntityDescriptor;
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
35 /**
36  * create<Method> method tests
37  * Entity 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  * An Entity Bean's home interface defines zero or more create(...) methods.
43  *
44  * The throws clause must include javax.ejb.CreateException.
45  */

46 public class HomeInterfaceCreateMethodExceptionCreate extends EjbTest implements EjbCheck {
47     boolean foundAtLeastOneCreate = false;
48     Result result = null;
49     ComponentNameConstructor compName = null;
50     /**
51      * Entity 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      * An Entity Bean's home interface defines zero 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     boolean oneFailed = false;
69
70     if (descriptor instanceof EjbEntityDescriptor) {
71         if(descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName()))
72         oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(),descriptor);
73         if(oneFailed == false) {
74         if(descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName()))
75             oneFailed = commonToBothInterfaces(descriptor.getLocalHomeClassName(),descriptor);
76         }
77
78         if (!foundAtLeastOneCreate) {
79         result.addGoodDetails(smh.getLocalString
80                           ("tests.componentNameConstructor",
81                            "For [ {0} ]",
82                            new Object JavaDoc[] {compName.toString()}));
83         result.addGoodDetails(smh.getLocalString
84                       (getClass().getName() + ".debug3",
85                        "For Home Interface [ {0} ]",
86                        new Object JavaDoc[] {descriptor.getHomeClassName()}));
87         result.addGoodDetails(smh.getLocalString
88                       (getClass().getName() + ".notApplicable1",
89                        "No create method was found, test not applicable." ));
90         result.setStatus(result.PASSED);
91         } else {
92         if (oneFailed) {
93             result.setStatus(result.FAILED);
94         } else {
95             result.setStatus(result.PASSED);
96         }
97         }
98          
99         return result;
100         
101     } else {
102         result.addNaDetails(smh.getLocalString
103                           ("tests.componentNameConstructor",
104                            "For [ {0} ]",
105                            new Object JavaDoc[] {compName.toString()}));
106         result.notApplicable(smh.getLocalString
107                  (getClass().getName() + ".notApplicable",
108                   "[ {0} ] expected {1} bean, but called with {2} bean.",
109                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
110         return result;
111     }
112     }
113     /**
114      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
115      * @param descriptor the Enterprise Java Bean deployment descriptor
116      * @param home for the Home interface of the Ejb.
117      * @return boolean the results for this assertion i.e if a test has failed or not
118      */

119
120
121     private boolean commonToBothInterfaces(String JavaDoc home, EjbDescriptor descriptor) {
122     boolean oneFailed = false;
123     
124     // RULE: Entity home interface are only allowed to have create
125
// methods which must throw javax.ejb.CreateException
126
try {
127         Context context = getVerifierContext();
128         ClassLoader JavaDoc jcl = context.getClassLoader();
129         Class JavaDoc c = Class.forName(home, false, getVerifierContext().getClassLoader());
130         Method methods[] = c.getDeclaredMethods();
131         Class JavaDoc [] methodExceptionTypes;
132         boolean throwsCreateException = false;
133         
134         for (int i=0; i< methods.length; i++) {
135         // clear these from last time thru loop
136
throwsCreateException = false;
137         if (methods[i].getName().startsWith("create")) {
138             // set this once to indicate that test is applicable, if you didn't
139
// find any create methods, that's okay too, as entity beans can
140
// have zero or more create methods, & when you have zero, test
141
// is N/A
142
if (!foundAtLeastOneCreate) {
143             foundAtLeastOneCreate = true;
144             }
145             
146             methodExceptionTypes = methods[i].getExceptionTypes();
147             
148             // methods must also throw javax.ejb.CreateException
149
for (int kk = 0; kk < methodExceptionTypes.length; ++kk) {
150             if (methodExceptionTypes[kk].getName().equals("javax.ejb.CreateException")) {
151                 throwsCreateException = true;
152                 break;
153             }
154             }
155             
156             //report for this particular create method found in home interface
157
// now display the appropriate results for this particular create
158
// method
159
if (throwsCreateException ) {
160             result.addGoodDetails(smh.getLocalString
161                           ("tests.componentNameConstructor",
162                            "For [ {0} ]",
163                            new Object JavaDoc[] {compName.toString()}));
164             result.addGoodDetails(smh.getLocalString
165                           (getClass().getName() + ".debug1",
166                            "For Home Interface [ {0} ] Method [ {1} ]",
167                            new Object JavaDoc[] {c.getName(),methods[i].getName()}));
168             result.addGoodDetails(smh.getLocalString
169                           (getClass().getName() + ".passed",
170                            "The create method which must throw javax.ejb.CreateException was found."));
171             } else if (!throwsCreateException) {
172             oneFailed = true;
173             result.addErrorDetails(smh.getLocalString
174                           ("tests.componentNameConstructor",
175                            "For [ {0} ]",
176                            new Object JavaDoc[] {compName.toString()}));
177             result.addErrorDetails(smh.getLocalString
178                            (getClass().getName() + ".debug1",
179                         "For Home Interface [ {0} ] Method [ {1} ]",
180                         new Object JavaDoc[] {home,methods[i].getName()}));
181             result.addErrorDetails(smh.getLocalString
182                            (getClass().getName() + ".failed",
183                         "Error: A create method was found, but did not throw javax.ejb.CreateException." ));
184             break;
185             } // end of reporting for this particular 'create' method
186
} // if the home interface found a "create" method
187
} // for all the methods within the home interface class, loop
188

189         return oneFailed;
190     } catch (ClassNotFoundException JavaDoc e) {
191         Verifier.debug(e);
192         result.addErrorDetails(smh.getLocalString
193                    ("tests.componentNameConstructor",
194                     "For [ {0} ]",
195                     new Object JavaDoc[] {compName.toString()}));
196         result.failed(smh.getLocalString
197               (getClass().getName() + ".failedException",
198                "Error: Home interface [ {0} ] does not exist or is not loadable within bean [ {1} ]",
199                new Object JavaDoc[] {home, descriptor.getName()}));
200         return oneFailed;
201     }
202     }
203 }
204
Popular Tags