KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > ejbpostcreatemethod > EjbPostCreateMethodArgs


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.ejbpostcreatemethod;
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 java.util.*;
32 import com.sun.enterprise.tools.verifier.*;
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
34
35 /**
36  * Entity Bean's ejbPostCreate(...) methods test.
37  * Each entity Bean class may define zero or more ejbPostCreate(...) methods.
38  * The number and signatures of a entity Bean's create methods are specific
39  * to each EJB class. The method signatures must follow these rules:
40  *
41  * The method name must be ejbPostCreate.
42  *
43  * The methods arguments must be the same as the arguments of the
44  * matching ejbCreate(...) method.
45  *
46  */

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

65     public Result check(EjbDescriptor descriptor) {
66
67     Result result = getInitializedResult();
68     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
69
70     if (descriptor instanceof EjbEntityDescriptor) {
71         boolean oneFailed = false;
72         int foundAtLeastOne = 0;
73         try {
74         Context context = getVerifierContext();
75         ClassLoader JavaDoc jcl = context.getClassLoader();
76         Class JavaDoc c = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
77
78         Class JavaDoc [] ejbPostCreateMethodParameterTypes;
79         Class JavaDoc [] ejbCreateMethodParameterTypes;
80         boolean signaturesMatch = false;
81         boolean ejbCreateExists = false;
82
83         Method [] methods = c.getDeclaredMethods();
84         Vector<Method> createMethodSuffix = new Vector<Method>();
85         for (int i = 0; i < methods.length; i++) {
86             // The method name must start with create.
87
if (methods[i].getName().startsWith("ejbCreate")) {
88             createMethodSuffix.addElement((Method)methods[i]);
89             ejbCreateExists = true;
90             }
91         }
92         
93                 // start do while loop here....
94
do {
95             for (int i = 0; i < methods.length; i++) {
96             // reset flags from last time thru loop
97
signaturesMatch = false;
98             
99             // The method name must be ejbPostCreate.
100
if (methods[i].getName().startsWith("ejbPostCreate")) {
101                 foundAtLeastOne++;
102
103                 String JavaDoc matchSuffix = methods[i].getName().substring(13);
104                 for (int k = 0; k < createMethodSuffix.size(); k++) {
105                 if (matchSuffix.equals(((Method)createMethodSuffix.elementAt(k)).getName().substring(9))) {
106                     ejbCreateMethodParameterTypes = ((Method)createMethodSuffix.elementAt(k)).getParameterTypes();
107                     ejbPostCreateMethodParameterTypes = methods[i].getParameterTypes();
108                     if (Arrays.equals(ejbCreateMethodParameterTypes,ejbPostCreateMethodParameterTypes)) {
109                     signaturesMatch = true;
110                     break;
111                     }
112                 }
113                 }
114                 if (signaturesMatch) {
115                 result.addGoodDetails(smh.getLocalString
116                        ("tests.componentNameConstructor",
117                     "For [ {0} ]",
118                     new Object JavaDoc[] {compName.toString()}));
119                 result.addGoodDetails(smh.getLocalString
120                               (getClass().getName() + ".debug1",
121                                "For EJB Class [ {0} ] method [ {1} ]",
122                                new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName()}));
123                 result.addGoodDetails(smh.getLocalString
124                               (getClass().getName() + ".passed",
125                                "[ {0} ] declares [ {1} ] method with parameters that match corresponding [ {2} ] method.",
126                                new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName(), "ejbCreate<method>"}));
127                 } else if (!signaturesMatch) {
128                 oneFailed = true;
129                 result.addErrorDetails(smh.getLocalString
130                        ("tests.componentNameConstructor",
131                     "For [ {0} ]",
132                     new Object JavaDoc[] {compName.toString()}));
133                 result.addErrorDetails(smh.getLocalString
134                                (getClass().getName() + ".debug1",
135                             "For EJB Class [ {0} ] method [ {1} ]",
136                             new Object JavaDoc[] {descriptor.getEjbClassName(),methods[i].getName()}));
137                 result.addErrorDetails(smh.getLocalString
138                                (getClass().getName() + ".failed",
139                             "Error: An [ {0} ] method was found, but [ {1} ] method parameters did not match any corresponding [ {2} ] method parameters.",
140                             new Object JavaDoc[] {methods[i].getName(),methods[i].getName(),"ejbCreate<method>"}));
141                 break;
142                 }
143             }
144             }
145             if (oneFailed == true)
146             break;
147                 } while (((c = c.getSuperclass()) != null) && (foundAtLeastOne == 0));
148         
149         if (ejbCreateExists == false) {
150             result.addNaDetails(smh.getLocalString
151                        ("tests.componentNameConstructor",
152                     "For [ {0} ]",
153                     new Object JavaDoc[] {compName.toString()}));
154             result.notApplicable(smh.getLocalString
155                      (getClass().getName() + ".notApplicable1",
156                       "[ {0} ] does not declare any ejbPostCreate(...) methods.",
157                       new Object JavaDoc[] {descriptor.getEjbClassName()}));
158         }
159         if (foundAtLeastOne == 0 && ejbCreateExists == true){
160             oneFailed=true;
161             result.addErrorDetails(smh.getLocalString
162                        ("tests.componentNameConstructor",
163                     "For [ {0} ]",
164                     new Object JavaDoc[] {compName.toString()}));
165             result.failed(smh.getLocalString
166                   (getClass().getName() + ".failedException1",
167                    "Error: ejbPostCreate<Method> method corresponding to the ejbCreate<Method> method does not exist!",
168                    new Object JavaDoc[] {}));
169             
170         }
171         } catch (ClassNotFoundException JavaDoc e) {
172         Verifier.debug(e);
173         result.addErrorDetails(smh.getLocalString
174                        ("tests.componentNameConstructor",
175                     "For [ {0} ]",
176                     new Object JavaDoc[] {compName.toString()}));
177         result.failed(smh.getLocalString
178                   (getClass().getName() + ".failedException",
179                    "Error: [ {0} ] class not found.",
180                    new Object JavaDoc[] {descriptor.getEjbClassName()}));
181         oneFailed = true;
182         }
183
184         if (oneFailed) {
185         result.setStatus(result.FAILED);
186             } else if (foundAtLeastOne == 0) {
187                 result.setStatus(result.NOT_APPLICABLE);
188         } else {
189         result.setStatus(result.PASSED);
190         }
191
192         return result;
193  
194     } else {
195         result.addNaDetails(smh.getLocalString
196                        ("tests.componentNameConstructor",
197                     "For [ {0} ]",
198                     new Object JavaDoc[] {compName.toString()}));
199         result.notApplicable(smh.getLocalString
200                  (getClass().getName() + ".notApplicable",
201                   "[ {0} ] expected {1} bean, but called with {2} bean.",
202                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
203         return result;
204     }
205     }
206 }
207
Popular Tags