1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.communication;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.ObjectInputStream;
27
28 import junit.framework.TestCase;
29
30
31
32
33
34
35
36 public class TestStreamSender extends TestCase {
37
38 public TestStreamSender(String name) {
39 super(name);
40 }
41
42 public void testSend() throws Exception {
43
44 final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
45
46 final StreamSender streamSender = new StreamSender(byteOutputStream);
47
48 final SimpleMessage message1 = new SimpleMessage();
49 final SimpleMessage message2 = new SimpleMessage();
50
51 streamSender.send(message1);
52 streamSender.send(message2);
53
54 final ByteArrayInputStream byteInputStream =
55 new ByteArrayInputStream(byteOutputStream.toByteArray());
56
57
58
59 final ObjectInputStream inputStream1 =
60 new ObjectInputStream(byteInputStream);
61 final Object o1 = inputStream1.readObject();
62
63 final ObjectInputStream inputStream2 =
64 new ObjectInputStream(byteInputStream);
65 final Object o2 = inputStream2.readObject();
66
67 assertEquals(message1, o1);
68 assertEquals(message2, o2);
69
70 assertEquals(0, byteInputStream.available());
71 }
72
73 public void testShutdown() throws Exception {
74
75 final ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
76
77 final StreamSender streamSender = new StreamSender(byteOutputStream);
78
79 final Message message = new SimpleMessage();
80
81 streamSender.send(message);
82
83 streamSender.shutdown();
84
85 try {
86 streamSender.send(message);
87 fail("Expected CommunicationException");
88 }
89 catch (CommunicationException e) {
90 }
91
92 final ByteArrayInputStream byteInputStream =
93 new ByteArrayInputStream(byteOutputStream.toByteArray());
94
95 final ObjectInputStream inputStream1 =
96 new ObjectInputStream(byteInputStream);
97 final Object o1 = inputStream1.readObject();
98 assertNotNull(o1);
99
100 final ObjectInputStream inputStream2 =
101 new ObjectInputStream(byteInputStream);
102 final Object o2 = inputStream2.readObject();
103
104 assertTrue(o2 instanceof CloseCommunicationMessage);
105 }
106 }