KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > ejbcreatemethod > EjbCreateMethodReturn


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.ejbcreatemethod;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import java.lang.ClassLoader JavaDoc;
27 import com.sun.enterprise.tools.verifier.tests.*;
28 import javax.ejb.EntityBean JavaDoc;
29 import java.lang.reflect.*;
30 import com.sun.enterprise.deployment.*;
31 import com.sun.enterprise.tools.verifier.*;
32 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
33
34 /**
35  * Entity Bean's ejbCreate(...) methods return test.
36  * Each entity Bean class may define zero or more ejbCreate(...) methods.
37  * The number and signatures of a entity Bean's create methods are specific
38  * to each EJB class. The method signatures must follow these rules:
39  *
40  * The method name must be ejbCreate.
41  *
42  * The return type must be primary key type.
43  *
44  */

45 public class EjbCreateMethodReturn extends EjbTest implements EjbCheck {
46
47
48     /**
49      * Entity Bean's ejbCreate(...) methods return test.
50      * Each entity Bean class may define zero or more ejbCreate(...) methods.
51      * The number and signatures of a entity Bean's create methods are specific
52      * to each EJB class. The method signatures must follow these rules:
53      *
54      * The method name must be ejbCreate.
55      *
56      * The return type must be primary key type.
57      *
58      * @param descriptor the Enterprise Java Bean deployment descriptor
59      *
60      * @return <code>Result</code> the results for this assertion
61      */

62     public Result check(EjbDescriptor descriptor) {
63
64     Result result = getInitializedResult();
65     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
66
67     if (descriptor instanceof EjbEntityDescriptor) {
68         String JavaDoc persistence =
69         ((EjbEntityDescriptor)descriptor).getPersistenceType();
70         if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistence)) {
71         boolean oneFailed = false;
72                 boolean oneWarning = false;
73         int foundAtLeastOne = 0;
74         try {
75             Context context = getVerifierContext();
76             ClassLoader JavaDoc jcl = context.getClassLoader();
77             Class JavaDoc c = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
78   
79             String JavaDoc primaryKeyType = ((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName();
80             boolean ejbCreateFound = false;
81             boolean returnsPrimaryKeyType = false;
82                     // start do while loop here....
83
do {
84             Method [] methods = c.getDeclaredMethods();
85             for (int i = 0; i < methods.length; i++) {
86                 // reset flags from last time thru loop
87
ejbCreateFound = false;
88                 returnsPrimaryKeyType = false;
89                 // The method name must be ejbCreate.
90
if (methods[i].getName().startsWith("ejbCreate")) {
91                 foundAtLeastOne++;
92                 ejbCreateFound = true;
93   
94                 // The return type must be primary key type.
95
Class JavaDoc rt = methods[i].getReturnType();
96                 if (rt.getName().equals(primaryKeyType)) {
97                     returnsPrimaryKeyType = true;
98                 }
99   
100                 // now display the appropriate results for this particular ejbCreate
101
// method
102
if (ejbCreateFound && !returnsPrimaryKeyType) {
103                                     if (primaryKeyType.equals("java.lang.Object")) {
104                                         oneWarning = true;
105                     result.addWarningDetails(smh.getLocalString
106                                  ("tests.componentNameConstructor",
107                                   "For [ {0} ]",
108                                   new Object JavaDoc[] {compName.toString()}));
109                         result.addWarningDetails(smh.getLocalString
110                                  (getClass().getName() + ".debug1",
111                                   "For EJB Class [ {0} ] method [ {1} ]",
112                                   new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName()}));
113                         result.addWarningDetails(smh.getLocalString
114                                  (getClass().getName() + ".warning",
115                                   "Warning: An [ {0} ] method was found, but [ {1} ] method has [ {2} ] return type. Deployment descriptor primary key type [ {3} ]. Definition of the primary key type is deferred to deployment time ?",
116                                   new Object JavaDoc[] {methods[i].getName(),methods[i].getName(),methods[i].getReturnType().getName(),primaryKeyType}));
117                                     } else {
118                         oneFailed = true;
119                     result.addErrorDetails(smh.getLocalString
120                                    ("tests.componentNameConstructor",
121                                 "For [ {0} ]",
122                                 new Object JavaDoc[] {compName.toString()}));
123                         result.addErrorDetails(smh.getLocalString
124                                    (getClass().getName() + ".debug1",
125                                 "For EJB Class [ {0} ] method [ {1} ]",
126                                 new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName()}));
127                         result.addErrorDetails(smh.getLocalString
128                                    (getClass().getName() + ".failed",
129                                 "Error: An [ {0} ] method was found, but [ {1} ] method has illegal return value. [ {2} ] methods must return primary key type [ {3} ].",
130                                 new Object JavaDoc[] {methods[i].getName(),methods[i].getName(),methods[i].getName(),primaryKeyType}));
131                     break;
132                                     }
133                 }
134                 }
135             }
136             if (oneFailed == true)
137                 break;
138                     } while (((c = c.getSuperclass()) != null) && (foundAtLeastOne == 0));
139           
140             if (foundAtLeastOne == 0) {
141             result.addNaDetails(smh.getLocalString
142                           ("tests.componentNameConstructor",
143                            "For [ {0} ]",
144                            new Object JavaDoc[] {compName.toString()}));
145             result.notApplicable(smh.getLocalString
146                          (getClass().getName() + ".notApplicable0",
147                           "[ {0} ] does not declare any ejbCreate(...) methods.",
148                           new Object JavaDoc[] {descriptor.getEjbClassName()}));
149             }
150             if (oneFailed == false && foundAtLeastOne > 0) {
151             result.addGoodDetails(smh.getLocalString
152                           ("tests.componentNameConstructor",
153                            "For [ {0} ]",
154                            new Object JavaDoc[] {compName.toString()}));
155             result.addGoodDetails(smh.getLocalString
156                           (getClass().getName() + ".debug1",
157                            "For EJB Class [ {0} ]",
158                            new Object JavaDoc[] {descriptor.getEjbClassName()}));
159             result.addGoodDetails(smh.getLocalString
160                           (getClass().getName() + ".passed",
161                            "[ {0} ] properly declares ejbCreate<method> method to return primary key type [ {1} ].",
162                            new Object JavaDoc[] {descriptor.getEjbClassName(), primaryKeyType}));
163             }
164         } catch (ClassNotFoundException JavaDoc e) {
165             Verifier.debug(e);
166             result.addErrorDetails(smh.getLocalString
167                        ("tests.componentNameConstructor",
168                         "For [ {0} ]",
169                         new Object JavaDoc[] {compName.toString()}));
170             result.failed(smh.getLocalString
171                   (getClass().getName() + ".failedException",
172                    "Error: [ {0} ] class not found.",
173                    new Object JavaDoc[] {descriptor.getEjbClassName()}));
174             oneFailed = true;
175         }
176   
177         if (oneFailed) {
178                     result.setStatus(result.FAILED);
179                 } else if (foundAtLeastOne == 0) {
180                     result.setStatus(result.NOT_APPLICABLE);
181                 } else {
182                     if (oneWarning) {
183                         result.setStatus(result.WARNING);
184                     } else {
185             
186                         result.setStatus(result.PASSED);
187                     }
188                 }
189   
190         return result;
191  
192         } else { // if (CONTAINER_PERSISTENCE.equals(persistence)) {
193
result.addNaDetails(smh.getLocalString
194                           ("tests.componentNameConstructor",
195                            "For [ {0} ]",
196                            new Object JavaDoc[] {compName.toString()}));
197         result.notApplicable(smh.getLocalString
198                      (getClass().getName() + ".notApplicable1",
199                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
200                       new Object JavaDoc[] {EjbEntityDescriptor.BEAN_PERSISTENCE,descriptor.getName(),persistence}));
201         return result;
202         }
203
204     } else {
205         result.addNaDetails(smh.getLocalString
206                           ("tests.componentNameConstructor",
207                            "For [ {0} ]",
208                            new Object JavaDoc[] {compName.toString()}));
209         result.notApplicable(smh.getLocalString
210                  (getClass().getName() + ".notApplicable",
211                   "[ {0} ] expected {1} bean, but called with {2} bean.",
212                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
213         return result;
214     }
215     }
216 }
217
Popular Tags