1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.james.mpt;
21
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.io.Writer;
25 import java.net.Socket;
26
27 import javax.net.SocketFactory;
28
29 import junit.framework.TestCase;
30
31 public class TestDiscardProtocol extends TestCase {
32
33 private final class InputLater implements Runnable {
34 private Exception e;
35
36 public void run() {
37 try {
38 Thread.sleep(1000);
39 input();
40 } catch (Exception e) {
41 this.e = e;
42 }
43 }
44
45 public void assertExecutedSuccessfully() throws Exception {
46 if (e != null) {
47 e.printStackTrace();
48 throw e;
49 }
50 }
51 }
52
53 private static final String INPUT = "One, two, three - Testing";
54
55 private static final int PORT = 10001;
56
57 private DiscardProtocol protocol;
58 private Socket socket;
59
60 private DiscardProtocol.Record record;
61
62 @Override
63 protected void setUp() throws Exception {
64 super.setUp();
65 protocol = new DiscardProtocol(PORT);
66 protocol.start();
67 socket = SocketFactory.getDefault().createSocket("127.0.0.1", PORT);
68 record = protocol.recordNext();
69 }
70
71 @Override
72 protected void tearDown() throws Exception {
73 protocol.stop();
74 super.tearDown();
75 }
76
77 public void testRecord() throws Exception {
78 assertTrue(socket.isConnected());
79 input();
80 String output = record.complete();
81 assertEquals(INPUT, output);
82 }
83
84 private void input() throws IOException {
85 Writer out = new OutputStreamWriter(socket.getOutputStream());
86 out.append(INPUT);
87 out.close();
88 socket.close();
89 }
90
91 public void testComplete() throws Exception {
92 InputLater inputLater = new InputLater();
93 Thread thread = new Thread(inputLater);
94 thread.start();
95 String output = record.complete();
96 assertEquals(INPUT, output);
97 inputLater.assertExecutedSuccessfully();
98 }
99 }