KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > test > restart > RestartTestEnvironmentTest


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.test.restart;
5
6 import com.tc.test.TCTestCase;
7 import com.tc.util.PortChooser;
8 import com.tc.util.concurrent.NoExceptionLinkedQueue;
9
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.net.ServerSocket JavaDoc;
13 import java.net.Socket JavaDoc;
14
15 public class RestartTestEnvironmentTest extends TCTestCase {
16
17   public void testChoosePort() throws Exception JavaDoc {
18     PortChooser portChooser = new PortChooser();
19     RestartTestEnvironment env = new RestartTestEnvironment(getTempDirectory(), portChooser, RestartTestEnvironment.PROD_MODE);
20     int port = env.chooseAdminPort();
21     NoExceptionLinkedQueue control = new NoExceptionLinkedQueue();
22     System.err.println("I chose port " + port);
23     Listener listener = new Listener(port, control);
24     listener.start();
25     control.take();
26     Socket JavaDoc s = new Socket JavaDoc("localhost", port);
27     int value = 10;
28     s.getOutputStream().write(value);
29     assertEquals(new Integer JavaDoc(value), control.take());
30   }
31
32   private static final class Listener extends Thread JavaDoc {
33     private int port;
34     private final NoExceptionLinkedQueue control;
35
36     public Listener(int port, NoExceptionLinkedQueue control) {
37       this.port = port;
38       this.control = control;
39     }
40
41     public void run() {
42       try {
43         ServerSocket JavaDoc s = new ServerSocket JavaDoc(port);
44         control.put(new Object JavaDoc());
45         Socket JavaDoc socket = s.accept();
46         InputStream JavaDoc in = socket.getInputStream();
47         control.put(new Integer JavaDoc(in.read()));
48       } catch (IOException JavaDoc e) {
49         e.printStackTrace();
50       }
51     }
52   }
53 }
54
Popular Tags