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 */ 21 22 package javax.net; 23 24 import java.io.IOException; 25 import java.net.InetAddress; 26 import java.net.ServerSocket; 27 import java.net.SocketException; 28 import java.net.UnknownHostException; 29 30 import junit.framework.TestCase; 31 32 33 /** 34 * Tests for <code>ServerSocketFactory</code> class constructors and methods. 35 */ 36 37 public class ServerSocketFactoryTest extends TestCase { 38 /* 39 * Class under test for java.net.ServerSocket createServerSocket() 40 */ testCreateServerSocket()41 public final void testCreateServerSocket() { 42 ServerSocketFactory sf = new MyServerSocketFactory(); 43 try { 44 sf.createServerSocket(); 45 fail("No expected SocketException"); 46 } catch (SocketException e) { 47 } catch (IOException e) { 48 fail(e.toString()); 49 } 50 } 51 52 /* 53 * Class under test for javax.net.ServerSocketFactory getDefault() 54 */ testGetDefault()55 public final void testGetDefault() { 56 ServerSocketFactory sf = ServerSocketFactory.getDefault(); 57 ServerSocket s; 58 if (!(sf instanceof DefaultServerSocketFactory)) { 59 fail("Incorrect class instance"); 60 } 61 try { 62 s = sf.createServerSocket(0); 63 s.close(); 64 } catch (IOException e) { 65 } 66 try { 67 s = sf.createServerSocket(0, 50); 68 s.close(); 69 } catch (IOException e) { 70 } 71 try { 72 s = sf.createServerSocket(0, 50, InetAddress.getLocalHost()); 73 s.close(); 74 } catch (IOException e) { 75 } 76 } 77 } 78 class MyServerSocketFactory extends ServerSocketFactory { 79 @Override createServerSocket(int port)80 public ServerSocket createServerSocket(int port) throws IOException, UnknownHostException { 81 throw new IOException(); 82 } 83 84 @Override createServerSocket(int port, int backlog)85 public ServerSocket createServerSocket(int port, int backlog) 86 throws IOException, UnknownHostException { 87 throw new IOException(); 88 } 89 90 @Override createServerSocket(int port, int backlog, InetAddress ifAddress)91 public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException { 92 throw new IOException(); 93 } 94 } 95