KickJava   Java API By Example, From Geeks To Geeks.

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


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.reflect.Field JavaDoc;
27 import java.util.*;
28 import java.util.logging.Level JavaDoc;
29
30 import com.sun.enterprise.deployment.Descriptor;
31 import com.sun.enterprise.deployment.EjbDescriptor;
32 import com.sun.enterprise.deployment.EjbEntityDescriptor;
33 import com.sun.enterprise.deployment.EjbCMPEntityDescriptor;
34 import com.sun.enterprise.tools.verifier.*;
35 import java.lang.ClassLoader JavaDoc;
36 import com.sun.enterprise.tools.verifier.tests.*;
37 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
38
39 /**
40  * Container-managed fields test.
41  * If the enterprise bean is a Entity Bean w/Container managed persistence
42  * the Bean provider must specify container managed fields in the
43  * "persistent-fields" element.
44  */

45 public class PersistenceFieldsElement extends EjbTest implements EjbCheck {
46
47     /**
48      * Container-managed fields test.
49      * If the enterprise bean is a Entity Bean w/Container managed persistence
50      * the Bean provider must specify container managed fields in the
51      * "persistent-fields" element.
52      *
53      * @param descriptor the Enterprise Java Bean deployment descriptor
54      *
55      * @return <code>Result</code> the results for this assertion
56      */

57     public Result check(EjbDescriptor descriptor) {
58
59     Result result = getInitializedResult();
60     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
61
62     if (descriptor instanceof EjbEntityDescriptor) {
63         String JavaDoc persistentType =
64         ((EjbEntityDescriptor)descriptor).getPersistenceType();
65         if (EjbEntityDescriptor.CONTAINER_PERSISTENCE.equals(persistentType)) {
66
67                 // this test apply only to 1.x cmp beans, in 2.x fields are virtual fields only
68
if (EjbCMPEntityDescriptor.CMP_1_1!=((EjbCMPEntityDescriptor) descriptor).getCMPVersion()) {
69             result.addNaDetails(smh.getLocalString
70                     ("tests.componentNameConstructor",
71                      "For [ {0} ]",
72                      new Object JavaDoc[] {compName.toString()}));
73                 result.notApplicable(smh.getLocalString
74                  ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.notApplicable3",
75                   "Test do not apply to this cmp-version of container managed persistence EJBs"));
76                 return result;
77                 }
78             logger.log(Level.FINE, getClass().getName() + ".debug1",
79                     new Object JavaDoc[] {persistentType});
80         // RULE: Entity w/Container managed persistence bean provider must
81
// specify container managed fields in the persistent-fields
82
// element
83
Set persistentFields =
84             ((EjbCMPEntityDescriptor)descriptor).getPersistenceDescriptor().getCMPFields();
85         Iterator iterator = persistentFields.iterator();
86
87         // check class to see if fields actually exist
88
try {
89             Context context = getVerifierContext();
90             ClassLoader JavaDoc jcl = context.getClassLoader();
91             Class JavaDoc c = Class.forName(descriptor.getEjbClassName(), false, getVerifierContext().getClassLoader());
92
93             Descriptor persistentField;
94             Field JavaDoc field;
95             boolean oneFailed = false;
96             while (iterator.hasNext()) {
97             persistentField = (Descriptor)iterator.next();
98             try {
99                 field = c.getField(persistentField.getName());
100                 result.addGoodDetails(smh.getLocalString
101                           ("tests.componentNameConstructor",
102                            "For [ {0} ]",
103                            new Object JavaDoc[] {compName.toString()}));
104                 result.passed(smh.getLocalString
105                       (getClass().getName() + ".passed",
106                        "[ {0} ] field found in [ {1} ]",
107                        new Object JavaDoc[] {((Descriptor)persistentField).getName(),descriptor.getEjbClassName()}));
108             } catch (NoSuchFieldException JavaDoc e) {
109                 result.addErrorDetails(smh.getLocalString
110                            ("tests.componentNameConstructor",
111                             "For [ {0} ]",
112                             new Object JavaDoc[] {compName.toString()}));
113                 result.failed(smh.getLocalString
114                       (getClass().getName() + ".failedException1",
115                        "Error: NoSuchFieldException: [ {0} ] not found in [ {1} ]",
116                        new Object JavaDoc[] {((Descriptor)persistentField).getName(),descriptor.getEjbClassName()}));
117                 if (!oneFailed) {
118                 oneFailed = true;
119                 }
120
121             } catch (SecurityException JavaDoc e) {
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() + ".failedException2",
128                        "Error: SecurityException: [ {0} ] not found in [ {1} ]",
129                        new Object JavaDoc[] {((Descriptor)persistentField).getName(),descriptor.getEjbClassName()}));
130                 if (!oneFailed) {
131                 oneFailed = true;
132                 }
133             }
134             }
135             if (oneFailed) {
136             result.setStatus(Result.FAILED);
137             } else {
138             result.setStatus(Result.PASSED);
139             }
140         } catch (ClassNotFoundException JavaDoc e) {
141             Verifier.debug(e);
142             result.addErrorDetails(smh.getLocalString
143                        ("tests.componentNameConstructor",
144                         "For [ {0} ]",
145                         new Object JavaDoc[] {compName.toString()}));
146             result.failed(smh.getLocalString
147                   (getClass().getName() + ".failedException3",
148                    "Error: Fields don't exist or are not loadable within bean [ {0} ]",
149                    new Object JavaDoc[] {descriptor.getName()}));
150         }
151         } else if (EjbEntityDescriptor.BEAN_PERSISTENCE.equals(persistentType))
152         {
153             result.addNaDetails(smh.getLocalString
154                     ("tests.componentNameConstructor",
155                      "For [ {0} ]",
156                      new Object JavaDoc[] {compName.toString()}));
157             result.notApplicable(smh.getLocalString
158                      (getClass().getName() + ".notApplicable1",
159                       "Expected persistence type [ {0} ], but [ {1} ] bean has persistence type [ {2} ]",
160                       new Object JavaDoc[] {EjbEntityDescriptor.CONTAINER_PERSISTENCE,descriptor.getName(),persistentType}));
161         } else {
162             result.addErrorDetails(smh.getLocalString
163                        ("tests.componentNameConstructor",
164                         "For [ {0} ]",
165                         new Object JavaDoc[] {compName.toString()}));
166             result.failed(smh.getLocalString
167                   (getClass().getName() + ".failed",
168                    "Error: [ {0} ] is not valid persistentType within bean [ {1} ]",
169                    new Object JavaDoc[] {persistentType, descriptor.getName()}));
170         }
171         return result;
172     } else {
173         result.addNaDetails(smh.getLocalString
174                 ("tests.componentNameConstructor",
175                  "For [ {0} ]",
176                  new Object JavaDoc[] {compName.toString()}));
177         result.notApplicable(smh.getLocalString
178                  (getClass().getName() + ".notApplicable",
179                   "[ {0} ] expected {1} bean, but called with {2} bean.",
180                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
181         return result;
182     }
183     }
184 }
185
Popular Tags