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.File;
34  import java.io.IOException;
35  import java.io.InputStream;
36  import jtge.engine.io.FileInputConnector;
37  import jtge.engine.io.IConnectionObserver;
38  import jtge.engine.io.IInput;
39  import jtge.engine.io.IInputOutputFactory;
40  import jtge.util.EasyMockTestCase;
41  import org.easymock.MockControl;
42  
43  /***
44   * File input connector test case.
45   *
46   * @see FileInputConnector
47   * @author Mathieu Champlon
48   * @version $Revision$ $Date$
49   */
50  public class FileInputConnectorTest extends EasyMockTestCase
51  {
52      /***
53       * Tested object.
54       */
55      private FileInputConnector connector;
56      /***
57       * Mock objects.
58       */
59      private IInputOutputFactory mockFactory;
60      private IConnectionObserver mockObserver;
61      private IInput mockInput;
62      /***
63       * Dummy objects.
64       */
65      private File file;
66  
67      protected void setUp() throws Exception
68      {
69          mockFactory = (IInputOutputFactory)createMock( IInputOutputFactory.class );
70          mockObserver = (IConnectionObserver)createMock( IConnectionObserver.class );
71          mockInput = (IInput)createMock( IInput.class );
72          file = new File( "tmp_input_file_test" );
73          connector = new FileInputConnector( new CloseFactory( mockFactory ), mockObserver, "tmp_input_file_test" );
74      }
75  
76      protected void tearDown() throws Exception
77      {
78          file.delete();
79      }
80  
81      private void createFile()
82      {
83          try
84          {
85              file.createNewFile();
86          }
87          catch( IOException e )
88          {
89          }
90      }
91  
92      public void testOpenFileFails()
93      {
94          assertFalse( file.exists() );
95          replay();
96          try
97          {
98              connector.open();
99          }
100         catch( Exception e )
101         {
102             return;
103         }
104         fail( "should throw an exception" );
105     }
106 
107     public void testOpenSuccessfull()
108     {
109         createFile();
110         assertTrue( file.exists() );
111         mockObserver.opened( mockInput, null );
112         mockFactory.create( (InputStream)null );
113         control( mockFactory ).setMatcher( MockControl.ALWAYS_MATCHER );
114         control( mockFactory ).setDefaultReturnValue( mockInput );
115         replay();
116         connector.open();
117     }
118 }