OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / tests / api / javax / net / ServerSocketFactoryTest.java
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 dalvik.annotation.TestTargetClass;
26 import dalvik.annotation.TestLevel;
27 import dalvik.annotation.TestTargetNew;
28
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;
34
35 import junit.framework.TestCase;
36
37 import tests.support.Support_PortManager;
38
39
40 /**
41  * Tests for <code>ServerSocketFactory</code> class constructors and methods.
42  */
43 @TestTargetClass(ServerSocketFactory.class)
44 public class ServerSocketFactoryTest extends TestCase {
45
46     /**
47      * @tests javax.net.SocketFactory#SocketFactory()
48      */
49     @TestTargetNew(
50         level = TestLevel.COMPLETE,
51         notes = "",
52         method = "ServerSocketFactory",
53         args = {}
54     )
55     public void test_Constructor() {
56         try {
57             ServerSocketFactory sf = new MyServerSocketFactory();
58         } catch (Exception e) {
59             fail("Unexpected exception " + e.toString());
60         }
61     }
62
63     /**
64      * @tests javax.net.ServerSocketFactory#createServerSocket()
65      */
66     @TestTargetNew(
67         level = TestLevel.SUFFICIENT,
68         notes = "IOException checking missed",
69         method = "createServerSocket",
70         args = {}
71     )
72     public final void test_createServerSocket_01() {
73         ServerSocketFactory sf = ServerSocketFactory.getDefault();
74         try {
75             ServerSocket ss = sf.createServerSocket();
76             assertNotNull(ss);
77         } catch (SocketException e) {
78         } catch (Exception e) {
79             fail(e.toString());
80         }
81     }
82
83     /**
84      * @tests javax.net.ServerSocketFactory#createServerSocket(int port)
85      */
86     @TestTargetNew(
87         level = TestLevel.COMPLETE,
88         notes = "",
89         method = "createServerSocket",
90         args = {int.class}
91     )
92     public final void test_createServerSocket_02() {
93         ServerSocketFactory sf = ServerSocketFactory.getDefault();
94         int portNumber = Support_PortManager.getNextPort();
95
96         try {
97             ServerSocket ss = sf.createServerSocket(portNumber);
98             assertNotNull(ss);
99         } catch (Exception ex) {
100             fail("Unexpected exception: " + ex);
101         }
102
103         try {
104             sf.createServerSocket(portNumber);
105             fail("IOException wasn't thrown");
106         } catch (IOException ioe) {
107             //expected
108         } catch (Exception ex) {
109             fail(ex + " was thrown instead of IOException");
110         }
111
112         try {
113             sf.createServerSocket(-1);
114             fail("IllegalArgumentException wasn't thrown");
115         } catch (IllegalArgumentException ioe) {
116             //expected
117         } catch (Exception ex) {
118             fail(ex + " was thrown instead of IllegalArgumentException");
119         }
120     }
121
122     /**
123      * @tests javax.net.ServerSocketFactory#createServerSocket(int port, int backlog)
124      */
125     @TestTargetNew(
126         level = TestLevel.COMPLETE,
127         notes = "",
128         method = "createServerSocket",
129         args = {int.class, int.class}
130     )
131     public final void test_createServerSocket_03() {
132         ServerSocketFactory sf = ServerSocketFactory.getDefault();
133         int portNumber = Support_PortManager.getNextPort();
134
135         try {
136             ServerSocket ss = sf.createServerSocket(portNumber, 0);
137             assertNotNull(ss);
138         } catch (Exception ex) {
139             fail("Unexpected exception: " + ex);
140         }
141
142         try {
143             sf.createServerSocket(portNumber, 0);
144             fail("IOException wasn't thrown");
145         } catch (IOException ioe) {
146             //expected
147         } catch (Exception ex) {
148             fail(ex + " was thrown instead of IOException");
149         }
150
151         try {
152             sf.createServerSocket(65536, 0);
153             fail("IllegalArgumentException wasn't thrown");
154         } catch (IllegalArgumentException ioe) {
155             //expected
156         } catch (Exception ex) {
157             fail(ex + " was thrown instead of IllegalArgumentException");
158         }
159     }
160
161     /**
162      * @tests javax.net.ServerSocketFactory#createServerSocket(int port, int backlog, InetAddress ifAddress)
163      */
164     @TestTargetNew(
165         level = TestLevel.COMPLETE,
166         notes = "",
167         method = "createServerSocket",
168         args = {int.class, int.class, InetAddress.class}
169     )
170     public final void test_createServerSocket_04() {
171         ServerSocketFactory sf = ServerSocketFactory.getDefault();
172         int portNumber = Support_PortManager.getNextPort();
173
174         try {
175             ServerSocket ss = sf.createServerSocket(portNumber, 0, InetAddress.getLocalHost());
176             assertNotNull(ss);
177         } catch (Exception ex) {
178             fail("Unexpected exception: " + ex);
179         }
180
181         try {
182             sf.createServerSocket(portNumber, 0, InetAddress.getLocalHost());
183             fail("IOException wasn't thrown");
184         } catch (IOException ioe) {
185             //expected
186         } catch (Exception ex) {
187             fail(ex + " was thrown instead of IOException");
188         }
189
190         try {
191             sf.createServerSocket(Integer.MAX_VALUE, 0, InetAddress.getLocalHost());
192             fail("IllegalArgumentException wasn't thrown");
193         } catch (IllegalArgumentException ioe) {
194             //expected
195         } catch (Exception ex) {
196             fail(ex + " was thrown instead of IllegalArgumentException");
197         }
198     }
199
200     /**
201      * @tests javax.net.ServerSocketFactory#getDefault()
202      */
203     @TestTargetNew(
204         level = TestLevel.COMPLETE,
205         notes = "",
206         method = "getDefault",
207         args = {}
208     )
209     public final void test_getDefault() {
210         ServerSocketFactory sf = ServerSocketFactory.getDefault();
211         ServerSocket s;
212         try {
213             s = sf.createServerSocket(0);
214             s.close();
215         } catch (IOException e) {
216         }
217         try {
218             s = sf.createServerSocket(0, 50);
219             s.close();
220         } catch (IOException e) {
221         }
222         try {
223             s = sf.createServerSocket(0, 50, InetAddress.getLocalHost());
224             s.close();
225         } catch (IOException e) {
226         }
227     }
228 }
229 class MyServerSocketFactory extends ServerSocketFactory {
230
231     public MyServerSocketFactory() {
232         super();
233     }
234
235     @Override
236     public ServerSocket createServerSocket(int port) throws IOException {
237         return null;
238     }
239
240     @Override
241     public ServerSocket createServerSocket(int port, int backlog)
242             throws IOException {
243         return null;
244     }
245
246     @Override
247     public ServerSocket createServerSocket(int port, int backlog,
248             InetAddress address) throws IOException {
249         return null;
250     }
251 }