KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > homeintf > HomeMethodTest


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.homeintf;
24
25 import com.sun.enterprise.tools.verifier.Result;
26 import com.sun.enterprise.tools.verifier.Verifier;
27
28 import java.lang.ClassLoader JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.util.Vector JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
34 import com.sun.enterprise.tools.verifier.tests.ComponentNameConstructor;
35 import com.sun.enterprise.deployment.EjbDescriptor;
36 import com.sun.enterprise.deployment.EjbSessionDescriptor;
37 import com.sun.enterprise.deployment.EjbEntityDescriptor;
38
39 /**
40  * Superclass for all Home methods tests.
41  *
42  * @author Jerome Dochez
43  * @version
44  */

45 abstract public class HomeMethodTest extends EjbTest {
46
47     /** Method tells the name of the home interface class that called this test
48      */

49     abstract protected String JavaDoc getHomeInterfaceName(EjbDescriptor descriptor);
50     abstract protected String JavaDoc getSuperInterface();
51     
52     /** <p>
53      * run an individual home method test
54      * </p>
55      * @param descriptor the deployment descriptor for the entity bean
56      * @param result the result object
57      * @param m the mehtod to test
58      */

59   
60     abstract protected void runIndividualHomeMethodTest( Method JavaDoc m,EjbDescriptor descriptor, Result result);
61     
62     /**
63      *
64      * @param descriptor the Enterprise Java Bean deployment descriptor
65      *
66      * @return <code>Result</code> the results for this assertion
67      */

68     public Result check(EjbDescriptor descriptor) {
69
70     Result result = getInitializedResult();
71     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
72
73     if(getHomeInterfaceName(descriptor) == null || "".equals(getHomeInterfaceName(descriptor))){
74             addNaDetails(result, compName);
75             result.notApplicable(smh.getLocalString
76                        ("com.sun.enterprise.tools.verifier.tests.ejb.localinterfaceonly.notapp",
77                         "Not Applicable because, EJB [ {0} ] has Local Interfaces only.",
78                                           new Object JavaDoc[] {descriptor.getEjbClassName()}));
79
80         return result;
81     }
82
83     if (!(descriptor instanceof EjbSessionDescriptor) &&
84         !(descriptor instanceof EjbEntityDescriptor)) {
85         addNaDetails(result, compName);
86         result.notApplicable(smh.getLocalString
87                  ("com.sun.enterprise.tools.verifier.tests.ejb.homeintf.HomeMethodTest.notApplicable1",
88                   "Test apply only to session or entity beans."));
89         return result;
90         }
91         boolean homeMethodFound = false;
92         
93     try {
94         // retrieve the remote interface methods
95
ClassLoader JavaDoc jcl = getVerifierContext().getClassLoader();
96         Class JavaDoc homeInterfaceClass = Class.forName(getClassName(descriptor), false, jcl);
97             
98             Vector JavaDoc<Method JavaDoc> v = new Vector JavaDoc<Method JavaDoc>();
99
100             while (homeInterfaceClass != null &&
101                     !homeInterfaceClass.getName().equals(getSuperInterface()) &&
102                     !homeInterfaceClass.getName().equals("java.lang.Object")) {
103             Method JavaDoc [] homeInterfaceMethods = homeInterfaceClass.getDeclaredMethods();
104                 for (int i=0;i<homeInterfaceMethods.length;i++) {
105                     v.add(homeInterfaceMethods[i]);
106                 }
107                 homeInterfaceClass = homeInterfaceClass.getSuperclass();
108             }
109                 
110             
111         Iterator JavaDoc iterator = v.iterator();
112             while (iterator.hasNext()) {
113                 Method JavaDoc method = (Method JavaDoc) iterator.next();
114                 String JavaDoc methodName = method.getName();
115                 if (methodName.startsWith("create") || methodName.startsWith("find") ||
116                     methodName.startsWith("remove"))
117                     continue;
118                 
119                 Method JavaDoc m = getMethod(javax.ejb.EJBHome JavaDoc.class, methodName,
120                                      method.getParameterTypes());
121                 if (m!=null) {
122                     // this is an EJBHome method...
123
continue;
124                 }
125             
126                 homeMethodFound = true;
127  
128         // if (!runIndividualHomeMethodTest( method,descriptor, result))
129
// oneFailed = true;
130
runIndividualHomeMethodTest( method,descriptor, result);
131         if (result.getStatus() == Result.FAILED)
132             break;
133                 
134         }
135     } catch (ClassNotFoundException JavaDoc e) {
136         Verifier.debug(e);
137         addErrorDetails(result, compName);
138         result.failed(smh.getLocalString
139               ("com.sun.enterprise.tools.verifier.tests.ejb.homeintf.HomeMethodTest.failedException",
140                "Error: Home interface [ {0} ] does not exist or is not loadable within bean [ {1} ]",
141                new Object JavaDoc[] {getClassName(descriptor),descriptor.getName()}));
142     }
143
144         if (!homeMethodFound) {
145         addNaDetails(result, compName);
146         result.notApplicable(smh.getLocalString
147               ("com.sun.enterprise.tools.verifier.tests.ejb.homeintf.HomeMethodTest.notApplicable2",
148                "Home interface [ {0} ] does not define any home methods",
149                new Object JavaDoc[] {getClassName(descriptor)}));
150         } //else {
151
// result.setStatus(oneFailed?result.FAILED:Result.PASSED);
152
// }
153
return result;
154     }
155
156     private String JavaDoc getClassName(EjbDescriptor descriptor) {
157     return getHomeInterfaceName(descriptor);
158     }
159 }
160
Popular Tags