1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.apache.harmony.tests.java.nio.channels;
18
19import java.io.IOException;
20import java.net.InetSocketAddress;
21import java.net.ServerSocket;
22import java.nio.channels.CancelledKeyException;
23import java.nio.channels.SelectableChannel;
24import java.nio.channels.SelectionKey;
25import java.nio.channels.Selector;
26import java.nio.channels.SocketChannel;
27import junit.framework.TestCase;
28
29/*
30 * Tests for SelectionKey and its default implementation
31 */
32public class SelectionKeyTest extends TestCase {
33
34    Selector selector;
35
36    SocketChannel sc;
37
38    SelectionKey selectionKey;
39
40    private static String LOCAL_ADDR = "127.0.0.1";
41
42    protected void setUp() throws Exception {
43        super.setUp();
44        selector = Selector.open();
45        sc = SocketChannel.open();
46        sc.configureBlocking(false);
47        selectionKey = sc.register(selector, SelectionKey.OP_CONNECT);
48    }
49
50    protected void tearDown() throws Exception {
51        selectionKey.cancel();
52        selectionKey = null;
53        selector.close();
54        selector = null;
55        super.tearDown();
56    }
57
58    static class MockSelectionKey extends SelectionKey {
59        private int interestOps;
60
61        MockSelectionKey(int ops) {
62            interestOps = ops;
63        }
64
65        public void cancel() {
66            // do nothing
67        }
68
69        public SelectableChannel channel() {
70            return null;
71        }
72
73        public int interestOps() {
74            return 0;
75        }
76
77        public SelectionKey interestOps(int operations) {
78            return null;
79        }
80
81        public boolean isValid() {
82            return true;
83        }
84
85        public int readyOps() {
86            return interestOps;
87        }
88
89        public Selector selector() {
90            return null;
91        }
92    }
93
94    /**
95     * @tests java.nio.channels.SelectionKey#attach(Object)
96     */
97    public void test_attach() {
98        MockSelectionKey mockSelectionKey = new MockSelectionKey(SelectionKey.OP_ACCEPT);
99        // no previous, return null
100        Object o = new Object();
101        Object check = mockSelectionKey.attach(o);
102        assertNull(check);
103
104        // null parameter is ok
105        check = mockSelectionKey.attach(null);
106        assertSame(o, check);
107
108        check = mockSelectionKey.attach(o);
109        assertNull(check);
110    }
111
112    /**
113     * @tests java.nio.channels.SelectionKey#attachment()
114     */
115    public void test_attachment() {
116        MockSelectionKey mockSelectionKey = new MockSelectionKey(SelectionKey.OP_ACCEPT);
117        assertNull(mockSelectionKey.attachment());
118        Object o = new Object();
119        mockSelectionKey.attach(o);
120        assertSame(o, mockSelectionKey.attachment());
121    }
122
123    /**
124     * @tests java.nio.channels.SelectionKey#channel()
125     */
126    public void test_channel() {
127        assertSame(sc, selectionKey.channel());
128        // can be invoked even canceled
129        selectionKey.cancel();
130        assertSame(sc, selectionKey.channel());
131    }
132
133    /**
134     * @tests java.nio.channels.SelectionKey#interestOps()
135     */
136    public void test_interestOps() {
137        assertEquals(SelectionKey.OP_CONNECT, selectionKey.interestOps());
138    }
139
140    /**
141     * @tests java.nio.channels.SelectionKey#interestOps(int)
142     */
143    public void test_interestOpsI() {
144        selectionKey.interestOps(SelectionKey.OP_WRITE);
145        assertEquals(SelectionKey.OP_WRITE, selectionKey.interestOps());
146
147        try {
148            selectionKey.interestOps(SelectionKey.OP_ACCEPT);
149            fail("should throw IAE.");
150        } catch (IllegalArgumentException ex) {
151            // expected;
152        }
153
154        try {
155            selectionKey.interestOps(~sc.validOps());
156            fail("should throw IAE.");
157        } catch (IllegalArgumentException ex) {
158            // expected;
159        }
160        try {
161            selectionKey.interestOps(-1);
162            fail("should throw IAE.");
163        } catch (IllegalArgumentException ex) {
164            // expected;
165        }
166
167    }
168
169    /**
170     * @tests java.nio.channels.SelectionKey#isValid()
171     */
172    public void test_isValid() {
173        assertTrue(selectionKey.isValid());
174    }
175
176    /**
177     * @tests java.nio.channels.SelectionKey#isValid()
178     */
179    public void test_isValid_KeyCancelled() {
180        selectionKey.cancel();
181        assertFalse(selectionKey.isValid());
182    }
183
184    /**
185     * @tests java.nio.channels.SelectionKey#isValid()
186     */
187    public void test_isValid_ChannelColsed() throws IOException {
188        sc.close();
189        assertFalse(selectionKey.isValid());
190    }
191
192    /**
193     * @tests java.nio.channels.SelectionKey#isValid()
194     */
195    public void test_isValid_SelectorClosed() throws IOException {
196        selector.close();
197        assertFalse(selectionKey.isValid());
198    }
199
200    /**
201     * @tests java.nio.channels.SelectionKey#isAcceptable()
202     */
203    public void test_isAcceptable() throws IOException {
204        MockSelectionKey mockSelectionKey1 = new MockSelectionKey(SelectionKey.OP_ACCEPT);
205        assertTrue(mockSelectionKey1.isAcceptable());
206        MockSelectionKey mockSelectionKey2 = new MockSelectionKey(SelectionKey.OP_CONNECT);
207        assertFalse(mockSelectionKey2.isAcceptable());
208    }
209
210    /**
211     * @tests java.nio.channels.SelectionKey#isConnectable()
212     */
213    public void test_isConnectable() {
214        MockSelectionKey mockSelectionKey1 = new MockSelectionKey(SelectionKey.OP_CONNECT);
215        assertTrue(mockSelectionKey1.isConnectable());
216        MockSelectionKey mockSelectionKey2 = new MockSelectionKey(SelectionKey.OP_ACCEPT);
217        assertFalse(mockSelectionKey2.isConnectable());
218    }
219
220    /**
221     * @tests java.nio.channels.SelectionKey#isReadable()
222     */
223    public void test_isReadable() {
224        MockSelectionKey mockSelectionKey1 = new MockSelectionKey(SelectionKey.OP_READ);
225        assertTrue(mockSelectionKey1.isReadable());
226        MockSelectionKey mockSelectionKey2 = new MockSelectionKey(SelectionKey.OP_ACCEPT);
227        assertFalse(mockSelectionKey2.isReadable());
228    }
229
230    /**
231     * @tests java.nio.channels.SelectionKey#isWritable()
232     */
233    public void test_isWritable() {
234        MockSelectionKey mockSelectionKey1 = new MockSelectionKey(SelectionKey.OP_WRITE);
235        assertTrue(mockSelectionKey1.isWritable());
236        MockSelectionKey mockSelectionKey2 = new MockSelectionKey(SelectionKey.OP_ACCEPT);
237        assertFalse(mockSelectionKey2.isWritable());
238    }
239
240    /**
241     * @tests java.nio.channels.SelectionKey#cancel()
242     */
243    public void test_cancel() {
244        selectionKey.cancel();
245        try {
246            selectionKey.isAcceptable();
247            fail("should throw CancelledKeyException.");
248        } catch (CancelledKeyException ex) {
249            // expected
250        }
251        try {
252            selectionKey.isConnectable();
253            fail("should throw CancelledKeyException.");
254        } catch (CancelledKeyException ex) {
255            // expected
256        }
257        try {
258            selectionKey.isReadable();
259            fail("should throw CancelledKeyException.");
260        } catch (CancelledKeyException ex) {
261            // expected
262        }
263        try {
264            selectionKey.isWritable();
265            fail("should throw CancelledKeyException.");
266        } catch (CancelledKeyException ex) {
267            // expected
268        }
269
270        try {
271            selectionKey.readyOps();
272            fail("should throw CancelledKeyException.");
273        } catch (CancelledKeyException ex) {
274            // expected
275        }
276
277        try {
278            selectionKey.interestOps(SelectionKey.OP_CONNECT);
279            fail("should throw CancelledKeyException.");
280        } catch (CancelledKeyException ex) {
281            // expected
282        }
283
284        try {
285            selectionKey.interestOps();
286            fail("should throw CancelledKeyException.");
287        } catch (CancelledKeyException ex) {
288            // expected
289        }
290    }
291
292    /**
293     * @tests java.nio.channels.SelectionKey#readyOps()
294     */
295    public void test_readyOps() throws IOException {
296        ServerSocket ss = new ServerSocket(0);
297        try {
298            sc.connect(new InetSocketAddress(LOCAL_ADDR, ss.getLocalPort()));
299            assertEquals(0, selectionKey.readyOps());
300            assertFalse(selectionKey.isConnectable());
301            selector.select();
302            assertEquals(SelectionKey.OP_CONNECT, selectionKey.readyOps());
303        } finally {
304            ss.close();
305            ss = null;
306        }
307
308    }
309
310    /**
311     * @tests java.nio.channels.SelectionKey#selector()
312     */
313    public void test_selector() {
314        assertSame(selector, selectionKey.selector());
315        selectionKey.cancel();
316        assertSame(selector, selectionKey.selector());
317    }
318}
319