KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > ejbfindermethod > EjbFinderMethodNameCmp


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.ejbfindermethod;
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  * ejbFind<METHOD>(...) methods not defined for entity bean with container
38  * managed persistence test.
39  *
40  * EJB class contains no ejbFind<METHOD>(...) methods declared in the bean
41  * class when the bean is container managed.
42  *
43  * A finder method name must start with the prefix ``ejbFind''
44  * (e.g. ejbFindByPrimaryKey, ejbFindLargeAccounts, ejbFindLateShipments).
45  *
46  */

47 public class EjbFinderMethodNameCmp extends EjbTest implements EjbCheck {
48
49
50     /**
51      * ejbFind<METHOD>(...) methods not defined for entity bean with container
52      * managed persistence test.
53      *
54      * EJB class contains no ejbFind<METHOD>(...) methods declared in the bean
55      * class when the bean is container managed.
56      *
57      * A finder method name must start with the prefix ``ejbFind''
58      * (e.g. ejbFindByPrimaryKey, ejbFindLargeAccounts, ejbFindLateShipments).
59      *
60      * @param descriptor the Enterprise Java Bean deployment descriptor
61      *
62      * @return <code>Result</code> the results for this assertion
63      */

64     public Result check(EjbDescriptor descriptor) {
65
66     Result result = getInitializedResult();
67     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
68
69     if (descriptor instanceof EjbEntityDescriptor) {
70         String JavaDoc persistence =
71         ((EjbEntityDescriptor)descriptor).getPersistenceType();
72         if (EjbEntityDescriptor.CONTAINER_PERSISTENCE.equals(persistence)) {
73
74         boolean oneFailed = false;
75         int foundAtLeastOne = 0;
76         try {
77     
78             // retrieve the home interface methods
79
Context context = getVerifierContext();
80             ClassLoader JavaDoc jcl = context.getClassLoader();
81           
82             // retrieve the EJB Class Methods
83
Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
84                     // start do while loop here....
85
do {
86             Method [] ejbFinderMethods = EJBClass.getDeclaredMethods();
87           
88             for (int j = 0; j < ejbFinderMethods.length; ++j) {
89                 if (ejbFinderMethods[j].getName().startsWith("ejbFind")) {
90                 foundAtLeastOne++;
91                 if (!oneFailed) {
92                     oneFailed = true;
93                 }
94                 result.addErrorDetails(smh.getLocalString
95                        ("tests.componentNameConstructor",
96                     "For [ {0} ]",
97                     new Object JavaDoc[] {compName.toString()}));
98                 result.addErrorDetails(smh.getLocalString
99                                (getClass().getName() + ".debug1",
100                             "For EJB Class [ {0} ] method [ {1} ]",
101                             new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
102                 result.addErrorDetails(smh.getLocalString
103                                (getClass().getName() + ".failed",
104                             "Error: An improperly named [ {0} ] method was found.",
105                             new Object JavaDoc[] {ejbFinderMethods[j].getName()}));
106                 }
107             }
108                     } while (((EJBClass = EJBClass.getSuperclass()) != null));
109            
110             if (foundAtLeastOne == 0){
111             result.addGoodDetails(smh.getLocalString
112                           ("tests.componentNameConstructor",
113                            "For [ {0} ]",
114                            new Object JavaDoc[] {compName.toString()}));
115             result.passed(smh.getLocalString
116                       (getClass().getName() + ".passed",
117                        "[ {0} ] does not declare any ejbFind<METHOD>(...) methods.",
118                        new Object JavaDoc[] {descriptor.getEjbClassName()}));
119             }
120   
121         } catch (ClassNotFoundException JavaDoc e) {
122             Verifier.debug(e);
123             result.addErrorDetails(smh.getLocalString
124                        ("tests.componentNameConstructor",
125                     "For [ {0} ]",
126                     new Object JavaDoc[] {compName.toString()}));
127             result.failed(smh.getLocalString
128                   (getClass().getName() + ".failedException",
129                    "Error: EJB Class [ {1} ] does not exist or is not loadable.",
130                    new Object JavaDoc[] {descriptor.getEjbClassName()}));
131             oneFailed = true;
132         }
133     
134         if (oneFailed) {
135             result.setStatus(result.FAILED);
136         } else {
137             result.setStatus(result.PASSED);
138         }
139     
140         return result;
141
142         } else { // if (BEAN_PERSISTENCE.equals(persistence))
143
result.addNaDetails(smh.getLocalString
144                        ("tests.componentNameConstructor",
145                     "For [ {0} ]",
146                     new Object JavaDoc[] {compName.toString()}));
147         result.notApplicable(smh.getLocalString
148                      (getClass().getName() + ".notApplicable1",
149                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
150                       new Object JavaDoc[] {EjbEntityDescriptor.BEAN_PERSISTENCE,descriptor.getName(),persistence}));
151         return result;
152         }
153
154     } else {
155         result.addNaDetails(smh.getLocalString
156                        ("tests.componentNameConstructor",
157                     "For [ {0} ]",
158                     new Object JavaDoc[] {compName.toString()}));
159         result.notApplicable(smh.getLocalString
160                  (getClass().getName() + ".notApplicable",
161                   "[ {0} ] expected {1} bean, but called with {2} bean.",
162                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
163         return result;
164     }
165     }
166 }
167
Popular Tags