001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.geronimo.axis.builder;
017:
018: import java.io.File;
019: import java.lang.reflect.Method;
020: import java.net.URL;
021: import java.net.URLClassLoader;
022: import java.util.ArrayList;
023: import java.util.Collections;
024: import java.util.List;
025: import java.util.Map;
026: import javax.wsdl.Binding;
027: import javax.wsdl.BindingInput;
028: import javax.wsdl.BindingOperation;
029: import javax.wsdl.BindingOutput;
030: import javax.wsdl.Definition;
031: import javax.wsdl.Input;
032: import javax.wsdl.Message;
033: import javax.wsdl.Operation;
034: import javax.wsdl.OperationType;
035: import javax.wsdl.Output;
036: import javax.wsdl.Part;
037: import javax.wsdl.Port;
038: import javax.wsdl.PortType;
039: import javax.wsdl.WSDLException;
040: import javax.wsdl.extensions.ExtensionRegistry;
041: import javax.wsdl.extensions.soap.SOAPAddress;
042: import javax.wsdl.extensions.soap.SOAPBinding;
043: import javax.wsdl.extensions.soap.SOAPBody;
044: import javax.wsdl.extensions.soap.SOAPOperation;
045: import javax.wsdl.factory.WSDLFactory;
046: import javax.wsdl.xml.WSDLReader;
047: import javax.xml.namespace.QName;
048:
049: import org.apache.geronimo.testsupport.TestSupport;
050:
051: import org.apache.axis.constants.Style;
052: import org.apache.axis.soap.SOAPConstants;
053: import org.apache.geronimo.axis.builder.bookquote.BookQuote;
054: import org.apache.geronimo.axis.builder.bookquote.BookQuoteService;
055: import org.apache.geronimo.axis.builder.interop.InteropLab;
056: import org.apache.geronimo.axis.builder.interop.InteropTestPortType;
057: import org.apache.geronimo.axis.builder.mock.MockPort;
058: import org.apache.geronimo.axis.builder.mock.MockService;
059: import org.apache.geronimo.axis.client.OperationInfo;
060: import org.apache.geronimo.axis.client.AxisServiceReference;
061: import org.apache.geronimo.common.DeploymentException;
062: import org.apache.geronimo.deployment.DeploymentContext;
063: import org.apache.geronimo.deployment.util.UnpackedJarFile;
064: import org.apache.geronimo.gbean.AbstractName;
065: import org.apache.geronimo.j2ee.deployment.EJBModule;
066: import org.apache.geronimo.j2ee.deployment.Module;
067: import org.apache.geronimo.j2ee.j2eeobjectnames.NameFactory;
068: import org.apache.geronimo.kernel.Jsr77Naming;
069: import org.apache.geronimo.kernel.config.ConfigurationModuleType;
070: import org.apache.geronimo.kernel.config.ConfigurationManager;
071: import org.apache.geronimo.kernel.config.SimpleConfigurationManager;
072: import org.apache.geronimo.kernel.repository.Artifact;
073: import org.apache.geronimo.kernel.repository.Environment;
074: import org.apache.geronimo.kernel.repository.ArtifactManager;
075: import org.apache.geronimo.kernel.repository.DefaultArtifactManager;
076: import org.apache.geronimo.kernel.repository.ArtifactResolver;
077: import org.apache.geronimo.kernel.repository.DefaultArtifactResolver;
078: import org.apache.geronimo.xbeans.geronimo.naming.GerServiceRefType;
079: import org.apache.geronimo.xbeans.j2ee.JavaWsdlMappingDocument;
080: import org.apache.geronimo.xbeans.j2ee.JavaWsdlMappingType;
081: import org.apache.geronimo.xbeans.j2ee.PackageMappingType;
082: import org.apache.geronimo.webservices.builder.SchemaInfoBuilder;
083:
084: /**
085: * @version $Rev:385232 $ $Date: 2007-03-14 20:06:17 -0700 (Wed, 14 Mar 2007) $
086: */
087: public class ServiceReferenceTest extends TestSupport {
088: public final static String NAMESPACE = "http://geronimo.apache.org/axis/mock";
089: private File tmpbasedir;
090: private Environment environment = new Environment();
091: private Artifact configID = new Artifact("group", "test", "1",
092: "car");
093: private DeploymentContext context;
094: private ClassLoader isolatedCl = new URLClassLoader(new URL[0],
095: this .getClass().getClassLoader());
096: private final String operationName = "doMockOperation";
097: private final File wsdlDir = new File(BASEDIR,
098: "src/test/resources/interop");
099: private final File wsdlFile = new File(wsdlDir, "interop.wsdl");
100: private List handlerInfos = new ArrayList();
101: private GerServiceRefType gerServiceRefType = null;
102:
103: private Module module;
104:
105: private boolean runExternalWSTest;
106:
107: protected void setUp() throws Exception {
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(
116: artifactManager, Collections.EMPTY_SET, null);
117: ConfigurationManager configurationManager = new SimpleConfigurationManager(
118: Collections.EMPTY_SET, artifactResolver,
119: Collections.EMPTY_SET);
120: AbstractName moduleName = naming.createRootName(configID,
121: "testejb", NameFactory.EJB_MODULE);
122: context = new DeploymentContext(tmpbasedir, null, environment,
123: moduleName, ConfigurationModuleType.CAR, naming,
124: configurationManager, Collections.EMPTY_SET);
125:
126: File moduleLocation = new File(tmpbasedir, "ejb");
127: moduleLocation.mkdirs();
128: module = new EJBModule(true, moduleName, environment,
129: new UnpackedJarFile(moduleLocation), "ejb", null, null,
130: null, null);
131:
132: runExternalWSTest = System.getProperty(
133: "geronimo.run.external.webservicetest", "false")
134: .equals("true");
135: }
136:
137: protected void tearDown() throws Exception {
138: recursiveDelete(tmpbasedir);
139: super .tearDown();
140: }
141:
142: /*
143: public void testServiceProxy() throws Exception {
144: //construct the SEI proxy
145: Map portMap = new HashMap();
146: MockSEIFactory factory = new MockSEIFactory();
147: portMap.put("MockPort", factory);
148: Map seiClassNameToFactoryMap = new HashMap();
149: seiClassNameToFactoryMap.put(MockPort.class.getName(), factory);
150: AxisBuilder builder = new AxisBuilder();
151: Object service = builder.createServiceInterfaceProxy(MockService.class, portMap, seiClassNameToFactoryMap, context, module, isolatedCl);
152: assertTrue(service instanceof MockService);
153: MockService mockService = (MockService) service;
154: MockPort mockPort = mockService.getMockPort();
155: assertNotNull(mockPort);
156: }
157: */
158:
159: /*
160: public void testServiceEndpointProxy() throws Exception {
161: AxisBuilder builder = new AxisBuilder();
162:
163: ServiceImpl serviceInstance = new ServiceImpl(null, null);
164:
165: URL location = new URL("http://geronimo.apache.org/ws");
166:
167: OperationInfo op = buildOperationInfoForMockOperation(builder);
168: OperationInfo[] operationInfos = new OperationInfo[]{op};
169: Class serviceEndpointClass = builder.enhanceServiceEndpointInterface(MockPort.class, context, module, isolatedCl);
170: String portName = "foo";
171: SEIFactory serviceInterfaceFactory = builder.createSEIFactory(null, portName, serviceEndpointClass, serviceInstance, Collections.EMPTY_LIST, location, operationInfos, handlerInfos, null, context, isolatedCl);
172: assertNotNull(serviceInterfaceFactory);
173: Remote serviceInterface = serviceInterfaceFactory.createServiceEndpoint();
174: assertTrue(serviceInterface instanceof MockPort);
175: }
176: */
177:
178: public void testBuildOperationInfo() throws Exception {
179: AxisBuilder builder = new AxisBuilder(null);
180: OperationInfo operationInfo = buildOperationInfoForMockOperation(builder);
181: assertNotNull(operationInfo);
182: }
183:
184: public void testBuildFullServiceProxy() throws Exception {
185: Definition definition = buildDefinition();
186: SchemaInfoBuilder schemaInfoBuilder = new SchemaInfoBuilder(
187: null, definition);
188: JavaWsdlMappingType mapping = buildLightweightMappingType();
189: QName serviceQName = new QName(NAMESPACE, "MockService");
190: AxisBuilder builder = new AxisBuilder(null);
191: Object reference = builder.createService(MockService.class,
192: schemaInfoBuilder, mapping, serviceQName,
193: SOAPConstants.SOAP11_CONSTANTS, handlerInfos,
194: gerServiceRefType, module, isolatedCl);
195: assertNotNull(reference);
196: assertTrue(reference instanceof AxisServiceReference);
197: AxisServiceReference claReference = (AxisServiceReference) reference;
198: claReference.setClassLoader(isolatedCl);
199: Object proxy = claReference.getContent();
200: assertTrue(proxy instanceof MockService);
201: MockPort mockPort = ((MockService) proxy).getMockPort();
202: assertNotNull(mockPort);
203: }
204:
205: public void testBuildBookQuoteProxy() throws Exception {
206: File wsdl = new File(BASEDIR,
207: "src/test/resources/BookQuote.wsdl");
208: WSDLFactory factory = WSDLFactory.newInstance();
209: WSDLReader reader = factory.newWSDLReader();
210: Definition definition = reader
211: .readWSDL(wsdl.toURI().toString());
212: SchemaInfoBuilder schemaInfoBuilder = new SchemaInfoBuilder(
213: null, definition);
214: File jaxrpcMapping = new File(BASEDIR,
215: "src/test/resources/BookQuote.xml");
216: JavaWsdlMappingDocument mappingDocument = JavaWsdlMappingDocument.Factory
217: .parse(jaxrpcMapping);
218: JavaWsdlMappingType mapping = mappingDocument
219: .getJavaWsdlMapping();
220: QName serviceQName = new QName(
221: "http://www.Monson-Haefel.com/jwsbook/BookQuote",
222: "BookQuoteService");
223: AxisBuilder builder = new AxisBuilder(null);
224: Object reference = builder.createService(
225: BookQuoteService.class, schemaInfoBuilder, mapping,
226: serviceQName, SOAPConstants.SOAP11_CONSTANTS,
227: handlerInfos, gerServiceRefType, module, isolatedCl);
228: assertNotNull(reference);
229: assertTrue(reference instanceof AxisServiceReference);
230: AxisServiceReference claReference = (AxisServiceReference) reference;
231: claReference.setClassLoader(isolatedCl);
232: Object proxy = claReference.getContent();
233: assertTrue(proxy instanceof BookQuoteService);
234: BookQuote bookQuote = ((BookQuoteService) proxy)
235: .getBookQuotePort();
236: assertNotNull(bookQuote);
237: }
238:
239: //needs to have heavyweight mapping
240: public void xtestBuildInteropProxy() throws Exception {
241: WSDLFactory factory = WSDLFactory.newInstance();
242: WSDLReader reader = factory.newWSDLReader();
243: Definition definition = reader.readWSDL(wsdlFile.toURI()
244: .toString());
245: SchemaInfoBuilder schemaInfoBuilder = new SchemaInfoBuilder(
246: null, definition);
247: File jaxrpcMapping = new File(BASEDIR,
248: "src/test/resources/interop/interop-jaxrpcmapping.xml");
249: JavaWsdlMappingDocument mappingDocument = JavaWsdlMappingDocument.Factory
250: .parse(jaxrpcMapping);
251: JavaWsdlMappingType mapping = mappingDocument
252: .getJavaWsdlMapping();
253: QName serviceQName = new QName(
254: "http://tempuri.org/4s4c/1/3/wsdl/def/interopLab",
255: "interopLab");
256: AxisBuilder builder = new AxisBuilder(null);
257: Object proxy = builder.createService(InteropLab.class,
258: schemaInfoBuilder, mapping, serviceQName,
259: SOAPConstants.SOAP11_CONSTANTS, handlerInfos,
260: gerServiceRefType, module, isolatedCl);
261: assertNotNull(proxy);
262: assertTrue(proxy instanceof InteropLab);
263: InteropTestPortType interopTestPort = ((InteropLab) proxy)
264: .getinteropTestPort();
265: assertNotNull(interopTestPort);
266: testInteropPort(interopTestPort);
267: }
268:
269: private void testInteropPort(InteropTestPortType interopTestPort)
270: throws java.rmi.RemoteException {
271: if (runExternalWSTest) {
272: log.debug("Running external ws test");
273: int result = interopTestPort.echoInteger(1);
274: assertEquals(result, 1);
275: } else {
276: log.debug("Skipping external ws test");
277: }
278: }
279:
280: public void testBuildComplexTypeMap() throws Exception {
281: WSDLFactory factory = WSDLFactory.newInstance();
282: WSDLReader reader = factory.newWSDLReader();
283: Definition definition = reader.readWSDL(wsdlFile.toURI()
284: .toString());
285: SchemaInfoBuilder schemaInfoBuilder = new SchemaInfoBuilder(
286: null, definition);
287: Map complexTypeMap = schemaInfoBuilder.getComplexTypesInWsdl();
288: assertEquals(7, complexTypeMap.size());
289: }
290:
291: private OperationInfo buildOperationInfoForMockOperation(
292: AxisBuilder builder) throws NoSuchMethodException,
293: DeploymentException, WSDLException {
294: Class portClass = MockPort.class;
295: Method method = portClass.getDeclaredMethod("doMockOperation",
296: new Class[] { String.class });
297: WSDLFactory factory = WSDLFactory.newInstance();
298: Definition definition = factory.newDefinition();
299: ExtensionRegistry extensionRegistry = factory
300: .newPopulatedExtensionRegistry();
301: BindingOperation bindingOperation = buildBindingOperation(
302: definition, extensionRegistry);
303:
304: Style defaultStyle = Style.DOCUMENT;
305:
306: OperationInfo operationInfo = builder
307: .buildOperationInfoLightweight(method,
308: bindingOperation, defaultStyle,
309: SOAPConstants.SOAP11_CONSTANTS);
310: return operationInfo;
311: }
312:
313: private Definition buildDefinition() throws WSDLException {
314: WSDLFactory factory = WSDLFactory.newInstance();
315: Definition definition = factory.newDefinition();
316: definition.setDocumentBaseURI("META-INF/wsdl/fake.wsdl");
317: ExtensionRegistry extensionRegistry = factory
318: .newPopulatedExtensionRegistry();
319: BindingOperation bindingOperation = buildBindingOperation(
320: definition, extensionRegistry);
321: Binding binding = definition.createBinding();
322: binding.setQName(new QName(NAMESPACE, "MockPortBinding"));
323: //add soap:binding
324: SOAPBinding soapBinding = (SOAPBinding) extensionRegistry
325: .createExtension(Binding.class, new QName(
326: "http://schemas.xmlsoap.org/wsdl/soap/",
327: "binding"));
328: soapBinding
329: .setTransportURI("http://schemas.xmlsoap.org/soap/http");
330: soapBinding.setStyle("rpc");
331: binding.addExtensibilityElement(soapBinding);
332: binding.addBindingOperation(bindingOperation);
333: PortType portType = definition.createPortType();
334: portType.setQName(new QName(NAMESPACE, "MockPort"));
335: portType.addOperation(bindingOperation.getOperation());
336: binding.setPortType(portType);
337: Port port = definition.createPort();
338: port.setName("MockPort");
339: //add soap:address
340: SOAPAddress soapAddress = (SOAPAddress) extensionRegistry
341: .createExtension(Port.class, new QName(
342: "http://schemas.xmlsoap.org/wsdl/soap/",
343: "address"));
344: soapAddress.setLocationURI("http://127.0.0.1:8080/foo");
345: port.addExtensibilityElement(soapAddress);
346: port.setBinding(binding);
347: javax.wsdl.Service service = definition.createService();
348: service.setQName(new QName(NAMESPACE, "MockService"));
349: service.addPort(port);
350: definition.addService(service);
351: return definition;
352: }
353:
354: private BindingOperation buildBindingOperation(
355: Definition definition, ExtensionRegistry extensionRegistry)
356: throws WSDLException {
357: Operation operation = definition.createOperation();
358: operation.setName(operationName);
359: operation.setStyle(OperationType.REQUEST_RESPONSE);
360: Input input = definition.createInput();
361: Message inputMessage = definition.createMessage();
362: Part inputPart = definition.createPart();
363: inputPart.setName("string");
364: inputPart.setTypeName(new QName(
365: "http://www.w3.org/2001/XMLSchema", "string"));
366: inputMessage.addPart(inputPart);
367: operation.setInput(input);
368: input.setMessage(inputMessage);
369: Output output = definition.createOutput();
370: Message outputMessage = definition.createMessage();
371: operation.setOutput(output);
372: output.setMessage(outputMessage);
373: BindingOperation bindingOperation = definition
374: .createBindingOperation();
375: SOAPOperation soapOperation = (SOAPOperation) extensionRegistry
376: .createExtension(BindingOperation.class, new QName(
377: "http://schemas.xmlsoap.org/wsdl/soap/",
378: "operation"));
379: soapOperation.setSoapActionURI("actionURI");
380: soapOperation.setStyle("rpc");
381: bindingOperation.addExtensibilityElement(soapOperation);
382: bindingOperation.setOperation(operation);
383: bindingOperation.setName(operation.getName());
384: BindingInput bindingInput = definition.createBindingInput();
385: SOAPBody inputBody = (SOAPBody) extensionRegistry
386: .createExtension(
387: BindingInput.class,
388: new QName(
389: "http://schemas.xmlsoap.org/wsdl/soap/",
390: "body"));
391: inputBody.setUse("encoded");
392: bindingInput.addExtensibilityElement(inputBody);
393: bindingOperation.setBindingInput(bindingInput);
394: BindingOutput bindingOutput = definition.createBindingOutput();
395: bindingOutput.addExtensibilityElement(inputBody);
396: bindingOperation.setBindingOutput(bindingOutput);
397: return bindingOperation;
398: }
399:
400: private JavaWsdlMappingType buildLightweightMappingType() {
401: JavaWsdlMappingType mapping = JavaWsdlMappingType.Factory
402: .newInstance();
403: PackageMappingType packageMapping = mapping
404: .addNewPackageMapping();
405: packageMapping.addNewNamespaceURI().setStringValue(NAMESPACE);
406: packageMapping.addNewPackageType().setStringValue(
407: "org.apache.geronimo.axis.builder.mock");
408: return mapping;
409: }
410:
411: private void recursiveDelete(File file) {
412: if (file.isDirectory()) {
413: File[] files = file.listFiles();
414: for (int i = 0; i < files.length; i++) {
415: recursiveDelete(files[i]);
416: }
417: }
418: file.delete();
419: }
420: }
|