KickJava   Java API By Example, From Geeks To Geeks.

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


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 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  * Entity beans home interface find<METHOD> method return type test.
38  *
39  * The following are the requirements for the signatures of the finder methods
40  * defined in Bean's home interface:
41  *
42  * An Entity Bean's home interface defines one or more find<METHOD>(...)
43  * methods.
44  *
45  * The return type for a find<METHOD>(...) method must be the enterprise
46  * Bean's remote interface type or a collection thereof.
47  *
48  */

49 public class HomeInterfaceFindMethodReturn extends EjbTest implements EjbCheck {
50
51     Result result = null;
52     ComponentNameConstructor compName = null;
53     /**
54      * Entity beans home interface find<METHOD> method return type test.
55      *
56      * The following are the requirements for the signatures of the finder methods
57      * defined in Bean's home interface:
58      *
59      * An Entity Bean's home interface defines one or more find<METHOD>(...)
60      * methods.
61      *
62      * The return type for a find<METHOD>(...) method must be the enterprise
63      * Bean's remote interface type or a collection thereof.
64      *
65      * @param descriptor the Enterprise Java Bean deployment descriptor
66      *
67      * @return <code>Result</code> the results for this assertion
68      */

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

130
131     private boolean commonToBothInterfaces(String JavaDoc home, String JavaDoc remote, EjbDescriptor descriptor) {
132     boolean oneFailed = false;
133     try {
134             Context context = getVerifierContext();
135         ClassLoader JavaDoc jcl = context.getClassLoader();
136             Class JavaDoc c = Class.forName(home, false, getVerifierContext().getClassLoader());
137             Class JavaDoc rc = Class.forName(remote, false, getVerifierContext().getClassLoader());
138             Method methods[] = c.getDeclaredMethods();
139             Class JavaDoc methodReturnType;
140             boolean validReturn = false;
141   
142             for (int i=0; i< methods.length; i++) {
143             // clear these from last time thru loop
144
validReturn = false;
145             if (methods[i].getName().startsWith("find")) {
146                 // return type must be the remote interface
147
// or collection thereof
148
methodReturnType = methods[i].getReturnType();
149                 if ((methodReturnType.getName().equals(rc.getName())) ||
150                 (methodReturnType.getName().equals("java.util.Collection")) ||
151                 (methodReturnType.getName().equals("java.util.Enumeration"))) {
152                 // this is the right return type for find method
153
validReturn = true;
154                 } else {
155                 validReturn = false;
156                 } // return valid
157

158                 //report for this particular find method found in home interface
159
// now display the appropriate results for this particular find
160
// method
161
if (validReturn) {
162                 result.addGoodDetails(smh.getLocalString
163                    ("tests.componentNameConstructor",
164                     "For [ {0} ]",
165                     new Object JavaDoc[] {compName.toString()}));
166                 result.addGoodDetails(smh.getLocalString
167                               (getClass().getName() + ".debug1",
168                                "For Home Interface [ {0} ] Method [ {1} ]",
169                                new Object JavaDoc[] {c.getName(),methods[i].getName()}));
170                 result.addGoodDetails(smh.getLocalString
171                               (getClass().getName() + ".passed",
172                                "The find<METHOD> which returns remote interface or a collection there of was found."));
173                 } else if (!validReturn) {
174                 oneFailed = true;
175                 result.addErrorDetails(smh.getLocalString
176                    ("tests.componentNameConstructor",
177                     "For [ {0} ]",
178                     new Object JavaDoc[] {compName.toString()}));
179                 result.addErrorDetails(smh.getLocalString
180                                (getClass().getName() + ".debug1",
181                             "For Home Interface [ {0} ] Method [ {1} ]",
182                             new Object JavaDoc[] {c.getName(),methods[i].getName()}));
183                 result.addErrorDetails(smh.getLocalString
184                                (getClass().getName() + ".failed",
185                             "Error: A find<METHOD> was found, but the return type [ {0} ] was not the Remote interface [ {1} ] or a collection there of." ,
186                             new Object JavaDoc[] {methodReturnType.getName(),rc.getName()}));
187                 } // end of reporting for this particular 'find' method
188
} // if the home interface found a "find" method
189
} // for all the methods within the home interface class, loop
190
return oneFailed;
191   
192         } catch (ClassNotFoundException JavaDoc e) {
193             Verifier.debug(e);
194             result.addErrorDetails(smh.getLocalString
195                        ("tests.componentNameConstructor",
196                         "For [ {0} ]",
197                         new Object JavaDoc[] {compName.toString()}));
198             result.failed(smh.getLocalString
199                   (getClass().getName() + ".failedException",
200                    "Error: Home interface [ {0} ] or Remote interface [ {1} ] does not exist or is not loadable within bean [ {2} ]",
201                    new Object JavaDoc[] {home,remote, descriptor.getName()}));
202             return oneFailed;
203         }
204     }
205 }
206
Popular Tags