You are viewing a plain text version of this content. The canonical link for it is here.
Posted to wsif-dev@ws.apache.org by an...@apache.org on 2003/05/01 11:50:24 UTC
cvs commit: xml-axis-wsif/java/test/models JavaModelTest.java
antelder 2003/05/01 02:50:24
Added: java/test/models/javaProvider JavaWSIFProvider.java
JavaWSIFPort.java JavaWSIFOperation.java
java/test/models JavaModelTest.java
Log:
Testcase for the provider models.
Revision Changes Path
1.1 xml-axis-wsif/java/test/models/javaProvider/JavaWSIFProvider.java
Index: JavaWSIFProvider.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "WSIF" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, 2002, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package models.javaProvider;
import javax.wsdl.Definition;
import javax.wsdl.Port;
import javax.wsdl.extensions.ExtensionRegistry;
import org.apache.wsif.WSIFException;
import org.apache.wsif.logging.Trc;
import org.apache.wsif.providers.ModelWSIFPort;
import org.apache.wsif.providers.ModelWSIFProvider;
import org.apache.wsif.providers.WSIFDynamicTypeMap;
import org.apache.wsif.tools.wsdl.BindingGenerator;
import org.apache.wsif.tools.wsdl.JavaBindingGenerator;
import org.apache.wsif.wsdl.extensions.java.JavaBinding;
/**
* @author <a href="mailto:antelder@apache.org">Ant Elder</a>
*/
public class JavaWSIFProvider extends ModelWSIFProvider {
public JavaWSIFProvider() throws WSIFException {
super();
}
protected String getProviderName() {
return "java";
}
protected String[] getRequiredClasses() {
return new String[0];
}
protected ExtensionRegistry[] getExtensionRegistries() {
return new ExtensionRegistry[0];
//TODO: why isn't this done for Java???
}
protected Class getImplementedBindingClass() {
return JavaBinding.class;
}
public BindingGenerator[] getBindingGenerators() {
return new BindingGenerator[] { new JavaBindingGenerator()};
}
protected ModelWSIFPort makeWSIFPort(
Definition def,
Port port,
WSIFDynamicTypeMap typeMap)
throws WSIFException {
ModelWSIFPort wsifPort = new JavaWSIFPort(def, port, typeMap);
Trc.exit(wsifPort);
return wsifPort;
}
}
1.1 xml-axis-wsif/java/test/models/javaProvider/JavaWSIFPort.java
Index: JavaWSIFPort.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "WSIF" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, 2002, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package models.javaProvider;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import javax.wsdl.BindingOperation;
import javax.wsdl.Definition;
import javax.wsdl.Port;
import javax.wsdl.extensions.ExtensibilityElement;
import org.apache.wsif.WSIFException;
import org.apache.wsif.logging.Trc;
import org.apache.wsif.providers.ModelWSIFOperation;
import org.apache.wsif.providers.ModelWSIFPort;
import org.apache.wsif.providers.WSIFDynamicTypeMap;
import org.apache.wsif.wsdl.extensions.java.JavaAddress;
/**
* @author <a href="mailto:ant.elder@uk.ibm.com">Ant Elder</a>
*/
public class JavaWSIFPort extends ModelWSIFPort {
protected String targetObjectClassName;
transient private Class targetObjectClass;
transient private Method[] targetObjectMethods;
transient private Constructor[] targetObjectConstructors;
transient private java.lang.Object targetObject;
public JavaWSIFPort(Definition def, Port port, WSIFDynamicTypeMap typeMap)
throws WSIFException {
super(def, port, typeMap);
}
protected ModelWSIFOperation makeWSIFOperation(
ModelWSIFPort port,
BindingOperation bop)
throws WSIFException {
return new JavaWSIFOperation(port, bop);
}
protected Class getImplementedAddressClass() {
return JavaAddress.class;
}
protected void validateAddress(ExtensibilityElement ee) {
JavaAddress address = (JavaAddress) ee;
targetObjectClassName = address.getClassName();
}
public void close() {
targetObject = null;
}
/**
* Gets the Java class of the service object
* @return Class the class of the service object
*/
Class getTargetObjectClass() throws WSIFException {
// no method access modifier as it used by WSIFOperation_Java
if (targetObjectClass == null) {
try {
targetObjectClass =
Class.forName(
targetObjectClassName,
true,
Thread.currentThread().getContextClassLoader());
} catch (Throwable ex) {
Trc.exception(ex);
throw new WSIFException(
"Exception getting target object class '"
+ targetObjectClassName
+ "': "
+ ex.getLocalizedMessage());
}
}
return targetObjectClass;
}
/**
* Gets the constructors of the service object
* @return Constructor[] the constructors of the service object
*/
Constructor[] getServiceObjectConstructors() throws WSIFException {
// no method access modifier as it used by JavaWSIFOperation
Trc.entry(this);
if (targetObjectConstructors == null) {
Class c = getTargetObjectClass();
targetObjectConstructors = c.getConstructors();
}
Trc.exit(targetObjectConstructors);
return targetObjectConstructors;
}
/**
* Gets the methods of the service object
* @return Method[] the methods of the service object
*/
Method[] getServiceObjectMethods() throws WSIFException {
// no method access modifier as it used by WSIFOperation_Java
Trc.entry(this);
if (targetObjectMethods == null) {
Class c = getTargetObjectClass();
targetObjectMethods = c.getMethods();
}
Trc.exit(targetObjectMethods);
return targetObjectMethods;
}
/**
* Gets the service object.
* @return Object the service object instance
*/
public Object getObjectReference() throws WSIFException {
Trc.entry(this);
if (targetObject == null) {
Class c = getTargetObjectClass();
try {
targetObject = c.newInstance();
} catch (Throwable ex) {
Trc.exception(ex);
throw new WSIFException(
"exception instantiating target object: "
+ ex.getLocalizedMessage());
}
}
Trc.exit(targetObject);
return targetObject;
}
/**
* Sets a new instance of the target service object
* Used by WSIFOperation_Java when it runs a Constructor
* instead of an instance method.
*/
void setTargetObject(Object targetObject) {
Trc.entry(this, targetObject);
this.targetObject = targetObject;
Trc.exit();
}
/**
* Bypass WSIFDynamicProxy with direct access to the target object
*/
public Object getStub(Class iface) throws WSIFException {
Trc.entry(this);
Object stub = null;
Object o = getObjectReference();
if (iface.isAssignableFrom(o.getClass())) {
stub = o;
}
Trc.exit(stub);
return stub;
}
}
1.1 xml-axis-wsif/java/test/models/javaProvider/JavaWSIFOperation.java
Index: JavaWSIFOperation.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "WSIF" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, 2002, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package models.javaProvider;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import javax.wsdl.BindingOperation;
import javax.wsdl.extensions.ExtensibilityElement;
import org.apache.wsif.WSIFException;
import org.apache.wsif.providers.ModelWSIFOperation;
import org.apache.wsif.providers.ModelWSIFPort;
import org.apache.wsif.wsdl.extensions.java.JavaOperation;
/**
* @author <a href="mailto:ant.elder@uk.ibm.com">Ant Elder</a>
*/
public class JavaWSIFOperation extends ModelWSIFOperation {
protected JavaOperation javaOperation;
public JavaWSIFOperation(ModelWSIFPort wsifPort, BindingOperation bop)
throws WSIFException {
super(wsifPort, bop);
}
protected Class getOperationExtensabilityClass() {
return JavaOperation.class;
}
protected void validateOperationExtensibilityElement(ExtensibilityElement javaOperation)
throws WSIFException {
this.javaOperation = (JavaOperation) javaOperation;
}
protected boolean doInvoke(ArrayList inputArgs, ArrayList responseArgs)
throws WSIFException {
boolean workedOK = false;
JavaWSIFPort p = (JavaWSIFPort) wsifPort;
Method m = getMethod();
Object target;
if ("static".equalsIgnoreCase(javaOperation.getMethodType())) {
target = null;
} else {
target = p.getObjectReference();
}
Object response;
try {
response = m.invoke(target, inputArgs.toArray());
} catch (Throwable e) {
throw new WSIFException(
"exception invoking method: " + e.getLocalizedMessage(),
e);
}
responseArgs.add(response);
workedOK = true;
return workedOK;
}
protected Method getMethod() throws WSIFException {
Method method = null;
JavaWSIFPort p = (JavaWSIFPort) wsifPort;
Method[] methods = p.getServiceObjectMethods();
for (int i = 0; method == null && i < methods.length; i++) {
if (methods[i].getName().equals(javaOperation.getMethodName())) {
if (isMethodArgsOK(methods[i])) {
method = methods[i];
}
}
}
if (method == null) {
throw new WSIFException(
"no method on target object named: "
+ javaOperation.getMethodName());
}
return method;
}
protected boolean isMethodArgsOK(Method m) throws WSIFException {
boolean ok = false;
JavaWSIFPort p = (JavaWSIFPort) wsifPort;
List argNames = javaOperation.getParameterOrder();
if (argNames == null) {
argNames = getInputParts();
}
if (m.getParameterTypes().length == argNames.size()) {
//TODO:
ok = true;
}
return ok;
}
}
1.1 xml-axis-wsif/java/test/models/JavaModelTest.java
Index: JavaModelTest.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "WSIF" and "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation and was
* originally based on software copyright (c) 2001, 2002, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package models;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import models.javaProvider.JavaWSIFProvider;
import org.apache.wsif.WSIFConstants;
import org.apache.wsif.WSIFCorrelationId;
import org.apache.wsif.WSIFException;
import org.apache.wsif.WSIFMessage;
import org.apache.wsif.WSIFOperation;
import org.apache.wsif.WSIFPort;
import org.apache.wsif.WSIFService;
import org.apache.wsif.WSIFServiceFactory;
import org.apache.wsif.spi.WSIFProvider;
import org.apache.wsif.util.WSIFPluggableProviders;
import util.AddressUtility;
import util.TestUtilities;
import addressbook.wsifservice.AddressBook;
import addressbook.wsiftypes.Address;
import addressbook.wsiftypes.Phone;
import async.AsyncResponseHandler;
/**
* Junit tests for the provider model classes
* @author Ant Elder <an...@uk.ibm.com>
*/
public class JavaModelTest extends TestCase {
String wsdlLocation =
TestUtilities.getWsdlPath("java\\test\\addressbook\\wsifservice")
+ "AddressBook.wsdl";
static String server = TestUtilities.getSoapServer().toUpperCase();
static String name1 = "Purdue Boilermaker";
static Address addr1 =
new Address(
1,
"University Drive",
"West Lafayette",
"IN",
47907,
new Phone(765, "494", "4900"));
static String firstName2 = "Someone";
static String lastName2 = "Else";
static Address addr2 =
new Address(
0,
"Somewhere Else",
"No Where",
"NO",
71983,
new Phone(600, "391", "5682"));
public JavaModelTest(String name) {
super(name);
}
public static void main(String[] args) {
// TestUtilities.startListeners(
// TestUtilities.ADDRESSBOOK_LISTENER
// | TestUtilities.ASYNC_LISTENER
// | TestUtilities.NATIVEJMS_LISTENER);
junit.textui.TestRunner.run(suite());
// TestUtilities.stopListeners();
}
public static Test suite() {
return new TestSuite(JavaModelTest.class);
}
public void setUp() {
TestUtilities.setUpExtensionsAndProviders();
}
public void testJava() {
doit("JavaPort", "java");
}
public void testDynamicJava() {
doitDyn("JavaPort", "java", false);
}
private void doit(String portName, String protocol) {
if (portName.toUpperCase().indexOf("JMS") != -1
&& !TestUtilities.areWeTesting("jms"))
return;
// TestUtilities.setProviderForProtocol(protocol);
try {
WSIFPluggableProviders.overrideDefaultProvider(
"http://schemas.xmlsoap.org/wsdl/java/",
new JavaWSIFProvider());
} catch (WSIFException e) {
e.printStackTrace();
assertTrue("exception setting provider: " + e.getLocalizedMessage(), false);
}
try {
WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
WSIFService service =
factory.getService(wsdlLocation, null, // serviceNS
null, // serviceName
"http://wsifservice.addressbook/", // portTypeNS
"AddressBook"); // portTypeName
AddressBook abStub =
(AddressBook) service.getStub(portName, AddressBook.class);
abStub.addEntry(name1, addr1);
abStub.addEntry(firstName2, lastName2, addr2);
Address resp1 = abStub.getAddressFromName(name1);
assertTrue(new AddressUtility(addr1).equals(resp1));
Address resp2 =
abStub.getAddressFromName(firstName2 + " " + lastName2);
assertTrue(new AddressUtility(addr2).equals(resp2));
if (TestUtilities.areWeTesting("async")) {
testAsyncOPs(service, portName, name1, addr1);
testAsyncOPs(
service,
portName,
firstName2 + " " + lastName2,
addr2);
}
} catch (Exception e) {
System.err.println(
"AddressBookTest(" + portName + ") caught exception " + e);
e.printStackTrace();
assertTrue(false);
} finally {
TestUtilities.resetDefaultProviders();
}
}
private void doitDyn(
String portName,
String protocol,
boolean autoMapTypes) {
if (portName.toUpperCase().indexOf("JMS") != -1
&& !TestUtilities.areWeTesting("jms"))
return;
TestUtilities.setProviderForProtocol(protocol);
try {
WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
if (autoMapTypes) {
factory.setFeature(
WSIFConstants.WSIF_FEATURE_AUTO_MAP_TYPES,
new Boolean(true));
}
WSIFService service =
factory.getService(wsdlLocation, null, // serviceNS
null, // serviceName
"http://wsifservice.addressbook/", // portTypeNS
"AddressBook"); // portTypeName
if (!autoMapTypes) {
service.mapType(
new javax.xml.namespace.QName(
"http://wsiftypes.addressbook/",
"address"),
Class.forName("addressbook.wsiftypes.Address"));
service.mapType(
new javax.xml.namespace.QName(
"http://wsiftypes.addressbook/",
"phone"),
Class.forName("addressbook.wsiftypes.Phone"));
}
WSIFPort port = null;
port = service.getPort(portName);
WSIFOperation operation =
port.createOperation(
"addEntry",
"AddEntryWholeNameRequest",
null);
WSIFMessage inputMessage = operation.createInputMessage();
WSIFMessage outputMessage = operation.createOutputMessage();
WSIFMessage faultMessage = operation.createFaultMessage();
// Create a name and address to add to the addressbook
String nameToAdd = "Chris P. Bacon";
Address addressToAdd =
new Address(
1,
"The Waterfront",
"Some City",
"NY",
47907,
new Phone(765, "494", "4900"));
// Add the name and address to the input message
inputMessage.setObjectPart("name", nameToAdd);
inputMessage.setObjectPart("address", addressToAdd);
// Execute the operation, obtaining a flag to indicate its success
operation.executeInputOnlyOperation(inputMessage);
// Start from fresh
operation = null;
inputMessage = null;
outputMessage = null;
faultMessage = null;
operation = port.createOperation("getAddressFromName");
// Create the messages
inputMessage = operation.createInputMessage();
outputMessage = operation.createOutputMessage();
faultMessage = operation.createFaultMessage();
// Set the name to find in the addressbook
String nameToLookup = "Chris P. Bacon";
inputMessage.setObjectPart("name", nameToLookup);
// Execute the operation
boolean operationSucceeded =
operation.executeRequestResponseOperation(
inputMessage,
outputMessage,
faultMessage);
if (operationSucceeded) {
// We can obtain the address that was found by querying the output message
Address addressFound =
(Address) outputMessage.getObjectPart("address");
assertTrue(
new AddressUtility(addressToAdd).equals(addressFound));
} else {
System.out.println("Failed to lookup name in addressbook");
}
// Check that we can't reuse an operation.
boolean caughtException = false;
try {
operationSucceeded =
operation.executeRequestResponseOperation(
inputMessage,
outputMessage,
faultMessage);
} catch (WSIFException we) {
caughtException = true;
}
assertTrue(caughtException);
} catch (Exception e) {
System.err.println(
"AddressBookTest(" + portName + ") caught exception " + e);
e.printStackTrace();
assertTrue(false);
} finally {
TestUtilities.resetDefaultProviders();
}
}
private void testAsyncOPs(
WSIFService service,
String portName,
String name,
Address addr) {
try {
WSIFPort port =
(portName == null)
? service.getPort()
: service.getPort(portName);
if (!port.supportsAsync()) {
return;
}
WSIFOperation op = port.createOperation("getAddressFromName");
AsyncResponseHandler abHandler = new AsyncResponseHandler(1);
// 1 async call
WSIFMessage inMsg = op.createInputMessage();
inMsg.setObjectPart("name", name);
WSIFMessage outmsg = op.createOutputMessage();
WSIFMessage faultMsg = op.createFaultMessage();
WSIFMessage context = op.getContext();
context.setObjectPart(
WSIFConstants.CONTEXT_JMS_PREFIX + "JMSReplyTo",
TestUtilities.getWsifProperty("wsif.async.replytoq"));
op.setContext(context);
WSIFCorrelationId id =
op.executeRequestResponseAsync(inMsg, abHandler);
assertTrue(
"null correlation id returned from async request!",
id != null);
int i = 10;
while (i-- > 0 && !abHandler.isDone()) {
System.out.println("waiting for async responses - " + i);
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
}
}
assertTrue("no response from async operation!", i > 0);
// no responses in time
WSIFMessage[] faults = abHandler.getFaults();
WSIFMessage[] outputs = abHandler.getOutputs();
Address respAddr = (Address) outputs[0].getObjectPart("address");
assertTrue("incorrect address response!", addr.equals(respAddr));
} catch (Exception ex) {
ex.printStackTrace();
assertTrue("exception making async request!!", false);
}
}
}