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
18package libcore.java.nio.channels;
19
20import java.io.IOException;
21import java.net.InetSocketAddress;
22import java.net.ServerSocket;
23import java.nio.channels.ClosedByInterruptException;
24import java.nio.channels.ServerSocketChannel;
25import java.nio.channels.SocketChannel;
26import java.nio.channels.spi.SelectorProvider;
27import junit.framework.TestCase;
28
29public class OldServerSocketChannelTest extends TestCase {
30
31    private static final int TIME_UNIT = 200;
32
33    private ServerSocketChannel serverChannel;
34
35    private SocketChannel clientChannel;
36
37    protected void setUp() throws Exception {
38        super.setUp();
39        this.serverChannel = ServerSocketChannel.open();
40        this.clientChannel = SocketChannel.open();
41    }
42
43    protected void tearDown() throws Exception {
44        if (null != this.serverChannel) {
45            try {
46                this.serverChannel.close();
47            } catch (Exception e) {
48                //ignore
49            }
50
51        }
52        if (null != this.clientChannel) {
53            try {
54                this.clientChannel.close();
55            } catch (Exception e) {
56                //ignore
57            }
58        }
59        super.tearDown();
60    }
61
62    // -------------------------------------------------------------------
63    // Test for methods in abstract class.
64    // -------------------------------------------------------------------
65    public void testConstructor() throws IOException {
66        ServerSocketChannel channel =
67                SelectorProvider.provider().openServerSocketChannel();
68        assertNotNull(channel);
69        assertSame(SelectorProvider.provider(),channel.provider());
70    }
71
72    public void testIsOpen() throws Exception {
73        assertTrue(this.serverChannel.isOpen());
74        this.serverChannel.close();
75        assertFalse(this.serverChannel.isOpen());
76    }
77
78    public void test_accept_Block_NoConnect_interrupt() throws IOException {
79        assertTrue(this.serverChannel.isBlocking());
80        ServerSocket gotSocket = this.serverChannel.socket();
81        gotSocket.bind(null);
82
83        class MyThread extends Thread {
84            public String errMsg = null;
85            public void run() {
86                try {
87                    serverChannel.accept();
88                    errMsg = "should throw ClosedByInterruptException";
89                } catch (ClosedByInterruptException e) {
90                    // expected
91                } catch (Exception e) {
92                    errMsg = "caught wrong Exception: " + e.getClass() + ": " +
93                            e.getMessage();
94                }
95            }
96        }
97        MyThread thread = new MyThread();
98        thread.start();
99        try {
100            Thread.currentThread().sleep(TIME_UNIT);
101            thread.interrupt();
102        } catch (InterruptedException e) {
103            fail("Should not throw a InterruptedException");
104        }
105        if (thread.errMsg != null) {
106            fail(thread.errMsg);
107        }
108    }
109}
110