001: /* Copyright (c) 2005 Per S Hustad. All rights reserved.
002: *
003: * Redistribution and use in source and binary forms, with or without
004: * modification, are permitted provided that the following conditions are met:
005: *
006: * o Redistributions of source code must retain the above copyright notice,
007: * this list of conditions and the following disclaimer.
008: *
009: * o Redistributions in binary form must reproduce the above copyright notice,
010: * this list of conditions and the following disclaimer in the documentation
011: * and/or other materials provided with the distribution.
012: *
013: * o Neither the name of surrogate.sourceforge.net nor the names of
014: * its contributors may be used to endorse or promote products derived
015: * from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
019: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
020: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
024: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
025: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
026: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
027: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029: package net.sf.surrogate.core;
030:
031: import junit.framework.AssertionFailedError;
032: import junit.framework.TestCase;
033:
034: /**
035: * Tests the MockMethod internals.
036: * <p>
037: *
038: * @author Per S Hustad
039: */
040: public class MockMethodTestCase extends TestCase {
041:
042: public MockMethodTestCase(String name) {
043: super (name);
044: }
045:
046: /**
047: * Test MockMethod standard behavior
048: */
049: public void testStandardExecute() throws Throwable {
050:
051: MockMethod mockMethod = new MockMethod(TestClass01.class,
052: "testCall", new Class[] { String.class, Integer.TYPE });
053:
054: mockMethod.addReturnValue("1");
055: mockMethod.addReturnValue("2");
056: mockMethod.addExpectedParameters(new Object[] { "A",
057: new Integer(1) });
058: mockMethod.addExpectedParameters(new Object[] { "B",
059: new Integer(2) });
060: mockMethod.addExpectedParameters(new Object[] { "C",
061: new Integer(3) });
062:
063: mockMethod.setExpectedCalls(2);
064: Object testRet = mockMethod.execute(new Object[] { "A",
065: new Integer(1) });
066: assertEquals("1", testRet);
067:
068: // Too few calls
069: try {
070: mockMethod.verify();
071: fail("Verify should fail - not exact calls");
072: } catch (AssertionFailedError e) {
073: }
074: testRet = mockMethod
075: .execute(new Object[] { "B", new Integer(2) });
076: assertEquals("2", testRet);
077: mockMethod.verify();
078: // Too many calls
079: try {
080: testRet = mockMethod.execute(new Object[] { "C",
081: new Integer(3) });
082: fail("Expected exception because called more than specified");
083: } catch (AssertionFailedError e) {
084: }
085: // Wrong actual argument
086: mockMethod.setExpectedCalls(3);
087: try {
088: testRet = mockMethod.execute(new Object[] { "WRONG",
089: new Integer(3) });
090: fail("Expected AssertionFailedError - wrong arguments");
091: } catch (AssertionFailedError e) {
092: }
093: // Wrong number of expected arguments
094: try {
095: mockMethod.addExpectedParameters(new Object[] { "B" });
096: fail("Expected an IllegalArgumentException - to few arguments");
097: } catch (IllegalArgumentException e) {
098:
099: }
100:
101: }
102:
103: /**
104: * Test MockMethod for method with no arguments and void return
105: *
106: * @throws Throwable
107: * if TestCase error
108: */
109: public void testExecuteWithVoid() throws Throwable {
110: MockMethod mockMethod = new MockMethod(TestClass01.class,
111: "noArgsAndVoid");
112: mockMethod.setExpectedCalls(1);
113: mockMethod.execute(null);
114: mockMethod.verify();
115: mockMethod.reset();
116:
117: try {
118: mockMethod.addReturnValue(new Object());
119: fail("Expected an IllegalArgumentException - no return for void method");
120: } catch (IllegalArgumentException e) {
121:
122: }
123:
124: }
125:
126: /**
127: * Test wrong arguments to constructors
128: *
129: * @throws Throwable
130: */
131: public void testConstructors() throws Throwable {
132: // Correct
133: new MockMethod(TestClass01.class, new Class[] { String.class,
134: Integer.TYPE });
135: new MockMethod(TestClass01.class, "testCall", new Class[] {
136: String.class, Integer.TYPE });
137: new MockMethod(OverloadedTestClass.class, new Class[] {
138: String.class, Integer.TYPE });
139: new MockMethod(OverloadedTestClass.class, new Class[] {
140: String.class, Integer.TYPE, String.class });
141: new MockMethod(OverloadedTestClass.class, "testCall",
142: new Class[] { String.class, Integer.TYPE });
143: new MockMethod(
144: OverloadedTestClass.class,
145: "testCall",
146: new Class[] { String.class, Integer.TYPE, String.class });
147:
148: // Test with wrong args
149: try {
150: new MockMethod(TestClass01.class, "testCall", new Class[] {
151: String.class, Integer.TYPE, String.class });
152: fail("Expected NoSuchMethodException");
153: } catch (NoSuchMethodException e) {
154: }
155:
156: // Test with non-existing method
157: try {
158: new MockMethod(TestClass01.class, "testCallNOT_EXISTING",
159: new Class[] { String.class, Integer.TYPE });
160: fail("Expected NoSuchMethodException");
161: } catch (NoSuchMethodException e) {
162: }
163: // Test overloaded class
164: try {
165: new MockMethod(OverloadedTestClass.class, "testCall");
166: fail("Expected IllegalArgumentException");
167: } catch (IllegalArgumentException e) {
168: }
169:
170: }
171:
172: /**
173: * Test the setMock method for Exceptions
174: *
175: * @see MockMethod#addThrowableReturnValue(Throwable)
176: */
177: public void testAddThrowableReturnValue() throws Throwable {
178:
179: MockMethod mockMethod = new MockMethod(TestClass01.class,
180: "noArgs");
181: mockMethod.addReturnValue("1");
182: mockMethod.addThrowableReturnValue(new TestException());
183: mockMethod.addReturnValue("2");
184: Object retVal = mockMethod.execute(null);
185: assertEquals("1", retVal);
186: try {
187: mockMethod.execute(null);
188: fail("expected a TestException");
189: } catch (TestException e) {
190: }
191: retVal = mockMethod.execute(null);
192: assertEquals("2", retVal);
193: }
194:
195: private static class TestClass01 {
196:
197: private TestClass01(String a, int b) {
198:
199: }
200:
201: private String testCall(String a, int b) {
202: return "OK";
203: }
204:
205: private String noArgs() {
206: return "OK";
207: }
208:
209: private void noArgsAndVoid() {
210:
211: }
212: }
213:
214: private static class OverloadedTestClass {
215: private OverloadedTestClass(String a, int b) {
216:
217: }
218:
219: private OverloadedTestClass(String a, int b, String c) {
220:
221: }
222:
223: private String testCall(String a, int b) {
224: return "OK";
225: }
226:
227: private String testCall(String a, int b, String c) {
228: return "OK";
229: }
230: }
231:
232: private static class TestException extends Exception {
233:
234: }
235:
236: }
|