KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > remoting > stream > StreamingTestClient


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.remoting.stream;
8
9 import java.io.File JavaDoc;
10 import java.io.FileInputStream JavaDoc;
11 import java.net.URL JavaDoc;
12 import org.jboss.remoting.Client;
13 import org.jboss.remoting.InvokerLocator;
14
15 import junit.framework.TestCase;
16
17 /**
18  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
19  */

20 public class StreamingTestClient extends TestCase
21 {
22    // Default locator values
23
private static String JavaDoc transport = "socket";
24    private static String JavaDoc host = "localhost";
25    private static int port = 5400;
26
27    private String JavaDoc locatorURI = transport + "://" + host + ":" + port;
28    private Client remotingClient = null;
29    private File JavaDoc testFile = null;
30    private FileInputStream JavaDoc fileInput = null;
31
32    public void testStream() throws Throwable JavaDoc
33    {
34       URL JavaDoc fileURL = this.getClass().getResource("test.txt");
35       if(fileURL == null)
36       {
37          throw new Exception JavaDoc("Can not find file test.txt");
38       }
39       testFile = new File JavaDoc(fileURL.getFile());
40       fileInput = new FileInputStream JavaDoc(testFile);
41
42       String JavaDoc param = "foobar";
43       long fileLength = testFile.length();
44       System.out.println("File size = " + fileLength);
45       Object JavaDoc ret = remotingClient.invoke(fileInput, param);
46
47       assertEquals(param, ret);
48
49       Object JavaDoc response = remotingClient.invoke("get_size");
50       int returnedFileLength = ((Integer JavaDoc)response).intValue();
51       System.out.println("Invocation response: " + response);
52
53       if(fileLength == returnedFileLength)
54       {
55          System.out.println("PASS");
56       }
57       else
58       {
59          System.out.println("FAILED - returned file length was " + returnedFileLength);
60       }
61       assertEquals(fileLength, returnedFileLength);
62    }
63
64    public void setUp() throws Exception JavaDoc
65    {
66       InvokerLocator locator = new InvokerLocator(locatorURI);
67       System.out.println("Calling remoting server with locator uri of: " + locatorURI);
68
69       remotingClient = new Client(locator);
70    }
71
72    public void tearDown() throws Exception JavaDoc
73    {
74       if(remotingClient != null)
75       {
76          remotingClient.disconnect();
77       }
78       if(fileInput != null)
79       {
80          fileInput.close();
81       }
82    }
83
84    /**
85     * Can pass transport and port to be used as parameters.
86     * Valid transports are 'rmi' and 'socket'.
87     *
88     * @param args
89     */

90    public static void main(String JavaDoc[] args)
91    {
92       if(args != null && args.length == 2)
93       {
94          transport = args[0];
95          port = Integer.parseInt(args[1]);
96       }
97       String JavaDoc locatorURI = transport + "://" + host + ":" + port;
98       StreamingTestClient client = new StreamingTestClient();
99       try
100       {
101          client.setUp();
102          client.testStream();
103          client.tearDown();
104       }
105       catch(Throwable JavaDoc e)
106       {
107          e.printStackTrace();
108       }
109    }
110
111
112 }
Popular Tags