Source Code Cross Referenced for MockMethodTestCase.java in  » Testing » Surrogate » net » sf » surrogate » core » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » Surrogate » net.sf.surrogate.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.