jdk/test/java/net/NetworkInterface/NetworkInterfaceStreamTest.java
changeset 31469 92cc72d2a11a
child 32835 8bfe2ea5617d
equal deleted inserted replaced
31468:6cd3ae8de51c 31469:92cc72d2a11a
       
     1 /*
       
     2  * Copyright (c) 2015, 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 /* @test
       
    25  * @bug 8081678
       
    26  * @summary Tests for stream returning methods
       
    27  * @library ../../util/stream/bootlib
       
    28  * @build java.util.stream.OpTestCase
       
    29  * @run testng/othervm NetworkInterfaceStreamTest
       
    30  * @run testng/othervm -Djava.net.preferIPv4Stack=true NetworkInterfaceStreamTest
       
    31  */
       
    32 
       
    33 import org.testng.annotations.Test;
       
    34 
       
    35 import java.net.InetAddress;
       
    36 import java.net.NetworkInterface;
       
    37 import java.net.SocketException;
       
    38 import java.util.ArrayList;
       
    39 import java.util.Collection;
       
    40 import java.util.Collections;
       
    41 import java.util.function.Supplier;
       
    42 import java.util.stream.OpTestCase;
       
    43 import java.util.stream.Stream;
       
    44 import java.util.stream.TestData;
       
    45 
       
    46 public class NetworkInterfaceStreamTest extends OpTestCase {
       
    47 
       
    48     @Test
       
    49     public void testNetworkInterfaces() throws SocketException {
       
    50         Supplier<Stream<NetworkInterface>> ss = () -> {
       
    51             try {
       
    52                 return NetworkInterface.networkInterfaces();
       
    53             }
       
    54             catch (SocketException e) {
       
    55                 throw new RuntimeException(e);
       
    56             }
       
    57         };
       
    58 
       
    59         Collection<NetworkInterface> expected = Collections.list(NetworkInterface.getNetworkInterfaces());
       
    60         withData(TestData.Factory.ofSupplier("Top-level network interfaces", ss))
       
    61                 .stream(s -> s)
       
    62                 .expectedResult(expected)
       
    63                 .exercise();
       
    64     }
       
    65 
       
    66 
       
    67     private Collection<NetworkInterface> getAllNetworkInterfaces() throws SocketException {
       
    68         Collection<NetworkInterface> anis = new ArrayList<>();
       
    69         for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) {
       
    70             getAllSubNetworkInterfaces(ni, anis);
       
    71         }
       
    72         return anis;
       
    73     }
       
    74 
       
    75     private void getAllSubNetworkInterfaces(NetworkInterface ni, Collection<NetworkInterface> result) {
       
    76         result.add(ni);
       
    77 
       
    78         for (NetworkInterface sni : Collections.list(ni.getSubInterfaces())) {
       
    79             getAllSubNetworkInterfaces(sni, result);
       
    80         }
       
    81     }
       
    82 
       
    83     private Stream<NetworkInterface> allNetworkInterfaces() throws SocketException {
       
    84         return NetworkInterface.networkInterfaces().flatMap(this::allSubNetworkInterfaces);
       
    85     }
       
    86 
       
    87     private Stream<NetworkInterface> allSubNetworkInterfaces(NetworkInterface ni) {
       
    88         return Stream.concat(
       
    89                 Stream.of(ni),
       
    90                 ni.subInterfaces().flatMap(this::allSubNetworkInterfaces));
       
    91     }
       
    92 
       
    93     @Test
       
    94     public void testSubNetworkInterfaces() throws SocketException {
       
    95         Supplier<Stream<NetworkInterface>> ss = () -> {
       
    96             try {
       
    97                 return allNetworkInterfaces();
       
    98             }
       
    99             catch (SocketException e) {
       
   100                 throw new RuntimeException(e);
       
   101             }
       
   102         };
       
   103 
       
   104         Collection<NetworkInterface> expected = getAllNetworkInterfaces();
       
   105         withData(TestData.Factory.ofSupplier("All network interfaces", ss))
       
   106                 .stream(s -> s)
       
   107                 .expectedResult(expected)
       
   108                 .exercise();
       
   109     }
       
   110 
       
   111 
       
   112     @Test
       
   113     public void testInetAddresses() throws SocketException {
       
   114         Supplier<Stream<InetAddress>> ss = () -> {
       
   115             try {
       
   116                 return NetworkInterface.networkInterfaces().flatMap(NetworkInterface::inetAddresses);
       
   117             }
       
   118             catch (SocketException e) {
       
   119                 throw new RuntimeException(e);
       
   120             }
       
   121         };
       
   122 
       
   123         Collection<NetworkInterface> nis = Collections.list(NetworkInterface.getNetworkInterfaces());
       
   124         Collection<InetAddress> expected = new ArrayList<>();
       
   125         for (NetworkInterface ni : nis) {
       
   126             expected.addAll(Collections.list(ni.getInetAddresses()));
       
   127         }
       
   128         withData(TestData.Factory.ofSupplier("All inet addresses", ss))
       
   129                 .stream(s -> s)
       
   130                 .expectedResult(expected)
       
   131                 .exercise();
       
   132     }
       
   133 
       
   134 
       
   135 }