KickJava   Java API By Example, From Geeks To Geeks.

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


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.cmp;
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 com.sun.enterprise.deployment.*;
30 import com.sun.enterprise.tools.verifier.*;
31 import java.util.*;
32 import com.sun.enterprise.tools.verifier.tests.ejb.EjbCheck;
33
34 /**
35  * The Bean Provider is responsible for using the cmp-field elements of the
36  * deployment descriptor to declare the instance's fields that the Container
37  * must load and store at the defined times.
38  *
39  * The fields must not be defined in the entity bean class as transient.
40  */

41 public class CmpFieldsTransient extends EjbTest implements EjbCheck {
42
43
44     /**
45      * The Bean Provider is responsible for using the cmp-field elements of the
46      * deployment descriptor to declare the instance's fields that the Container
47      * must load and store at the defined times.
48      *
49      * The fields must not be defined in the entity bean class as transient.
50      *
51      * @param descriptor the Enterprise Java Bean deployment descriptor
52      *
53      * @return <code>Result</code> the results for this assertion
54      */

55     public Result check(EjbDescriptor descriptor) {
56
57     Result result = getInitializedResult();
58     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
59
60     if (descriptor instanceof EjbEntityDescriptor) {
61         String JavaDoc persistence =
62         ((EjbEntityDescriptor)descriptor).getPersistenceType();
63         if (EjbEntityDescriptor.CONTAINER_PERSISTENCE.equals(persistence)) {
64                 
65                 // this test apply only to 1.x cmp beans. in cmp 2.x, fields are virtual fields only
66
if (EjbCMPEntityDescriptor.CMP_1_1 != ((EjbCMPEntityDescriptor) descriptor).getCMPVersion()) {
67             result.addNaDetails(smh.getLocalString
68                        ("tests.componentNameConstructor",
69                     "For [ {0} ]",
70                     new Object JavaDoc[] {compName.toString()}));
71                 result.notApplicable(smh.getLocalString
72                  ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.notApplicable3",
73                   "Test do not apply to this cmp-version of container managed persistence EJBs"));
74                 return result;
75                 }
76         boolean oneFailed = false;
77         boolean badField = false;
78         for (Iterator itr =
79              ((EjbCMPEntityDescriptor)descriptor).getPersistenceDescriptor().getCMPFields().iterator();
80              itr.hasNext();) {
81  
82             FieldDescriptor nextPersistentField = (FieldDescriptor)itr.next();
83             badField = false;
84             boolean foundField = false;
85
86             // fields must not be defined in the entity bean class as transient.
87
Class JavaDoc c1 = null;
88             try {
89             Class JavaDoc c = Class.forName(((EjbEntityDescriptor)descriptor).getEjbClassName(), false, getVerifierContext().getClassLoader());
90             // start do while loop here....
91
do {
92                 try {
93                                 c1 = c;
94                 Field f = c.getDeclaredField(nextPersistentField.getName());
95
96                 foundField = true;
97                 int modifiers = f.getModifiers();
98                 if (!Modifier.isTransient(modifiers)) {
99                     continue;
100                 } else {
101                     if (!oneFailed) {
102                     oneFailed = true;
103                     }
104                     badField = true;
105                 }
106         
107                 if (badField) {
108                     result.addErrorDetails(smh.getLocalString
109                                ("tests.componentNameConstructor",
110                                 "For [ {0} ]",
111                                 new Object JavaDoc[] {compName.toString()}));
112                     result.failed(smh.getLocalString
113                           (getClass().getName() + ".failed",
114                            "Error: Field [ {0} ] defined within entity bean class [ {1} ] is defined as transient. Container managed field must not be defined in the entity bean class as transient.",
115                            new Object JavaDoc[] {nextPersistentField.getName(),c.getName()}));
116                 }
117                 } catch (NoSuchFieldException JavaDoc e) {
118                 foundField = false;
119                 }
120             } while (((c = c.getSuperclass()) != null) && (!foundField));
121             if (!foundField) {
122                 if (!oneFailed) {
123                 oneFailed = true;
124                 }
125                 result.addErrorDetails(smh.getLocalString
126                            ("tests.componentNameConstructor",
127                             "For [ {0} ]",
128                             new Object JavaDoc[] {compName.toString()}));
129                 result.failed(smh.getLocalString
130                       (getClass().getName() + ".failedException1",
131                        "Error: [ {0} ] field not found within class [ {1} ]",
132                        new Object JavaDoc[] {nextPersistentField.getName(),((EjbEntityDescriptor)descriptor).getEjbClassName()}));
133             }
134
135             } catch (ClassNotFoundException JavaDoc e) {
136             Verifier.debug(e);
137             if (!oneFailed) {
138                 oneFailed = true;
139             }
140             result.addErrorDetails(smh.getLocalString
141                            ("tests.componentNameConstructor",
142                         "For [ {0} ]",
143                         new Object JavaDoc[] {compName.toString()}));
144             result.failed(smh.getLocalString
145                       (getClass().getName() + ".failedException",
146                        "Error: [ {0} ] class not found.",
147                        new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getEjbClassName()}));
148             }
149             if (!oneFailed) {
150             result.addGoodDetails(smh.getLocalString
151                        ("tests.componentNameConstructor",
152                     "For [ {0} ]",
153                     new Object JavaDoc[] {compName.toString()}));
154             result.passed(smh.getLocalString
155                       (getClass().getName() + ".passed",
156                        "This entity bean class [ {0} ] has defined [ {1} ] container managed field as non-transient field.",
157                        new Object JavaDoc[] {c1.getName(),nextPersistentField.getName()}));
158             }
159         }
160                 if (oneFailed) {
161                     result.setStatus(Result.FAILED);
162                 } else {
163                     result.setStatus(Result.PASSED);
164                 }
165         return result;
166  
167         } else { // if (BEAN_PERSISTENCE.equals(persistence)) {
168
result.addNaDetails(smh.getLocalString
169                     ("tests.componentNameConstructor",
170                      "For [ {0} ]",
171                      new Object JavaDoc[] {compName.toString()}));
172         result.notApplicable(smh.getLocalString
173                      (getClass().getName() + ".notApplicable1",
174                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
175                       new Object JavaDoc[] {EjbEntityDescriptor.CONTAINER_PERSISTENCE,descriptor.getName(),persistence}));
176         return result;
177         }
178     } else {
179         result.addNaDetails(smh.getLocalString
180                        ("tests.componentNameConstructor",
181                     "For [ {0} ]",
182                     new Object JavaDoc[] {compName.toString()}));
183         result.notApplicable(smh.getLocalString
184                  (getClass().getName() + ".notApplicable",
185                   "{0} expected {1} bean, but called with {2}.",
186                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
187         return result;
188     }
189     }
190 }
191
Popular Tags