KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > primarykeyclass > PrimaryKeyClassOpt


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.primarykeyclass;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import java.lang.reflect.Method JavaDoc;
27 import com.sun.enterprise.deployment.EjbDescriptor;
28 import com.sun.enterprise.deployment.EjbEntityDescriptor;
29 import com.sun.enterprise.tools.verifier.*;
30 import java.lang.ClassLoader JavaDoc;
31 import com.sun.enterprise.tools.verifier.tests.*;
32 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
33
34 /**
35  * Entity bean's Primary Key Class test.
36  * If the enterprise bean is a Entity Bean, the Bean provider specifies
37  * the fully qualified name of the Entity bean's primary key class in the
38  * "<prim-key-class>" element. The Bean provider 'must' specify the primary key
39  * class for an Entity with bean managed persistence, and 'may' (but is not
40  * required to) specify the primary key class for an Entity with
41  * Container-managed persistence.
42  *
43  * Special case: Unknown primary key class
44  * In special situations, the Bean Provider may choose not to specify the
45  * primary key class for an entity bean with container-managed persistence. This
46  * case happens if the Bean Provider wants to allow the Deployer to select the
47  * primary key fields at deployment time. The Deployer uses instructions
48  * supplied by the Bean Provider (these instructions are beyond the scope of
49  * the EJB spec.) to define a suitable primary key class.
50  *
51  * In this special case, the type of the argument of the findByPrimaryKey method
52  * must be declared as java.lang.Object, and the return value of ejbCreate()
53  * must be declared as java.lang.Object. The Bean Provider must specify the
54  * primary key class in the deployment descriptor as the type
55  * java.lang.Object.
56  *
57  * The primary key class is specified at deployment time when the Bean Provider
58  * develops enterprise beans that is intended to be used with multiple back-ends
59  * that provide persistence, and when these multiple back-ends require different
60  * primary key structures.
61  */

62 public class PrimaryKeyClassOpt extends EjbTest implements EjbCheck {
63
64     Result result = null;
65     ComponentNameConstructor compName = null;
66     /**
67      * Entity bean's Primary Key Class test.
68      * If the enterprise bean is a Entity Bean, the Bean provider specifies
69      * the fully qualified name of the Entity bean's primary key class in the
70      * "<prim-key-class>" element. The Bean provider 'must' specify the primary
71      * key class for an Entity with bean managed persistence, and 'may' (but is
72      * not required to) specify the primary key class for an Entity with
73      * Container-managed persistence.
74      *
75      * Special case: Unknown primary key class
76      * In special situations, the Bean Provider may choose not to specify the
77      * primary key class for an entity bean with container-managed persistence. This
78      * case happens if the Bean Provider wants to allow the Deployer to select the
79      * primary key fields at deployment time. The Deployer uses instructions
80      * supplied by the Bean Provider (these instructions are beyond the scope of
81      * the EJB spec.) to define a suitable primary key class.
82      *
83      * In this special case, the type of the argument of the findByPrimaryKey method
84      * must be declared as java.lang.Object, and the return value of ejbCreate()
85      * must be declared as java.lang.Object. The Bean Provider must specify the
86      * primary key class in the deployment descriptor as of the type
87      * java.lang.Object.
88      *
89      * The primary key class is specified at deployment time when the Bean Provider
90      * develops enterprise beans that is intended to be used with multiple back-ends
91      * that provide persistence, and when these multiple back-ends require different
92      * primary key structures.
93      *
94      * @param descriptor the Enterprise Java Bean deployment descriptor
95      *
96      * @return <code>Result</code> the results for this assertion
97      */

98     public Result check(EjbDescriptor descriptor) {
99
100     result = getInitializedResult();
101     compName = getVerifierContext().getComponentNameConstructor();
102
103     boolean oneFailed = false;
104
105     if (descriptor instanceof EjbEntityDescriptor) {
106         String JavaDoc persistence =
107         ((EjbEntityDescriptor)descriptor).getPersistenceType();
108         if (EjbEntityDescriptor.CONTAINER_PERSISTENCE.equals(persistence)) {
109         String JavaDoc primkey =
110             ((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName();
111
112         // primkey can be not set, via setting xml element
113
// <prim-key-class> to "java.lang.Object"
114
if (primkey.equals("java.lang.Object")) {
115             if(descriptor.getHomeClassName() != null && !"".equals(descriptor.getHomeClassName())) {
116             oneFailed = commonToBothInterfaces(descriptor.getHomeClassName(), descriptor);
117             }
118             if(oneFailed == false) {
119             if(descriptor.getLocalHomeClassName() != null && !"".equals(descriptor.getLocalHomeClassName())) {
120                 oneFailed = commonToBothInterfaces(descriptor.getLocalHomeClassName(), descriptor);
121             }
122             }
123         } else {
124             result.addNaDetails(smh.getLocalString
125                        ("tests.componentNameConstructor",
126                     "For [ {0} ]",
127                     new Object JavaDoc[] {compName.toString()}));
128             result.notApplicable(smh.getLocalString
129                      (getClass().getName() + ".notApplicable1",
130                       "Primary Key Class is [ {0} ]",
131                       new Object JavaDoc[] {primkey}));
132         }
133
134         return result;
135
136         } else if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistence)) {
137         result.addNaDetails(smh.getLocalString
138                        ("tests.componentNameConstructor",
139                     "For [ {0} ]",
140                     new Object JavaDoc[] {compName.toString()}));
141         result.notApplicable(smh.getLocalString
142                      (getClass().getName() + ".notApplicable2",
143                       "Entity Bean with [ {0} ] managed persistence, primkey mandatory.",
144                       new Object JavaDoc[] {persistence}));
145         return result;
146         }
147         else {
148         result.addNaDetails(smh.getLocalString
149                        ("tests.componentNameConstructor",
150                     "For [ {0} ]",
151                     new Object JavaDoc[] {compName.toString()}));
152         result.notApplicable(smh.getLocalString
153                      (getClass().getName() + ".notApplicable3",
154                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
155                       new Object JavaDoc[] {EjbEntityDescriptor.CONTAINER_PERSISTENCE,descriptor.getName(),persistence}));
156         return result;
157         }
158     } else {
159         result.addNaDetails(smh.getLocalString
160                        ("tests.componentNameConstructor",
161                     "For [ {0} ]",
162                     new Object JavaDoc[] {compName.toString()}));
163         result.addNaDetails(smh.getLocalString
164                        ("tests.componentNameConstructor",
165                     "For [ {0} ]",
166                     new Object JavaDoc[] {compName.toString()}));
167         result.notApplicable(smh.getLocalString
168                  (getClass().getName() + ".notApplicable",
169                   "[ {0} ] expected {1} bean, but called with {2} bean.",
170                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
171         return result;
172     }
173     }
174
175     /**
176      * This method is responsible for the logic of the test. It is called for both local and remote interfaces.
177      * @param descriptor the Enterprise Java Bean deployment descriptor
178      * @param home for the Home interface of the Ejb.
179      * @return boolean the results for this assertion i.e if a test has failed or not
180      */

181
182
183     private boolean commonToBothInterfaces(String JavaDoc home,EjbDescriptor descriptor) {
184     boolean oneFailed = false;
185     try {
186         Context context = getVerifierContext();
187         ClassLoader JavaDoc jcl = context.getClassLoader();
188         Class JavaDoc c = Class.forName(home, false, getVerifierContext().getClassLoader());
189         Method JavaDoc [] ejbFinderMethods = c.getDeclaredMethods();
190         boolean paramValid = false;
191         for (int j = 0; j < ejbFinderMethods.length; ++j) {
192         // reset all booleans for next method within the loop
193
if (ejbFinderMethods[j].getName().equals("findByPrimaryKey")) {
194             Class JavaDoc [] ejbFinderMethodParameterTypes;
195             ejbFinderMethodParameterTypes = ejbFinderMethods[j].getParameterTypes();
196             for (int k = 0; k < ejbFinderMethodParameterTypes.length; ++k) {
197             if (ejbFinderMethodParameterTypes[k].getName().equals("java.lang.Object")) {
198                 paramValid = true;
199                 break;
200             }
201             }
202         }
203         }
204         
205         if (paramValid) {
206         result.addGoodDetails(smh.getLocalString
207                        ("tests.componentNameConstructor",
208                     "For [ {0} ]",
209                     new Object JavaDoc[] {compName.toString()}));
210         result.passed(smh.getLocalString
211                   (getClass().getName() + ".passed",
212                    "findByPrimaryKey method properly defines method parameter [ {0} ]",
213                    new Object JavaDoc[] {"java.lang.Object"}));
214         } else {
215         result.addErrorDetails(smh.getLocalString
216                        ("tests.componentNameConstructor",
217                     "For [ {0} ]",
218                     new Object JavaDoc[] {compName.toString()}));
219         result.failed(smh.getLocalString
220                   (getClass().getName() + ".failed",
221                    "Error: findByPrimaryKey method does not properly define method parameter [ {0} ]",
222                    new Object JavaDoc[] {"java.lang.Object"}));
223         }
224         return oneFailed;
225     } catch (Exception JavaDoc e) {
226         Verifier.debug(e);
227         result.addErrorDetails(smh.getLocalString
228                    ("tests.componentNameConstructor",
229                     "For [ {0} ]",
230                     new Object JavaDoc[] {compName.toString()}));
231         result.failed(smh.getLocalString
232               (getClass().getName() + ".failedException",
233                "Error: Loading Home interface class [ {0} ]",
234                new Object JavaDoc[] {home}));
235         return oneFailed;
236     }
237     }
238 }
239
Popular Tags