When testing the a webservice, it can come in handy not only to have look at the soap-resonse but also to serialise it to disk (in xml-form). To test the system that makes the SOAP-Requests it might then be even more interesting to take the data from the (maybe modified) xml document as an input. Since I got the answer for this problem from this group and Yves Langisch asked me to present the solution on the list I will now demonstrate how it is possible to do it.
All you really need is your development environment and of course the webservice-classes generated by axis.I created a simple junit-test to deserialize the xml stream and test the result.
public class ReaderTest extends TestCase {
public ShipmentReaderTest(String arg0) {
super(arg0);
}
public void testDeserializeFromDisk() {
File inFile = new File("/yourpath/yourXMLFile.out");
FileReader reader;
try {
reader = new FileReader(inFile);
YourResponseClass yrc = new YourResponseClass();
QName qname = new javax.xml.namespace.QName(
"http://www.yourXMLSchema.com", "YourResponseClass");
yrc = (YourResponseClass) deserializeFromSOAPReader(reader, qname,
YourResponseClass.class);
if (yrc!= null && yrc.getSomeProperty() != null) {
System.out.println("Got Value!");
} else {
System.out.println("Shit!");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (AxisFault e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
This will call some method that works as it is for AXIS-1.2beta. In AXIS-1.1 you will have to make some modification to the generated stub class in order to be able to get the TypeMapping. This is not necessary in Axis-1.2 anymore.
Add this to your Axis-1.1 Stub-Class:
public TypeMapping getTypeMapping() throws ServiceException, RemoteException{
TypeMapping mapping = createCall().getTypeMapping();
return mapping;
}
Then you will need to have some reader method which will be called from the JUnit-Test:
/**
* Deserializes a SOAP Reader (i.e. a XML file) to the appropriate Java
* Class generated by Axis.
*
* @param reader
* is the Reader for deserializing (a SOAP envelope)
* @param qname
* is the qualified name of the namespace of the XML Type of
* clazz
* @param clazz
* is the Java Type representation of the XML stream
* @return Object which has to be casted to the specified Java Class (clazz)
* @throws Exception
* @throws AxisFault
*/
public static Object deserializeFromSOAPReader(Reader reader, QName qname,
Class clazz) throws Exception, AxisFault {
YourAXISStub stub = new YourAXISStub();
MessageContext msgContext = new MessageContext(new AxisServer());
msgContext.setEncodingStyle(qname.getNamespaceURI());
// get TypeMapping from generated stub so we don't need to build it
// TypeMapping is not necessary anymore with Axis 1.2, see below
TypeMapping tm = stub.getTypeMapping();
TypeMappingRegistry tmr = msgContext.getTypeMappingRegistry();
tmr.register(qname.getNamespaceURI(), tm);
msgContext.setTypeMappingRegistry(tmr);
DeserializationContext dser = new DeserializationContextImpl(
new InputSource(reader), msgContext,
org.apache.axis.Message.REQUEST);
// parse the InputSource
dser.parse();
SOAPEnvelope env2 = dser.getEnvelope();
RPCElement rpcElem = (RPCElement) env2.getFirstBody();
/**
* yla, 06.02.04; Ab Axis 1.2 getValueAsType auf die zwei-parametrige
* Version umstellen (Axis 1.2 unterst�tzt die getValueAsType mit dem
* zus�tzlichen Class Parameter, so dass der Umweg �ber das TypeMapping
* (siehe oben) nicht mehr notwendig ist)
*/
//Object result = rpcElem.getValueAsType(qname, clazz);
Object result = rpcElem.getValueAsType(qname);
return result;
}
}
I hope that someone does find it as useful as I do.
Cheers,
Michael
