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