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.InputStream;
25  import java.io.ObjectOutputStream;
26  import java.io.PipedInputStream;
27  import java.io.PipedOutputStream;
28  
29  import net.grinder.common.UncheckedInterruptedException;
30  import net.grinder.testutility.IsolatedObjectFactory;
31  
32  import junit.framework.TestCase;
33  
34  
35  /**
36   *  Unit test case for <code>StreamReceiver</code>.
37   *
38   * @author Philip Aston
39   */
40  public class TestStreamReceiver extends TestCase {
41  
42    public TestStreamReceiver(String name) {
43      super(name);
44    }
45  
46    public void testReceive() throws Exception {
47  
48      final PipedOutputStream outputStream = new PipedOutputStream();
49      final InputStream inputStream = new PipedInputStream(outputStream);
50  
51      final StreamReceiver streamReceiver = new StreamReceiver(inputStream);
52  
53      final SimpleMessage message1 = new SimpleMessage();
54  
55      final ObjectOutputStream objectStream1 =
56        new ObjectOutputStream(outputStream);
57      objectStream1.writeObject(message1);
58      objectStream1.flush();
59  
60      // Message that we can't read using the standard class loaders.
61      final SimpleMessage message2 = new SimpleMessage();
62      message2.setPayload(IsolatedObjectFactory.getIsolatedObject());
63  
64      final ObjectOutputStream objectStream2 =
65        new ObjectOutputStream(outputStream);
66      objectStream2.writeObject(message2);
67      objectStream2.flush();
68  
69      final SimpleMessage message3 = new SimpleMessage();
70  
71      final ObjectOutputStream objectStream3 =
72        new ObjectOutputStream(outputStream);
73      objectStream3.writeObject(message3);
74      objectStream3.flush();
75  
76      final Message receivedMessage1 = streamReceiver.waitForMessage();
77  
78      try {
79        streamReceiver.waitForMessage();
80        fail("Expected CommunicationException");
81      }
82      catch (CommunicationException e) {
83      }
84  
85      final Message receivedMessage2 = streamReceiver.waitForMessage();
86  
87      assertEquals(message1, receivedMessage1);
88      assertEquals(message3, receivedMessage2);
89  
90      assertEquals(
91        UncheckedInterruptedException.class,
92        new BlockingActionThread() {
93          protected void blockingAction() throws CommunicationException {
94            streamReceiver.waitForMessage();
95          }
96        }.getException().getClass());
97  
98  
99      outputStream.close();
100 
101     try {
102       streamReceiver.waitForMessage();
103       fail("Expected CommunicationException");
104     }
105     catch (CommunicationException e) {
106     }
107   }
108 
109   public void testShutdown() throws Exception {
110 
111     final PipedOutputStream outputStream = new PipedOutputStream();
112     final InputStream inputStream = new PipedInputStream(outputStream);
113 
114     final StreamReceiver streamReceiver = new StreamReceiver(inputStream);
115 
116     final SimpleMessage message = new SimpleMessage();
117 
118     final ObjectOutputStream objectStream =
119       new ObjectOutputStream(outputStream);
120     objectStream.writeObject(message);
121     objectStream.flush();
122 
123     final Message receivedMessage = streamReceiver.waitForMessage();
124     assertNotNull(receivedMessage);
125 
126     streamReceiver.shutdown();
127 
128     assertNull(streamReceiver.waitForMessage());
129   }
130 
131   public void testCloseCommunicationMessage() throws Exception {
132 
133     final PipedOutputStream outputStream = new PipedOutputStream();
134     final InputStream inputStream = new PipedInputStream(outputStream);
135 
136     final StreamReceiver streamReceiver = new StreamReceiver(inputStream);
137 
138     final SimpleMessage message = new SimpleMessage();
139 
140     final ObjectOutputStream objectStream1 =
141       new ObjectOutputStream(outputStream);
142     objectStream1.writeObject(message);
143     objectStream1.flush();
144 
145     final Message receivedMessage = streamReceiver.waitForMessage();
146     assertNotNull(receivedMessage);
147 
148     final Message closeCommunicationMessage = new CloseCommunicationMessage();
149 
150     final ObjectOutputStream objectStream2 =
151       new ObjectOutputStream(outputStream);
152     objectStream2.writeObject(closeCommunicationMessage);
153     objectStream2.flush();
154 
155     assertNull(streamReceiver.waitForMessage());
156   }
157 }
158