KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > ejbfindbyprimarykey > EjbFindByPrimaryKeyStatic


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.ejbfindbyprimarykey;
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  * Define ejbFindByPrimaryKey method static test.
38  *
39  * Every entity enterprise Bean class must define the ejbFindByPrimaryKey
40  * method.
41  *
42  * The method must not be declared as static.
43  *
44  */

45 public class EjbFindByPrimaryKeyStatic extends EjbTest implements EjbCheck {
46
47
48     /**
49      * Define ejbFindByPrimaryKey method static test.
50      *
51      * Every entity enterprise Bean class must define the ejbFindByPrimaryKey
52      * method.
53      *
54      * The method must not be declared as static.
55      *
56      * @param descriptor the Enterprise Java Bean deployment descriptor
57      *
58      * @return <code>Result</code> the results for this assertion
59      */

60     public Result check(EjbDescriptor descriptor) {
61
62     Result result = getInitializedResult();
63     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
64
65     if (descriptor instanceof EjbEntityDescriptor) {
66         String JavaDoc persistentType =
67         ((EjbEntityDescriptor)descriptor).getPersistenceType();
68         if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistentType)) {
69
70         boolean ejbFindByPrimaryKeyMethodFound = false;
71         boolean isStatic = false;
72         boolean oneFailed = false;
73         int findMethodModifiers = 0;
74         try {
75             // retrieve the EJB Class Methods
76
Context context = getVerifierContext();
77         ClassLoader JavaDoc jcl = context.getClassLoader();
78             Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
79                     // start do while loop here....
80
do {
81             Method [] ejbFinderMethods = EJBClass.getDeclaredMethods();
82           
83             for (int j = 0; j < ejbFinderMethods.length; ++j) {
84                 if (ejbFinderMethods[j].getName().equals("ejbFindByPrimaryKey")) {
85                 // Every entity enterprise Bean class must define the
86
// ejbFindByPrimaryKey method.
87
ejbFindByPrimaryKeyMethodFound = true;
88   
89                 // The method must not be declared as static.
90
findMethodModifiers = ejbFinderMethods[j].getModifiers();
91                 if (Modifier.isStatic(findMethodModifiers)) {
92                     isStatic = true;
93                 }
94     
95                 if (ejbFindByPrimaryKeyMethodFound && !isStatic) {
96                     result.addGoodDetails(smh.getLocalString
97                        ("tests.componentNameConstructor",
98                     "For [ {0} ]",
99                     new Object JavaDoc[] {compName.toString()}));
100                     result.addGoodDetails(smh.getLocalString
101                               (getClass().getName() + ".debug1",
102                                "For EJB Class [ {0} ] Finder Method [ {1} ]",
103                                new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
104                     result.addGoodDetails(smh.getLocalString
105                               (getClass().getName() + ".passed",
106                                "A non-static [ {0} ] method was found.",
107                                new Object JavaDoc[] {ejbFinderMethods[j].getName()}));
108                 } else if (ejbFindByPrimaryKeyMethodFound && isStatic) {
109                     oneFailed = true;
110                     result.addErrorDetails(smh.getLocalString
111                        ("tests.componentNameConstructor",
112                     "For [ {0} ]",
113                     new Object JavaDoc[] {compName.toString()}));
114                     result.addErrorDetails(smh.getLocalString
115                                (getClass().getName() + ".debug1",
116                                 "For EJB Class [ {0} ] Finder Method [ {1} ]",
117                                 new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
118                     result.addErrorDetails(smh.getLocalString
119                                (getClass().getName() + ".failed",
120                                 "Error: A static [ {0} ] method was found, but [ {1} ] cannot be declared as static.",
121                                 new Object JavaDoc[] {ejbFinderMethods[j].getName(),ejbFinderMethods[j].getName()}));
122                 }
123                 // found one, and there should only be one, break out
124
break;
125                 }
126             }
127                     } while (((EJBClass = EJBClass.getSuperclass()) != null) && (!ejbFindByPrimaryKeyMethodFound));
128   
129             if (!ejbFindByPrimaryKeyMethodFound) {
130             oneFailed = true;
131             result.addErrorDetails(smh.getLocalString
132                        ("tests.componentNameConstructor",
133                     "For [ {0} ]",
134                     new Object JavaDoc[] {compName.toString()}));
135             result.addErrorDetails(smh.getLocalString
136                            (getClass().getName() + ".debug3",
137                         "For EJB Class [ {0} ]",
138                         new Object JavaDoc[] {descriptor.getEjbClassName()}));
139             result.addErrorDetails(smh.getLocalString
140                            (getClass().getName() + ".failed1",
141                         "Error: No ejbFindByPrimaryKey method was found in bean class."));
142             }
143   
144         } catch (ClassNotFoundException JavaDoc e) {
145             Verifier.debug(e);
146             result.addErrorDetails(smh.getLocalString
147                        ("tests.componentNameConstructor",
148                     "For [ {0} ]",
149                     new Object JavaDoc[] {compName.toString()}));
150             result.failed(smh.getLocalString
151                   (getClass().getName() + ".failedException",
152                    "Error: EJB Class [ {0} ] does not exist or is not loadable.",
153                    new Object JavaDoc[] {descriptor.getEjbClassName()}));
154             oneFailed = true;
155         }
156     
157         if (oneFailed) {
158             result.setStatus(result.FAILED);
159         } else {
160             result.setStatus(result.PASSED);
161         }
162   
163         } else { //(CONTAINER_PERSISTENCE.equals(persistentType))
164
result.addNaDetails(smh.getLocalString
165                        ("tests.componentNameConstructor",
166                     "For [ {0} ]",
167                     new Object JavaDoc[] {compName.toString()}));
168         result.notApplicable(smh.getLocalString
169                      (getClass().getName() + ".notApplicable2",
170                       "Expected persistence type [ {0} ], but bean [ {1} ] has persistence type [ {2} ]",
171                       new Object JavaDoc[] {EjbEntityDescriptor.BEAN_PERSISTENCE,descriptor.getName(),persistentType}));
172         }
173         return result;
174
175     } else {
176         result.addNaDetails(smh.getLocalString
177                        ("tests.componentNameConstructor",
178                     "For [ {0} ]",
179                     new Object JavaDoc[] {compName.toString()}));
180         result.notApplicable(smh.getLocalString
181                  (getClass().getName() + ".notApplicable",
182                   "[ {0} ] expected {1} bean, but called with {2} bean.",
183                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
184         return result;
185     }
186     }
187 }
188
Popular Tags