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.SocketException; 29 import javax.net.ServerSocketFactory; 30 31 import junit.framework.TestCase; 32 33 import tests.support.Support_PortManager; 34 35 36 /** 37 * Tests for <code>ServerSocketFactory</code> class constructors and methods. 38 */ 39 public class ServerSocketFactoryTest extends TestCase { 40 41 /** 42 * javax.net.SocketFactory#SocketFactory() 43 */ 44 public void test_Constructor() { 45 try { 46 ServerSocketFactory sf = new MyServerSocketFactory(); 47 } catch (Exception e) { 48 fail("Unexpected exception " + e.toString()); 49 } 50 } 51 52 /** 53 * javax.net.ServerSocketFactory#createServerSocket() 54 */ 55 public final void test_createServerSocket_01() { 56 ServerSocketFactory sf = ServerSocketFactory.getDefault(); 57 try { 58 ServerSocket ss = sf.createServerSocket(); 59 assertNotNull(ss); 60 } catch (SocketException e) { 61 } catch (Exception e) { 62 fail(e.toString()); 63 } 64 } 65 66 /** 67 * javax.net.ServerSocketFactory#createServerSocket(int port) 68 */ 69 public final void test_createServerSocket_02() { 70 ServerSocketFactory sf = ServerSocketFactory.getDefault(); 71 int portNumber = Support_PortManager.getNextPort(); 72 73 try { 74 ServerSocket ss = sf.createServerSocket(portNumber); 75 assertNotNull(ss); 76 } catch (Exception ex) { 77 fail("Unexpected exception: " + ex); 78 } 79 80 try { 81 sf.createServerSocket(portNumber); 82 fail("IOException wasn't thrown"); 83 } catch (IOException ioe) { 84 //expected 85 } catch (Exception ex) { 86 fail(ex + " was thrown instead of IOException"); 87 } 88 89 try { 90 sf.createServerSocket(-1); 91 fail("IllegalArgumentException wasn't thrown"); 92 } catch (IllegalArgumentException ioe) { 93 //expected 94 } catch (Exception ex) { 95 fail(ex + " was thrown instead of IllegalArgumentException"); 96 } 97 } 98 99 /** 100 * javax.net.ServerSocketFactory#createServerSocket(int port, int backlog) 101 */ 102 public final void test_createServerSocket_03() { 103 ServerSocketFactory sf = ServerSocketFactory.getDefault(); 104 int portNumber = Support_PortManager.getNextPort(); 105 106 try { 107 ServerSocket ss = sf.createServerSocket(portNumber, 0); 108 assertNotNull(ss); 109 } catch (Exception ex) { 110 fail("Unexpected exception: " + ex); 111 } 112 113 try { 114 sf.createServerSocket(portNumber, 0); 115 fail("IOException wasn't thrown"); 116 } catch (IOException ioe) { 117 //expected 118 } catch (Exception ex) { 119 fail(ex + " was thrown instead of IOException"); 120 } 121 122 try { 123 sf.createServerSocket(65536, 0); 124 fail("IllegalArgumentException wasn't thrown"); 125 } catch (IllegalArgumentException ioe) { 126 //expected 127 } catch (Exception ex) { 128 fail(ex + " was thrown instead of IllegalArgumentException"); 129 } 130 } 131 132 /** 133 * javax.net.ServerSocketFactory#createServerSocket(int port, int backlog, InetAddress ifAddress) 134 */ 135 public final void test_createServerSocket_04() { 136 ServerSocketFactory sf = ServerSocketFactory.getDefault(); 137 int portNumber = Support_PortManager.getNextPort(); 138 139 try { 140 ServerSocket ss = sf.createServerSocket(portNumber, 0, InetAddress.getLocalHost()); 141 assertNotNull(ss); 142 } catch (Exception ex) { 143 fail("Unexpected exception: " + ex); 144 } 145 146 try { 147 sf.createServerSocket(portNumber, 0, InetAddress.getLocalHost()); 148 fail("IOException wasn't thrown"); 149 } catch (IOException ioe) { 150 //expected 151 } catch (Exception ex) { 152 fail(ex + " was thrown instead of IOException"); 153 } 154 155 try { 156 sf.createServerSocket(Integer.MAX_VALUE, 0, InetAddress.getLocalHost()); 157 fail("IllegalArgumentException wasn't thrown"); 158 } catch (IllegalArgumentException ioe) { 159 //expected 160 } catch (Exception ex) { 161 fail(ex + " was thrown instead of IllegalArgumentException"); 162 } 163 } 164 165 /** 166 * javax.net.ServerSocketFactory#getDefault() 167 */ 168 public final void test_getDefault() { 169 ServerSocketFactory sf = ServerSocketFactory.getDefault(); 170 ServerSocket s; 171 try { 172 s = sf.createServerSocket(0); 173 s.close(); 174 } catch (IOException e) { 175 } 176 try { 177 s = sf.createServerSocket(0, 50); 178 s.close(); 179 } catch (IOException e) { 180 } 181 try { 182 s = sf.createServerSocket(0, 50, InetAddress.getLocalHost()); 183 s.close(); 184 } catch (IOException e) { 185 } 186 } 187 } 188 class MyServerSocketFactory extends ServerSocketFactory { 189 190 public MyServerSocketFactory() { 191 super(); 192 } 193 194 @Override 195 public ServerSocket createServerSocket(int port) throws IOException { 196 return null; 197 } 198 199 @Override 200 public ServerSocket createServerSocket(int port, int backlog) 201 throws IOException { 202 return null; 203 } 204 205 @Override 206 public ServerSocket createServerSocket(int port, int backlog, 207 InetAddress address) throws IOException { 208 return null; 209 } 210 } 211