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.command;
32  
33  import jtge.engine.command.Flow;
34  import jtge.engine.command.ICommand;
35  import jtge.engine.command.IFilter;
36  import jtge.engine.command.ISorter;
37  import jtge.engine.data.IContext;
38  import jtge.util.EasyMockTestCase;
39  import org.easymock.MockControl;
40  
41  /***
42   * Flow test.
43   *
44   * @author Mathieu Champlon
45   * @version $Revision$ $Date$
46   */
47  public class FlowTest extends EasyMockTestCase
48  {
49      /***
50       * Tested object.
51       */
52      private Flow composite;
53      /***
54       * Mock objects.
55       */
56      private ICommand mockCommand1, mockCommand2, mockCommand3;
57  
58      protected void setUp()
59      {
60          mockCommand1 = (ICommand)createMock( ICommand.class );
61          mockCommand2 = (ICommand)createMock( ICommand.class );
62          mockCommand3 = (ICommand)createMock( ICommand.class );
63          composite = new Flow();
64          composite.add( mockCommand1 );
65          composite.add( mockCommand2 );
66          composite.add( mockCommand3 );
67      }
68  
69      public void testEqualsNull()
70      {
71          assertFalse( composite.equals( null ) );
72      }
73  
74      public void testEquals()
75      {
76          Flow multi1 = new Flow();
77          Flow multi2 = new Flow();
78          assertTrue( multi1.equals( multi2 ) );
79          assertFalse( composite.equals( multi1 ) );
80          multi1.add( mockCommand1 );
81          assertFalse( composite.equals( multi1 ) );
82          multi1.add( mockCommand2 );
83          assertFalse( composite.equals( multi1 ) );
84          multi1.add( mockCommand3 );
85          assertTrue( composite.equals( multi1 ) );
86          multi1.add( mockCommand3 );
87          assertFalse( composite.equals( multi1 ) );
88      }
89  
90      public void testExecute()
91      {
92          IContext mockContext = (IContext)createMock( IContext.class );
93          mockCommand1.execute( mockContext );
94          mockCommand2.execute( mockContext );
95          mockCommand3.execute( mockContext );
96          replay();
97          composite.execute( mockContext );
98      }
99  
100     public void testFilter()
101     {
102         IFilter mockFilter = (IFilter)createMock( IFilter.class );
103         mockCommand1.filter( mockFilter );
104         control( mockCommand1 ).setReturnValue( mockCommand1 );
105         mockCommand2.filter( mockFilter );
106         control( mockCommand2 ).setReturnValue( null );
107         mockCommand3.filter( mockFilter );
108         control( mockCommand3 ).setReturnValue( mockCommand3 );
109         replay();
110         Flow expectedComposite = new Flow();
111         expectedComposite.add( mockCommand1 );
112         expectedComposite.add( mockCommand3 );
113         assertEquals( expectedComposite, composite.filter( mockFilter ) );
114     }
115 
116     public void testFilterOutAllCommandsReturnsNull()
117     {
118         IFilter mockFilter = (IFilter)createMock( IFilter.class );
119         mockCommand1.filter( mockFilter );
120         control( mockCommand1 ).setReturnValue( null );
121         mockCommand2.filter( mockFilter );
122         control( mockCommand2 ).setReturnValue( null );
123         mockCommand3.filter( mockFilter );
124         control( mockCommand3 ).setReturnValue( null );
125         replay();
126         assertNull( composite.filter( mockFilter ) );
127     }
128 
129     public void testSort()
130     {
131         ISorter mockSorter = (ISorter)createMock( ISorter.class );
132         mockSorter.lessThan( mockCommand2, mockCommand1 );
133         control( mockSorter ).setReturnValue( false, MockControl.ZERO_OR_MORE );
134         mockSorter.lessThan( mockCommand3, mockCommand2 );
135         control( mockSorter ).setReturnValue( true, MockControl.ZERO_OR_MORE );
136         mockSorter.lessThan( mockCommand3, mockCommand1 );
137         control( mockSorter ).setReturnValue( false, MockControl.ZERO_OR_MORE );
138         replay();
139         Flow expectedComposite = new Flow();
140         expectedComposite.add( mockCommand1 );
141         expectedComposite.add( mockCommand3 );
142         expectedComposite.add( mockCommand2 );
143         assertEquals( expectedComposite, composite.sort( mockSorter ) );
144     }
145 }