KickJava   Java API By Example, From Geeks To Geeks.

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


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;
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.Field JavaDoc;
29 import java.util.*;
30 import java.util.logging.Level JavaDoc;
31
32 import com.sun.enterprise.deployment.*;
33 import com.sun.enterprise.tools.verifier.*;
34 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
35
36 /**
37  * The field-name element name must be a public field of the enterprise bean
38  * class or one of its superclasses.
39  */

40 public class FieldNameElementPublic extends EjbTest implements EjbCheck {
41
42     /**
43      * The field-name element name must be a public field of the enterprise bean
44      * class or one of its superclasses.
45      *
46      * @param descriptor the Enterprise Java Bean deployment descriptor
47      *
48      * @return <code>Result</code> the results for this assertion
49      */

50     public Result check(EjbDescriptor descriptor) {
51
52     Result result = getInitializedResult();
53     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
54
55     if (descriptor instanceof EjbEntityDescriptor) {
56         String JavaDoc persistentType =
57         ((EjbEntityDescriptor)descriptor).getPersistenceType();
58         if (EjbEntityDescriptor.CONTAINER_PERSISTENCE.equals(persistentType)) {
59                 
60                 // this test apply only to 1.x cmp beans, in 2.x fields are virtual fields only
61
if (EjbCMPEntityDescriptor.CMP_1_1!=((EjbCMPEntityDescriptor) descriptor).getCMPVersion()) {
62             result.addNaDetails(smh.getLocalString
63                     ("tests.componentNameConstructor",
64                      "For [ {0} ]",
65                      new Object JavaDoc[] {compName.toString()}));
66                 result.notApplicable(smh.getLocalString
67                  ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.notApplicable3",
68                   "Test do not apply to this cmp-version of container managed persistence EJBs"));
69                 return result;
70                 }
71                 
72         // RULE: Entity w/Container managed persistence bean provider must
73
// specify container managed fields in the persistent-fields
74
// element. The field-name element name must be a public field
75
// of the enterprise bean class or one of its superclasses.
76

77         boolean resolved = false;
78         boolean oneFailed = false;
79         
80         if (!((EjbCMPEntityDescriptor)descriptor).getPersistenceDescriptor().getCMPFields().isEmpty()) {
81             // check class to get all fields that actually exist
82
try {
83             Context context = getVerifierContext();
84         ClassLoader JavaDoc jcl = context.getClassLoader();
85
86             for (Iterator itr =
87                  ((EjbCMPEntityDescriptor)descriptor).getPersistenceDescriptor().getCMPFields().iterator();
88                  itr.hasNext();) {
89
90                 FieldDescriptor nextPersistentField = (FieldDescriptor)itr.next();
91                 String JavaDoc fieldName = nextPersistentField.getName();
92                 Class JavaDoc c = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
93
94                             // start do while loop here....
95
do {
96                 // Returns an array containing Field objects
97
// reflecting all the accessible public fields of the class
98
// or interface represented by this Class object.
99
Field JavaDoc fields[] = c.getFields();
100     
101                 //loop thru all field array elements and ensure fieldName exist
102
for (int i=0; i < fields.length; i++) {
103                     if (fieldName.equals(fields[i].getName())) {
104                     resolved = true;
105                     logger.log(Level.FINE, getClass().getName() + ".debug1",
106                             new Object JavaDoc[] {fieldName,fields[i].toString(),c.getName()});
107                     result.addGoodDetails(smh.getLocalString
108                                   ("tests.componentNameConstructor",
109                                    "For [ {0} ]",
110                                    new Object JavaDoc[] {compName.toString()}));
111                     result.addGoodDetails
112                         (smh.getLocalString
113                          (getClass().getName() + ".passed",
114                           "[ {0} ] found in public fields of bean [ {1} ]",
115                           new Object JavaDoc[] {fieldName,c.getName()}));
116                     break;
117                     } else {
118                         logger.log(Level.FINE, getClass().getName() + ".debug",
119                              new Object JavaDoc[] {fieldName,fields[i].toString(),c.getName()});
120                     }
121                 }
122                 } while (((c = c.getSuperclass()) != null) && (!resolved));
123  
124                 // before you go onto the next field name, tell me whether you
125
// resolved the last field name okay
126
if (!resolved) {
127                 if (!oneFailed) {
128                     oneFailed = true;
129                 }
130                 result.addErrorDetails(smh.getLocalString
131                                ("tests.componentNameConstructor",
132                             "For [ {0} ]",
133                             new Object JavaDoc[] {compName.toString()}));
134                 result.addErrorDetails
135                     (smh.getLocalString
136                      (getClass().getName() + ".failed1",
137                       "Error: [ {0} ] not found in public fields of bean [ {1} ]",
138                       new Object JavaDoc[] {fieldName,descriptor.getEjbClassName()}));
139                 }
140                 // clear the resolved flag for the next field name
141
if (resolved) {
142                 resolved = false;
143                 }
144             }
145  
146             if (oneFailed) {
147                 result.setStatus(Result.FAILED);
148             } else {
149                 result.setStatus(Result.PASSED);
150             }
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
159                 (smh.getLocalString
160                  (getClass().getName() + ".failedException",
161                   "Error: [ {0} ] within bean [ {1} ]",
162                   new Object JavaDoc[] {e.getMessage(),descriptor.getName()}));
163             }
164         } else {
165             // persistent fields are empty
166
result.addNaDetails(smh.getLocalString
167                     ("tests.componentNameConstructor",
168                      "For [ {0} ]",
169                      new Object JavaDoc[] {compName.toString()}));
170             result.notApplicable(smh.getLocalString
171                      (getClass().getName() + ".notApplicable1",
172                       "No persistent fields are defined for bean [ {0} ]",
173                       new Object JavaDoc[] {descriptor.getName()}));
174         }
175         } else { //BEAN_PERSISTENCE.equals(persistentType)
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() + ".notApplicable2",
182                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
183                       new Object JavaDoc[] {EjbEntityDescriptor.CONTAINER_PERSISTENCE,descriptor.getName(),persistentType}));
184         }
185         return result;
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 \n {1} bean, but called with {2} bean",
194                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
195         return result;
196     }
197     }
198 }
199
Popular Tags