KickJava   Java API By Example, From Geeks To Geeks.

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


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.cmp2;
24
25 import java.util.*;
26 import java.lang.reflect.Method JavaDoc;
27 import com.sun.enterprise.deployment.*;
28 import com.sun.enterprise.tools.verifier.Result;
29 import com.sun.enterprise.tools.verifier.tests.ejb.EjbUtils;
30 import com.sun.enterprise.tools.verifier.tests.ejb.RmiIIOPUtils;
31 import com.sun.enterprise.tools.verifier.tests.*;
32
33
34 /**
35  * Check that the field type for all declated cmp fields of the entity bean
36  * are of an acceptable type :
37  * - Java Primitive Type
38  * - Java Serializable class
39  * - Reference to a bean's home or bean's remote interface
40  *
41  * @author Jerome Dochez
42  * @version 1.0
43  */

44 public class CmpFields extends CmpFieldTest {
45  
46     /**
47      * run an individual verifier test of a declated cmp field of the class
48      *
49      * @param entity the descriptor for the entity bean containing the cmp-field
50      * @param f the descriptor for the declared cmp field
51      * @param c the class owning the cmp field
52      * @parma r the result object to use to put the test results in
53      *
54      * @return true if the test passed
55      */

56
57     protected boolean runIndividualCmpFieldTest(Descriptor entity, Descriptor persistentField, Class JavaDoc c, Result result) {
58     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
59         String JavaDoc cmpFieldName = persistentField.getName();
60         String JavaDoc getMethodName = "get" + Character.toUpperCase(cmpFieldName.charAt(0)) + cmpFieldName.substring(1);
61         Method JavaDoc getMethod = getMethod(c, getMethodName, null);
62         Class JavaDoc fieldType;
63
64         if (getMethod != null) {
65         boolean run = false;
66             // get the return type for the setMethod
67
fieldType = getMethod.getReturnType();
68         EjbBundleDescriptor bundle = ((EjbDescriptor) entity).getEjbBundleDescriptor();
69         if (!RmiIIOPUtils.isValidRmiIDLPrimitiveType(fieldType) &&
70         !EjbUtils.isValidSerializableType(fieldType)) {
71         // it must be a reference to a bean's home or local interface
72
if (!isValidInterface(fieldType, bundle.getEjbs(),MethodDescriptor.EJB_REMOTE, result)) {
73              result.addErrorDetails(smh.getLocalString
74                        ("tests.componentNameConstructor",
75                     "For [ {0} ]",
76                     new Object JavaDoc[] {compName.toString()}));
77             result.addErrorDetails(smh.getLocalString
78                (getClass().getName() + ".failed",
79                "Error : Invalid type assigned for container managed field [ {0} ] in bean [ {1} ]",
80                 new Object JavaDoc[] {((Descriptor)persistentField).getName(),entity.getName()}));
81             return false;
82         }
83         if (!isValidInterface(fieldType, bundle.getEjbs(),MethodDescriptor.EJB_LOCAL, result)) {
84              result.addErrorDetails(smh.getLocalString
85                        ("tests.componentNameConstructor",
86                     "For [ {0} ]",
87                     new Object JavaDoc[] {compName.toString()}));
88             result.addErrorDetails(smh.getLocalString
89                   (getClass().getName() + ".failed",
90                     "Error : Invalid type assigned for container managed field [ {0} ] in bean [ {1} ]",
91                 new Object JavaDoc[] {((Descriptor)persistentField).getName(),entity.getName()}));
92                 return false;
93         }
94         }
95         result.addGoodDetails(smh.getLocalString
96                        ("tests.componentNameConstructor",
97                     "For [ {0} ]",
98                     new Object JavaDoc[] {compName.toString()}));
99         result.addGoodDetails(smh.getLocalString
100            (getClass().getName() + ".passed",
101             "Valid type assigned for container managed field [ {0} ] in bean [ {1} ]",
102             new Object JavaDoc[] {((Descriptor)persistentField).getName(),entity.getName()}));
103         run = true;
104         return run;
105         
106         } else {
107         result.addErrorDetails(smh.getLocalString
108                        ("tests.componentNameConstructor",
109                     "For [ {0} ]",
110                     new Object JavaDoc[] {compName.toString()}));
111             result.addErrorDetails(smh.getLocalString
112         ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.isAccessorDeclared.failed2",
113          "Error : Cannot find accessor [ {0} ] method for [ {1} ] field ",
114          new Object JavaDoc[] {getMethodName , persistentField.getName()}));
115         }
116         return false;
117     }
118
119     private boolean isValidInterface(Class JavaDoc fieldType, Set entities,String JavaDoc interfaceType, Result result) {
120     try {
121         if (entities==null)
122             return false;
123         
124         Iterator iterator = entities.iterator();
125     if(interfaceType.equals(MethodDescriptor.EJB_REMOTE)) {
126         while (iterator.hasNext()) {
127         EjbAbstractDescriptor entity = (EjbAbstractDescriptor) iterator.next();
128         
129         if (fieldType.getName().equals(entity.getHomeClassName()) ||
130             fieldType.getName().equals(entity.getRemoteClassName()))
131             return true;
132         }
133     }
134     if(interfaceType.equals(MethodDescriptor.EJB_LOCAL)) {
135         while (iterator.hasNext()) {
136         EjbAbstractDescriptor entity = (EjbAbstractDescriptor) iterator.next();
137         
138         if (fieldType.getName().equals(entity.getLocalHomeClassName()) ||
139             fieldType.getName().equals(entity.getLocalClassName()))
140             return true;
141         }
142     }
143         return false;
144     }catch(Throwable JavaDoc t) {
145         result.addErrorDetails(smh.getLocalString
146                   (getClass().getName() + ".failed",
147                     "Error occured in accessing remote/local interface",
148                 new Object JavaDoc[] {}));
149         return false;
150     }
151     }
152 }
153
Popular Tags