1+ /*
2+ * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+ *
5+ * This code is free software; you can redistribute it and/or modify it
6+ * under the terms of the GNU General Public License version 2 only, as
7+ * published by the Free Software Foundation.
8+ *
9+ * This code is distributed in the hope that it will be useful, but WITHOUT
10+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+ * version 2 for more details (a copy is included in the LICENSE file that
13+ * accompanied this code).
14+ *
15+ * You should have received a copy of the GNU General Public License version
16+ * 2 along with this work; if not, write to the Free Software Foundation,
17+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+ *
19+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+ * or visit www.oracle.com if you need additional information or have any
21+ * questions.
22+ */
23+
24+ /*
25+ * @test
26+ * @bug 8254692
27+ * @summary Basic test for java.nio.channels.spi.SelectorProvider.java default implementation
28+ * @run testng TestDefaultImplementation
29+ */
30+
31+ import org .testng .annotations .Test ;
32+
33+ import java .io .IOException ;
34+ import java .net .ProtocolFamily ;
35+ import java .nio .channels .*;
36+ import java .nio .channels .spi .AbstractSelector ;
37+ import java .nio .channels .spi .SelectorProvider ;
38+
39+ import static org .testng .Assert .assertNull ;
40+ import static org .testng .Assert .assertThrows ;
41+
42+ public class TestDefaultImplementation {
43+ static final Class <UnsupportedOperationException > UOE = UnsupportedOperationException .class ;
44+ static final Class <NullPointerException > NPE = NullPointerException .class ;
45+ static final ProtocolFamily BAD_PF = () -> "BAD_PROTOCOL_FAMILY" ;
46+
47+ @ Test
48+ public void testSelectorProvider () throws IOException {
49+ CustomSelectorProviderImpl customSpi = new CustomSelectorProviderImpl ();
50+
51+ assertNull (customSpi .inheritedChannel ());
52+
53+ assertThrows (NPE , () -> customSpi .openSocketChannel (null ));
54+ assertThrows (NPE , () -> customSpi .openServerSocketChannel (null ));
55+
56+ assertThrows (UOE , () -> customSpi .openSocketChannel (BAD_PF ));
57+ assertThrows (UOE , () -> customSpi .openServerSocketChannel (BAD_PF ));
58+ }
59+
60+ // A concrete subclass of SelectorProvider, in order to test the
61+ // default java.nio.channels.spi.SelectorProvider implementation
62+ static class CustomSelectorProviderImpl extends SelectorProvider {
63+ @ Override public DatagramChannel openDatagramChannel () { return null ; }
64+ @ Override public DatagramChannel openDatagramChannel (ProtocolFamily family ) { return null ; }
65+ @ Override public Pipe openPipe () { return null ; }
66+ @ Override public AbstractSelector openSelector () { return null ; }
67+ @ Override public ServerSocketChannel openServerSocketChannel () { return null ; }
68+ @ Override public SocketChannel openSocketChannel () { return null ; }
69+ }
70+ }
0 commit comments