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: EasyMockTestCaseTest.java,v 1.5 2005/08/26 18:09:36 mat007 Exp $
29   */
30  
31  package jtge.util;
32  
33  import java.util.Collection;
34  import java.util.Vector;
35  import junit.framework.TestCase;
36  import junit.framework.TestResult;
37  
38  /***
39   * Test case for EasyMockTestCase.
40   *
41   * @author Mathieu Champlon
42   * @version $Revision: 1.5 $ $Date: 2005/08/26 18:09:36 $
43   */
44  public class EasyMockTestCaseTest extends TestCase
45  {
46      /***
47       * Tested object.
48       */
49      private EasyMockTestCase test;
50  
51      protected void setUp()
52      {
53          test = new EasyMockTestCase();
54      }
55  
56      public void testCreateMockFromInterfaceProvidesNonNullObject()
57      {
58          assertNotNull( test.createMock( Collection.class ) );
59      }
60  
61      public void testCreateMockFromClassProvidesNonNullObject()
62      {
63          assertNotNull( test.createMock( Vector.class ) );
64      }
65  
66      public void testControlObjectForCreatedMockIsNonNull()
67      {
68          assertNotNull( test.control( test.createMock( Collection.class ) ) );
69      }
70  
71      public void testControlObjectForInvalidMockIsNull()
72      {
73          final Vector invalidMock = new Vector();
74          assertNull( test.control( invalidMock ) );
75      }
76  
77      public void testCreateMockAndRunTestPasses()
78      {
79          final EasyMockTestCase test = new EasyMockTestCase()
80          {
81              protected void setUp()
82              {
83                  createMock( Collection.class );
84              }
85  
86              protected void runTest()
87              {
88              }
89          };
90          assertSuccess( test );
91      }
92  
93      public void testCreateMockWithExpectationAndRunTestFails()
94      {
95          final EasyMockTestCase test = new EasyMockTestCase()
96          {
97              private Collection mock;
98  
99              protected void setUp()
100             {
101                 mock = (Collection)createMock( Collection.class );
102             }
103 
104             protected void runTest()
105             {
106                 mock.add( null );
107             }
108         };
109         assertError( test );
110     }
111 
112     public void testCreateMockWithExpectationAndRunTestPasses()
113     {
114         final EasyMockTestCase test = new EasyMockTestCase()
115         {
116             private Collection mock;
117 
118             protected void setUp()
119             {
120                 mock = (Collection)createMock( Collection.class );
121             }
122 
123             protected void runTest()
124             {
125                 mock.add( null );
126                 replay();
127                 mock.add( null );
128             }
129         };
130         assertError( test );
131     }
132 
133     private void assertSuccess( final TestCase test )
134     {
135         final TestResult result = test.run();
136         assertEquals( 1, result.runCount() );
137         assertEquals( 0, result.failureCount() );
138         assertEquals( 0, result.errorCount() );
139     }
140 
141     private void assertError( final TestCase test )
142     {
143         final TestResult result = test.run();
144         assertEquals( 1, result.runCount() );
145         assertEquals( 0, result.failureCount() );
146         assertEquals( 1, result.errorCount() );
147     }
148 }