KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
27 import java.lang.ClassLoader JavaDoc;
28 import com.sun.enterprise.tools.verifier.tests.*;
29 import java.util.*;
30 import java.lang.reflect.*;
31 import com.sun.enterprise.tools.verifier.*;
32 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbUtils;
34
35 /**
36  * Entity beans home interface find<METHOD> method throws
37  * javax.ejb.FinderException test.
38  *
39  * The following are the requirements for the enterprise Bean's home interface
40  * find<METHOD> signature:
41  *
42  * The find<METHOD> must have a query associated with it (except findByPrimaryKey).
43  */

44 public class HomeInterfaceFindMethodHasQuery extends EjbTest implements EjbCheck {
45     Result result = null;
46     ComponentNameConstructor compName = null;
47     private final static String JavaDoc FINDBYPRIMARYKEY = "findByPrimaryKey";
48
49
50     /**
51      * Entity beans home interface find<METHOD> method throws
52      * javax.ejb.FinderException test.
53      *
54      * The following are the requirements for the enterprise Bean's home interface
55      * find<METHOD> signature:
56      *
57      *The find<METHOD> must have a query associated with it (except findByPrimaryKey).
58      *
59      * @param descriptor the Enterprise Java Bean deployment descriptor
60      *
61      * @return <code>Result</code> the results for this assertion
62      */

63     public Result check(EjbDescriptor descriptor) {
64
65         result = getInitializedResult();
66     compName = getVerifierContext().getComponentNameConstructor();
67     boolean oneFailed = false;
68     if (descriptor instanceof EjbEntityDescriptor) {
69         String JavaDoc persistence =
70         ((EjbEntityDescriptor)descriptor).getPersistenceType();
71         if (EjbEntityDescriptor.CONTAINER_PERSISTENCE.equals(persistence)) {
72                 if (((EjbCMPEntityDescriptor) descriptor).getCMPVersion()==EjbCMPEntityDescriptor.CMP_2_x) {
73                     if(descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName())) {
74                         oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(),descriptor, MethodDescriptor.EJB_HOME);
75                     }
76                     if(oneFailed == false) {
77                         if(descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName())) {
78                             oneFailed = commonToBothInterfaces(descriptor.getLocalHomeClassName(),descriptor, MethodDescriptor.EJB_LOCALHOME);
79                         }
80                     }
81                     if (oneFailed) {
82                         result.setStatus(result.FAILED);
83                     } else {
84                         result.setStatus(result.PASSED);
85                     }
86                     return result;
87                 }
88         }
89             //if (Bean_PERSISTENCE.equals(persistence)) or wrong version
90
result.addNaDetails(smh.getLocalString
91                    ("tests.componentNameConstructor",
92                     "For [ {0} ]",
93                     new Object JavaDoc[] {compName.toString()}));
94             result.notApplicable(smh.getLocalString
95                  (getClass().getName() + ".notApplicable2",
96                              "Expected [ {0} {1} ] managed persistence, but [ {2} ] bean has [ {3} ] managed persistence.",
97                             new Object JavaDoc[] {EjbEntityDescriptor.CONTAINER_PERSISTENCE, new Integer JavaDoc(EjbCMPEntityDescriptor.CMP_2_x), descriptor.getName(),persistence}));
98             return result;
99         
100     } else {
101         result.addNaDetails(smh.getLocalString
102                    ("tests.componentNameConstructor",
103                     "For [ {0} ]",
104                     new Object JavaDoc[] {compName.toString()}));
105         result.notApplicable(smh.getLocalString
106                  (getClass().getName() + ".notApplicable",
107                   "[ {0} ] expected {1} bean, but called with {2} bean.",
108                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
109         return result;
110     }
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      * @param methodIntf is the interface type
118      * @return boolean the results for this assertion i.e if a test has failed or not
119      */

120
121     private boolean commonToBothInterfaces(String JavaDoc home, EjbDescriptor descriptor, String JavaDoc methodIntf) {
122         boolean oneFailed = false;
123     // RULE: Entity home interface are only allowed to have find<METHOD>
124
// methods which must throw javax.ejb.FinderException
125
try {
126         PersistenceDescriptor pers = ((EjbCMPEntityDescriptor)descriptor).getPersistenceDescriptor();
127
128         Context context = getVerifierContext();
129         ClassLoader JavaDoc jcl = context.getClassLoader();
130         Class JavaDoc c = Class.forName(home, false, getVerifierContext().getClassLoader());
131         Method methods[] = c.getDeclaredMethods();
132         
133         for (int i=0; i< methods.length; i++) {
134             if (methods[i].getName().startsWith("find") && !(methods[i].getName()).equals(FINDBYPRIMARYKEY)) {
135             QueryDescriptor query = pers.getQueryFor(new MethodDescriptor(methods[i], methodIntf));
136             if (query != null) {
137                 if (query.getQuery() != null && !"".equals(query.getQuery())) {
138                     result.addGoodDetails(smh.getLocalString
139                           ("tests.componentNameConstructor",
140                            "For [ {0} ]",
141                            new Object JavaDoc[] {compName.toString()}));
142                 result.addGoodDetails(smh.getLocalString
143                           (getClass().getName() + ".debug1",
144                            "For Home Interface [ {0} ] Method [ {1} ]",
145                            new Object JavaDoc[] {c.getName(),methods[i].getName()}));
146                 result.addGoodDetails(smh.getLocalString
147                           (getClass().getName() + ".passed",
148                            "The [ {0} ] method has a query assigned to it",
149                            new Object JavaDoc[] {methods[i].getName()}));
150             } else {
151                 oneFailed = true;
152                 result.addErrorDetails(smh.getLocalString
153                            ("tests.componentNameConstructor",
154                             "For [ {0} ]",
155                             new Object JavaDoc[] {compName.toString()}));
156                 result.addErrorDetails(smh.getLocalString
157                            (getClass().getName() + ".debug1",
158                             "For Home Interface [ {0} ] Method [ {1} ]",
159                             new Object JavaDoc[] {c.getName(),methods[i].getName()}));
160                 result.addErrorDetails(smh.getLocalString
161                            (getClass().getName() + ".failed",
162                             "Error: A [ {0} ] method was found, but did not have a query element assigned",
163                             new Object JavaDoc[] {methods[i].getName()}));
164             } // end of reporting for this particular 'find' method
165
}
166             else {
167                 oneFailed = true;
168             result.addErrorDetails(smh.getLocalString
169                            ("tests.componentNameConstructor",
170                         "For [ {0} ]",
171                         new Object JavaDoc[] {compName.toString()}));
172             result.addErrorDetails(smh.getLocalString
173                            (getClass().getName() + ".debug1",
174                         "For Home Interface [ {0} ] Method [ {1} ]",
175                         new Object JavaDoc[] {c.getName(),methods[i].getName()}));
176             result.addErrorDetails(smh.getLocalString
177                            (getClass().getName() + ".failed",
178                         "Error: A [ {0} ] method was found, but did not have a query element assigned",
179                         new Object JavaDoc[] {methods[i].getName()}));
180                 
181             }
182         } // if the home interface found a "find" method
183

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