KickJava   Java API By Example, From Geeks To Geeks.

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


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 test.
38  *
39  * EJB class contains all ejbFind<METHOD>(...) methods declared in the bean
40  * class.
41  *
42  * The signatures of the finder methods must follow the following rules:
43  *
44  * A finder method name must start with the prefix ``ejbFind''
45  * (e.g. ejbFindByPrimaryKey, ejbFindLargeAccounts, ejbFindLateShipments).
46  *
47  */

48 public class EjbFinderMethodName extends EjbTest implements EjbCheck {
49
50
51     /**
52      * ejbFind<METHOD>(...) methods test.
53      *
54      * EJB class contains all ejbFind<METHOD>(...) methods declared in the bean
55      * class.
56      *
57      * The signatures of the finder methods must follow the following rules:
58      *
59      * A finder method name must start with the prefix ``ejbFind''
60      * (e.g. ejbFindByPrimaryKey, ejbFindLargeAccounts, ejbFindLateShipments).
61      *
62      * @param descriptor the Enterprise Java Bean deployment descriptor
63      *
64      * @return <code>Result</code> the results for this assertion
65      */

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