View Javadoc

1   // Copyright (C) 2003, 2004, 2005 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
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   *  Unit test case for <code>StreamSender</code>.
33   *
34   * @author Philip Aston
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      // Need an ObjectInputStream for every message. See note in
58      // StreamSender.writeMessage.
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 }