/external/apache-harmony/nio/src/test/java/common/org/apache/harmony/nio/tests/java/nio/channels/SelectionKeyTest.java

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk · Java · 321 lines · 197 code · 44 blank · 80 comment · 0 complexity · 4dcc104cc1fa594aa09828921eb40e0c MD5 · raw file

  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. package org.apache.harmony.nio.tests.java.nio.channels;
  17. import java.io.IOException;
  18. import java.net.InetSocketAddress;
  19. import java.net.ServerSocket;
  20. import java.nio.channels.CancelledKeyException;
  21. import java.nio.channels.SelectableChannel;
  22. import java.nio.channels.SelectionKey;
  23. import java.nio.channels.Selector;
  24. import java.nio.channels.SocketChannel;
  25. import junit.framework.TestCase;
  26. import tests.support.Support_PortManager;
  27. /*
  28. * Tests for SelectionKey and its default implementation
  29. */
  30. public class SelectionKeyTest extends TestCase {
  31. Selector selector;
  32. SocketChannel sc;
  33. SelectionKey selectionKey;
  34. private static String LOCAL_ADDR = "127.0.0.1";
  35. protected void setUp() throws Exception {
  36. super.setUp();
  37. selector = Selector.open();
  38. sc = SocketChannel.open();
  39. sc.configureBlocking(false);
  40. selectionKey = sc.register(selector, SelectionKey.OP_CONNECT);
  41. }
  42. protected void tearDown() throws Exception {
  43. selectionKey.cancel();
  44. selectionKey = null;
  45. selector.close();
  46. selector = null;
  47. super.tearDown();
  48. }
  49. static class MockSelectionKey extends SelectionKey {
  50. private int interestOps;
  51. MockSelectionKey(int ops) {
  52. interestOps = ops;
  53. }
  54. public void cancel() {
  55. // do nothing
  56. }
  57. public SelectableChannel channel() {
  58. return null;
  59. }
  60. public int interestOps() {
  61. return 0;
  62. }
  63. public SelectionKey interestOps(int operations) {
  64. return null;
  65. }
  66. public boolean isValid() {
  67. return true;
  68. }
  69. public int readyOps() {
  70. return interestOps;
  71. }
  72. public Selector selector() {
  73. return null;
  74. }
  75. }
  76. /**
  77. * @tests java.nio.channels.SelectionKey#attach(Object)
  78. */
  79. public void test_attach() {
  80. MockSelectionKey mockSelectionKey = new MockSelectionKey(SelectionKey.OP_ACCEPT);
  81. // no previous, return null
  82. Object o = new Object();
  83. Object check = mockSelectionKey.attach(o);
  84. assertNull(check);
  85. // null parameter is ok
  86. check = mockSelectionKey.attach(null);
  87. assertSame(o, check);
  88. check = mockSelectionKey.attach(o);
  89. assertNull(check);
  90. }
  91. /**
  92. * @tests java.nio.channels.SelectionKey#attachment()
  93. */
  94. public void test_attachment() {
  95. MockSelectionKey mockSelectionKey = new MockSelectionKey(SelectionKey.OP_ACCEPT);
  96. assertNull(mockSelectionKey.attachment());
  97. Object o = new Object();
  98. mockSelectionKey.attach(o);
  99. assertSame(o, mockSelectionKey.attachment());
  100. }
  101. /**
  102. * @tests java.nio.channels.SelectionKey#channel()
  103. */
  104. public void test_channel() {
  105. assertSame(sc, selectionKey.channel());
  106. // can be invoked even canceled
  107. selectionKey.cancel();
  108. assertSame(sc, selectionKey.channel());
  109. }
  110. /**
  111. * @tests java.nio.channels.SelectionKey#interestOps()
  112. */
  113. public void test_interestOps() {
  114. assertEquals(SelectionKey.OP_CONNECT, selectionKey.interestOps());
  115. }
  116. /**
  117. * @tests java.nio.channels.SelectionKey#interestOps(int)
  118. */
  119. public void test_interestOpsI() {
  120. selectionKey.interestOps(SelectionKey.OP_WRITE);
  121. assertEquals(SelectionKey.OP_WRITE, selectionKey.interestOps());
  122. try {
  123. selectionKey.interestOps(SelectionKey.OP_ACCEPT);
  124. fail("should throw IAE.");
  125. } catch (IllegalArgumentException ex) {
  126. // expected;
  127. }
  128. try {
  129. selectionKey.interestOps(~sc.validOps());
  130. fail("should throw IAE.");
  131. } catch (IllegalArgumentException ex) {
  132. // expected;
  133. }
  134. try {
  135. selectionKey.interestOps(-1);
  136. fail("should throw IAE.");
  137. } catch (IllegalArgumentException ex) {
  138. // expected;
  139. }
  140. }
  141. /**
  142. * @tests java.nio.channels.SelectionKey#isValid()
  143. */
  144. public void test_isValid() {
  145. assertTrue(selectionKey.isValid());
  146. }
  147. /**
  148. * @tests java.nio.channels.SelectionKey#isValid()
  149. */
  150. public void test_isValid_KeyCancelled() {
  151. selectionKey.cancel();
  152. assertFalse(selectionKey.isValid());
  153. }
  154. /**
  155. * @tests java.nio.channels.SelectionKey#isValid()
  156. */
  157. public void test_isValid_ChannelColsed() throws IOException {
  158. sc.close();
  159. assertFalse(selectionKey.isValid());
  160. }
  161. /**
  162. * @tests java.nio.channels.SelectionKey#isValid()
  163. */
  164. public void test_isValid_SelectorClosed() throws IOException {
  165. selector.close();
  166. assertFalse(selectionKey.isValid());
  167. }
  168. /**
  169. * @tests java.nio.channels.SelectionKey#isAcceptable()
  170. */
  171. public void test_isAcceptable() throws IOException {
  172. MockSelectionKey mockSelectionKey1 = new MockSelectionKey(SelectionKey.OP_ACCEPT);
  173. assertTrue(mockSelectionKey1.isAcceptable());
  174. MockSelectionKey mockSelectionKey2 = new MockSelectionKey(SelectionKey.OP_CONNECT);
  175. assertFalse(mockSelectionKey2.isAcceptable());
  176. }
  177. /**
  178. * @tests java.nio.channels.SelectionKey#isConnectable()
  179. */
  180. public void test_isConnectable() {
  181. MockSelectionKey mockSelectionKey1 = new MockSelectionKey(SelectionKey.OP_CONNECT);
  182. assertTrue(mockSelectionKey1.isConnectable());
  183. MockSelectionKey mockSelectionKey2 = new MockSelectionKey(SelectionKey.OP_ACCEPT);
  184. assertFalse(mockSelectionKey2.isConnectable());
  185. }
  186. /**
  187. * @tests java.nio.channels.SelectionKey#isReadable()
  188. */
  189. public void test_isReadable() {
  190. MockSelectionKey mockSelectionKey1 = new MockSelectionKey(SelectionKey.OP_READ);
  191. assertTrue(mockSelectionKey1.isReadable());
  192. MockSelectionKey mockSelectionKey2 = new MockSelectionKey(SelectionKey.OP_ACCEPT);
  193. assertFalse(mockSelectionKey2.isReadable());
  194. }
  195. /**
  196. * @tests java.nio.channels.SelectionKey#isWritable()
  197. */
  198. public void test_isWritable() {
  199. MockSelectionKey mockSelectionKey1 = new MockSelectionKey(SelectionKey.OP_WRITE);
  200. assertTrue(mockSelectionKey1.isWritable());
  201. MockSelectionKey mockSelectionKey2 = new MockSelectionKey(SelectionKey.OP_ACCEPT);
  202. assertFalse(mockSelectionKey2.isWritable());
  203. }
  204. /**
  205. * @tests java.nio.channels.SelectionKey#cancel()
  206. */
  207. public void test_cancel() {
  208. selectionKey.cancel();
  209. try {
  210. selectionKey.isAcceptable();
  211. fail("should throw CancelledKeyException.");
  212. } catch (CancelledKeyException ex) {
  213. // expected
  214. }
  215. try {
  216. selectionKey.isConnectable();
  217. fail("should throw CancelledKeyException.");
  218. } catch (CancelledKeyException ex) {
  219. // expected
  220. }
  221. try {
  222. selectionKey.isReadable();
  223. fail("should throw CancelledKeyException.");
  224. } catch (CancelledKeyException ex) {
  225. // expected
  226. }
  227. try {
  228. selectionKey.isWritable();
  229. fail("should throw CancelledKeyException.");
  230. } catch (CancelledKeyException ex) {
  231. // expected
  232. }
  233. try {
  234. selectionKey.readyOps();
  235. fail("should throw CancelledKeyException.");
  236. } catch (CancelledKeyException ex) {
  237. // expected
  238. }
  239. try {
  240. selectionKey.interestOps(SelectionKey.OP_CONNECT);
  241. fail("should throw CancelledKeyException.");
  242. } catch (CancelledKeyException ex) {
  243. // expected
  244. }
  245. try {
  246. selectionKey.interestOps();
  247. fail("should throw CancelledKeyException.");
  248. } catch (CancelledKeyException ex) {
  249. // expected
  250. }
  251. }
  252. /**
  253. * @tests java.nio.channels.SelectionKey#readyOps()
  254. */
  255. public void test_readyOps() throws IOException {
  256. int port = Support_PortManager.getNextPort();
  257. ServerSocket ss = new ServerSocket(port);
  258. try {
  259. sc.connect(new InetSocketAddress(LOCAL_ADDR, port));
  260. assertEquals(0, selectionKey.readyOps());
  261. assertFalse(selectionKey.isConnectable());
  262. selector.select();
  263. assertEquals(SelectionKey.OP_CONNECT, selectionKey.readyOps());
  264. } finally {
  265. ss.close();
  266. ss = null;
  267. }
  268. }
  269. /**
  270. * @tests java.nio.channels.SelectionKey#selector()
  271. */
  272. public void test_selector() {
  273. assertSame(selector, selectionKey.selector());
  274. selectionKey.cancel();
  275. assertSame(selector, selectionKey.selector());
  276. }
  277. }