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.IOException;
34  import java.io.OutputStream;
35  import jtge.engine.command.ICommand;
36  import jtge.util.EasyMockTestCase;
37  import org.easymock.MockControl;
38  
39  /***
40   * Abstract test class for bridge output implementations.
41   *
42   * @author Mathieu Champlon
43   * @version $Revision$ $Date$
44   */
45  public abstract class OutputImpTestHelper extends EasyMockTestCase
46  {
47      /***
48       * Tested object.
49       */
50      private IOutputImp output;
51      /***
52       * Mock objects
53       */
54      private OutputStream mockStream;
55      private ICommand mockCommand;
56  
57      /***
58       * Factory method to create a concrete IOutputImp.
59       *
60       * @param stream the output stream
61       * @return a concrete IOutputImp
62       */
63      abstract protected IOutputImp create( OutputStream stream );
64  
65      protected void setUp()
66      {
67          mockStream = (OutputStream)createMock( OutputStream.class );
68          mockCommand = (ICommand)createMock( ICommand.class );
69          output = create( mockStream );
70      }
71  
72      public void testCreateWithNullStreamThrowsAnException()
73      {
74          try
75          {
76              create( null );
77          }
78          catch( Exception e )
79          {
80              return;
81          }
82          fail();
83      }
84  
85      public void testCreateDoesNotWriteAnythingToTheStream()
86      {
87          assertFalse( create( mockStream ).isClosed() );
88      }
89  
90      public void testCloseForwardsToTheStream() throws IOException
91      {
92          mockStream.close();
93          replay();
94          output.close();
95      }
96  
97      public void testWriteThrowsAnExceptionWhenTheStreamDoes() throws IOException
98      {
99          mockStream.write( new byte[0], 0, 0 );
100         control( mockStream ).setMatcher( MockControl.ALWAYS_MATCHER );
101         control( mockStream ).setThrowable( new IOException( "mock_io_exception" ), MockControl.ONE_OR_MORE );
102         replay();
103         try
104         {
105             output.write( mockCommand );
106         }
107         catch( InputOutputException e )
108         {
109             assertFalse( output.isClosed() );
110             return;
111         }
112         verify();
113         fail();
114     }
115 
116     public void testWriteNullCommandDoesNotWriteDataToTheStream()
117     {
118         output.write( null ); // might write a header on the first command
119         reset();
120         replay();
121         output.write( null );
122     }
123 
124     public void testWriteAppendsDataToTheStream() throws IOException
125     {
126         mockStream.write( new byte[0], 0, 0 );
127         control( mockStream ).setMatcher( MockControl.ALWAYS_MATCHER );
128         control( mockStream ).setVoidCallable( MockControl.ONE_OR_MORE );
129         mockStream.flush();
130         control( mockStream ).setVoidCallable( MockControl.ZERO_OR_MORE );
131         replay();
132         output.write( new DummyCommand() );
133     }
134 }