Debian Codegraph @

src/java/org/astrogrid/samp/test/TestXmlrpcClient.java

raw
1 package org.astrogrid.samp.test;
2
3 import java.io.InputStream;
4 import java.io.IOException;
5 import java.net.URL;
6 import java.util.List;
7 import java.util.Map;
8 import javax.xml.parsers.DocumentBuilderFactory;
9 import javax.xml.parsers.ParserConfigurationException;
10 import org.astrogrid.samp.SampUtils;
11 import org.astrogrid.samp.xmlrpc.internal.InternalClient;
12 import org.astrogrid.samp.xmlrpc.internal.XmlUtils;
13 import org.w3c.dom.DOMException;
14 import org.w3c.dom.Document;
15 import org.w3c.dom.Element;
16 import org.xml.sax.SAXException;
17
18 /**
19 * SampXmlrpcClient implementation for testing success or failure of
20 * XML-RPC method invocations.
21 * Note the return value of the {@link #callAndWait} method is either
22 * {@link #SUCCESS} or {@link #FAILURE}, rather than the actual result
23 * of the call.
24 *
25 * Methods may throw {@link TestException}s to indicate assertion failures.
26 *
27 * @author Mark Taylor
28 * @since 28 Aug 2009
29 */
30 class TestXmlrpcClient extends InternalClient {
31
32 public static final Object SUCCESS = "Success";
33 public static final Object FAILURE = "Failure";
34
35 /**
36 * Constructor.
37 *
38 * @param endpoint hub HTTP endpoint
39 */
40 TestXmlrpcClient( URL endpoint ) {
41 super( endpoint );
42 }
43
44 /**
45 * Returns {@link #SUCCESS} or {@link #FAILURE}.
46 */
47 protected Object deserializeResponse( InputStream in )
48 throws IOException {
49 try {
50 Document doc = DocumentBuilderFactory.newInstance()
51 .newDocumentBuilder().parse( in );
52 Element topEl = XmlUtils.getChild( doc, "methodResponse" );
53 Element contentEl = XmlUtils.getChild( topEl );
54 String contentTag = contentEl.getTagName();
55 if ( "params".equals( contentTag ) ) {
56 Element paramEl = XmlUtils.getChild( contentEl, "param" );
57 Element valueEl = XmlUtils.getChild( paramEl, "value" );
58 Object value = XmlUtils.parseSampValue( valueEl );
59 SampUtils.checkObject( value );
60 return SUCCESS;
61 }
62 else if ( "fault".equals( contentTag ) ) {
63 Element valueEl = XmlUtils.getChild( contentEl, "value" );
64 Map value = (Map) XmlUtils.parseSampValue( valueEl );
65 String faultString = (String) value.get( "faultString" );
66 int faultCode = ((Integer) value.get( "faultCode" )).intValue();
67 Tester.assertEquals( 2, value.size() );
68 return FAILURE;
69 }
70 else {
71 throw new TestException( "Unknown <methodResponse> child: "
72 + contentTag );
73 }
74 }
75 catch ( ParserConfigurationException e ) {
76 throw (IOException) new IOException( "Trouble with XML parsing" )
77 .initCause( e );
78 }
79 catch ( SAXException e ) {
80 throw (IOException) new IOException( "Trouble with XML parsing" )
81 .initCause( e );
82 }
83 catch ( DOMException e ) {
84 throw (IOException) new IOException( "Trouble with XML parsing" )
85 .initCause( e );
86 }
87 }
88
89 /**
90 * Makes a call, and asserts that the result is a normal XML-RPC
91 * response.
92 *
93 * @param method XML-RPC method name
94 * @param params parameters for XML-RPC call (SAMP-compatible)
95 * @throws TestException if the response is not a success
96 */
97 public void checkSuccessCall( String method, List params )
98 throws IOException {
99 Tester.assertEquals( SUCCESS, callAndWait( method, params ) );
100 }
101
102 /**
103 * Makes a call, and asserts that the result is an XML-RPC fault.
104 *
105 * @param method XML-RPC method name
106 * @param params parameters for XML-RPC call (SAMP-compatible)
107 * @throws TestException if the response is not a fault
108 */
109 public void checkFailureCall( String method, List params )
110 throws IOException {
111 Tester.assertEquals( FAILURE, callAndWait( method, params ) );
112 }
113 }