KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > setup > sources > URLConfigurationSourceTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.config.schema.setup.sources;
5
6 import com.tc.util.Assert;
7
8 import java.io.BufferedWriter JavaDoc;
9 import java.io.OutputStreamWriter JavaDoc;
10 import java.net.ServerSocket JavaDoc;
11 import java.net.Socket JavaDoc;
12 import java.net.SocketTimeoutException JavaDoc;
13
14 import junit.framework.TestCase;
15
16 public class URLConfigurationSourceTest extends TestCase {
17
18   private URLConfigurationSource configSrc;
19   private ServerSocket JavaDoc server;
20
21   public void setUp() throws Exception JavaDoc {
22     this.server = new ServerSocket JavaDoc(0);
23     Thread JavaDoc t = new Thread JavaDoc() {
24       public void run() {
25         try {
26           Socket JavaDoc socket = server.accept();
27           BufferedWriter JavaDoc response = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(socket.getOutputStream()));
28           response.write("connected to server");
29           sleep(5000);
30           response.write("\n");
31           response.flush();
32           socket.close();
33         } catch (Exception JavaDoc e) {
34           // not implemented
35
}
36       }
37     };
38     t.start();
39     this.configSrc = new URLConfigurationSource("http://" + server.getInetAddress().getHostAddress() + ":"
40                                                 + server.getLocalPort());
41   }
42
43   public void testGetInputStreamFailure() throws Exception JavaDoc {
44     try {
45       configSrc.getInputStream(1);
46       throw Assert.failure("Connection should have timed out");
47     } catch (SocketTimeoutException JavaDoc e) {
48       // pass
49
}
50   }
51
52   public void tearDown() throws Exception JavaDoc {
53     server.close();
54   }
55 }
56
Popular Tags