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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @author Boris V. Kuznetsov
23 package tests.api.javax.net;
25 import dalvik.annotation.TestTargetClass;
26 import dalvik.annotation.TestLevel;
27 import dalvik.annotation.TestTargetNew;
29 import java.io.IOException;
30 import java.net.InetAddress;
31 import java.net.ServerSocket;
32 import java.net.SocketException;
33 import javax.net.ServerSocketFactory;
35 import junit.framework.TestCase;
37 import tests.support.Support_PortManager;
41 * Tests for <code>ServerSocketFactory</code> class constructors and methods.
43 @TestTargetClass(ServerSocketFactory.class)
44 public class ServerSocketFactoryTest extends TestCase {
47 * @tests javax.net.SocketFactory#SocketFactory()
50 level = TestLevel.COMPLETE,
52 method = "ServerSocketFactory",
55 public void test_Constructor() {
57 ServerSocketFactory sf = new MyServerSocketFactory();
58 } catch (Exception e) {
59 fail("Unexpected exception " + e.toString());
64 * @tests javax.net.ServerSocketFactory#createServerSocket()
67 level = TestLevel.SUFFICIENT,
68 notes = "IOException checking missed",
69 method = "createServerSocket",
72 public final void test_createServerSocket_01() {
73 ServerSocketFactory sf = ServerSocketFactory.getDefault();
75 ServerSocket ss = sf.createServerSocket();
77 } catch (SocketException e) {
78 } catch (Exception e) {
84 * @tests javax.net.ServerSocketFactory#createServerSocket(int port)
87 level = TestLevel.COMPLETE,
89 method = "createServerSocket",
92 public final void test_createServerSocket_02() {
93 ServerSocketFactory sf = ServerSocketFactory.getDefault();
94 int portNumber = Support_PortManager.getNextPort();
97 ServerSocket ss = sf.createServerSocket(portNumber);
99 } catch (Exception ex) {
100 fail("Unexpected exception: " + ex);
104 sf.createServerSocket(portNumber);
105 fail("IOException wasn't thrown");
106 } catch (IOException ioe) {
108 } catch (Exception ex) {
109 fail(ex + " was thrown instead of IOException");
113 sf.createServerSocket(-1);
114 fail("IllegalArgumentException wasn't thrown");
115 } catch (IllegalArgumentException ioe) {
117 } catch (Exception ex) {
118 fail(ex + " was thrown instead of IllegalArgumentException");
123 * @tests javax.net.ServerSocketFactory#createServerSocket(int port, int backlog)
126 level = TestLevel.COMPLETE,
128 method = "createServerSocket",
129 args = {int.class, int.class}
131 public final void test_createServerSocket_03() {
132 ServerSocketFactory sf = ServerSocketFactory.getDefault();
133 int portNumber = Support_PortManager.getNextPort();
136 ServerSocket ss = sf.createServerSocket(portNumber, 0);
138 } catch (Exception ex) {
139 fail("Unexpected exception: " + ex);
143 sf.createServerSocket(portNumber, 0);
144 fail("IOException wasn't thrown");
145 } catch (IOException ioe) {
147 } catch (Exception ex) {
148 fail(ex + " was thrown instead of IOException");
152 sf.createServerSocket(65536, 0);
153 fail("IllegalArgumentException wasn't thrown");
154 } catch (IllegalArgumentException ioe) {
156 } catch (Exception ex) {
157 fail(ex + " was thrown instead of IllegalArgumentException");
162 * @tests javax.net.ServerSocketFactory#createServerSocket(int port, int backlog, InetAddress ifAddress)
165 level = TestLevel.COMPLETE,
167 method = "createServerSocket",
168 args = {int.class, int.class, InetAddress.class}
170 public final void test_createServerSocket_04() {
171 ServerSocketFactory sf = ServerSocketFactory.getDefault();
172 int portNumber = Support_PortManager.getNextPort();
175 ServerSocket ss = sf.createServerSocket(portNumber, 0, InetAddress.getLocalHost());
177 } catch (Exception ex) {
178 fail("Unexpected exception: " + ex);
182 sf.createServerSocket(portNumber, 0, InetAddress.getLocalHost());
183 fail("IOException wasn't thrown");
184 } catch (IOException ioe) {
186 } catch (Exception ex) {
187 fail(ex + " was thrown instead of IOException");
191 sf.createServerSocket(Integer.MAX_VALUE, 0, InetAddress.getLocalHost());
192 fail("IllegalArgumentException wasn't thrown");
193 } catch (IllegalArgumentException ioe) {
195 } catch (Exception ex) {
196 fail(ex + " was thrown instead of IllegalArgumentException");
201 * @tests javax.net.ServerSocketFactory#getDefault()
204 level = TestLevel.COMPLETE,
206 method = "getDefault",
209 public final void test_getDefault() {
210 ServerSocketFactory sf = ServerSocketFactory.getDefault();
213 s = sf.createServerSocket(0);
215 } catch (IOException e) {
218 s = sf.createServerSocket(0, 50);
220 } catch (IOException e) {
223 s = sf.createServerSocket(0, 50, InetAddress.getLocalHost());
225 } catch (IOException e) {
229 class MyServerSocketFactory extends ServerSocketFactory {
231 public MyServerSocketFactory() {
236 public ServerSocket createServerSocket(int port) throws IOException {
241 public ServerSocket createServerSocket(int port, int backlog)
247 public ServerSocket createServerSocket(int port, int backlog,
248 InetAddress address) throws IOException {