KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > intf > InterfaceMethodTest


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.intf;
24
25 import com.sun.enterprise.deployment.EjbDescriptor;
26 import com.sun.enterprise.deployment.EjbEntityDescriptor;
27 import com.sun.enterprise.deployment.EjbSessionDescriptor;
28 import com.sun.enterprise.tools.verifier.Result;
29 import com.sun.enterprise.tools.verifier.Verifier;
30 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
31 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
32
33 import java.lang.reflect.Method JavaDoc;
34 import java.util.Arrays JavaDoc;
35
36 /**
37  * Superclass for all local/remote interfaces method testing.
38  *
39  */

40 abstract public class InterfaceMethodTest extends EjbTest {
41     
42     static String JavaDoc[] EJBObjectMethods =
43             { "getEJBHome", "getHandle", "getPrimaryKey",
44               "isIdentical", "remove", "getEJBLocalHome",
45             };
46     
47     /**
48      * Methods to get the type of interface: local/remote and the name of the class
49      */

50     
51     abstract protected String JavaDoc getInterfaceName(EjbDescriptor descriptor);
52     abstract protected String JavaDoc getInterfaceType();
53     
54     
55     /**
56      * <p>
57      * run an individual verifier test against a declared method of the
58      * local or remote interface.
59      * </p>
60      *
61      * @param descriptor the deployment descriptor for the bean
62      * @param method the method to run the test on
63      * @return true if the test passes
64      */

65     
66     abstract protected boolean runIndividualMethodTest(EjbDescriptor descriptor, Method JavaDoc method, Result result);
67     
68     /**
69      * Run the verifier test against the local or remote interface, get all methods
70      * and delegate actual testing for individual methods to the
71      * runIndividualMethodTest
72      *
73      * @param descriptor the Enterprise Java Bean deployment descriptor
74      *
75      * @return <code>Result</code> the results for this assertion
76      */

77     public Result check(EjbDescriptor descriptor) {
78         
79         Result result = getInitializedResult();
80         ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
81         
82         if (!(descriptor instanceof EjbSessionDescriptor) &&
83                 !(descriptor instanceof EjbEntityDescriptor)) {
84             addNaDetails(result, compName);
85             result.notApplicable(smh.getLocalString
86                     ("com.sun.enterprise.tools.verifier.tests.ejb.homeintf.HomeMethodTest.notApplicable1",
87                     "Test apply only to session or entity beans."));
88             return result;
89         }
90         
91         if(getInterfaceName(descriptor) == null || "".equals(getInterfaceName(descriptor))){
92             addNaDetails(result, compName);
93             result.notApplicable(smh.getLocalString
94                     ("com.sun.enterprise.tools.verifier.tests.ejb.intf.InterfaceTest.notApplicable",
95                     "Not Applicable because, EJB [ {0} ] does not have {1} Interface.",
96                     new Object JavaDoc[] {descriptor.getEjbClassName(), getInterfaceType()}));
97             return result;
98         }
99         
100         try {
101             
102             Arrays.sort(EJBObjectMethods);
103             
104             // retrieve the local/remote interface methods
105
ClassLoader JavaDoc jcl = getVerifierContext().getClassLoader();
106             Class JavaDoc interfaceClass = Class.forName(getClassName(descriptor), false, jcl);
107             
108             if (studyInterface(descriptor, interfaceClass, result)) {
109                 result.setStatus(Result.PASSED);
110             } else {
111                 result.setStatus(Result.FAILED);
112             }
113         } catch (ClassNotFoundException JavaDoc e) {
114             Verifier.debug(e);
115             addErrorDetails(result, compName);
116             result.failed(smh.getLocalString
117                     (getClass().getName() + ".failedException",
118                     "Error: "+ getInterfaceType()+"interface [ {0} ] does not " +
119                     "exist or is not loadable within bean [ {1} ]",
120                     new Object JavaDoc[] {getClassName(descriptor),descriptor.getName()}));
121         }
122         
123         return result;
124     }
125     
126     /**
127      * <p>
128      * study an interface by running an individual test on each method of the
129      * inteface then recursively study all the interfaces this interface extends
130      * </p>
131      *
132      * @param descriptor the bean deployment descriptor
133      * @param clazz the interface to study
134      * @param result to place the results of the tests in
135      * @return true if all tests passed
136      */

137     private boolean studyInterface(EjbDescriptor descriptor, Class JavaDoc clazz, Result result) {
138         
139         boolean allGood = true;
140         Method JavaDoc [] interfaceMethods = clazz.getDeclaredMethods();
141         
142         for (Method JavaDoc interfaceMethod : interfaceMethods) {
143             if (Arrays.binarySearch(EJBObjectMethods, interfaceMethod.getName()) < 0) {
144                 
145                 if (!runIndividualMethodTest(descriptor, interfaceMethod,result))
146                     allGood = false;
147                 
148             } // if you found a business method
149
} // for all local or remote interface methods for the current class
150

151         // now all superinterfaces....
152
for (Class JavaDoc intf : clazz.getInterfaces()) {
153             if (!studyInterface(descriptor, intf, result))
154                 allGood = false;
155         }
156         return allGood;
157     }
158     
159     private String JavaDoc getClassName(EjbDescriptor descriptor) {
160         return getInterfaceName(descriptor);
161     }
162 }
163
164
Popular Tags