KickJava   Java API By Example, From Geeks To Geeks.

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


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

97     public Result check(EjbDescriptor descriptor) {
98
99     Result result = getInitializedResult();
100     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
101
102     if (descriptor instanceof EjbEntityDescriptor) {
103         String JavaDoc persistence =
104         ((EjbEntityDescriptor)descriptor).getPersistenceType();
105         if (EjbEntityDescriptor.CONTAINER_PERSISTENCE.equals(persistence)) {
106         String JavaDoc primkey =
107             ((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName();
108
109         // primkey can be not set, via setting xml element
110
// <prim-key-class> to "java.lang.Object"
111
if (primkey.equals("java.lang.Object")) {
112             try {
113             Context context = getVerifierContext();
114         ClassLoader JavaDoc jcl = context.getClassLoader();
115             Class JavaDoc c = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
116             boolean returnsJLO = false;
117                         // start do while loop here....
118
do {
119                 Method JavaDoc [] methods = c.getDeclaredMethods();
120                 returnsJLO = false;
121                 for (int j = 0; j < methods.length; ++j) {
122                 if (methods[j].getName().equals("ejbCreate")) {
123                 // The return type must be java.lang.Object.
124
Class JavaDoc rt = methods[j].getReturnType();
125                     if (rt.getName().equals("java.lang.Object")) {
126                     returnsJLO = true;
127                     break;
128                     }
129                 }
130                 }
131                         } while (((c = c.getSuperclass()) != null) && (!returnsJLO));
132
133             if (returnsJLO) {
134                 result.addGoodDetails(smh.getLocalString
135                        ("tests.componentNameConstructor",
136                     "For [ {0} ]",
137                     new Object JavaDoc[] {compName.toString()}));
138                 result.passed(smh.getLocalString
139                       (getClass().getName() + ".passed",
140                        "ejbCreate() method properly defines method return type [ {0} ]",
141                        new Object JavaDoc[] {"java.lang.Object"}));
142             } else {
143                 result.addErrorDetails(smh.getLocalString
144                            ("tests.componentNameConstructor",
145                             "For [ {0} ]",
146                             new Object JavaDoc[] {compName.toString()}));
147                 result.failed(smh.getLocalString
148                       (getClass().getName() + ".failed",
149                        "ejbCreate() method does not properly define method return type [ {0} ]",
150                        new Object JavaDoc[] {"java.lang.Object"}));
151             }
152             } catch (Exception JavaDoc e) {
153             Verifier.debug(e);
154             result.addErrorDetails(smh.getLocalString
155                            ("tests.componentNameConstructor",
156                         "For [ {0} ]",
157                         new Object JavaDoc[] {compName.toString()}));
158             result.failed(smh.getLocalString
159                       (getClass().getName() + ".failedException",
160                        "Error: Loading bean class [ {0} ]",
161                        new Object JavaDoc[] {descriptor.getEjbClassName()}));
162             return result;
163             }
164         } else {
165             result.addNaDetails(smh.getLocalString
166                     ("tests.componentNameConstructor",
167                      "For [ {0} ]",
168                      new Object JavaDoc[] {compName.toString()}));
169             result.notApplicable(smh.getLocalString
170                      (getClass().getName() + ".notApplicable1",
171                       "Primary Key Class is [ {0} ]",
172                       new Object JavaDoc[] {primkey}));
173         }
174
175         return result;
176
177         } else if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistence)) {
178         result.addNaDetails(smh.getLocalString
179                        ("tests.componentNameConstructor",
180                     "For [ {0} ]",
181                     new Object JavaDoc[] {compName.toString()}));
182         result.notApplicable(smh.getLocalString
183                      (getClass().getName() + ".notApplicable2",
184                       "Entity bean with [ {0} ] managed persistence, primkey mandatory.",
185                       new Object JavaDoc[] {persistence}));
186         return result;
187         }
188         else {
189         result.addNaDetails(smh.getLocalString
190                        ("tests.componentNameConstructor",
191                     "For [ {0} ]",
192                     new Object JavaDoc[] {compName.toString()}));
193         result.notApplicable(smh.getLocalString
194                      (getClass().getName() + ".notApplicable3",
195                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
196                       new Object JavaDoc[] {EjbEntityDescriptor.CONTAINER_PERSISTENCE,descriptor.getName(),persistence}));
197         return result;
198         }
199     } else {
200         result.addNaDetails(smh.getLocalString
201                        ("tests.componentNameConstructor",
202                     "For [ {0} ]",
203                     new Object JavaDoc[] {compName.toString()}));
204         result.notApplicable(smh.getLocalString
205                  (getClass().getName() + ".notApplicable",
206                   "[ {0} ] expected {1} bean, but called with {2} bean.",
207                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
208         return result;
209     }
210     }
211 }
212
Popular Tags