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 org.apache.harmony.tests.javax.net;
     24 
     25 import java.io.IOException;
     26 import java.net.InetAddress;
     27 import java.net.ServerSocket;
     28 import java.net.SocketException;
     29 import javax.net.ServerSocketFactory;
     30 
     31 import junit.framework.TestCase;
     32 
     33 public class ServerSocketFactoryTest extends TestCase {
     34 
     35     public void test_Constructor() {
     36         ServerSocketFactory sf = new MyServerSocketFactory();
     37     }
     38 
     39     public final void test_createServerSocket() throws Exception {
     40         ServerSocketFactory sf = ServerSocketFactory.getDefault();
     41         ServerSocket ss = sf.createServerSocket();
     42         assertNotNull(ss);
     43         ss.close();
     44     }
     45 
     46     public final void test_createServerSocket_I() throws Exception {
     47         ServerSocketFactory sf = ServerSocketFactory.getDefault();
     48         ServerSocket ss = sf.createServerSocket(0);
     49         assertNotNull(ss);
     50 
     51         try {
     52             sf.createServerSocket(ss.getLocalPort());
     53             fail("IOException wasn't thrown");
     54         } catch (IOException expected) {
     55         }
     56 
     57         ss.close();
     58 
     59         try {
     60             sf.createServerSocket(-1);
     61             fail("IllegalArgumentException wasn't thrown");
     62         } catch (IllegalArgumentException expected) {
     63         }
     64     }
     65 
     66     public final void test_createServerSocket_II() throws Exception {
     67         ServerSocketFactory sf = ServerSocketFactory.getDefault();
     68         ServerSocket ss = sf.createServerSocket(0, 0);
     69         assertNotNull(ss);
     70 
     71         try {
     72             sf.createServerSocket(ss.getLocalPort(), 0);
     73             fail("IOException wasn't thrown");
     74         } catch (IOException expected) {
     75         }
     76 
     77         ss.close();
     78 
     79         try {
     80             sf.createServerSocket(65536, 0);
     81             fail("IllegalArgumentException wasn't thrown");
     82         } catch (IllegalArgumentException expected) {
     83         }
     84     }
     85 
     86     public final void test_createServerSocket_IIInetAddress() throws Exception {
     87         ServerSocketFactory sf = ServerSocketFactory.getDefault();
     88 
     89         ServerSocket ss = sf.createServerSocket(0, 0, InetAddress.getLocalHost());
     90         assertNotNull(ss);
     91 
     92         try {
     93             sf.createServerSocket(ss.getLocalPort(), 0, InetAddress.getLocalHost());
     94             fail("IOException wasn't thrown");
     95         } catch (IOException expected) {
     96         }
     97 
     98         ss.close();
     99 
    100         try {
    101             sf.createServerSocket(Integer.MAX_VALUE, 0, InetAddress.getLocalHost());
    102             fail("IllegalArgumentException wasn't thrown");
    103         } catch (IllegalArgumentException expected) {
    104         }
    105     }
    106 }
    107 class MyServerSocketFactory extends ServerSocketFactory {
    108 
    109     public MyServerSocketFactory() {
    110         super();
    111     }
    112 
    113     @Override
    114     public ServerSocket createServerSocket(int port) throws IOException {
    115         return null;
    116     }
    117 
    118     @Override
    119     public ServerSocket createServerSocket(int port, int backlog)
    120             throws IOException {
    121         return null;
    122     }
    123 
    124     @Override
    125     public ServerSocket createServerSocket(int port, int backlog,
    126             InetAddress address) throws IOException {
    127         return null;
    128     }
    129 }
    130