这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions core-java-modules/core-java-networking-5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>${net.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down Expand Up @@ -71,7 +66,6 @@

<properties>
<commons-validator.version>1.7</commons-validator.version>
<net.version>3.8.0</net.version>
<httpclient.version>4.5.2</httpclient.version>
<javax.ws.rs-api.version>2.1.1</javax.ws.rs-api.version>
<jersey-common.version>2.22.2</jersey-common.version>
Expand Down
6 changes: 6 additions & 0 deletions core-java-modules/core-java-networking-6/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@
<version>${okhttp.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>${commons-net.version}</version>
</dependency>
</dependencies>

<build>
Expand All @@ -76,6 +81,7 @@
<apache.httpclient5.version>5.4.2</apache.httpclient5.version>
<okhttp.version>4.12.0</okhttp.version>
<webflux.version>3.4.3</webflux.version>
<commons-net.version>3.8.0</commons-net.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.baeldung.getwebfilesize;

import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.junit.jupiter.api.Test;

class GetWebFileSizeLiveTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,85 +1,85 @@
package com.baeldung.ipaddresses;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.util.SubnetUtils;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class SubnetScannerUnitTest {
@Test
public void givenSubnet_whenScanningForDevices_thenReturnConnectedIPs() throws Exception {
String subnet = getSubnet();
List<String> connectedIPs = new ArrayList<>();
for (int i = 1; i <= 254; i++) {
String ip = subnet + "." + i;
if (InetAddress.getByName(ip).isReachable(100)) {
connectedIPs.add(ip);
}
}
assertFalse(connectedIPs.isEmpty());
}
@Test
public void givenSubnet_whenUsingStream_thenReturnConnectedIPs() throws UnknownHostException {
String subnet = getSubnet();
List<String> connectedIPs = IntStream.rangeClosed(1, 254)
.mapToObj(i -> subnet + "." + i)
.filter(ip -> {
try {
return InetAddress.getByName(ip).isReachable(100);
} catch (Exception e) {
return false;
}
})
.toList();
assertFalse(connectedIPs.isEmpty());
}
@Test
public void givenSubnet_whenCheckingForOpenPorts_thenReturnDevicesWithOpenPort() throws UnknownHostException {
SubnetUtils utils = new SubnetUtils(getSubnet() + ".0/24");
int port = 80;
List<String> devicesWithOpenPort = Arrays.stream(utils.getInfo().getAllAddresses())
.filter(ip -> {
TelnetClient telnetClient = new TelnetClient();
try {
telnetClient.setConnectTimeout(100);
telnetClient.connect(ip, port);
return telnetClient.isConnected();
} catch (Exception e) {
return false;
} finally {
try {
if (telnetClient.isConnected()) {
telnetClient.disconnect();
}
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
})
.toList();
assertFalse(devicesWithOpenPort.isEmpty());
}
private String getSubnet() throws UnknownHostException {
InetAddress localHost = InetAddress.getLocalHost();
byte[] ipAddr = localHost.getAddress();
return String.format("%d.%d.%d", (ipAddr[0] & 0xFF), (ipAddr[1] & 0xFF), (ipAddr[2] & 0xFF));
}
}
package com.baeldung.ipaddresses;

import static org.junit.jupiter.api.Assertions.assertFalse;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.util.SubnetUtils;
import org.junit.jupiter.api.Test;

public class SubnetScannerUnitTest {

@Test
public void givenSubnet_whenScanningForDevices_thenReturnConnectedIPs() throws Exception {
String subnet = getSubnet();
List<String> connectedIPs = new ArrayList<>();

for (int i = 1; i <= 254; i++) {
String ip = subnet + "." + i;
if (InetAddress.getByName(ip).isReachable(100)) {
connectedIPs.add(ip);
}
}

assertFalse(connectedIPs.isEmpty());
}

@Test
public void givenSubnet_whenUsingStream_thenReturnConnectedIPs() throws UnknownHostException {
String subnet = getSubnet();

List<String> connectedIPs = IntStream.rangeClosed(1, 254)
.mapToObj(i -> subnet + "." + i)
.filter(ip -> {
try {
return InetAddress.getByName(ip).isReachable(100);
} catch (Exception e) {
return false;
}
})
.toList();

assertFalse(connectedIPs.isEmpty());
}

@Test
public void givenSubnet_whenCheckingForOpenPorts_thenReturnDevicesWithOpenPort() throws UnknownHostException {
SubnetUtils utils = new SubnetUtils(getSubnet() + ".0/24");
int port = 80;
List<String> devicesWithOpenPort = Arrays.stream(utils.getInfo().getAllAddresses())
.filter(ip -> {
TelnetClient telnetClient = new TelnetClient();
try {
telnetClient.setConnectTimeout(100);
telnetClient.connect(ip, port);
return telnetClient.isConnected();
} catch (Exception e) {
return false;
} finally {
try {
if (telnetClient.isConnected()) {
telnetClient.disconnect();
}
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
}
})
.toList();

assertFalse(devicesWithOpenPort.isEmpty());
}

private String getSubnet() throws UnknownHostException {
InetAddress localHost = InetAddress.getLocalHost();
byte[] ipAddr = localHost.getAddress();
return String.format("%d.%d.%d", (ipAddr[0] & 0xFF), (ipAddr[1] & 0xFF), (ipAddr[2] & 0xFF));
}
}