Skip to content

Commit 62dc048

Browse files
authored
[JAVA-47895] Moved code from core-java-networking-5 to core-java-networking-6 (#18657)
1 parent 14a44e1 commit 62dc048

File tree

4 files changed

+96
-94
lines changed

4 files changed

+96
-94
lines changed

core-java-modules/core-java-networking-5/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
<artifactId>jsoup</artifactId>
3737
<version>${jsoup.version}</version>
3838
</dependency>
39-
<dependency>
40-
<groupId>commons-net</groupId>
41-
<artifactId>commons-net</artifactId>
42-
<version>${net.version}</version>
43-
</dependency>
4439
<dependency>
4540
<groupId>org.apache.httpcomponents</groupId>
4641
<artifactId>httpclient</artifactId>
@@ -71,7 +66,6 @@
7166

7267
<properties>
7368
<commons-validator.version>1.7</commons-validator.version>
74-
<net.version>3.8.0</net.version>
7569
<httpclient.version>4.5.2</httpclient.version>
7670
<javax.ws.rs-api.version>2.1.1</javax.ws.rs-api.version>
7771
<jersey-common.version>2.22.2</jersey-common.version>

core-java-modules/core-java-networking-6/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
<version>${okhttp.version}</version>
6161
<scope>test</scope>
6262
</dependency>
63+
<dependency>
64+
<groupId>commons-net</groupId>
65+
<artifactId>commons-net</artifactId>
66+
<version>${commons-net.version}</version>
67+
</dependency>
6368
</dependencies>
6469

6570
<build>
@@ -76,6 +81,7 @@
7681
<apache.httpclient5.version>5.4.2</apache.httpclient5.version>
7782
<okhttp.version>4.12.0</okhttp.version>
7883
<webflux.version>3.4.3</webflux.version>
84+
<commons-net.version>3.8.0</commons-net.version>
7985
</properties>
8086

8187
</project>
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.baeldung.getwebfilesize;
22

3-
import org.junit.jupiter.api.Test;
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.fail;
5+
46
import java.io.IOException;
57
import java.net.URL;
68
import java.net.URLConnection;
7-
import static org.junit.Assert.assertEquals;
8-
import static org.junit.Assert.fail;
9+
10+
import org.junit.jupiter.api.Test;
911

1012
class GetWebFileSizeLiveTest {
1113

Original file line numberDiff line numberDiff line change
@@ -1,85 +1,85 @@
1-
package com.baeldung.ipaddresses;
2-
3-
import org.apache.commons.net.telnet.TelnetClient;
4-
import org.apache.commons.net.util.SubnetUtils;
5-
import org.junit.jupiter.api.Test;
6-
7-
import java.io.IOException;
8-
import java.net.InetAddress;
9-
import java.net.UnknownHostException;
10-
import java.util.ArrayList;
11-
import java.util.Arrays;
12-
import java.util.List;
13-
import java.util.stream.IntStream;
14-
15-
import static org.junit.jupiter.api.Assertions.assertFalse;
16-
17-
public class SubnetScannerUnitTest {
18-
19-
@Test
20-
public void givenSubnet_whenScanningForDevices_thenReturnConnectedIPs() throws Exception {
21-
String subnet = getSubnet();
22-
List<String> connectedIPs = new ArrayList<>();
23-
24-
for (int i = 1; i <= 254; i++) {
25-
String ip = subnet + "." + i;
26-
if (InetAddress.getByName(ip).isReachable(100)) {
27-
connectedIPs.add(ip);
28-
}
29-
}
30-
31-
assertFalse(connectedIPs.isEmpty());
32-
}
33-
34-
@Test
35-
public void givenSubnet_whenUsingStream_thenReturnConnectedIPs() throws UnknownHostException {
36-
String subnet = getSubnet();
37-
38-
List<String> connectedIPs = IntStream.rangeClosed(1, 254)
39-
.mapToObj(i -> subnet + "." + i)
40-
.filter(ip -> {
41-
try {
42-
return InetAddress.getByName(ip).isReachable(100);
43-
} catch (Exception e) {
44-
return false;
45-
}
46-
})
47-
.toList();
48-
49-
assertFalse(connectedIPs.isEmpty());
50-
}
51-
52-
@Test
53-
public void givenSubnet_whenCheckingForOpenPorts_thenReturnDevicesWithOpenPort() throws UnknownHostException {
54-
SubnetUtils utils = new SubnetUtils(getSubnet() + ".0/24");
55-
int port = 80;
56-
List<String> devicesWithOpenPort = Arrays.stream(utils.getInfo().getAllAddresses())
57-
.filter(ip -> {
58-
TelnetClient telnetClient = new TelnetClient();
59-
try {
60-
telnetClient.setConnectTimeout(100);
61-
telnetClient.connect(ip, port);
62-
return telnetClient.isConnected();
63-
} catch (Exception e) {
64-
return false;
65-
} finally {
66-
try {
67-
if (telnetClient.isConnected()) {
68-
telnetClient.disconnect();
69-
}
70-
} catch (IOException ex) {
71-
System.err.println(ex.getMessage());
72-
}
73-
}
74-
})
75-
.toList();
76-
77-
assertFalse(devicesWithOpenPort.isEmpty());
78-
}
79-
80-
private String getSubnet() throws UnknownHostException {
81-
InetAddress localHost = InetAddress.getLocalHost();
82-
byte[] ipAddr = localHost.getAddress();
83-
return String.format("%d.%d.%d", (ipAddr[0] & 0xFF), (ipAddr[1] & 0xFF), (ipAddr[2] & 0xFF));
84-
}
85-
}
1+
package com.baeldung.ipaddresses;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
5+
import java.io.IOException;
6+
import java.net.InetAddress;
7+
import java.net.UnknownHostException;
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.List;
11+
import java.util.stream.IntStream;
12+
13+
import org.apache.commons.net.telnet.TelnetClient;
14+
import org.apache.commons.net.util.SubnetUtils;
15+
import org.junit.jupiter.api.Test;
16+
17+
public class SubnetScannerUnitTest {
18+
19+
@Test
20+
public void givenSubnet_whenScanningForDevices_thenReturnConnectedIPs() throws Exception {
21+
String subnet = getSubnet();
22+
List<String> connectedIPs = new ArrayList<>();
23+
24+
for (int i = 1; i <= 254; i++) {
25+
String ip = subnet + "." + i;
26+
if (InetAddress.getByName(ip).isReachable(100)) {
27+
connectedIPs.add(ip);
28+
}
29+
}
30+
31+
assertFalse(connectedIPs.isEmpty());
32+
}
33+
34+
@Test
35+
public void givenSubnet_whenUsingStream_thenReturnConnectedIPs() throws UnknownHostException {
36+
String subnet = getSubnet();
37+
38+
List<String> connectedIPs = IntStream.rangeClosed(1, 254)
39+
.mapToObj(i -> subnet + "." + i)
40+
.filter(ip -> {
41+
try {
42+
return InetAddress.getByName(ip).isReachable(100);
43+
} catch (Exception e) {
44+
return false;
45+
}
46+
})
47+
.toList();
48+
49+
assertFalse(connectedIPs.isEmpty());
50+
}
51+
52+
@Test
53+
public void givenSubnet_whenCheckingForOpenPorts_thenReturnDevicesWithOpenPort() throws UnknownHostException {
54+
SubnetUtils utils = new SubnetUtils(getSubnet() + ".0/24");
55+
int port = 80;
56+
List<String> devicesWithOpenPort = Arrays.stream(utils.getInfo().getAllAddresses())
57+
.filter(ip -> {
58+
TelnetClient telnetClient = new TelnetClient();
59+
try {
60+
telnetClient.setConnectTimeout(100);
61+
telnetClient.connect(ip, port);
62+
return telnetClient.isConnected();
63+
} catch (Exception e) {
64+
return false;
65+
} finally {
66+
try {
67+
if (telnetClient.isConnected()) {
68+
telnetClient.disconnect();
69+
}
70+
} catch (IOException ex) {
71+
System.err.println(ex.getMessage());
72+
}
73+
}
74+
})
75+
.toList();
76+
77+
assertFalse(devicesWithOpenPort.isEmpty());
78+
}
79+
80+
private String getSubnet() throws UnknownHostException {
81+
InetAddress localHost = InetAddress.getLocalHost();
82+
byte[] ipAddr = localHost.getAddress();
83+
return String.format("%d.%d.%d", (ipAddr[0] & 0xFF), (ipAddr[1] & 0xFF), (ipAddr[2] & 0xFF));
84+
}
85+
}

0 commit comments

Comments
 (0)