KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > verifier > tests > webservices > WSTest


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
24 package com.sun.enterprise.tools.verifier.tests.webservices;
25
26
27 import com.sun.enterprise.tools.verifier.*;
28 import com.sun.enterprise.deployment.*;
29 import com.sun.enterprise.deployment.util.ModuleDescriptor;
30
31 import java.lang.ClassLoader JavaDoc;
32 import com.sun.enterprise.tools.verifier.tests.*;
33 import com.sun.enterprise.util.io.FileUtils;
34
35 import java.lang.reflect.*;
36
37 import java.io.File JavaDoc;
38
39 /**
40  * Superclass for all EJB tests, contains common services.
41  *
42  * @version
43  */

44 /*
45  * @class.setup_props: ;
46  */

47
48 /*
49  * @testName: check
50  * @assertion_ids: ;
51  * @test_Strategy:
52  * @class.testArgs: Additional arguments (if any) to be passed when execing the client
53  */

54
55 public abstract class WSTest extends VerifierTest implements VerifierCheck, WSCheck {
56         
57     /**
58      * <p>
59      * run an individual test against the deployment descriptor for the
60      * archive the verifier is performing compliance tests against.
61      * </p>
62      *
63      * @paramm descriptor deployment descriptor for the archive
64      * @return result object containing the result of the individual test
65      * performed
66      */

67     public Result check(Descriptor descriptor) {
68         return check((WebServiceEndpoint) descriptor);
69     }
70    
71     // tests which require the WebService descriptor can obtain it from the WebServiceEndpoint
72
// similarly tests which require WebServiceDescriptor can query one more level up to get the
73
// descriptor.
74
/**
75      * @param descriptor deployment descriptor for the archive file
76      * @return result object containing the result of the individual test performed
77      */

78     public abstract Result check(WebServiceEndpoint descriptor);
79     
80     /**
81      * <p>
82      * load the declared SEI class from the archive
83      * </p>
84      *
85      * @param descriptor the deployment descriptors for the WebService
86      * @param result result to use if the load fails
87      * @return the class object for the Service Endpoint Interface
88      */

89     protected Class JavaDoc loadSEIClass(WebServiceEndpoint descriptor, Result result) {
90     ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
91         
92        try {
93        Context context = getVerifierContext();
94        ClassLoader JavaDoc jcl = context.getClassLoader();
95        Class JavaDoc cl = Class.forName(descriptor.getServiceEndpointInterface(), false, getVerifierContext().getClassLoader());
96            result.passed(smh.getLocalString (
97            "com.sun.enterprise.tools.verifier.tests.webservices.clpassed",
98            "The [{0}] Class [{1}] exists and was loaded successfully.",
99            new Object JavaDoc[] {"SEI",descriptor.getServiceEndpointInterface()}));
100            return cl;
101         } catch (ClassNotFoundException JavaDoc e) {
102         Verifier.debug(e);
103         result.failed(smh.getLocalString
104         ("com.sun.enterprise.tools.verifier.tests.webservices.WSTest.SEIClassExists",
105          "Error: Service Endpoint Interface class [ {0} ] not found.",
106          new Object JavaDoc[] {descriptor.getServiceEndpointInterface()}));
107             return null;
108     }
109     }
110
111     /**
112      * <p>
113      * load the declared Service Impl Bean class from the archive
114      * </p>
115      *
116      * @param descriptor the deployment descriptors for the WebService
117      * @param result result to use if the load fails
118      * @return the class object for the Service Endpoint Interface
119      */

120     protected Class JavaDoc loadImplBeanClass(WebServiceEndpoint descriptor, Result result) {
121          // here we could be an EJB Endpoint or a Servlet Endpoint take care of that
122
ComponentNameConstructor compName = getVerifierContext().getComponentNameConstructor();
123         String JavaDoc beanClassName = null;
124
125         if (descriptor.implementedByEjbComponent()) {
126             beanClassName = descriptor.getEjbComponentImpl().getEjbClassName();
127         }
128         else if (descriptor.implementedByWebComponent()) {
129             WebComponentDescriptor wcd = descriptor.getWebComponentImpl();
130             if(wcd!=null)
131                 beanClassName = wcd.getWebComponentImplementation();
132         }
133         else {
134            //result.fail, neither implemented by web nor EJB
135
result.addErrorDetails(smh.getLocalString
136             ("com.sun.enterprise.tools.verifier.tests.webservices.Error",
137              "Error: Unexpected error occurred [ {0} ]",
138             new Object JavaDoc[] {"The WebService is neither implemented by an EJB nor a Servlet"}));
139         }
140        if (beanClassName != null) {
141          try {
142          Context context = getVerifierContext();
143          ClassLoader JavaDoc jcl = context.getClassLoader();
144              return Class.forName(beanClassName, false, getVerifierContext().getClassLoader());
145              }
146              catch (ClassNotFoundException JavaDoc e) {
147             Verifier.debug(e);
148             result.failed(smh.getLocalString
149             ("com.sun.enterprise.tools.verifier.tests.webservices.WSTest.BeanClassExists",
150          "Error: Service Endpoint Implementation Bean class [ {0} ] not found.",
151          new Object JavaDoc[] {beanClassName}));
152               return null;
153          }
154         }
155        //result.fail , beanclass name is NULL
156
result.addErrorDetails(smh.getLocalString
157          ("com.sun.enterprise.tools.verifier.tests.webservices.Error",
158           "Error: Unexpected error occurred [ {0} ]",
159           new Object JavaDoc[] {"The Servlet Impl Bean class name could not be resolved"}));
160
161        return null;
162     }
163
164     public boolean isSEIMethod (MethodDescriptor mdesc, EjbDescriptor desc, Class JavaDoc sei, ClassLoader JavaDoc cl) {
165
166           Method[] seiMeths = sei.getMethods();
167           Method methToBeTested = null;
168
169           try {
170             methToBeTested = mdesc.getMethod(desc);
171           } catch(Exception JavaDoc e) {
172             // internal error cannot get Method for MethodDescriptor
173
/*
174             result.addErrorDetails(smh.getLocalString
175               ("com.sun.enterprise.tools.verifier.tests.webservices.Error",
176                "Error: Unexpected error occurred [ {0} ]",
177                new Object[] {e.getMessage()}));
178              */

179             return false;
180           }
181
182           for (int i=0; i < seiMeths.length; i++) {
183             if (WSTest.matchesSignatureAndReturn(seiMeths[i], methToBeTested))
184                return true;
185           }
186         return false;
187      }
188
189      public static boolean matchesSignatureAndReturn (Method meth1, Method meth2) {
190
191         if (!(meth1.getName()).equals(meth2.getName()))
192            return false;
193
194         /*
195         int mod1 = meth1.getModifiers();
196         int mod2 = meth2.getModifiers();
197
198         if (mod1 != mod2)
199            return false;
200         */

201
202         Class JavaDoc ret1 = meth1.getReturnType();
203         Class JavaDoc ret2 = meth2.getReturnType();
204         if (ret1 != ret2)
205            return false;
206
207         Class JavaDoc[] param1 = meth1.getParameterTypes();
208         Class JavaDoc[] param2 = meth2.getParameterTypes();
209
210         if(param1.length != param2.length)
211           return false;
212
213         for(int i = 0; i < param1.length; i++)
214             if(param1[i] != param2[i])
215                return false;
216
217
218         // for exceptions, every exception in meth2 should be defined
219
// in meth1
220
Class JavaDoc[] excep1 = meth1.getExceptionTypes();
221         Class JavaDoc[] excep2 = meth2.getExceptionTypes();
222
223         for(int i = 0; i < excep2.length; i++) {
224             if(!isMatching(excep2[i], excep1))
225                return false;
226         }
227
228       return true;
229      }
230
231      private static boolean isMatching(Class JavaDoc cl, Class JavaDoc[] classes) {
232
233       for (int i= 0; i < classes.length; i++) {
234            /*
235           if (classes[i].isAssignableFrom(cl))
236              return true;
237           if (cl.isAssignableFrom(classes[i]))
238              return true;
239           */

240           if (classes[i].equals(cl))
241               return true;
242       }
243       return false;
244      }
245
246     protected String JavaDoc getAbstractArchiveUri(WebServiceEndpoint desc) {
247         String JavaDoc archBase = getVerifierContext().getAbstractArchive().
248                 getArchiveUri();
249         final ModuleDescriptor moduleDescriptor = desc.getBundleDescriptor().
250                 getModuleDescriptor();
251         if (moduleDescriptor.isStandalone()) {
252             return archBase; // it must be a stand-alone module; no such physical dir exists
253
} else {
254             return archBase + File.separator +
255                     FileUtils.makeFriendlyFileName(moduleDescriptor.getArchiveUri());
256         }
257     }
258
259 }
260
Popular Tags