Source Code Cross Referenced for AbstractTestAbstractCactusTestCase.java in  » Testing » jakarta-cactus » org » apache » cactus » internal » 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 » jakarta cactus » org.apache.cactus.internal 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* 
002:         * ========================================================================
003:         * 
004:         * Copyright 2001-2004 The Apache Software Foundation.
005:         *
006:         * Licensed under the Apache License, Version 2.0 (the "License");
007:         * you may not use this file except in compliance with the License.
008:         * You may obtain a copy of the License at
009:         * 
010:         *   http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing, software
013:         * distributed under the License is distributed on an "AS IS" BASIS,
014:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015:         * See the License for the specific language governing permissions and
016:         * limitations under the License.
017:         * 
018:         * ========================================================================
019:         */
020:        package org.apache.cactus.internal;
021:
022:        import java.io.ByteArrayInputStream;
023:
024:        import java.net.URL;
025:
026:        import junit.framework.AssertionFailedError;
027:        import junit.framework.TestCase;
028:
029:        import org.apache.cactus.WebRequest;
030:        import org.apache.cactus.internal.client.ClientException;
031:        import org.apache.cactus.internal.client.ClientTestCaseCaller;
032:        import org.apache.cactus.internal.client.WebResponseObjectFactory;
033:        import org.apache.cactus.internal.client.connector.http.HttpProtocolHandler;
034:        import org.apache.cactus.internal.configuration.DefaultServletConfiguration;
035:        import org.apache.cactus.internal.util.JUnitVersionHelper;
036:        import org.apache.cactus.mock.MockHttpURLConnection;
037:
038:        /**
039:         * Test class that intercepts all exceptions (and assert them) coming from 
040:         * test case classes that inherits from it. This class is used to unit test 
041:         * the {@link AbstractCactusTestCase} class.
042:         *
043:         * @version $Id: AbstractTestAbstractCactusTestCase.java 238991 2004-05-22 11:34:50Z vmassol $
044:         */
045:        public abstract class AbstractTestAbstractCactusTestCase extends
046:                TestCase {
047:            /**
048:             * Override default method so that configuration checks are not run during
049:             * these unit tests.
050:             *
051:             * @exception Throwable if any exception is thrown during the test. Any
052:             *            exception will be displayed by the JUnit Test Runner
053:             * @see AbstractTestCase#runBare()
054:             */
055:            public void runBare() throws Throwable {
056:                runTest();
057:            }
058:
059:            /**
060:             * Intercepts running test cases to check for normal exceptions.
061:             *
062:             * @exception Throwable any error that occurred when calling the test method
063:             *            for the current test case.
064:             * @see AbstractTestCase#runTest()
065:             */
066:            protected void runTest() throws Throwable {
067:                ClientTestCaseCaller delegator = new ClientTestCaseCaller(this ,
068:                        this , new HttpProtocolHandler(
069:                                new DefaultServletConfiguration()));
070:
071:                try {
072:                    // Call the begin method
073:                    WebRequest request = new WebRequestImpl(
074:                            new DefaultServletConfiguration());
075:
076:                    delegator.callBeginMethod(request);
077:
078:                    // Create a mock HttpURLConnection as it is needed by HttpUnit
079:                    // for creating a WebResponse
080:                    MockHttpURLConnection connection = new MockHttpURLConnection(
081:                            new URL("http://something"));
082:
083:                    // Set the values expected by Http Unit. Note: only the test
084:                    // cases that have an end method with an HttpUnit WebReponse
085:                    // will use the HttpURLConnection.
086:                    connection.setExpectedGetHeaderField("HTTP/1.1 200 OK");
087:                    connection
088:                            .setExpectedGetInputStream(new ByteArrayInputStream(
089:                                    "".getBytes()));
090:
091:                    // Create a WebResponse object and call the end method
092:                    delegator.callEndMethod(request,
093:                            new WebResponseObjectFactory(connection));
094:                } catch (AssertionFailedError e) {
095:                    // Perform asserts
096:                    if (!verifyBeginMethodsOk(e.getMessage())
097:                            && !verifyEndMethodsOk(e.getMessage())) {
098:                        throw e;
099:                    }
100:                } catch (ClientException e) {
101:                    // Perform asserts
102:                    if (!verifyBeginMethodsOk(e.getMessage())
103:                            && !verifyEndMethodsOk(e.getMessage())) {
104:                        throw e;
105:                    }
106:                }
107:            }
108:
109:            /**
110:             * @param theTestName the test name to verify
111:             * @return true if the test name to verify corresponds to the currently
112:             *         executing test
113:             */
114:            private boolean checkName(String theTestName) {
115:                return JUnitVersionHelper.getTestCaseName(this ).equals(
116:                        theTestName);
117:            }
118:
119:            /**
120:             * Assert begin method tests.
121:             *
122:             * @param theMessage the error message from the exception
123:             * @return false is no test matches
124:             */
125:            private boolean verifyBeginMethodsOk(String theMessage) {
126:                // Test that when a begin method for a given test does not have the
127:                // correct return type (i.e. void), a
128:                // <code>AssertionFailedError</code> exception is returned.
129:                if (checkName("testBeginMethodBadReturnType")) {
130:                    assertEquals(
131:                            "The method "
132:                                    + "[beginBeginMethodBadReturnType] should return void and "
133:                                    + "not [java.lang.String]", theMessage);
134:
135:                    return true;
136:                }
137:
138:                // Test that when a begin method for a given test is not declared
139:                // public a <code>AssertionFailedError</code> exception is returned.
140:                if (checkName("testBeginMethodNotPublic")) {
141:                    assertEquals(
142:                            "Method [beginBeginMethodNotPublic] should be "
143:                                    + "declared public", theMessage);
144:
145:                    return true;
146:                }
147:
148:                // Test that when a begin method for a given test has the wrong
149:                // type of parameters, a <code>AssertionFailedError</code> exception
150:                // is returned.
151:                if (checkName("testBeginMethodBadParamType")) {
152:                    assertEquals(
153:                            "The method "
154:                                    + "[beginBeginMethodBadParamType] must accept "
155:                                    + "[org.apache.cactus.Request] as 1st parameter, but "
156:                                    + "found a [java.lang.String] parameter instead",
157:                            theMessage);
158:
159:                    return true;
160:                }
161:
162:                // Test that when a begin method for a given test has the wrong
163:                // number of parameters, a <code>AssertionFailedError</code>
164:                // exception is returned.
165:                if (checkName("testBeginMethodBadParamNumber")) {
166:                    assertEquals("The method "
167:                            + "[beginBeginMethodBadParamNumber] must have "
168:                            + "1 parameter(s), but 2 parameter(s) were found",
169:                            theMessage);
170:
171:                    return true;
172:                }
173:
174:                // Verify that the begin method with a
175:                // <code>WebRequest</code> parameter is called correctly.
176:                if (checkName("testBeginMethodOK")) {
177:                    assertEquals("beginBeginMethodOK", theMessage);
178:
179:                    return true;
180:                }
181:
182:                return false;
183:            }
184:
185:            /**
186:             * Assert end method tests.
187:             *
188:             * @param theMessage the error message from the exception
189:             * @return false is no test matches
190:             */
191:            private boolean verifyEndMethodsOk(String theMessage) {
192:                // Test that when an end method for a given test does not have the
193:                // correct return type (i.e. void), a
194:                // <code>AssertionFailedError</code> exception is returned.
195:                if (checkName("testEndMethodBadReturnType")) {
196:                    assertEquals(
197:                            "The method "
198:                                    + "[endEndMethodBadReturnType] should return void and "
199:                                    + "not [java.lang.String]", theMessage);
200:
201:                    return true;
202:                }
203:
204:                // Test that when an end method for a given test is not declared
205:                // public a <code>AssertionFailedError</code> exception is returned.
206:                if (checkName("testEndMethodNotPublic")) {
207:                    assertEquals("Method [endEndMethodNotPublic] should be "
208:                            + "declared public", theMessage);
209:
210:                    return true;
211:                }
212:
213:                // Test that when an end method for a given test has the wrong
214:                // type of parameters, a <code>AssertionFailedError</code> exception
215:                // is returned.
216:                if (checkName("testEndMethodBadParamType")) {
217:                    assertEquals("The method [endEndMethodBadParamType] "
218:                            + "has a bad parameter of type [java.lang.String]",
219:                            theMessage);
220:
221:                    return true;
222:                }
223:
224:                // Test that when an end method for a given test has the wrong
225:                // number of parameters, a <code>AssertionFailedError</code>
226:                // exception is returned.
227:                if (checkName("testEndMethodBadParamNumber")) {
228:                    assertEquals(
229:                            "The method [endEndMethodBadParamNumber] "
230:                                    + "must have 1 parameter(s), but 2 parameter(s) were found",
231:                            theMessage);
232:
233:                    return true;
234:                }
235:
236:                // Test that the end method is called correctly when it's signature
237:                // contains a <code>org.apache.cactus.WebResponse</code>
238:                // parameter.
239:                if (checkName("testEndMethodOK1")) {
240:                    assertEquals("endEndMethodOK1", theMessage);
241:
242:                    return true;
243:                }
244:
245:                // Test that the end method is called correctly when it's signature
246:                // contains a <code>com.meterware.httpunit.WebResponse</code>
247:                // parameter.
248:                if (checkName("testEndMethodOK2")) {
249:                    assertEquals("endEndMethodOK2", theMessage);
250:
251:                    return true;
252:                }
253:
254:                // Test that the deprecated end method with the
255:                // <code>HttpURLConnection</code> parameter can still be called
256:                // correctly.
257:                if (checkName("testEndMethodOK3")) {
258:                    assertEquals("endEndMethodOK3", theMessage);
259:
260:                    return true;
261:                }
262:
263:                return false;
264:            }
265:
266:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.