KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > ejb > businessmethod > BusinessMethodMatchesWithDD


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.businessmethod;
24
25 import com.sun.enterprise.tools.verifier.tests.ejb.EjbTest;
26 import java.lang.reflect.*;
27 import com.sun.enterprise.deployment.*;
28 import com.sun.enterprise.tools.verifier.*;
29 import com.sun.enterprise.tools.verifier.tests.ejb.*;
30 import com.sun.enterprise.tools.verifier.tests.*;
31 import java.util.*;
32
33 /**
34  * Enterprise Bean's business(...) methods name test.
35  * Each enterprise Bean class must define zero or more business(...) methods.
36  * The method signatures must follow these rules:
37  *
38  * The methods in the remote/local interface should be present in the deployment
39  * descriptor
40  *
41  */

42 public class BusinessMethodMatchesWithDD extends EjbTest implements EjbCheck {
43
44     Result result = null;
45     ComponentNameConstructor compName = null;
46
47     /**
48      * Enterprise Bean's business(...) methods name test.
49      * Each enterprise Bean class must define zero or more business(...) methods.
50      * The method signatures must follow these rules:
51      *
52      * The methods in the remote/local interface should be present in the
53      * deployment descriptor
54      *
55      * @param descriptor the Enterprise Java Bean deployment descriptor
56      * @return <code>Result</code> the results for this assertion
57      */

58     public Result check(EjbDescriptor descriptor) {
59
60     result = getInitializedResult();
61     compName = getVerifierContext().getComponentNameConstructor();
62
63     if ((descriptor instanceof EjbSessionDescriptor) ||
64         (descriptor instanceof EjbEntityDescriptor)) {
65          
66         if(descriptor.getRemoteClassName() != null &&
67                 !"".equals(descriptor.getRemoteClassName()))
68             commonToBothInterfaces(descriptor.getRemoteClassName(),
69                                    descriptor,
70                                    MethodDescriptor.EJB_REMOTE);
71         
72         if(descriptor.getLocalClassName() != null &&
73                 !"".equals(descriptor.getLocalClassName()))
74             commonToBothInterfaces(descriptor.getLocalClassName(),
75                                    descriptor,
76                                    MethodDescriptor.EJB_LOCAL);
77     }
78         
79     if(result.getStatus() != Result.FAILED) {
80         addGoodDetails(result, compName);
81         result.passed(smh.getLocalString
82                         (getClass().getName() + ".passed",
83                         "Business method(s) are valid."));
84     }
85     return result;
86     }
87     
88     /**
89      * This method is responsible for the logic of the test. It is called for
90      * both local and remote interfaces.
91      * @param intf for the Remote/Local interface of the Ejb.
92      * @param descriptor the Enterprise Java Bean deployment descriptor
93      * This parameter may be optional depending on the test
94      * @param methodIntf for the interface type
95      */

96
97     private void commonToBothInterfaces(String JavaDoc intf,
98                                         EjbDescriptor descriptor,
99                                         String JavaDoc methodIntf) {
100         try {
101             Class JavaDoc intfClass = Class.forName(intf, false, getVerifierContext().getClassLoader());
102             
103             boolean found = false;
104             Set allMethods = new HashSet();
105             
106             for (Iterator e =
107                     descriptor.getPermissionedMethodsByPermission().values().iterator();e.hasNext();) {
108                 Set methodDescriptors = (Set) e.next();
109                 if (methodDescriptors != null)
110                     allMethods.addAll(methodDescriptors);
111             }
112             for (Enumeration e =
113                     descriptor.getMethodContainerTransactions().keys();e.hasMoreElements();) {
114                 allMethods.add(e.nextElement());
115             }
116             
117             for (Method remoteMethod : intfClass.getMethods()) {
118                 found = false;
119                 
120                 // we don't test the EJB methods
121
if (remoteMethod.getDeclaringClass().getName().equals("javax.ejb.EJBObject"))
122                     continue;
123                 if (!remoteMethod.getName().startsWith("ejb") &&
124                         !remoteMethod.getName().equals("class$") &&
125                         !remoteMethod.getName().equals("setSessionContext")) {
126                     
127                     Iterator methods = allMethods.iterator();
128                     while (methods.hasNext()) {
129                         MethodDescriptor methodDescriptor = (MethodDescriptor)methods.next();
130                         
131                         if (methodDescriptor.getName().equals(remoteMethod.getName())) {
132                             if (MethodUtils.stringArrayEquals(methodDescriptor.getParameterClassNames(),
133                                     (new MethodDescriptor(remoteMethod,methodIntf)).getParameterClassNames())) {
134                                 found = true;
135                                 break;
136                             }
137                         }
138                     }
139                 }
140                 if (!found) {
141                     addErrorDetails(result, compName);
142                     result.failed(smh.getLocalString
143                                     (getClass().getName() + ".failed",
144                                     "Error: Business method [ {0} ] is not defined " +
145                                     "in the deployment descriptor.",
146                                     new Object JavaDoc[] {remoteMethod.getName()}));
147                 }
148             }
149             
150         } catch (Exception JavaDoc e) {
151             Verifier.debug(e);
152             addErrorDetails(result, compName);
153             result.failed(smh.getLocalString
154                             (getClass().getName() + ".failedException",
155                             "Error: Component interface [ {0} ] does not exist " +
156                             "or is not loadable within bean [ {1} ].",
157                             new Object JavaDoc[] {intf,descriptor.getName()}));
158         }
159     }
160 }
161
Popular Tags