Source Code Cross Referenced for TestHttpConnection.java in  » Net » Apache-common-HttpClient » org » apache » commons » httpclient » 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 » Net » Apache common HttpClient » org.apache.commons.httpclient 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Header$
003:         * $Revision: 480424 $
004:         * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005:         * ====================================================================
006:         *
007:         *  Licensed to the Apache Software Foundation (ASF) under one or more
008:         *  contributor license agreements.  See the NOTICE file distributed with
009:         *  this work for additional information regarding copyright ownership.
010:         *  The ASF licenses this file to You under the Apache License, Version 2.0
011:         *  (the "License"); you may not use this file except in compliance with
012:         *  the License.  You may obtain a copy of the License at
013:         *
014:         *      http://www.apache.org/licenses/LICENSE-2.0
015:         *
016:         *  Unless required by applicable law or agreed to in writing, software
017:         *  distributed under the License is distributed on an "AS IS" BASIS,
018:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019:         *  See the License for the specific language governing permissions and
020:         *  limitations under the License.
021:         * ====================================================================
022:         *
023:         * This software consists of voluntary contributions made by many
024:         * individuals on behalf of the Apache Software Foundation.  For more
025:         * information on the Apache Software Foundation, please see
026:         * <http://www.apache.org/>.
027:         *
028:         * [Additional notices, if required by prior licensing conditions]
029:         *
030:         */
031:
032:        package org.apache.commons.httpclient;
033:
034:        import java.io.IOException;
035:        import java.io.InputStream;
036:        import java.io.OutputStream;
037:        import java.net.InetAddress;
038:        import java.net.Socket;
039:        import java.net.UnknownHostException;
040:
041:        import junit.framework.Test;
042:        import junit.framework.TestSuite;
043:
044:        import org.apache.commons.httpclient.methods.GetMethod;
045:        import org.apache.commons.httpclient.params.HttpConnectionParams;
046:        import org.apache.commons.httpclient.protocol.Protocol;
047:        import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
048:        import org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory;
049:
050:        /**
051:         *
052:         * Unit tests for {@link HttpConnection}.
053:         *
054:         * @author Sean C. Sullivan
055:         *
056:         * @version $Id: TestHttpConnection.java 480424 2006-11-29 05:56:49Z bayard $
057:         *
058:         */
059:        public class TestHttpConnection extends HttpClientTestBase {
060:
061:            // ------------------------------------------------------------ Constructor
062:            public TestHttpConnection(String testName) throws Exception {
063:                super (testName);
064:            }
065:
066:            // ------------------------------------------------------------------- Main
067:            public static void main(String args[]) {
068:                String[] testCaseName = { TestHttpConnection.class.getName() };
069:                junit.textui.TestRunner.main(testCaseName);
070:            }
071:
072:            // ------------------------------------------------------- TestCase Methods
073:
074:            public static Test suite() {
075:                return new TestSuite(TestHttpConnection.class);
076:            }
077:
078:            // ----------------------------------------------------------- Test Methods
079:
080:            public void testConstructThenClose() {
081:                this .server.setHttpService(new EchoService());
082:                HttpConnection conn = new HttpConnection(this .server
083:                        .getLocalAddress(), this .server.getLocalPort());
084:                conn.close();
085:                assertTrue(!conn.isOpen());
086:            }
087:
088:            public void testConnTimeoutRelease() {
089:                this .server.setHttpService(new EchoService());
090:                // create a custom protocol that will delay for 500 milliseconds
091:                Protocol testProtocol = new Protocol("timeout",
092:                        new DelayedProtocolSocketFactory(500, Protocol
093:                                .getProtocol("http").getSocketFactory()),
094:                        this .server.getLocalPort());
095:
096:                NoHostHttpConnectionManager connectionManager = new NoHostHttpConnectionManager();
097:                connectionManager.setConnection(new HttpConnection(this .server
098:                        .getLocalAddress(), this .server.getLocalPort(),
099:                        testProtocol));
100:                this .client.setHttpConnectionManager(connectionManager);
101:                client.getHostConfiguration().setHost(
102:                        this .server.getLocalAddress(),
103:                        this .server.getLocalPort(), testProtocol);
104:                client.getHttpConnectionManager().getParams()
105:                        .setConnectionTimeout(1);
106:
107:                try {
108:                    GetMethod get = new GetMethod();
109:                    client.executeMethod(get);
110:                    fail("Should have timed out");
111:                } catch (IOException e) {
112:                    /* should fail */
113:                    assertTrue(e instanceof  ConnectTimeoutException);
114:                    assertTrue(connectionManager.isConnectionReleased());
115:                }
116:            }
117:
118:            public void testConnTimeout() {
119:
120:                // create a custom protocol that will delay for 500 milliseconds
121:                Protocol testProtocol = new Protocol("timeout",
122:                        new DelayedProtocolSocketFactory(500, Protocol
123:                                .getProtocol("http").getSocketFactory()),
124:                        this .server.getLocalPort());
125:
126:                HttpConnection conn = new HttpConnection(this .server
127:                        .getLocalAddress(), this .server.getLocalPort(),
128:                        testProtocol);
129:                // 1 ms is short enough to make this fail
130:                conn.getParams().setConnectionTimeout(1);
131:                try {
132:                    conn.open();
133:                    fail("Should have timed out");
134:                } catch (IOException e) {
135:                    assertTrue(e instanceof  ConnectTimeoutException);
136:                    /* should fail */
137:                }
138:            }
139:
140:            public void testForIllegalStateExceptions() {
141:                HttpConnection conn = new HttpConnection(this .server
142:                        .getLocalAddress(), this .server.getLocalPort());
143:                try {
144:                    OutputStream out = conn.getRequestOutputStream();
145:                    fail("getRequestOutputStream did not throw the expected exception");
146:                } catch (IllegalStateException expected) {
147:                    // this exception is expected
148:                } catch (IOException ex) {
149:                    fail("getRequestOutputStream did not throw the expected exception");
150:                }
151:
152:                try {
153:                    OutputStream out = new ChunkedOutputStream(conn
154:                            .getRequestOutputStream());
155:                    fail("getRequestOutputStream(true) did not throw the expected exception");
156:                } catch (IllegalStateException expected) {
157:                    // this exception is expected
158:                } catch (IOException ex) {
159:                    fail("getRequestOutputStream(true) did not throw the expected exception");
160:                }
161:
162:                try {
163:                    InputStream in = conn.getResponseInputStream();
164:                    fail("getResponseInputStream() did not throw the expected exception");
165:                } catch (IllegalStateException expected) {
166:                    // this exception is expected
167:                } catch (IOException ex) {
168:                    fail("getResponseInputStream() did not throw the expected exception");
169:                }
170:
171:            }
172:
173:            /**
174:             * A ProtocolSocketFactory that delays before creating a socket.
175:             */
176:            class DelayedProtocolSocketFactory implements  ProtocolSocketFactory {
177:
178:                private int delay;
179:                private ProtocolSocketFactory realFactory;
180:
181:                public DelayedProtocolSocketFactory(int delay,
182:                        ProtocolSocketFactory realFactory) {
183:                    this .delay = delay;
184:                    this .realFactory = realFactory;
185:                }
186:
187:                public Socket createSocket(String host, int port,
188:                        InetAddress localAddress, int localPort)
189:                        throws IOException, UnknownHostException {
190:
191:                    synchronized (this ) {
192:                        try {
193:                            this .wait(delay);
194:                        } catch (InterruptedException e) {
195:                        }
196:                    }
197:                    return realFactory.createSocket(host, port, localAddress,
198:                            localPort);
199:                }
200:
201:                public Socket createSocket(final String host, final int port,
202:                        final InetAddress localAddress, final int localPort,
203:                        final HttpConnectionParams params) throws IOException,
204:                        UnknownHostException {
205:
206:                    if (params == null) {
207:                        throw new IllegalArgumentException(
208:                                "Parameters may not be null");
209:                    }
210:                    int timeout = params.getConnectionTimeout();
211:                    ControllerThreadSocketFactory.SocketTask task = new ControllerThreadSocketFactory.SocketTask() {
212:                        public void doit() throws IOException {
213:                            synchronized (this ) {
214:                                try {
215:                                    this .wait(delay);
216:                                } catch (InterruptedException e) {
217:                                }
218:                            }
219:                            setSocket(realFactory.createSocket(host, port,
220:                                    localAddress, localPort));
221:                        }
222:                    };
223:                    return ControllerThreadSocketFactory.createSocket(task,
224:                            timeout);
225:                }
226:
227:                public Socket createSocket(String host, int port)
228:                        throws IOException, UnknownHostException {
229:                    synchronized (this ) {
230:                        try {
231:                            this .wait(delay);
232:                        } catch (InterruptedException e) {
233:                        }
234:                    }
235:                    return realFactory.createSocket(host, port);
236:                }
237:
238:            }
239:
240:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.