KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > findbyprimarykey > HomeInterfaceFindByPrimaryKeyArg


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.findbyprimarykey;
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  * Define findByPrimaryKey method parameter test.
38  *
39  * Every entity enterprise home interface must define the findByPrimaryKey
40  * method. The method must declare the primary key class as the method
41  * argument.
42  *
43  */

44 public class HomeInterfaceFindByPrimaryKeyArg extends EjbTest implements EjbCheck {
45     Result result = null;
46     ComponentNameConstructor compName = null;
47
48     /**
49      * Define findByPrimaryKey method parameter test.
50      *
51      * Every entity enterprise home interface must define the findByPrimaryKey
52      * method. The method must declare the primary key class as the method
53      * argument.
54      *
55      * @param descriptor the Enterprise Java Bean deployment descriptor
56      *
57      * @return <code>Result</code> the results for this assertion
58      */

59     public Result check(EjbDescriptor descriptor) {
60
61        result = getInitializedResult();
62        compName = getVerifierContext().getComponentNameConstructor();
63        boolean oneFailed = false;
64     if (descriptor instanceof EjbEntityDescriptor) {
65
66         if(descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName()))
67         oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(),descriptor);
68         if(oneFailed == false) {
69         if(descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName()))
70             oneFailed = commonToBothInterfaces(descriptor.getLocalHomeClassName(),descriptor);
71         }
72         
73         if (oneFailed) {
74         result.setStatus(result.FAILED);
75         } else {
76         result.setStatus(result.PASSED);
77         }
78         return result;
79
80     } else {
81         result.addNaDetails(smh.getLocalString
82                 ("tests.componentNameConstructor",
83                  "For [ {0} ]",
84                  new Object JavaDoc[] {compName.toString()}));
85         result.notApplicable(smh.getLocalString
86                  (getClass().getName() + ".notApplicable",
87                   "[ {0} ] expected {1} bean, but called with {2} bean.",
88                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
89         return result;
90     }
91     }
92
93     /**
94      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
95      * @param descriptor the Enterprise Java Bean deployment descriptor
96      * @param home for the Home interface of the Ejb.
97      * @return boolean the results for this assertion i.e if a test has failed or not
98      */

99
100
101     private boolean commonToBothInterfaces(String JavaDoc home, EjbDescriptor descriptor) {
102     Class JavaDoc [] ejbFinderMethodParameterTypes;
103     boolean findByPrimaryKeyMethodFound = false;
104     boolean foundOne = false;
105     boolean oneFailed = false;
106     boolean paramValid = false;
107     boolean onlyOneParam = false;
108     try {
109         // retrieve the home interface methods
110
Context context = getVerifierContext();
111         ClassLoader JavaDoc jcl = context.getClassLoader();
112         Class JavaDoc homeInterfaceClass = Class.forName(home, false, getVerifierContext().getClassLoader());
113         Method [] ejbFinderMethods = homeInterfaceClass.getDeclaredMethods();
114         
115         String JavaDoc primaryKeyType = ((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName();
116         
117         for (int j = 0; j < ejbFinderMethods.length; ++j) {
118         // reset all booleans for next method within the loop
119
paramValid = false;
120         onlyOneParam = false;
121         findByPrimaryKeyMethodFound = false;
122         
123         if (ejbFinderMethods[j].getName().equals("findByPrimaryKey")) {
124             // Every entity enterprise Bean class must define the
125
// findByPrimaryKey method. The result type for this method must
126
// be the primary key type (i.e. the findByPrimaryKey method
127
// must be a single-object finder).
128
findByPrimaryKeyMethodFound = true;
129             
130             ejbFinderMethodParameterTypes = ejbFinderMethods[j].getParameterTypes();
131             if (ejbFinderMethodParameterTypes.length == 1) {
132             onlyOneParam = true;
133             for (int k = 0; k < ejbFinderMethodParameterTypes.length; ++k) {
134                 if (ejbFinderMethodParameterTypes[k].getName().equals(primaryKeyType)) {
135                 paramValid = true;
136                 break;
137                 }
138             }
139             } else {
140             // should already be set...
141
onlyOneParam = false;
142             paramValid = false;
143             }
144             
145             // report for this particular findByPrimaryKey(...)
146
if (findByPrimaryKeyMethodFound && paramValid) {
147             result.addGoodDetails(smh.getLocalString
148                           ("tests.componentNameConstructor",
149                            "For [ {0} ]",
150                            new Object JavaDoc[] {compName.toString()}));
151             result.addGoodDetails(smh.getLocalString
152                           (getClass().getName() + ".debug1",
153                            "For Home interface [ {0} ] Finder Method [ {1} ]",
154                            new Object JavaDoc[] {homeInterfaceClass.getName(),ejbFinderMethods[j].getName()}));
155             result.addGoodDetails(smh.getLocalString
156                           (getClass().getName() + ".passed",
157                            "A findByPrimaryKey method with valid parameter type was found."));
158             foundOne = true;
159             break;
160             } else if (findByPrimaryKeyMethodFound && onlyOneParam && !paramValid) {
161             result.addNaDetails(smh.getLocalString
162                         ("tests.componentNameConstructor",
163                          "For [ {0} ]",
164                          new Object JavaDoc[] {compName.toString()}));
165             result.addNaDetails(smh.getLocalString
166                         (getClass().getName() + ".debug1",
167                          "For home interface [ {0} ] Finder Method [ {1} ]",
168                          new Object JavaDoc[] {homeInterfaceClass.getName(),ejbFinderMethods[j].getName()}));
169             result.addNaDetails(smh.getLocalString
170                         (getClass().getName() + ".notApplicable2",
171                          "A findByPrimaryKey method was found, but with non-PrimaryKeyClass arg parameter type."));
172             } else if (findByPrimaryKeyMethodFound && !onlyOneParam) {
173             result.addNaDetails(smh.getLocalString
174                           ("tests.componentNameConstructor",
175                            "For [ {0} ]",
176                            new Object JavaDoc[] {compName.toString()}));
177             result.addNaDetails(smh.getLocalString
178                         (getClass().getName() + ".debug1",
179                          "For home interface [ {0} ] Finder Method [ {1} ]",
180                          new Object JavaDoc[] {homeInterfaceClass.getName(),ejbFinderMethods[j].getName()}));
181             result.addNaDetails(smh.getLocalString
182                         (getClass().getName() + ".notApplicable1",
183                          "A findByPrimaryKey method was found, but with non-single arg parameters."));
184             }
185         }
186         }
187         if (!foundOne) {
188         oneFailed = true;
189         result.addErrorDetails(smh.getLocalString
190                        ("tests.componentNameConstructor",
191                     "For [ {0} ]",
192                     new Object JavaDoc[] {compName.toString()}));
193         result.addErrorDetails(smh.getLocalString
194                        (getClass().getName() + ".debug3",
195                     "For home interface [ {0} ]",
196                     new Object JavaDoc[] {homeInterfaceClass.getName()}));
197         result.addErrorDetails(smh.getLocalString
198                        (getClass().getName() + ".failed",
199                     "Error: No single arg findByPrimaryKey(PrimaryKeyClass) method was found in home interface class [ {0} ].",
200                     new Object JavaDoc[] {homeInterfaceClass.getName()}));
201         }
202         return oneFailed;
203     } catch (ClassNotFoundException JavaDoc e) {
204         Verifier.debug(e);
205         result.addErrorDetails(smh.getLocalString
206                    ("tests.componentNameConstructor",
207                     "For [ {0} ]",
208                     new Object JavaDoc[] {compName.toString()}));
209         result.failed(smh.getLocalString
210               (getClass().getName() + ".failedException",
211                "Error: Home interface [ {0} ] does not exist or is not loadable.",
212                new Object JavaDoc[] {home}));
213         return oneFailed;
214     }
215     
216     }
217 }
218
Popular Tags