KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > findermethod > HomeInterfaceFindMethodExceptionFinder


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.findermethod;
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 import com.sun.enterprise.tools.verifier.tests.ejb.EjbUtils;
35
36 /**
37  * Entity beans home interface find<METHOD> method throws
38  * javax.ejb.FinderException test.
39  *
40  * The following are the requirements for the enterprise Bean's home interface
41  * find<METHOD> signature:
42  *
43  * An Entity Bean's home interface defines one or more find<METHOD>(...)
44  * methods.
45  *
46  * The throws clause must include javax.ejb.FinderException.
47  */

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

68     public Result check(EjbDescriptor descriptor) {
69
70         result = getInitializedResult();
71     compName = getVerifierContext().getComponentNameConstructor();
72     boolean oneFailed = false;
73     if (descriptor instanceof EjbEntityDescriptor) {
74         String JavaDoc persistence =
75         ((EjbEntityDescriptor)descriptor).getPersistenceType();
76         if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistence)) {
77         if(descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName())) {
78             oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(),descriptor);
79         }
80         if(oneFailed == false) {
81             if(descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName())) {
82             oneFailed = commonToBothInterfaces(descriptor.getLocalHomeClassName(),descriptor);
83             }
84         }
85         if (oneFailed) {
86             result.setStatus(result.FAILED);
87         } else {
88             result.setStatus(result.PASSED);
89         }
90         return result;
91
92         } else { //if (CONTAINER_PERSISTENCE.equals(persistence))
93
result.addNaDetails(smh.getLocalString
94                    ("tests.componentNameConstructor",
95                     "For [ {0} ]",
96                     new Object JavaDoc[] {compName.toString()}));
97         result.notApplicable(smh.getLocalString
98                      (getClass().getName() + ".notApplicable2",
99                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
100                       new Object JavaDoc[] {EjbEntityDescriptor.BEAN_PERSISTENCE,descriptor.getName(),persistence}));
101         return result;
102         }
103         
104     } else {
105         result.addNaDetails(smh.getLocalString
106                    ("tests.componentNameConstructor",
107                     "For [ {0} ]",
108                     new Object JavaDoc[] {compName.toString()}));
109         result.notApplicable(smh.getLocalString
110                  (getClass().getName() + ".notApplicable",
111                   "[ {0} ] expected {1} bean, but called with {2} bean.",
112                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
113         return result;
114     }
115     }
116
117    /**
118      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
119      * @param descriptor the Enterprise Java Bean deployment descriptor
120      * @param home for the Home interface of the Ejb.
121      * @return boolean the results for this assertion i.e if a test has failed or not
122      */

123
124     private boolean commonToBothInterfaces(String JavaDoc home, EjbDescriptor descriptor) {
125     boolean oneFailed = false;
126         // RULE: Entity home interface are only allowed to have find<METHOD>
127
// methods which must throw javax.ejb.FinderException
128
try {
129             Context context = getVerifierContext();
130         ClassLoader JavaDoc jcl = context.getClassLoader();
131             Class JavaDoc c = Class.forName(home, false, getVerifierContext().getClassLoader());
132             Method methods[] = c.getDeclaredMethods();
133             Class JavaDoc [] methodExceptionTypes;
134             boolean throwsFinderException = false;
135   
136             for (int i=0; i< methods.length; i++) {
137             // clear these from last time thru loop
138
throwsFinderException = false;
139             if (methods[i].getName().startsWith("find")) {
140                 // reset from last time thru loop
141
throwsFinderException = false;
142                 methodExceptionTypes = methods[i].getExceptionTypes();
143                  
144                 // methods must throw javax.ejb.FinderException
145
if (EjbUtils.isValidFinderException(methodExceptionTypes)) {
146                 throwsFinderException = true;
147                 }
148   
149                 // report for this particular find<METHOD> method found in home
150
// interface , now display the appropriate results for this
151
// particular find<METHOD> method
152
if (throwsFinderException ) {
153                 result.addGoodDetails(smh.getLocalString
154                    ("tests.componentNameConstructor",
155                     "For [ {0} ]",
156                     new Object JavaDoc[] {compName.toString()}));
157                 result.addGoodDetails(smh.getLocalString
158                               (getClass().getName() + ".debug1",
159                                "For Home Interface [ {0} ] Method [ {1} ]",
160                                new Object JavaDoc[] {c.getName(),methods[i].getName()}));
161                 result.addGoodDetails(smh.getLocalString
162                               (getClass().getName() + ".passed",
163                                "The [ {0} ] method which must throw javax.ejb.FinderException was found.",
164                                new Object JavaDoc[] {methods[i].getName()}));
165                 } else if (!throwsFinderException) {
166                 oneFailed = true;
167                 result.addErrorDetails(smh.getLocalString
168                    ("tests.componentNameConstructor",
169                     "For [ {0} ]",
170                     new Object JavaDoc[] {compName.toString()}));
171                 result.addErrorDetails(smh.getLocalString
172                                (getClass().getName() + ".debug1",
173                             "For Home Interface [ {0} ] Method [ {1} ]",
174                             new Object JavaDoc[] {c.getName(),methods[i].getName()}));
175                 result.addErrorDetails(smh.getLocalString
176                                (getClass().getName() + ".failed",
177                             "Error: A [ {0} ] method was found, but did not throw javax.ejb.FinderException.",
178                             new Object JavaDoc[] {methods[i].getName()}));
179                 } // end of reporting for this particular 'find' method
180
} // if the home interface found a "find" method
181
} // for all the methods within the home interface class, loop
182
return oneFailed;
183   
184         } catch (ClassNotFoundException JavaDoc e) {
185             Verifier.debug(e);
186             result.addErrorDetails(smh.getLocalString
187                        ("tests.componentNameConstructor",
188                         "For [ {0} ]",
189                         new Object JavaDoc[] {compName.toString()}));
190             result.failed(smh.getLocalString
191                   (getClass().getName() + ".failedException",
192                    "Error: Home interface [ {0} ] does not exist or is not loadable within bean [ {1} ]",
193                    new Object JavaDoc[] {home, descriptor.getName()}));
194             return oneFailed;
195         }
196   
197     }
198 }
199
Popular Tags