KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > XmlRpcTransportTest


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

16
17
18 package org.apache.xmlrpc;
19
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25
26 /**
27  * Abstract Test class to be extended by the tests for each Transport.
28  * Guarantees that transports implement a base contract expected by the
29  * XmlRpcClient classes
30  *
31  * @author <a HREF="mailto:rhoegg@isisnetworks.net">Ryan Hoegg</a>
32  * @version $Id: XmlRpcTransportTest.java,v 1.4 2005/04/22 10:26:17 hgomez Exp $
33  */

34 abstract public class XmlRpcTransportTest
35     extends LocalServerRpcTest
36 {
37     /**
38      * Constructor
39      */

40     public XmlRpcTransportTest(String JavaDoc testName)
41     {
42         super(testName);
43     }
44     
45     /**
46      * This should return a new instance of the XmlRpcTransport being tested.
47      */

48     abstract protected XmlRpcTransport getTransport(URL JavaDoc url);
49     
50     /**
51      * This test is to enforce that every alternate implementation of
52      * XmlRpcTransport provides a minimum of the same functionality as
53      * @link DefaultXmlRpcTransport. We trust DefaultXmlRpcTransport
54      * because it is used by default in XmlRpcClient, which is tested
55      * in @link ClientServerRpcTest.
56      */

57     public void testSendXmlRpc() {
58         
59         try {
60             setUpWebServer();
61             startWebServer();
62             URL JavaDoc testUrl = buildURL("localhost", SERVER_PORT);
63             XmlRpcTransport controlTransport = new DefaultXmlRpcTransport(testUrl);
64             XmlRpcTransport testTransport = getTransport(testUrl);
65             InputStream JavaDoc controlResponse = controlTransport.sendXmlRpc(RPC_REQUEST.getBytes());
66             InputStream JavaDoc testResponse = testTransport.sendXmlRpc(RPC_REQUEST.getBytes());
67             assertTrue(
68                 "Response from XmlRpcTransport does not match that of DefaultXmlRpcTransport.",
69                 equalsInputStream(controlResponse, testResponse));
70             stopWebServer();
71         }
72         catch (MalformedURLException JavaDoc e) {
73             e.printStackTrace();
74             fail(e.getMessage());
75         }
76         catch (IOException JavaDoc e) {
77             e.printStackTrace();
78             fail(e.getMessage());
79         }
80         catch (XmlRpcClientException e) {
81             e.printStackTrace();
82             fail(e.getMessage());
83         }
84     }
85     
86     private URL JavaDoc buildURL(String JavaDoc hostname, int port) throws MalformedURLException JavaDoc {
87         return new URL JavaDoc("http://" + hostname + ':' + port + "/RPC2");
88     }
89     
90     protected boolean equalsInputStream(InputStream JavaDoc is1, InputStream JavaDoc is2) throws IOException JavaDoc {
91         BufferedInputStream JavaDoc stream1 = new BufferedInputStream JavaDoc(is1);
92         BufferedInputStream JavaDoc stream2 = new BufferedInputStream JavaDoc(is2);
93         int char1 = is1.read();
94         int char2 = is2.read();
95         while ((char1 != -1) && (char2 != -1) && (char1 == char2)) {
96             char1 = is1.read();
97             char2 = is2.read();
98         }
99         return char1 == char2;
100     }
101 }
102
Popular Tags