KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.*;
26 import com.sun.enterprise.deployment.*;
27 import com.sun.enterprise.tools.verifier.*;
28 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
29 import com.sun.enterprise.tools.verifier.tests.*;
30
31 /**
32  * Superclass containing various tests for all field related tests in cmp2.0
33  * these tests should apply to cmp and cmr fields for entity classes
34  *
35  * @author Jerome Dochez
36  * @version
37  */

38 public abstract class CMPTest extends EjbTest {
39
40     abstract protected Result check(EjbCMPEntityDescriptor descriptor);
41     
42     public Result check(EjbDescriptor descriptor) {
43         Result result = getInitializedResult();
44     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
45         if (descriptor instanceof EjbEntityDescriptor) {
46         String JavaDoc persistentType =
47         ((EjbEntityDescriptor)descriptor).getPersistenceType();
48         if (EjbEntityDescriptor.CONTAINER_PERSISTENCE.equals(persistentType)) {
49                 if (((EjbCMPEntityDescriptor) descriptor).getCMPVersion()<EjbCMPEntityDescriptor.CMP_2_x) {
50                     result.addNaDetails(smh.getLocalString
51                        ("tests.componentNameConstructor",
52                     "For [ {0} ]",
53                     new Object JavaDoc[] {compName.toString()}));
54                 result.notApplicable(smh.getLocalString
55                  ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.notApplicable3",
56                   "Test do not apply to this cmp-version of container managed persistence EJBs"));
57                 return result;
58                 }
59                 return check((EjbCMPEntityDescriptor) descriptor);
60             } else { // if (BEAN_PERSISTENCE.equals(persistentType))
61
result.addNaDetails(smh.getLocalString
62                        ("tests.componentNameConstructor",
63                     "For [ {0} ]",
64                     new Object JavaDoc[] {compName.toString()}));
65         result.notApplicable(smh.getLocalString
66                      ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.notApplicable2",
67                       "Test do not apply to EJB declared with persistence type [ {0} ]",
68                       new Object JavaDoc[] {persistentType}));
69                 return result;
70         }
71     } else {
72         result.addNaDetails(smh.getLocalString
73                        ("tests.componentNameConstructor",
74                     "For [ {0} ]",
75                     new Object JavaDoc[] {compName.toString()}));
76         result.notApplicable(smh.getLocalString
77                  ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.notApplicable1",
78                   "Test do not apply to non entity EJB "));
79         return result;
80     }
81     }
82       
83     /**
84      * check if a field has been declared in a class
85      *
86      * @param fieldName the field name to look for declaration
87      * @param c the class to look into
88      * @param result where to place the test result
89      */

90     public static boolean isFieldAbstract(String JavaDoc fieldName, Class JavaDoc c, Result result)
91     {
92         Class JavaDoc savedClass = c;
93         boolean foundField = false;
94         do {
95         try {
96             Field f = c.getDeclaredField(fieldName);
97                 foundField = true;
98     
99                 result.addErrorDetails(smh.getLocalString
100                 ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.isFieldDeclared.failed",
101                          "Error: [ {0} ] field is declared in the class [ {1} ]",
102              new Object JavaDoc[] {fieldName, c.getName()}));
103         } catch (NoSuchFieldException JavaDoc e) {
104             }
105         } while (((c = c.getSuperclass()) != null) && (!foundField));
106                        
107         if (!foundField) {
108       
109             result.addGoodDetails(smh.getLocalString
110         ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.isFieldDeclared.success",
111                 "[ {0} ] field is not declared in the class [ {1} ]",
112         new Object JavaDoc[] {fieldName, savedClass.getName()}));
113             return true;
114         } else return false;
115     }
116     
117     /**
118      * <p>
119      * check if fields accessor methods conformant to Spec 2.0 paragraph 9.4.1 have
120      * been declared in a class.
121      * </p>
122      *
123      * @param fieldName the field name to look accessor methods for
124      * @param fieldType the field type, maybe null, then the return type of the
125      * get method is used
126      * @param c the class to look for accessors in
127      * @param result where to place the result
128      */

129     public static boolean isAccessorDeclared(String JavaDoc fieldName, Class JavaDoc fieldType, Class JavaDoc c, Result result )
130     {
131         String JavaDoc getMethodName = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
132         String JavaDoc setMethodName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
133         Method getMethod = getMethod(c, getMethodName, null);
134         if (getMethod != null) {
135             if (fieldType != null) {
136                 if (!fieldType.getName().equals(getMethod.getReturnType().getName())) {
137          
138             result.addErrorDetails(smh.getLocalString
139                     ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.isAccessorDeclared.failed1",
140                  "Error : [ {0} ] field accessor method has the wrong return type [ {1} ] ",
141                  new Object JavaDoc[] {fieldName, getMethod.getReturnType().getName()}));
142             return false;
143                 }
144             }
145         // now look for the setmethod
146
Class JavaDoc parms[] = { getMethod.getReturnType() };
147             Method setMethod = getMethod(c, setMethodName, parms);
148             if (setMethod != null) {
149         if (setMethod.getReturnType().getName().equals("void")){
150           
151                     result.addGoodDetails(smh.getLocalString
152                     ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.isAccessorDeclared.success",
153              "[ {0} ] field accessor methods exist and have the correct signaures",
154              new Object JavaDoc[] {fieldName}));
155             return true;
156                 } else {
157           
158             result.addErrorDetails(smh.getLocalString
159                     ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.isAccessorDeclared.failed1",
160                  "Error : [ {0} ] field accessor method has the wrong return type [ {1} ] ",
161                  new Object JavaDoc[] {fieldName, setMethod.getReturnType().getName()}));
162                 }
163             } else {
164     
165         result.addErrorDetails(smh.getLocalString
166             ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.isAccessorDeclared.failed2",
167              "Error : Cannot find accessor [ {0 } ] method for [ {1} ] field ",
168              new Object JavaDoc[] {setMethodName , fieldName}));
169             }
170         } else {
171       
172         result.addErrorDetails(smh.getLocalString
173         ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.isAccessorDeclared.failed2",
174          "Error : Cannot find accessor [ {0} ] method for [ {1} ] field ",
175          new Object JavaDoc[] {getMethodName , fieldName}));
176         }
177         return false;
178     }
179     
180     
181     /**
182      * <p>
183      * Checks that a field name starts with a lowercased letter
184      * </p>
185      *
186      * @param fieldName is the field name
187      * @param result where to put the test result in
188      *
189      * @return true if the test passed
190      */

191     public static boolean startWithLowercasedLetter(String JavaDoc fieldName, Result result) {
192         if (Character.isLowerCase(fieldName.charAt(0))) {
193     
194             result.addGoodDetails(smh.getLocalString
195         ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.startWithLowercasedLetter.success",
196                 "[ {0} ] field first letter is lowercase",
197         new Object JavaDoc[] {fieldName}));
198             return true;
199         } else {
200      
201             result.addErrorDetails(smh.getLocalString
202         ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.startWithLowercasedLetter.failed",
203                 "Error : [ {0} ] field first letter is not lowercase",
204         new Object JavaDoc[] {fieldName}));
205             return false;
206         }
207     }
208     
209     public static boolean accessorMethodModifiers(String JavaDoc fieldName, Class JavaDoc c, Result result) {
210         String JavaDoc getMethodName = "get" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
211         String JavaDoc setMethodName = "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1);
212         
213         // check the get first
214
Method getMethod = getMethod(c, getMethodName, null);
215         if (getMethod != null) {
216             int modifiers = getMethod.getModifiers();
217             if (Modifier.isAbstract(modifiers) && (Modifier.isProtected(modifiers) || Modifier.isPublic(modifiers))) {
218                 // now look for the setmethod
219
Class JavaDoc parms[] = { getMethod.getReturnType() };
220                 Method setMethod = getMethod(c, setMethodName, parms);
221                 if (setMethod != null) {
222                     modifiers = setMethod.getModifiers();
223                     if (Modifier.isAbstract(modifiers) && (Modifier.isProtected(modifiers) || Modifier.isPublic(modifiers))) {
224             result.addGoodDetails(smh.getLocalString
225                         ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.accessorMethodModifiers.success",
226                             "[ {0} ] field accessor methods are abstract and public or protected",
227                     new Object JavaDoc[] {fieldName}));
228                         return true;
229                     }
230                 }
231     
232                 result.addErrorDetails(smh.getLocalString
233                     ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.accessorMethodModifiers.failed",
234                     "Error : [ {0} ] accessor method for field [ {1} ] is not abstract and public or protected",
235             new Object JavaDoc[] {setMethodName, fieldName}));
236                 return false;
237             }
238         }
239
240         result.addErrorDetails(smh.getLocalString
241         ("com.sun.enterprise.tools.verifier.tests.ejb.entity.cmp2.CMPTest.accessorMethodModifiers.failed",
242             "Error : [ {0} ] accessor method for field [ {1} ] is not abstract and public or protected",
243         new Object JavaDoc[] {getMethodName, fieldName}));
244         return false;
245     }
246 }
247
Popular Tags