1 /***
2 * Redistribution and use in source and binary forms, with or without
3 * modification, are permitted provided that the following conditions are
4 * met :
5 *
6 * . Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 *
9 * . Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * . The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 * $Id$
29 */
30
31 package jtge.engine.io;
32
33 import java.io.ByteArrayInputStream;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.io.OutputStream;
37 import java.io.PipedInputStream;
38 import java.io.PipedOutputStream;
39 import jtge.engine.command.ICommand;
40 import jtge.util.EasyMockTestCase;
41 import org.easymock.MockControl;
42
43 /***
44 * Abstract test class for bridge input implementations.
45 *
46 * @author Mathieu Champlon
47 * @version $Revision$ $Date$
48 */
49 public abstract class InputImpTestHelper extends EasyMockTestCase
50 {
51 /***
52 * Tested object.
53 */
54 private IInputImp input;
55 /***
56 * Mock objects
57 */
58 private InputStream mockStream;
59
60 /***
61 * Factory method to create a concrete IInputImp.
62 *
63 * @param stream the input stream
64 * @return a concrete IInputImp
65 */
66 abstract protected IInputImp create( InputStream stream );
67
68 /***
69 * Factory method to write a command into a dummy output stream.
70 *
71 * @param command the command to write
72 * @param stream the output stream to write into
73 * @throws IOException if an error occurs
74 */
75 abstract protected void write( ICommand command, OutputStream stream );
76
77 protected void setUp()
78 {
79 mockStream = (InputStream)createMock( InputStream.class );
80 input = create( mockStream );
81 }
82
83 public void testCreateWithNullStreamThrowsAnException()
84 {
85 try
86 {
87 create( null );
88 }
89 catch( Exception e )
90 {
91 return;
92 }
93 fail();
94 }
95
96 public void testCreateDoesNotReadAnythingFromTheStream()
97 {
98 assertFalse( create( mockStream ).isClosed() );
99 }
100
101 public void testCloseForwardsToTheStream() throws IOException
102 {
103 mockStream.close();
104 replay();
105 input.close();
106 }
107
108 public void testReadThrowsExceptionWhenTheStreamDoes() throws IOException
109 {
110 mockStream.read( new byte[0], 0, 0 );
111 control( mockStream ).setMatcher( MockControl.ALWAYS_MATCHER );
112 control( mockStream ).setThrowable( new IOException( "message_should_not_matter" ), MockControl.ONE );
113 replay();
114 try
115 {
116 input.read();
117 }
118 catch( InputOutputException e )
119 {
120 assertFalse( input.isClosed() );
121 return;
122 }
123 verify();
124 fail();
125 }
126
127 public void testReadEndOfFileClosesAndReturnsNull() throws IOException
128 {
129 assertFalse( input.isClosed() );
130 mockStream.read( new byte[0], 0, 0 );
131 control( mockStream ).setMatcher( MockControl.ALWAYS_MATCHER );
132 control( mockStream ).setReturnValue( -1 );
133 mockStream.close();
134 replay();
135 assertNull( input.read() );
136 assertTrue( input.isClosed() );
137 }
138
139 public void testCanReadDummyCommand() throws IOException
140 {
141 final PipedInputStream pipedInput = new PipedInputStream();
142 write( new DummyCommand(), new PipedOutputStream( pipedInput ) );
143 input = create( pipedInput );
144 assertFalse( input.isClosed() );
145 assertTrue( input.read() instanceof DummyCommand );
146 assertFalse( input.isClosed() );
147 }
148
149 public void testReadTrashThrowsAnException()
150 {
151 input = create( new ByteArrayInputStream( "a_string_is_not_a_command".getBytes() ) );
152 assertFalse( input.isClosed() );
153 try
154 {
155 input.read();
156 }
157 catch( InputOutputException e )
158 {
159 assertFalse( input.isClosed() );
160 return;
161 }
162 fail();
163 }
164 }