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.hc.client5.http;
29  
30  import java.io.IOException;
31  import java.net.InetAddress;
32  
33  import org.apache.hc.core5.http.HttpHost;
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * Unit tests for exceptions.
39   * Trivial, but it looks better in the Clover reports.
40   */
41  public class ConnectExceptionSupportTest {
42  
43      @Test
44      public void testConnectTimeoutExceptionFromNullMessageAndHost() {
45          final ConnectTimeoutException ctx = ConnectExceptionSupport.createConnectTimeoutException(null, null);
46          Assertions.assertEquals("Connect to remote endpoint timed out", ctx.getMessage());
47      }
48  
49      @Test
50      public void testConnectTimeoutExceptionFromCause() {
51          final IOException cause = new IOException("something awful");
52          final ConnectTimeoutException ctx = ConnectExceptionSupport.createConnectTimeoutException(cause, null);
53          Assertions.assertEquals("Connect to remote endpoint failed: something awful", ctx.getMessage());
54      }
55  
56      @Test
57      public void testConnectTimeoutExceptionFromCauseAndHost() {
58          final HttpHost target = new HttpHost("localhost");
59          final IOException cause = new IOException();
60          final ConnectTimeoutException ctx = ConnectExceptionSupport.createConnectTimeoutException(cause, target);
61          Assertions.assertEquals("Connect to http://localhost timed out", ctx.getMessage());
62      }
63  
64      @Test
65      public void testConnectTimeoutExceptionFromCauseHostAndRemoteAddress() throws Exception {
66          final HttpHost target = new HttpHost("localhost");
67          final InetAddress remoteAddress = InetAddress.getByAddress(new byte[] {1,2,3,4});
68          final IOException cause = new IOException();
69          final ConnectTimeoutException ctx = ConnectExceptionSupport.createConnectTimeoutException(cause, target, remoteAddress);
70          Assertions.assertEquals("Connect to http://localhost [/1.2.3.4] timed out", ctx.getMessage());
71      }
72  
73      @Test
74      public void testHttpHostConnectExceptionFromNullCause() {
75          final HttpHostConnectException ctx = ConnectExceptionSupport.createHttpHostConnectException(null, null,
76                  (InetAddress [])null);
77          Assertions.assertEquals("Connect to remote endpoint refused", ctx.getMessage());
78      }
79  
80      @Test
81      public void testHttpHostConnectExceptionFromCause() {
82          final IOException cause = new IOException("something awful");
83          final HttpHostConnectException ctx = ConnectExceptionSupport.createHttpHostConnectException(cause, null);
84          Assertions.assertEquals("Connect to remote endpoint failed: something awful", ctx.getMessage());
85      }
86  
87      @Test
88      public void testHttpHostConnectExceptionFromCauseAndHost() {
89          final HttpHost target = new HttpHost("localhost");
90          final IOException cause = new IOException();
91          final HttpHostConnectException ctx = ConnectExceptionSupport.createHttpHostConnectException(cause, target);
92          Assertions.assertEquals("Connect to http://localhost refused", ctx.getMessage());
93      }
94  
95      @Test
96      public void testHttpHostConnectExceptionFromCauseHostAndRemoteAddress() throws Exception {
97          final HttpHost target = new HttpHost("localhost");
98          final InetAddress remoteAddress1 = InetAddress.getByAddress(new byte[] {1,2,3,4});
99          final InetAddress remoteAddress2 = InetAddress.getByAddress(new byte[] {5,6,7,8});
100         final IOException cause = new IOException();
101         final HttpHostConnectException ctx = ConnectExceptionSupport.createHttpHostConnectException(cause, target,
102                 remoteAddress1, remoteAddress2);
103         Assertions.assertEquals("Connect to http://localhost [/1.2.3.4, /5.6.7.8] refused", ctx.getMessage());
104     }
105 
106 }