KickJava   Java API By Example, From Geeks To Geeks.

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


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 return test.
38  *
39  * Every entity enterprise Bean class must define the ejbFindByPrimaryKey
40  * method. The return type for this method must be the primary key type.
41  * (i.e. the ejbFindByPrimaryKey method must be a single-object finder).
42  *
43  */

44 public class EjbFindByPrimaryKeyReturn extends EjbTest implements EjbCheck {
45
46
47     /**
48      * Define ejbFindByPrimaryKey method return test.
49      *
50      * Every entity enterprise Bean class must define the ejbFindByPrimaryKey
51      * method. The return type for this method must be the primary key type.
52      * (i.e. the ejbFindByPrimaryKey method must be a single-object finder).
53      *
54      * @param descriptor the Enterprise Java Bean deployment descriptor
55      *
56      * @return <code>Result</code> the results for this assertion
57      */

58     public Result check(EjbDescriptor descriptor) {
59
60     Result result = getInitializedResult();
61     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
62
63     if (descriptor instanceof EjbEntityDescriptor) {
64         String JavaDoc persistentType =
65         ((EjbEntityDescriptor)descriptor).getPersistenceType();
66         if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistentType)) {
67         boolean ejbFindByPrimaryKeyMethodFound = false;
68         boolean oneFailed = false;
69                 boolean oneWarning = false;
70         boolean returnValueValid = false;
71         try {
72             // retrieve the EJB Class Methods
73
Context context = getVerifierContext();
74         ClassLoader JavaDoc jcl = context.getClassLoader();
75             Class JavaDoc EJBClass = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
76                     // start do while loop here....
77
do {
78             Method [] ejbFinderMethods = EJBClass.getDeclaredMethods();
79             String JavaDoc primaryKeyType = ((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName();
80           
81             for (int j = 0; j < ejbFinderMethods.length; ++j) {
82                 Class JavaDoc returnByPrimaryKeyValue = ejbFinderMethods[j].getReturnType();
83   
84                 if (ejbFinderMethods[j].getName().equals("ejbFindByPrimaryKey")) {
85                 // Every entity enterprise Bean class must define the
86
// ejbFindByPrimaryKey method. The result type for this method must
87
// be the primary key type (i.e. the ejbFindByPrimaryKey method
88
// must be a single-object finder).
89
ejbFindByPrimaryKeyMethodFound = true;
90                 if (returnByPrimaryKeyValue.getName().equals(primaryKeyType)) {
91                     returnValueValid = true;
92                 }
93   
94                 if (ejbFindByPrimaryKeyMethodFound && returnValueValid) {
95                     result.addGoodDetails(smh.getLocalString
96                        ("tests.componentNameConstructor",
97                     "For [ {0} ]",
98                     new Object JavaDoc[] {compName.toString()}));
99                     result.addGoodDetails(smh.getLocalString
100                               (getClass().getName() + ".debug1",
101                                "For EJB Class [ {0} ] Finder Method [ {1} ]",
102                                new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
103                     result.addGoodDetails(smh.getLocalString
104                               (getClass().getName() + ".passed",
105                                "An [ {0} ] method with valid return type was found.",
106                                new Object JavaDoc[] {ejbFinderMethods[j].getName()}));
107                 } else if (ejbFindByPrimaryKeyMethodFound && (!returnValueValid)) {
108                   /* if primary key is java.lang.Object then ejbFindByPrimaryKey method
109                      must return java.lang.Object. So the following check is not correct.
110
111                     if (primaryKeyType.equals("java.lang.Object")) {
112                                         oneWarning = true;
113                     result.addWarningDetails(smh.getLocalString
114                        ("tests.componentNameConstructor",
115                     "For [ {0} ]",
116                     new Object[] {compName.toString()}));
117                         result.addWarningDetails(smh.getLocalString
118                               (getClass().getName() + ".debug1",
119                                "For EJB Class [ {0} ] Finder Method [ {1} ]",
120                                new Object[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
121                         result.addWarningDetails(smh.getLocalString
122                                (getClass().getName() + ".warning",
123                                                            "Warning: An [ {0} ] method was found, but [ {1} ] method has [ {2} ] return type. Deployment descriptor primary key type is [ {3} ]. Definition of the primary key type is deferred to deployment time ?",
124                                                            new Object[] {ejbFinderMethods[j].getName(), ejbFinderMethods[j].getName(),ejbFinderMethods[j].getReturnType().getName(),primaryKeyType}));
125                                     } else { */

126                         oneFailed = true;
127                     result.addErrorDetails(smh.getLocalString
128                        ("tests.componentNameConstructor",
129                     "For [ {0} ]",
130                     new Object JavaDoc[] {compName.toString()}));
131                         result.addErrorDetails(smh.getLocalString
132                                (getClass().getName() + ".debug1",
133                                 "For EJB Class [ {0} ] Finder Method [ {1} ]",
134                                 new Object JavaDoc[] {EJBClass.getName(),ejbFinderMethods[j].getName()}));
135                         result.addErrorDetails(smh.getLocalString
136                                (getClass().getName() + ".failed",
137                                 "Error: An [ {0} ] method was found, but [ {1} ] return type must be the enterprise Bean's primary key type.",
138                                 new Object JavaDoc[] {ejbFinderMethods[j].getName(),ejbFinderMethods[j].getName()}));
139                     // }
140
}
141                 // found one, and there should only be one, break out
142
break;
143                 }
144             }
145                     } while (((EJBClass = EJBClass.getSuperclass()) != null) && (!ejbFindByPrimaryKeyMethodFound));
146   
147             if (!ejbFindByPrimaryKeyMethodFound) {
148             oneFailed = true;
149             result.addErrorDetails(smh.getLocalString
150                        ("tests.componentNameConstructor",
151                     "For [ {0} ]",
152                     new Object JavaDoc[] {compName.toString()}));
153             result.addErrorDetails(smh.getLocalString
154                            (getClass().getName() + ".debug3",
155                         "For EJB Class [ {0} ]",
156                         new Object JavaDoc[] {descriptor.getEjbClassName()}));
157             result.addErrorDetails(smh.getLocalString
158                            (getClass().getName() + ".failed1",
159                         "Error: No ejbFindByPrimaryKey method was found in bean class."));
160             }
161   
162         } catch (ClassNotFoundException JavaDoc e) {
163             Verifier.debug(e);
164             result.addErrorDetails(smh.getLocalString
165                        ("tests.componentNameConstructor",
166                     "For [ {0} ]",
167                     new Object JavaDoc[] {compName.toString()}));
168         result.failed(smh.getLocalString
169                   (getClass().getName() + ".failedException",
170                    "Error: EJB Class [ {0} ] does not exist or is not loadable.",
171                    new Object JavaDoc[] {descriptor.getEjbClassName()}));
172             oneFailed = true;
173         }
174     
175         if (oneFailed) {
176             result.setStatus(result.FAILED);
177         } else {
178                    if (oneWarning) {
179                         result.setStatus(result.WARNING);
180                     } else {
181                 result.setStatus(result.PASSED);
182                     }
183         }
184         } else { //(CONTAINER_PERSISTENCE.equals(persistentType))
185
result.addNaDetails(smh.getLocalString
186                        ("tests.componentNameConstructor",
187                     "For [ {0} ]",
188                     new Object JavaDoc[] {compName.toString()}));
189         result.notApplicable(smh.getLocalString
190                      (getClass().getName() + ".notApplicable2",
191                       "Expected persistence type [ {0} ], but bean [ {1} ] has persistence type [ {2} ]",
192                       new Object JavaDoc[] {EjbEntityDescriptor.BEAN_PERSISTENCE,descriptor.getName(),persistentType}));
193         }
194   
195         return result;
196
197     } else {
198         result.addNaDetails(smh.getLocalString
199                        ("tests.componentNameConstructor",
200                     "For [ {0} ]",
201                     new Object JavaDoc[] {compName.toString()}));
202         result.notApplicable(smh.getLocalString
203                  (getClass().getName() + ".notApplicable",
204                   "[ {0} ] expected {1} bean, but called with {2} bean.",
205                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
206         return result;
207     }
208     }
209 }
210
Popular Tags