Home | History | Annotate | Download | only in net
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 
     18 /**
     19 * @author Boris V. Kuznetsov
     20 * @version $Revision$
     21 */
     22 
     23 package tests.api.javax.net;
     24 
     25 import java.io.IOException;
     26 import java.net.InetAddress;
     27 import java.net.ServerSocket;
     28 import java.net.Socket;
     29 import java.net.SocketException;
     30 import java.net.UnknownHostException;
     31 
     32 import javax.net.SocketFactory;
     33 
     34 import junit.framework.TestCase;
     35 
     36 public class SocketFactoryTest extends TestCase {
     37 
     38     public void test_Constructor() throws Exception {
     39         new MySocketFactory();
     40     }
     41 
     42     public final void test_createSocket() throws Exception {
     43         SocketFactory sf = SocketFactory.getDefault();
     44 
     45         Socket s = sf.createSocket();
     46         assertNotNull(s);
     47         assertEquals(-1, s.getLocalPort());
     48         assertEquals(0, s.getPort());
     49 
     50         MySocketFactory msf = new MySocketFactory();
     51         try {
     52             msf.createSocket();
     53             fail("No expected SocketException");
     54         } catch (SocketException expected) {
     55         }
     56     }
     57 
     58     public final void test_createSocket_StringI() throws Exception {
     59         SocketFactory sf = SocketFactory.getDefault();
     60         int sport = new ServerSocket(0).getLocalPort();
     61         int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
     62 
     63         Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport);
     64         assertNotNull(s);
     65         assertTrue("Failed to create socket", s.getPort() == sport);
     66 
     67         try {
     68             sf.createSocket("1.2.3.4hello", sport);
     69             fail("UnknownHostException wasn't thrown");
     70         } catch (UnknownHostException expected) {
     71         }
     72 
     73         for (int i = 0; i < invalidPorts.length; i++) {
     74             try {
     75                 sf.createSocket(InetAddress.getLocalHost().getHostName(), invalidPorts[i]);
     76                 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
     77             } catch (IllegalArgumentException expected) {
     78             }
     79         }
     80 
     81         try {
     82             sf.createSocket(InetAddress.getLocalHost().getHostName(), s.getLocalPort());
     83             fail("IOException wasn't thrown");
     84         } catch (IOException expected) {
     85         }
     86 
     87         SocketFactory f = SocketFactory.getDefault();
     88         try {
     89             f.createSocket(InetAddress.getLocalHost().getHostName(), 8082);
     90             fail("IOException wasn't thrown ...");
     91         } catch (IOException expected) {
     92         }
     93     }
     94 
     95     public final void test_createSocket_InetAddressI() throws Exception {
     96         SocketFactory sf = SocketFactory.getDefault();
     97         int sport = new ServerSocket(0).getLocalPort();
     98         int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
     99 
    100         Socket s = sf.createSocket(InetAddress.getLocalHost(), sport);
    101         assertNotNull(s);
    102         assertTrue("Failed to create socket", s.getPort() == sport);
    103 
    104         for (int i = 0; i < invalidPorts.length; i++) {
    105             try {
    106                 sf.createSocket(InetAddress.getLocalHost(), invalidPorts[i]);
    107                 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
    108             } catch (IllegalArgumentException expected) {
    109             }
    110         }
    111 
    112         try {
    113             sf.createSocket(InetAddress.getLocalHost(), s.getLocalPort());
    114             fail("IOException wasn't thrown");
    115         } catch (IOException expected) {
    116         }
    117 
    118         SocketFactory f = SocketFactory.getDefault();
    119         try {
    120             f.createSocket(InetAddress.getLocalHost(), 8081);
    121             fail("IOException wasn't thrown ...");
    122         } catch (IOException expected) {
    123         }
    124     }
    125 
    126     public final void test_createSocket_InetAddressIInetAddressI() throws Exception {
    127         SocketFactory sf = SocketFactory.getDefault();
    128         int sport = new ServerSocket(0).getLocalPort();
    129         int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
    130 
    131         Socket s = sf.createSocket(InetAddress.getLocalHost(), sport,
    132                                    InetAddress.getLocalHost(), 0);
    133         assertNotNull(s);
    134         assertTrue("1: Failed to create socket", s.getPort() == sport);
    135         int portNumber = s.getLocalPort();
    136 
    137         for (int i = 0; i < invalidPorts.length; i++) {
    138             try {
    139               sf.createSocket(InetAddress.getLocalHost(), invalidPorts[i],
    140                               InetAddress.getLocalHost(), portNumber);
    141                 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
    142             } catch (IllegalArgumentException expected) {
    143             }
    144 
    145             try {
    146                 sf.createSocket(InetAddress.getLocalHost(), sport,
    147                                 InetAddress.getLocalHost(), invalidPorts[i]);
    148                 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
    149             } catch (IllegalArgumentException expected) {
    150             }
    151         }
    152 
    153         try {
    154             sf.createSocket(InetAddress.getLocalHost(), sport,
    155                             InetAddress.getLocalHost(), portNumber);
    156             fail("IOException wasn't thrown");
    157         } catch (IOException expected) {
    158         }
    159 
    160         SocketFactory f = SocketFactory.getDefault();
    161         try {
    162             f.createSocket(InetAddress.getLocalHost(), 8081, InetAddress.getLocalHost(), 8082);
    163             fail("IOException wasn't thrown ...");
    164         } catch (IOException expected) {
    165         }
    166     }
    167 
    168     /**
    169      * javax.net.SocketFactory#createSocket(String host, int port,
    170      *                                             InetAddress localHost, int localPort)
    171      */
    172     public final void test_createSocket_05() throws Exception {
    173         SocketFactory sf = SocketFactory.getDefault();
    174         int sport = new ServerSocket(0).getLocalPort();
    175         int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
    176 
    177         Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport,
    178                                    InetAddress.getLocalHost(), 0);
    179         assertNotNull(s);
    180         assertTrue("1: Failed to create socket", s.getPort() == sport);
    181 
    182         try {
    183             sf.createSocket("1.2.3.4hello", sport, InetAddress.getLocalHost(), 0);
    184             fail("UnknownHostException wasn't thrown");
    185         } catch (UnknownHostException expected) {
    186         }
    187 
    188         for (int i = 0; i < invalidPorts.length; i++) {
    189             try {
    190                 sf.createSocket(InetAddress.getLocalHost().getHostName(), invalidPorts[i],
    191                                 InetAddress.getLocalHost(), 0);
    192                 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
    193             } catch (IllegalArgumentException expected) {
    194             }
    195             try {
    196                 sf.createSocket(InetAddress.getLocalHost().getHostName(), sport,
    197                                 InetAddress.getLocalHost(), invalidPorts[i]);
    198                 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
    199             } catch (IllegalArgumentException expected) {
    200             }
    201         }
    202 
    203         try {
    204             sf.createSocket(InetAddress.getLocalHost().getHostName(), 8081, InetAddress.getLocalHost(), 8082);
    205             fail("IOException wasn't thrown ...");
    206         } catch (IOException expected) {
    207         }
    208     }
    209 
    210     /**
    211      * javax.net.SocketFactory#getDefault()
    212      */
    213     public final void test_getDefault() {
    214         SocketFactory sf = SocketFactory.getDefault();
    215         Socket s;
    216         try {
    217             s = sf.createSocket(InetAddress.getLocalHost().getHostName(), 8082);
    218             s.close();
    219         } catch (IOException e) {
    220         }
    221         try {
    222             s = sf.createSocket(InetAddress.getLocalHost().getHostName(), 8081, InetAddress.getLocalHost(), 8082);
    223             s.close();
    224         } catch (IOException e) {
    225         }
    226         try {
    227             s = sf.createSocket(InetAddress.getLocalHost(), 8081);
    228             s.close();
    229         } catch (IOException e) {
    230         }
    231         try {
    232             s = sf.createSocket(InetAddress.getLocalHost(), 8081, InetAddress.getLocalHost(), 8082);
    233             s.close();
    234         } catch (IOException e) {
    235         }
    236     }
    237 }
    238 
    239 class MySocketFactory extends SocketFactory {
    240 
    241     public MySocketFactory() {
    242         super();
    243     }
    244 
    245     @Override
    246     public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
    247         return null;
    248     }
    249 
    250     @Override
    251     public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
    252             throws IOException, UnknownHostException {
    253         return null;
    254     }
    255 
    256     @Override
    257     public Socket createSocket(InetAddress host, int port) throws IOException {
    258         return null;
    259      }
    260 
    261     @Override
    262     public Socket createSocket(InetAddress address, int port,
    263                                InetAddress localAddress, int localPort) throws IOException {
    264         return null;
    265      }
    266 
    267 }
    268