KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > cmp > CmpEjbCreateMethod


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.cmp;
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 with container managed persistence ejbCreate(...) and
36  * ejbPostCrete(...) methods must be defined to return the primary key class
37  * type. The implementation of the ejbCreate(...) methods should be coded to
38  * return a null. The returned value is ignored by the Container.
39  *
40  * The method signatures must follow these rules:
41  *
42  * The return type must be defined to return the primary key class type.
43  *
44  */

45 public class CmpEjbCreateMethod extends EjbTest implements EjbCheck {
46
47
48     /**
49      * Entity Bean's with container managed persistence ejbCreate(...) and
50      * ejbPostCrete(...) methods must be defined to return the primary key class
51      * type. The implementation of the ejbCreate(...) methods should be coded to
52      * return a null. The returned value is ignored by the Container.
53      *
54      * The method signatures must follow these rules:
55      *
56      * The return type must be defined to return the primary key class 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.CONTAINER_PERSISTENCE.equals(persistence)) {
71
72         boolean oneFailed = 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             Method [] methods1 = c.getDeclaredMethods();
79             int loopCounter = 0;
80   
81             boolean ejbCreateFound = false;
82             boolean returnsPrimaryKeyClassType = false;
83                     // start do while loop here....
84
do {
85                 Method [] methods = c.getDeclaredMethods();
86                 methods1 = null;
87                 methods1 = methods;
88             for (int i = 0; i < methods.length; i++) {
89                 // reset flags from last time thru loop
90
ejbCreateFound = false;
91                 loopCounter++;
92                 returnsPrimaryKeyClassType = false;
93   
94                 // The method name must be ejbCreate.
95
if (methods[i].getName().startsWith("ejbCreate")) {
96                 foundAtLeastOne++;
97                 ejbCreateFound = true;
98   
99                 // The return type must be be defined to
100
// return the primary key class type
101
Class JavaDoc rt = methods[i].getReturnType();
102                 Class JavaDoc str = Class.forName(((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName());
103                 do {
104                     if (rt.getName().equals(str.getName())) {
105                     returnsPrimaryKeyClassType = true;
106                     }
107                     str = str.getSuperclass();
108                 }while( str != null && returnsPrimaryKeyClassType != true ) ;
109   
110                 // now display the appropriate results for this particular ejbCreate
111
// method
112
if (ejbCreateFound && returnsPrimaryKeyClassType) {
113                     result.addGoodDetails(smh.getLocalString
114                                ("tests.componentNameConstructor",
115                                 "For [ {0} ]",
116                                 new Object JavaDoc[] {compName.toString()}));
117                     result.addGoodDetails(smh.getLocalString
118                               (getClass().getName() + ".debug1",
119                                "For EJB Class [ {0} ] ejbCreate(...) Method [ {1} ]",
120                                new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName()}));
121                     result.addGoodDetails(smh.getLocalString
122                               (getClass().getName() + ".passed",
123                                "[ {0} ] properly returns the primary key class type.",
124                                new Object JavaDoc[] {descriptor.getEjbClassName()}));
125                 } else if (ejbCreateFound && !returnsPrimaryKeyClassType) {
126                             oneFailed = true;
127                     result.addErrorDetails(smh.getLocalString
128                                ("tests.componentNameConstructor",
129                                 "For [ {0} ]",
130                                 new Object JavaDoc[] {compName.toString()}));
131                             result.addErrorDetails(smh.getLocalString
132                                (getClass().getName() + ".debug1",
133                                 "For EJB Class [ {0} ] ejbCreate(...) Method [ {1} ]",
134                                 new Object JavaDoc[] {descriptor.getEjbClassName(),methods1[loopCounter].getName()}));
135                             result.addErrorDetails(smh.getLocalString
136                                (getClass().getName() + ".failed",
137                                 "Error: An ejbCreate(...) method was found, but did not properly return the primary key class type."));
138                 }
139                 }
140             }
141                     } while (((c = c.getSuperclass()) != null) && (!(ejbCreateFound && returnsPrimaryKeyClassType)));
142           
143             if (foundAtLeastOne == 0) {
144             result.addNaDetails(smh.getLocalString
145                         ("tests.componentNameConstructor",
146                          "For [ {0} ]",
147                          new Object JavaDoc[] {compName.toString()}));
148             result.notApplicable(smh.getLocalString
149                          (getClass().getName() + ".notApplicable1",
150                           "[ {0} ] does not declare any ejbCreate(...) methods.",
151                           new Object JavaDoc[] {descriptor.getEjbClassName()}));
152             }
153
154         } catch (ClassNotFoundException JavaDoc e) {
155             Verifier.debug(e);
156             result.addErrorDetails(smh.getLocalString
157                        ("tests.componentNameConstructor",
158                         "For [ {0} ]",
159                         new Object JavaDoc[] {compName.toString()}));
160             result.failed(smh.getLocalString
161                   (getClass().getName() + ".failedException",
162                    "Error: [ {0} ] class not found.",
163                    new Object JavaDoc[] {descriptor.getEjbClassName()}));
164         }
165   
166         if (oneFailed) {
167             result.setStatus(result.FAILED);
168         } else if (foundAtLeastOne == 0) {
169             result.setStatus(result.NOT_APPLICABLE);
170         } else {
171             result.setStatus(result.PASSED);
172         }
173   
174         return result;
175
176         } else { // if (BEAN_PERSISTENCE.equals(persistence)) {
177
result.addNaDetails(smh.getLocalString
178                     ("tests.componentNameConstructor",
179                      "For [ {0} ]",
180                      new Object JavaDoc[] {compName.toString()}));
181         result.notApplicable(smh.getLocalString
182                      (getClass().getName() + ".notApplicable2",
183                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
184                       new Object JavaDoc[] {EjbEntityDescriptor.CONTAINER_PERSISTENCE,descriptor.getName(),persistence}));
185         return result;
186         }
187
188     } else {
189         result.addNaDetails(smh.getLocalString
190                 ("tests.componentNameConstructor",
191                  "For [ {0} ]",
192                  new Object JavaDoc[] {compName.toString()}));
193         result.notApplicable(smh.getLocalString
194                  (getClass().getName() + ".notApplicable",
195                   "[ {0} ] expected {1} bean, but called with {2} bean.",
196                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
197         return result;
198     }
199     }
200 }
201
Popular Tags