1   /* Copyright (c) 2005 Per S Hustad. All rights reserved.
2    *
3    * Redistribution and use in source and binary forms, with or without 
4    * modification, are permitted provided that the following conditions are met:
5    *
6    *  o Redistributions of source code must retain the above copyright notice, 
7    *    this list of conditions and the following disclaimer. 
8    *    
9    *  o Redistributions in binary form must reproduce the above copyright notice, 
10   *    this list of conditions and the following disclaimer in the documentation 
11   *    and/or other materials provided with the distribution. 
12   *    
13   *  o Neither the name of surrogate.sourceforge.net nor the names of 
14   *    its contributors may be used to endorse or promote products derived 
15   *    from this software without specific prior written permission. 
16   *    
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
18   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
19   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
20   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
21   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
22   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
23   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
24   * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
25   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
26   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
27   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28   */
29  package net.sf.surrogate.core;
30  
31  import junit.framework.AssertionFailedError;
32  import junit.framework.TestCase;
33  
34  /***
35   * Tests the MockMethod internals.
36   * <p>
37   * 
38   * @author Per S Hustad
39   */
40  public class MockMethodTestCase extends TestCase {
41  
42      public MockMethodTestCase(String name) {
43          super(name);
44      }
45  
46      /***
47       * Test MockMethod standard behavior
48       */
49      public void testStandardExecute() throws Throwable {
50  
51          MockMethod mockMethod = new MockMethod(TestClass01.class, "testCall",
52                  new Class[] { String.class, Integer.TYPE });
53  
54          mockMethod.addReturnValue("1");
55          mockMethod.addReturnValue("2");
56          mockMethod.addExpectedParameters(new Object[] { "A", new Integer(1) });
57          mockMethod.addExpectedParameters(new Object[] { "B", new Integer(2) });
58          mockMethod.addExpectedParameters(new Object[] { "C", new Integer(3) });
59  
60          mockMethod.setExpectedCalls(2);
61          Object testRet = mockMethod.execute(new Object[] { "A", new Integer(1) });
62          assertEquals("1", testRet);
63  
64          // Too few calls
65          try {
66              mockMethod.verify();
67              fail("Verify should fail  - not exact calls");
68          } catch (AssertionFailedError e) {
69          }
70          testRet = mockMethod.execute(new Object[] { "B", new Integer(2) });
71          assertEquals("2", testRet);
72          mockMethod.verify();
73          // Too many calls
74          try {
75              testRet = mockMethod.execute(new Object[] { "C", new Integer(3) });
76              fail("Expected exception because called more than specified");
77          } catch (AssertionFailedError e) {
78          }
79          // Wrong actual argument
80          mockMethod.setExpectedCalls(3);
81          try {
82              testRet = mockMethod.execute(new Object[] { "WRONG", new Integer(3) });
83              fail("Expected AssertionFailedError - wrong arguments");
84          } catch (AssertionFailedError e) {
85          }
86          // Wrong number of expected arguments
87          try {
88              mockMethod.addExpectedParameters(new Object[] { "B" });
89              fail("Expected an IllegalArgumentException - to few arguments");
90          } catch (IllegalArgumentException e) {
91  
92          }
93  
94      }
95  
96      /***
97       * Test MockMethod for method with no arguments and void return
98       * 
99       * @throws Throwable
100      *             if TestCase error
101      */
102     public void testExecuteWithVoid() throws Throwable {
103         MockMethod mockMethod = new MockMethod(TestClass01.class, "noArgsAndVoid");
104         mockMethod.setExpectedCalls(1);
105         mockMethod.execute(null);
106         mockMethod.verify();
107         mockMethod.reset();
108 
109         try {
110             mockMethod.addReturnValue(new Object());
111             fail("Expected an IllegalArgumentException - no return for void method");
112         } catch (IllegalArgumentException e) {
113 
114         }
115 
116     }
117 
118     /***
119      * Test wrong arguments to constructors
120      * 
121      * @throws Throwable
122      */
123     public void testConstructors() throws Throwable {
124         // Correct
125         new MockMethod(TestClass01.class, new Class[] { String.class, Integer.TYPE });
126         new MockMethod(TestClass01.class, "testCall", new Class[] { String.class, Integer.TYPE });
127         new MockMethod(OverloadedTestClass.class, new Class[] { String.class, Integer.TYPE });
128         new MockMethod(OverloadedTestClass.class, new Class[] { String.class, Integer.TYPE, String.class });
129         new MockMethod(OverloadedTestClass.class, "testCall", new Class[] { String.class, Integer.TYPE });
130         new MockMethod(OverloadedTestClass.class, "testCall", new Class[] { String.class, Integer.TYPE, String.class });
131 
132         // Test with wrong args
133         try {
134             new MockMethod(TestClass01.class, "testCall", new Class[] { String.class, Integer.TYPE, String.class });
135             fail("Expected NoSuchMethodException");
136         } catch (NoSuchMethodException e) {
137         }
138 
139         // Test with non-existing method
140         try {
141             new MockMethod(TestClass01.class, "testCallNOT_EXISTING", new Class[] { String.class, Integer.TYPE });
142             fail("Expected NoSuchMethodException");
143         } catch (NoSuchMethodException e) {
144         }
145         // Test overloaded class
146         try {
147             new MockMethod(OverloadedTestClass.class, "testCall");
148             fail("Expected IllegalArgumentException");
149         } catch (IllegalArgumentException e) {
150         }
151 
152     }
153 
154     /***
155      * Test the setMock method for Exceptions
156      * 
157      * @see MockMethod#addThrowableReturnValue(Throwable)
158      */
159     public void testAddThrowableReturnValue() throws Throwable {
160 
161         MockMethod mockMethod = new MockMethod(TestClass01.class, "noArgs");
162         mockMethod.addReturnValue("1");
163         mockMethod.addThrowableReturnValue(new TestException());
164         mockMethod.addReturnValue("2");
165         Object retVal = mockMethod.execute(null);
166         assertEquals("1", retVal);
167         try {
168             mockMethod.execute(null);
169             fail("expected a TestException");
170         } catch (TestException e) {
171         }
172         retVal = mockMethod.execute(null);
173         assertEquals("2", retVal);
174     }
175 
176     private static class TestClass01 {
177 
178         private TestClass01(String a, int b) {
179 
180         }
181 
182         private String testCall(String a, int b) {
183             return "OK";
184         }
185 
186         private String noArgs() {
187             return "OK";
188         }
189 
190         private void noArgsAndVoid() {
191 
192         }
193     }
194 
195     private static class OverloadedTestClass {
196         private OverloadedTestClass(String a, int b) {
197 
198         }
199 
200         private OverloadedTestClass(String a, int b, String c) {
201 
202         }
203 
204         private String testCall(String a, int b) {
205             return "OK";
206         }
207 
208         private String testCall(String a, int b, String c) {
209             return "OK";
210         }
211     }
212 
213     private static class TestException extends Exception {
214 
215     }
216 
217 }