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.nio.channels.ClosedChannelException;
21import java.nio.channels.SelectableChannel;
22import java.nio.channels.SelectionKey;
23import java.nio.channels.Selector;
24import java.nio.channels.spi.SelectorProvider;
25import junit.framework.TestCase;
26
27/*
28 * Tests for SelectableChannel
29 */
30public class SelectableChannelTest extends TestCase {
31
32    /**
33     * @tests SelectableChannel#register(Selector, int)
34     */
35    public void test_register_LSelectorI() throws IOException {
36        MockSelectableChannel msc = new MockSelectableChannel();
37        // Verify that calling register(Selector, int) leads to the method
38        // register(Selector, int, Object) being called with a null value
39        // for the third argument.
40        msc.register(Selector.open(), SelectionKey.OP_ACCEPT);
41        assertTrue(msc.isCalled);
42    }
43
44    private class MockSelectableChannel extends SelectableChannel {
45
46        private boolean isCalled = false;
47
48        public Object blockingLock() {
49            return null;
50        }
51
52        public SelectableChannel configureBlocking(boolean block)
53                throws IOException {
54            return null;
55        }
56
57        public boolean isBlocking() {
58            return false;
59        }
60
61        public boolean isRegistered() {
62            return false;
63        }
64
65        public SelectionKey keyFor(Selector sel) {
66            return null;
67        }
68
69        public SelectorProvider provider() {
70            return null;
71        }
72
73        public SelectionKey register(Selector sel, int ops, Object att)
74                throws ClosedChannelException {
75            if (null == att) {
76                isCalled = true;
77            }
78            return null;
79        }
80
81        public int validOps() {
82            return 0;
83        }
84
85        protected void implCloseChannel() throws IOException {
86            // empty
87        }
88    }
89}
90