View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.conn;
29  
30  import org.apache.http.HttpHost;
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  import java.io.IOException;
35  import java.net.InetAddress;
36  
37  /**
38   * Unit tests for exceptions.
39   * Trivial, but it looks better in the Clover reports.
40   */
41  public class TestExceptions {
42  
43      @Test
44      public void testConnectTimeoutExceptionNullMessage() {
45          final ConnectTimeoutException ctx = new ConnectTimeoutException();
46          Assert.assertNull(ctx.getMessage());
47      }
48  
49      @Test
50      public void testConnectTimeoutExceptionSimpleMessage() {
51          final ConnectTimeoutException ctx = new ConnectTimeoutException("sample exception message");
52          Assert.assertEquals("sample exception message", ctx.getMessage());
53      }
54  
55      @Test
56      public void testConnectTimeoutExceptionFromNullCause() {
57          final ConnectTimeoutException ctx = new ConnectTimeoutException(null, null);
58          Assert.assertEquals("Connect to remote host timed out", ctx.getMessage());
59      }
60  
61      @Test
62      public void testConnectTimeoutExceptionFromCause() {
63          final IOException cause = new IOException("something awful");
64          final ConnectTimeoutException ctx = new ConnectTimeoutException(cause, null);
65          Assert.assertEquals("Connect to remote host failed: something awful", ctx.getMessage());
66      }
67  
68      @Test
69      public void testConnectTimeoutExceptionFromCauseAndHost() {
70          final HttpHost target = new HttpHost("localhost");
71          final IOException cause = new IOException();
72          final ConnectTimeoutException ctx = new ConnectTimeoutException(cause, target);
73          Assert.assertEquals("Connect to localhost timed out", ctx.getMessage());
74      }
75  
76      @Test
77      public void testConnectTimeoutExceptionFromCauseHostAndRemoteAddress() throws Exception {
78          final HttpHost target = new HttpHost("localhost");
79          final InetAddress remoteAddress = InetAddress.getByAddress(new byte[] {1,2,3,4});
80          final IOException cause = new IOException();
81          final ConnectTimeoutException ctx = new ConnectTimeoutException(cause, target, remoteAddress);
82          Assert.assertEquals("Connect to localhost [/1.2.3.4] timed out", ctx.getMessage());
83      }
84  
85      @Test
86      public void testHttpHostConnectExceptionFromNullCause() {
87          final HttpHostConnectException ctx = new HttpHostConnectException(null, null,
88                  (InetAddress [])null);
89          Assert.assertEquals("Connect to remote host refused", ctx.getMessage());
90      }
91  
92      @Test
93      public void testHttpHostConnectExceptionFromCause() {
94          final IOException cause = new IOException("something awful");
95          final HttpHostConnectException ctx = new HttpHostConnectException(cause, null);
96          Assert.assertEquals("Connect to remote host failed: something awful", ctx.getMessage());
97      }
98  
99      @Test
100     public void testHttpHostConnectExceptionFromCauseAndHost() {
101         final HttpHost target = new HttpHost("localhost");
102         final IOException cause = new IOException();
103         final HttpHostConnectException ctx = new HttpHostConnectException(cause, target);
104         Assert.assertEquals("Connect to localhost refused", ctx.getMessage());
105     }
106 
107     @Test
108     public void testHttpHostConnectExceptionFromCauseHostAndRemoteAddress() throws Exception {
109         final HttpHost target = new HttpHost("localhost");
110         final InetAddress remoteAddress1 = InetAddress.getByAddress(new byte[] {1,2,3,4});
111         final InetAddress remoteAddress2 = InetAddress.getByAddress(new byte[] {5,6,7,8});
112         final IOException cause = new IOException();
113         final HttpHostConnectException ctx = new HttpHostConnectException(cause, target,
114                 remoteAddress1, remoteAddress2);
115         Assert.assertEquals("Connect to localhost [/1.2.3.4, /5.6.7.8] refused", ctx.getMessage());
116     }
117 
118     @Test
119     public void testConnectionPoolTimeoutException() {
120         final String msg = "sample exception message";
121         ConnectionPoolTimeoutException cptx = new ConnectionPoolTimeoutException(msg);
122         Assert.assertFalse(!cptx.toString().contains(msg));
123         Assert.assertSame(msg, cptx.getMessage());
124 
125         cptx = new ConnectionPoolTimeoutException();
126         Assert.assertNull(cptx.getMessage());
127     }
128 
129 }