KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Vector JavaDoc;
34
35 /**
36  * Entity Bean's ejbCreate(...) methods name test.
37  * Each entity Bean class may define zero or more ejbCreate(...) 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 ejbCreate.
42  */

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

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