KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > axis > builder > ServiceReferenceTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.axis.builder;
18
19 import java.io.File JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLClassLoader JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import javax.wsdl.Binding;
28 import javax.wsdl.BindingInput;
29 import javax.wsdl.BindingOperation;
30 import javax.wsdl.BindingOutput;
31 import javax.wsdl.Definition;
32 import javax.wsdl.Input;
33 import javax.wsdl.Message;
34 import javax.wsdl.Operation;
35 import javax.wsdl.OperationType;
36 import javax.wsdl.Output;
37 import javax.wsdl.Part;
38 import javax.wsdl.Port;
39 import javax.wsdl.PortType;
40 import javax.wsdl.WSDLException;
41 import javax.wsdl.extensions.ExtensionRegistry;
42 import javax.wsdl.extensions.soap.SOAPAddress;
43 import javax.wsdl.extensions.soap.SOAPBinding;
44 import javax.wsdl.extensions.soap.SOAPBody;
45 import javax.wsdl.extensions.soap.SOAPOperation;
46 import javax.wsdl.factory.WSDLFactory;
47 import javax.wsdl.xml.WSDLReader;
48 import javax.xml.namespace.QName JavaDoc;
49
50 import org.apache.geronimo.testsupport.TestSupport;
51
52 import org.apache.axis.constants.Style;
53 import org.apache.axis.soap.SOAPConstants;
54 import org.apache.geronimo.axis.builder.bookquote.BookQuote;
55 import org.apache.geronimo.axis.builder.bookquote.BookQuoteService;
56 import org.apache.geronimo.axis.builder.interop.InteropLab;
57 import org.apache.geronimo.axis.builder.interop.InteropTestPortType;
58 import org.apache.geronimo.axis.builder.mock.MockPort;
59 import org.apache.geronimo.axis.builder.mock.MockService;
60 import org.apache.geronimo.axis.client.OperationInfo;
61 import org.apache.geronimo.axis.client.AxisServiceReference;
62 import org.apache.geronimo.common.DeploymentException;
63 import org.apache.geronimo.deployment.DeploymentContext;
64 import org.apache.geronimo.deployment.util.UnpackedJarFile;
65 import org.apache.geronimo.gbean.AbstractName;
66 import org.apache.geronimo.j2ee.deployment.EJBModule;
67 import org.apache.geronimo.j2ee.deployment.Module;
68 import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
69 import org.apache.geronimo.kernel.Jsr77Naming;
70 import org.apache.geronimo.kernel.config.ConfigurationModuleType;
71 import org.apache.geronimo.kernel.config.ConfigurationManager;
72 import org.apache.geronimo.kernel.config.SimpleConfigurationManager;
73 import org.apache.geronimo.kernel.repository.Artifact;
74 import org.apache.geronimo.kernel.repository.Environment;
75 import org.apache.geronimo.kernel.repository.ArtifactManager;
76 import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
77 import org.apache.geronimo.kernel.repository.ArtifactResolver;
78 import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
79 import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefType;
80 import org.apache.geronimo.xbeans.j2ee.JavaWsdlMappingDocument;
81 import org.apache.geronimo.xbeans.j2ee.JavaWsdlMappingType;
82 import org.apache.geronimo.xbeans.j2ee.PackageMappingType;
83 import org.apache.geronimo.webservices.builder.SchemaInfoBuilder;
84
85 /**
86  * @version $Rev:385232 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
87  */

88 public class ServiceReferenceTest
89     extends TestSupport
90 {
91     public final static String JavaDoc NAMESPACE = "http://geronimo.apache.org/axis/mock";
92     private File JavaDoc tmpbasedir;
93     private Environment environment = new Environment();
94     private Artifact configID = new Artifact("group", "test", "1", "car");
95     private DeploymentContext context;
96     private ClassLoader JavaDoc isolatedCl = new URLClassLoader JavaDoc(new URL JavaDoc[0], this.getClass().getClassLoader());
97     private final String JavaDoc operationName = "doMockOperation";
98     private final File JavaDoc wsdlDir = new File JavaDoc(BASEDIR, "src/test/resources/interop");
99     private final File JavaDoc wsdlFile = new File JavaDoc(wsdlDir, "interop.wsdl");
100     private List JavaDoc handlerInfos = new ArrayList JavaDoc();
101     private GerServiceRefType gerServiceRefType = null;
102
103     private Module module;
104
105     private boolean runExternalWSTest;
106
107     protected void setUp() throws Exception JavaDoc {
108         super.setUp();
109         tmpbasedir = File.createTempFile("car", "tmp");
110         tmpbasedir.delete();
111         tmpbasedir.mkdirs();
112         environment.setConfigId(configID);
113         Jsr77Naming naming = new Jsr77Naming();
114         ArtifactManager artifactManager = new DefaultArtifactManager();
115         ArtifactResolver artifactResolver = new DefaultArtifactResolver(artifactManager, Collections.EMPTY_SET, null);
116         ConfigurationManager configurationManager = new SimpleConfigurationManager(Collections.EMPTY_SET, artifactResolver, Collections.EMPTY_SET);
117         AbstractName moduleName = naming.createRootName(configID, "testejb", NameFactory.EJB_MODULE);
118         context = new DeploymentContext(tmpbasedir, null, environment, moduleName, ConfigurationModuleType.CAR, naming, configurationManager, Collections.EMPTY_SET);
119
120         File JavaDoc moduleLocation = new File JavaDoc(tmpbasedir, "ejb");
121         moduleLocation.mkdirs();
122         module = new EJBModule(true, moduleName, environment, new UnpackedJarFile(moduleLocation), "ejb", null, null, null, null);
123
124         runExternalWSTest = System.getProperty("geronimo.run.external.webservicetest", "false").equals("true");
125     }
126
127     protected void tearDown() throws Exception JavaDoc {
128         recursiveDelete(tmpbasedir);
129         super.tearDown();
130     }
131
132 /*
133     public void testServiceProxy() throws Exception {
134         //construct the SEI proxy
135         Map portMap = new HashMap();
136         MockSEIFactory factory = new MockSEIFactory();
137         portMap.put("MockPort", factory);
138         Map seiClassNameToFactoryMap = new HashMap();
139         seiClassNameToFactoryMap.put(MockPort.class.getName(), factory);
140         AxisBuilder builder = new AxisBuilder();
141         Object service = builder.createServiceInterfaceProxy(MockService.class, portMap, seiClassNameToFactoryMap, context, module, isolatedCl);
142         assertTrue(service instanceof MockService);
143         MockService mockService = (MockService) service;
144         MockPort mockPort = mockService.getMockPort();
145         assertNotNull(mockPort);
146     }
147 */

148
149 /*
150     public void testServiceEndpointProxy() throws Exception {
151         AxisBuilder builder = new AxisBuilder();
152
153         ServiceImpl serviceInstance = new ServiceImpl(null, null);
154
155         URL location = new URL("http://geronimo.apache.org/ws");
156
157         OperationInfo op = buildOperationInfoForMockOperation(builder);
158         OperationInfo[] operationInfos = new OperationInfo[]{op};
159         Class serviceEndpointClass = builder.enhanceServiceEndpointInterface(MockPort.class, context, module, isolatedCl);
160         String portName = "foo";
161         SEIFactory serviceInterfaceFactory = builder.createSEIFactory(null, portName, serviceEndpointClass, serviceInstance, Collections.EMPTY_LIST, location, operationInfos, handlerInfos, null, context, isolatedCl);
162         assertNotNull(serviceInterfaceFactory);
163         Remote serviceInterface = serviceInterfaceFactory.createServiceEndpoint();
164         assertTrue(serviceInterface instanceof MockPort);
165     }
166 */

167
168     public void testBuildOperationInfo() throws Exception JavaDoc {
169         AxisBuilder builder = new AxisBuilder(null);
170         OperationInfo operationInfo = buildOperationInfoForMockOperation(builder);
171         assertNotNull(operationInfo);
172     }
173
174     public void testBuildFullServiceProxy() throws Exception JavaDoc {
175         Definition definition = buildDefinition();
176         SchemaInfoBuilder schemaInfoBuilder = new SchemaInfoBuilder(null, definition);
177         JavaWsdlMappingType mapping = buildLightweightMappingType();
178         QName JavaDoc serviceQName = new QName JavaDoc(NAMESPACE, "MockService");
179         AxisBuilder builder = new AxisBuilder(null);
180         Object JavaDoc reference = builder.createService(MockService.class, schemaInfoBuilder, mapping, serviceQName, SOAPConstants.SOAP11_CONSTANTS, handlerInfos, gerServiceRefType, module, isolatedCl);
181         assertNotNull(reference);
182         assertTrue(reference instanceof AxisServiceReference);
183         AxisServiceReference claReference = (AxisServiceReference) reference;
184         claReference.setClassLoader(isolatedCl);
185         Object JavaDoc proxy = claReference.getContent();
186         assertTrue(proxy instanceof MockService);
187         MockPort mockPort = ((MockService) proxy).getMockPort();
188         assertNotNull(mockPort);
189     }
190
191     public void testBuildBookQuoteProxy() throws Exception JavaDoc {
192         File JavaDoc wsdl = new File JavaDoc(BASEDIR, "src/test/resources/BookQuote.wsdl");
193         WSDLFactory factory = WSDLFactory.newInstance();
194         WSDLReader reader = factory.newWSDLReader();
195         Definition definition = reader.readWSDL(wsdl.toURI().toString());
196         SchemaInfoBuilder schemaInfoBuilder = new SchemaInfoBuilder(null, definition);
197         File JavaDoc jaxrpcMapping = new File JavaDoc(BASEDIR, "src/test/resources/BookQuote.xml");
198         JavaWsdlMappingDocument mappingDocument = JavaWsdlMappingDocument.Factory.parse(jaxrpcMapping);
199         JavaWsdlMappingType mapping = mappingDocument.getJavaWsdlMapping();
200         QName JavaDoc serviceQName = new QName JavaDoc("http://www.Monson-Haefel.com/jwsbook/BookQuote", "BookQuoteService");
201         AxisBuilder builder = new AxisBuilder(null);
202         Object JavaDoc reference = builder.createService(BookQuoteService.class, schemaInfoBuilder, mapping, serviceQName, SOAPConstants.SOAP11_CONSTANTS, handlerInfos, gerServiceRefType, module, isolatedCl);
203         assertNotNull(reference);
204         assertTrue(reference instanceof AxisServiceReference);
205         AxisServiceReference claReference = (AxisServiceReference) reference;
206         claReference.setClassLoader(isolatedCl);
207         Object JavaDoc proxy = claReference.getContent();
208         assertTrue(proxy instanceof BookQuoteService);
209         BookQuote bookQuote = ((BookQuoteService) proxy).getBookQuotePort();
210         assertNotNull(bookQuote);
211     }
212
213     //needs to have heavyweight mapping
214
public void xtestBuildInteropProxy() throws Exception JavaDoc {
215         WSDLFactory factory = WSDLFactory.newInstance();
216         WSDLReader reader = factory.newWSDLReader();
217         Definition definition = reader.readWSDL(wsdlFile.toURI().toString());
218         SchemaInfoBuilder schemaInfoBuilder = new SchemaInfoBuilder(null, definition);
219         File JavaDoc jaxrpcMapping = new File JavaDoc(BASEDIR, "src/test/resources/interop/interop-jaxrpcmapping.xml");
220         JavaWsdlMappingDocument mappingDocument = JavaWsdlMappingDocument.Factory.parse(jaxrpcMapping);
221         JavaWsdlMappingType mapping = mappingDocument.getJavaWsdlMapping();
222         QName JavaDoc serviceQName = new QName JavaDoc("http://tempuri.org/4s4c/1/3/wsdl/def/interopLab", "interopLab");
223         AxisBuilder builder = new AxisBuilder(null);
224         Object JavaDoc proxy = builder.createService(InteropLab.class, schemaInfoBuilder, mapping, serviceQName, SOAPConstants.SOAP11_CONSTANTS, handlerInfos, gerServiceRefType, module, isolatedCl);
225         assertNotNull(proxy);
226         assertTrue(proxy instanceof InteropLab);
227         InteropTestPortType interopTestPort = ((InteropLab) proxy).getinteropTestPort();
228         assertNotNull(interopTestPort);
229         testInteropPort(interopTestPort);
230     }
231
232     private void testInteropPort(InteropTestPortType interopTestPort) throws java.rmi.RemoteException JavaDoc {
233         if (runExternalWSTest) {
234             log.debug("Running external ws test");
235             int result = interopTestPort.echoInteger(1);
236             assertEquals(result, 1);
237         } else {
238             log.debug("Skipping external ws test");
239         }
240     }
241
242     public void testBuildComplexTypeMap() throws Exception JavaDoc {
243         WSDLFactory factory = WSDLFactory.newInstance();
244         WSDLReader reader = factory.newWSDLReader();
245         Definition definition = reader.readWSDL(wsdlFile.toURI().toString());
246         SchemaInfoBuilder schemaInfoBuilder = new SchemaInfoBuilder(null, definition);
247         Map JavaDoc complexTypeMap = schemaInfoBuilder.getComplexTypesInWsdl();
248         assertEquals(7, complexTypeMap.size());
249     }
250
251
252     private OperationInfo buildOperationInfoForMockOperation(AxisBuilder builder) throws NoSuchMethodException JavaDoc, DeploymentException, WSDLException {
253         Class JavaDoc portClass = MockPort.class;
254         Method JavaDoc method = portClass.getDeclaredMethod("doMockOperation", new Class JavaDoc[]{String JavaDoc.class});
255         WSDLFactory factory = WSDLFactory.newInstance();
256         Definition definition = factory.newDefinition();
257         ExtensionRegistry extensionRegistry = factory.newPopulatedExtensionRegistry();
258         BindingOperation bindingOperation = buildBindingOperation(definition, extensionRegistry);
259
260         Style defaultStyle = Style.DOCUMENT;
261
262         OperationInfo operationInfo = builder.buildOperationInfoLightweight(method, bindingOperation, defaultStyle, SOAPConstants.SOAP11_CONSTANTS);
263         return operationInfo;
264     }
265
266     private Definition buildDefinition() throws WSDLException {
267         WSDLFactory factory = WSDLFactory.newInstance();
268         Definition definition = factory.newDefinition();
269         definition.setDocumentBaseURI("META-INF/wsdl/fake.wsdl");
270         ExtensionRegistry extensionRegistry = factory.newPopulatedExtensionRegistry();
271         BindingOperation bindingOperation = buildBindingOperation(definition, extensionRegistry);
272         Binding binding = definition.createBinding();
273         binding.setQName(new QName JavaDoc(NAMESPACE, "MockPortBinding"));
274         //add soap:binding
275
SOAPBinding soapBinding = (SOAPBinding) extensionRegistry.createExtension(Binding.class, new QName JavaDoc("http://schemas.xmlsoap.org/wsdl/soap/", "binding"));
276         soapBinding.setTransportURI("http://schemas.xmlsoap.org/soap/http");
277         soapBinding.setStyle("rpc");
278         binding.addExtensibilityElement(soapBinding);
279         binding.addBindingOperation(bindingOperation);
280         PortType portType = definition.createPortType();
281         portType.setQName(new QName JavaDoc(NAMESPACE, "MockPort"));
282         portType.addOperation(bindingOperation.getOperation());
283         binding.setPortType(portType);
284         Port port = definition.createPort();
285         port.setName("MockPort");
286         //add soap:address
287
SOAPAddress soapAddress = (SOAPAddress) extensionRegistry.createExtension(Port.class, new QName JavaDoc("http://schemas.xmlsoap.org/wsdl/soap/", "address"));
288         soapAddress.setLocationURI("http://127.0.0.1:8080/foo");
289         port.addExtensibilityElement(soapAddress);
290         port.setBinding(binding);
291         javax.wsdl.Service service = definition.createService();
292         service.setQName(new QName JavaDoc(NAMESPACE, "MockService"));
293         service.addPort(port);
294         definition.addService(service);
295         return definition;
296     }
297
298     private BindingOperation buildBindingOperation(Definition definition, ExtensionRegistry extensionRegistry) throws WSDLException {
299         Operation operation = definition.createOperation();
300         operation.setName(operationName);
301         operation.setStyle(OperationType.REQUEST_RESPONSE);
302         Input input = definition.createInput();
303         Message inputMessage = definition.createMessage();
304         Part inputPart = definition.createPart();
305         inputPart.setName("string");
306         inputPart.setTypeName(new QName JavaDoc("http://www.w3.org/2001/XMLSchema", "string"));
307         inputMessage.addPart(inputPart);
308         operation.setInput(input);
309         input.setMessage(inputMessage);
310         Output output = definition.createOutput();
311         Message outputMessage = definition.createMessage();
312         operation.setOutput(output);
313         output.setMessage(outputMessage);
314         BindingOperation bindingOperation = definition.createBindingOperation();
315         SOAPOperation soapOperation = (SOAPOperation) extensionRegistry.createExtension(BindingOperation.class, new QName JavaDoc("http://schemas.xmlsoap.org/wsdl/soap/", "operation"));
316         soapOperation.setSoapActionURI("actionURI");
317         soapOperation.setStyle("rpc");
318         bindingOperation.addExtensibilityElement(soapOperation);
319         bindingOperation.setOperation(operation);
320         bindingOperation.setName(operation.getName());
321         BindingInput bindingInput = definition.createBindingInput();
322         SOAPBody inputBody = (SOAPBody) extensionRegistry.createExtension(BindingInput.class, new QName JavaDoc("http://schemas.xmlsoap.org/wsdl/soap/", "body"));
323         inputBody.setUse("encoded");
324         bindingInput.addExtensibilityElement(inputBody);
325         bindingOperation.setBindingInput(bindingInput);
326         BindingOutput bindingOutput = definition.createBindingOutput();
327         bindingOutput.addExtensibilityElement(inputBody);
328         bindingOperation.setBindingOutput(bindingOutput);
329         return bindingOperation;
330     }
331
332     private JavaWsdlMappingType buildLightweightMappingType() {
333         JavaWsdlMappingType mapping = JavaWsdlMappingType.Factory.newInstance();
334         PackageMappingType packageMapping = mapping.addNewPackageMapping();
335         packageMapping.addNewNamespaceURI().setStringValue(NAMESPACE);
336         packageMapping.addNewPackageType().setStringValue("org.apache.geronimo.axis.builder.mock");
337         return mapping;
338     }
339
340     private void recursiveDelete(File JavaDoc file) {
341         if (file.isDirectory()) {
342             File JavaDoc[] files = file.listFiles();
343             for (int i = 0; i < files.length; i++) {
344                 recursiveDelete(files[i]);
345             }
346         }
347         file.delete();
348     }
349 }
350
Popular Tags