KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > entity > pkmultiplefield > PrimaryKeyClassFieldsMatchBeanFields


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.pkmultiplefield;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import java.lang.ClassLoader JavaDoc;
27 import com.sun.enterprise.tools.verifier.tests.*;
28 import java.lang.reflect.*;
29 import javax.ejb.EntityBean JavaDoc;
30 import com.sun.enterprise.deployment.*;
31 import com.sun.enterprise.tools.verifier.*;
32 import java.util.*;
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
34 import com.sun.enterprise.tools.verifier.tests.ejb.EjbUtils;
35
36 /**
37  * Enterprise Java Bean primary key maps to multiple fields in the Entity bean
38  * class test.
39  *
40  * The primkey-field element is not used if the primary key maps to multiple
41  * container-managed fields (i.e. the key is a compound key). In this case, the
42  * fields of the primary key class must be public, and their names must
43  * correspond to the field names of the entity bean class that comprise the key.
44  */

45 public class PrimaryKeyClassFieldsMatchBeanFields extends EjbTest implements EjbCheck {
46
47
48     /**
49      * Enterprise Java Bean primary key maps to multiple fields in the Entity bean
50      * class test.
51      *
52      * The primkey-field element is not used if the primary key maps to multiple
53      * container-managed fields (i.e. the key is a compound key). In this case, the
54      * fields of the primary key class must be public, and their names must
55      * correspond to the field names of the entity bean class that comprise the key.
56      *
57      * @param descriptor the Enterprise Java Bean deployment descriptor
58      *
59      * @return <code>Result</code> the results for this assertion
60      */

61     public Result check(EjbDescriptor descriptor) {
62
63     Result result = getInitializedResult();
64     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
65
66     // fields of the primary key class names must correspond to the
67
// field names of the entity bean class that comprise the key.
68
if (descriptor instanceof EjbEntityDescriptor) {
69         String JavaDoc persistence =
70         ((EjbEntityDescriptor)descriptor).getPersistenceType();
71         if (EjbEntityDescriptor.CONTAINER_PERSISTENCE.equals(persistence)) {
72  
73         // do we have primekey that maps to single or multiple fields in entity
74
// bean class? if primekey-field exist, then primekey maps to single
75
// field in entity bean class and this test in notApplicable
76
try {
77                     FieldDescriptor fd = ((EjbCMPEntityDescriptor)descriptor).getPrimaryKeyFieldDesc();
78                     if (fd != null) {
79                         String JavaDoc pkf = fd.getName();
80                         if (pkf.length() > 0) {
81                 // N/A case
82
result.addNaDetails(smh.getLocalString
83                        ("tests.componentNameConstructor",
84                     "For [ {0} ]",
85                     new Object JavaDoc[] {compName.toString()}));
86                 result.notApplicable(smh.getLocalString
87                              (getClass().getName() + ".notApplicable2",
88                               "Entity Bean [ {0} ] with primekey-field non-blank, test not applicable.",
89                               new Object JavaDoc[] {descriptor.getEjbClassName()}));
90                         }
91             } else {
92             try {
93                 Context context = getVerifierContext();
94                 ClassLoader JavaDoc jcl = context.getClassLoader();
95                 Class JavaDoc c = Class.forName(((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName(), false, getVerifierContext().getClassLoader());
96                 Field [] fields = c.getDeclaredFields();
97       
98                 Vector beanFields = ((EjbDescriptor)descriptor).getFieldDescriptors();
99             
100                 boolean oneFailed = false;
101                 boolean badField = false;
102                 for (int i = 0; i < fields.length; i++) {
103                 badField = false;
104                 if (EjbUtils.isPKFieldMatchingBeanFields(fields[i],beanFields)) {
105                     result.addGoodDetails(smh.getLocalString
106                        ("tests.componentNameConstructor",
107                     "For [ {0} ]",
108                     new Object JavaDoc[] {compName.toString()}));
109                     result.addGoodDetails(smh.getLocalString
110                       (getClass().getName() + ".passed1",
111                        "Valid: Field [ {0} ] defined within primary key class [ {1} ] does correspond to the field names of the entity bean class [ {2} ] that comprise the key.",
112                        new Object JavaDoc[] {fields[i].getName(),((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName(),descriptor.getEjbClassName()}));
113                     continue;
114                 } else {
115                     if (!oneFailed) {
116                     oneFailed = true;
117                     }
118                     badField = true;
119                 }
120           
121                 if (badField == true) {
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() + ".failed",
128                            "Error: Field [ {0} ] defined within primary key class [ {1} ] does not correspond to the field names of the entity bean class [ {2} ] that comprise the key.",
129                            new Object JavaDoc[] {fields[i].getName(),((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName(),descriptor.getEjbClassName()}));
130                 }
131                 }
132                 if (!oneFailed) {
133                 result.addGoodDetails(smh.getLocalString
134                        ("tests.componentNameConstructor",
135                     "For [ {0} ]",
136                     new Object JavaDoc[] {compName.toString()}));
137                 result.passed(smh.getLocalString
138                           (getClass().getName() + ".passed",
139                            "This primary key class [ {0} ] has defined all fields which correspond to the field names of the entity bean class [ {1} ] that comprise the key.",
140                            new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName(),descriptor.getEjbClassName()}));
141                 }
142             } catch (ClassNotFoundException JavaDoc e) {
143                 Verifier.debug(e);
144                 result.addErrorDetails(smh.getLocalString
145                        ("tests.componentNameConstructor",
146                     "For [ {0} ]",
147                     new Object JavaDoc[] {compName.toString()}));
148                 result.failed(smh.getLocalString
149                       (getClass().getName() + ".failedException",
150                        "Error: [ {0} ] class or [ {1} ] class not found.",
151                        new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getPrimaryKeyClassName(),((EjbEntityDescriptor)descriptor).getEjbClassName()}));
152                         } catch (Throwable JavaDoc t) {
153                 result.addWarningDetails(smh.getLocalString
154                        ("tests.componentNameConstructor",
155                     "For [ {0} ]",
156                     new Object JavaDoc[] {compName.toString()}));
157                             result.warning(smh.getLocalString
158                                 (getClass().getName() + ".warningException",
159                                  "Warning: [ {0} ] class encountered [ {1} ]. Cannot access fields of class [ {2} ] which is external to [ {3} ].",
160                                  new Object JavaDoc[] {(descriptor).getEjbClassName(),t.toString(), t.getMessage(), descriptor.getEjbBundleDescriptor().getModuleDescriptor().getArchiveUri()}));
161             }
162             }
163         } catch (NullPointerException JavaDoc 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() + ".failedException2",
170                       "Error: Primkey field not defined within [ {0} ] bean.",
171                       new Object JavaDoc[] {descriptor.getName()}));
172         }
173         return result;
174  
175         } else { //if (BEAN_PERSISTENCE.equals(persistence)
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() + ".notApplicable1",
182                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
183                       new Object JavaDoc[] {EjbEntityDescriptor.CONTAINER_PERSISTENCE,descriptor.getName(),persistence}));
184         return result;
185         }
186     } else {
187         result.addNaDetails(smh.getLocalString
188                        ("tests.componentNameConstructor",
189                     "For [ {0} ]",
190                     new Object JavaDoc[] {compName.toString()}));
191         result.notApplicable(smh.getLocalString
192                  (getClass().getName() + ".notApplicable",
193                   "{0} expected {1} bean, but called with {2}.",
194                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
195         return result;
196     }
197     }
198 }
199
Popular Tags