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 jtge.engine.command.ICommand;
34 import jtge.engine.handler.IConsumer;
35 import jtge.engine.io.IDisconnectionObserver;
36 import jtge.engine.io.IInputImp;
37 import jtge.engine.io.Input;
38 import jtge.engine.io.InputOutputException;
39 import jtge.util.EasyMockTestCase;
40 import org.easymock.MockControl;
41
42 /***
43 * Input test case.
44 *
45 * @author Mathieu Champlon
46 * @version $Revision$ $Date$
47 */
48 public class InputTest extends EasyMockTestCase
49 {
50 /***
51 * Tested object.
52 */
53 private Input input;
54 /***
55 * Mock objects.
56 */
57 private IConsumer mockConsumer;
58 private IDisconnectionObserver mockObserver;
59 private IInputImp mockInputImp;
60 private ICommand mockCommand;
61
62 protected void setUp()
63 {
64 mockConsumer = (IConsumer)createMock( IConsumer.class );
65 mockObserver = (IDisconnectionObserver)createMock( IDisconnectionObserver.class );
66 mockInputImp = (IInputImp)createMock( IInputImp.class );
67 mockCommand = (ICommand)createMock( ICommand.class );
68 input = new Input( mockConsumer, mockObserver, mockInputImp );
69 }
70
71 public void testCloseClosesInputIfNotAlreadyClosed()
72 {
73 mockInputImp.isClosed();
74 control( mockInputImp ).setReturnValue( false );
75 mockInputImp.close();
76 mockObserver.closed( input );
77 replay();
78 input.close();
79 }
80
81 public void testCloseIsNoOpIfInputAlreadyClosed()
82 {
83 mockInputImp.isClosed();
84 control( mockInputImp ).setReturnValue( true );
85 replay();
86 input.close();
87 }
88
89 public void testReadIsNoOpWhenClosed()
90 {
91 mockInputImp.isClosed();
92 control( mockInputImp ).setReturnValue( true );
93 replay();
94 input.read();
95 }
96
97 public void testReadCommand()
98 {
99 mockInputImp.isClosed();
100 control( mockInputImp ).setReturnValue( false );
101 mockInputImp.read();
102 control( mockInputImp ).setReturnValue( mockCommand );
103 mockConsumer.handle( mockCommand );
104 mockInputImp.isClosed();
105 control( mockInputImp ).setReturnValue( true );
106 replay();
107 input.read();
108 }
109
110 public void testReadNullThrowsAnException()
111 {
112 mockInputImp.isClosed();
113 control( mockInputImp ).setReturnValue( false, 2 );
114 mockInputImp.read();
115 control( mockInputImp ).setReturnValue( null );
116 replay();
117 try
118 {
119 input.read();
120 }
121 catch( InputOutputException e )
122 {
123 return;
124 }
125 fail( "should have thrown an exception" );
126 }
127
128 public void testReadNullDetectsInputClosed()
129 {
130 mockInputImp.isClosed();
131 control( mockInputImp ).setReturnValue( false );
132 mockInputImp.read();
133 control( mockInputImp ).setReturnValue( null );
134 mockInputImp.isClosed();
135 control( mockInputImp ).setReturnValue( true, MockControl.ONE_OR_MORE );
136 mockObserver.closed( input );
137 replay();
138 input.read();
139 }
140 }