KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.sun.enterprise.tools.verifier.tests.ejb.EjbUtils;
34 import com.sun.enterprise.tools.verifier.tests.ejb.RmiIIOPUtils;
35
36 /**
37  * The Bean Provider must ensure that the Java types assigned to the
38  * container-managed fields are restricted to the following: Java primitive
39  * types, Java serializable types, and references of enterprise beans' remote
40  * or home interfaces.
41  */

42 public class CmpFieldsJavaTypesAssigned extends EjbTest implements EjbCheck {
43
44
45     /**
46      * The Bean Provider must ensure that the Java types assigned to the
47      * container-managed fields are restricted to the following: Java primitive
48      * types, Java serializable types, and references of enterprise beans' remote
49      * or home interfaces.
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
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                 
77         boolean oneFailed = false;
78         boolean badField = false;
79
80         Iterator itr = ((EjbCMPEntityDescriptor)descriptor).getPersistenceDescriptor().getCMPFields().iterator();
81         while (itr.hasNext()) {
82  
83             FieldDescriptor nextPersistentField = (FieldDescriptor)itr.next();
84             badField = false;
85             boolean foundField = false;
86
87             // ensure that the Java types assigned to the container-managed
88
// fields are restricted to the following: Java primitive types,
89
// Java serializable types, and references of enterprise beans'
90
// remote or home interfaces.
91
Class JavaDoc c1 = null;
92             try {
93             Class JavaDoc c = Class.forName(((EjbEntityDescriptor)descriptor).getEjbClassName(), false, getVerifierContext().getClassLoader());
94             // start do while loop here....
95
do {
96                 try {
97                 c1 = c;
98                 Field f = c.getDeclaredField(nextPersistentField.getName());
99                 foundField = true;
100                 Class JavaDoc fc = f.getType();
101                 if ((RmiIIOPUtils.isValidRmiIDLPrimitiveType(fc)) ||
102                     (descriptor.getRemoteClassName().equals(fc.getName())) ||
103                     (descriptor.getHomeClassName().equals(fc.getName())) ||
104                     (EjbUtils.isValidSerializableType(fc))||
105                     (descriptor.getLocalClassName().equals(fc.getName())) ||
106                     (descriptor.getLocalHomeClassName().equals(fc.getName()))) {
107                     continue;
108                 } else {
109                     if (!oneFailed) {
110                     oneFailed = true;
111                     }
112                     badField = true;
113                 }
114         
115                 if (badField) {
116                     result.addErrorDetails(smh.getLocalString
117                                ("tests.componentNameConstructor",
118                                 "For [ {0} ]",
119                                 new Object JavaDoc[] {compName.toString()}));
120                     result.failed(smh.getLocalString
121                           (getClass().getName() + ".failed",
122                            "Error: Field [ {0} ] defined within entity bean class [ {1} ] was assigned an invalid type. Container managed field must be assigned in the entity bean class with Java types restricted to the following: Java primitive types, Java serializable types, and references of enterprise beans' remote or home interfaces.",
123                            new Object JavaDoc[] {nextPersistentField.getName(),((EjbEntityDescriptor)descriptor).getEjbClassName()}));
124                 }
125                 } catch (NoSuchFieldException JavaDoc e) {
126                 foundField = false;
127                 }
128                         } while (((c = c.getSuperclass()) != null) && (!foundField));
129
130                         if (!foundField) {
131                             if (!oneFailed) {
132                                 oneFailed = true;
133                             }
134                 result.addErrorDetails(smh.getLocalString
135                            ("tests.componentNameConstructor",
136                             "For [ {0} ]",
137                             new Object JavaDoc[] {compName.toString()}));
138                 result.failed(smh.getLocalString
139                       (getClass().getName() + ".failedException1",
140                        "Error: [ {0} ] field not found within class [ {1} ]",
141                        new Object JavaDoc[] {nextPersistentField.getName(),((EjbEntityDescriptor)descriptor).getEjbClassName()}));
142                 }
143             } catch (ClassNotFoundException JavaDoc e) {
144             Verifier.debug(e);
145             result.addErrorDetails(smh.getLocalString
146                            ("tests.componentNameConstructor",
147                         "For [ {0} ]",
148                         new Object JavaDoc[] {compName.toString()}));
149             result.failed(smh.getLocalString
150                       (getClass().getName() + ".failedException",
151                        "Error: [ {0} ] class not found.",
152                        new Object JavaDoc[] {((EjbEntityDescriptor)descriptor).getEjbClassName()}));
153             }
154
155             if (!oneFailed) {
156             result.addGoodDetails(smh.getLocalString
157                           ("tests.componentNameConstructor",
158                            "For [ {0} ]",
159                            new Object JavaDoc[] {compName.toString()}));
160             result.passed(smh.getLocalString
161                       (getClass().getName() + ".passed",
162                        "This entity bean class [ {0} ] has assigned [ {1} ] container managed field with valid Java type.",
163                        new Object JavaDoc[] {c1.getName(),nextPersistentField.getName()}));
164             }
165                 }
166                 if (oneFailed) {
167                     result.setStatus(Result.FAILED);
168                 } else {
169                     result.setStatus(Result.PASSED);
170                 }
171         return result;
172  
173         } else { // if (BEAN_PERSISTENCE.equals(persistence)) {
174
result.addNaDetails(smh.getLocalString
175                     ("tests.componentNameConstructor",
176                      "For [ {0} ]",
177                      new Object JavaDoc[] {compName.toString()}));
178         result.notApplicable(smh.getLocalString
179                      (getClass().getName() + ".notApplicable1",
180                       "Expected [ {0} ] managed persistence, but [ {1} ] bean has [ {2} ] managed persistence.",
181                       new Object JavaDoc[] {EjbEntityDescriptor.CONTAINER_PERSISTENCE,descriptor.getName(),persistence}));
182         return result;
183         }
184     } else {
185         result.addNaDetails(smh.getLocalString
186                 ("tests.componentNameConstructor",
187                  "For [ {0} ]",
188                  new Object JavaDoc[] {compName.toString()}));
189         result.notApplicable(smh.getLocalString
190                  (getClass().getName() + ".notApplicable",
191                   "{0} expected {1} bean, but called with {2}.",
192                   new Object JavaDoc[] {getClass(),"Entity","Session"}));
193         return result;
194     }
195
196
197
198     }
199 }
200
Popular Tags