Skip to content

Commit f7525b8

Browse files
Egima profilepivovarit
authored andcommitted
network interfaces (eugenp#794)
* made changes to java reflection * removed redundant method makeSound in Animal abstract class * added project for play-framework article * added project for regex * changed regex project from own model to core-java * added project for routing in play * made changes to regex project * refactored code for REST API with Play project * refactored student store indexing to zero base * added unit tests, removed bad names * added NIO Selector project under core-java module * requested changes made * added project for nio2 * standardized exception based tests * fixed exception based tests * removed redundant files * added network interface project * used UUID other than timestamps
1 parent ddf41d6 commit f7525b8

2 files changed

Lines changed: 142 additions & 6 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.baeldung.java.networking.interfaces;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.net.InetAddress;
8+
import java.net.InterfaceAddress;
9+
import java.net.NetworkInterface;
10+
import java.net.SocketException;
11+
import java.net.UnknownHostException;
12+
import java.util.Enumeration;
13+
import java.util.List;
14+
15+
import org.junit.Test;
16+
17+
public class NetworkInterfaceTest {
18+
@Test
19+
public void givenName_whenReturnsNetworkInterface_thenCorrect() throws SocketException {
20+
NetworkInterface nif = NetworkInterface.getByName("lo");
21+
assertFalse(nif == null);
22+
}
23+
24+
@Test
25+
public void givenInExistentName_whenReturnsNull_thenCorrect() throws SocketException {
26+
NetworkInterface nif = NetworkInterface.getByName("inexistent_name");
27+
assertTrue(nif == null);
28+
}
29+
30+
@Test
31+
public void givenIP_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
32+
byte[] ip = new byte[] { 127, 0, 0, 1 };
33+
NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getByAddress(ip));
34+
assertFalse(nif == null);
35+
}
36+
37+
@Test
38+
public void givenHostName_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
39+
NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getByName("localhost"));
40+
assertFalse(nif == null);
41+
}
42+
43+
@Test
44+
public void givenLocalHost_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
45+
NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getLocalHost());
46+
assertFalse(nif == null);
47+
}
48+
49+
@Test
50+
public void givenLoopBack_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
51+
NetworkInterface nif = NetworkInterface.getByInetAddress(InetAddress.getLoopbackAddress());
52+
assertFalse(nif == null);
53+
}
54+
55+
@Test
56+
public void givenIndex_whenReturnsNetworkInterface_thenCorrect() throws SocketException, UnknownHostException {
57+
NetworkInterface nif = NetworkInterface.getByIndex(0);
58+
assertFalse(nif == null);
59+
}
60+
61+
@Test
62+
public void givenInterface_whenReturnsInetAddresses_thenCorrect() throws SocketException, UnknownHostException {
63+
NetworkInterface nif = NetworkInterface.getByName("lo");
64+
Enumeration<InetAddress> addressEnum = nif.getInetAddresses();
65+
InetAddress address = addressEnum.nextElement();
66+
assertEquals("127.0.0.1", address.getHostAddress());
67+
}
68+
69+
@Test
70+
public void givenInterface_whenReturnsInterfaceAddresses_thenCorrect() throws SocketException, UnknownHostException {
71+
NetworkInterface nif = NetworkInterface.getByName("lo");
72+
73+
List<InterfaceAddress> addressEnum = nif.getInterfaceAddresses();
74+
InterfaceAddress address = addressEnum.get(0);
75+
InetAddress localAddress = address.getAddress();
76+
InetAddress broadCastAddress = address.getBroadcast();
77+
assertEquals("127.0.0.1", localAddress.getHostAddress());
78+
assertEquals("127.255.255.255", broadCastAddress.getHostAddress());
79+
}
80+
81+
@Test
82+
public void givenInterface_whenChecksIfLoopback_thenCorrect() throws SocketException, UnknownHostException {
83+
NetworkInterface nif = NetworkInterface.getByName("lo");
84+
assertTrue(nif.isLoopback());
85+
}
86+
87+
@Test
88+
public void givenInterface_whenChecksIfUp_thenCorrect() throws SocketException, UnknownHostException {
89+
NetworkInterface nif = NetworkInterface.getByName("lo");
90+
assertTrue(nif.isUp());
91+
}
92+
93+
@Test
94+
public void givenInterface_whenChecksIfPointToPoint_thenCorrect() throws SocketException, UnknownHostException {
95+
NetworkInterface nif = NetworkInterface.getByName("lo");
96+
assertFalse(nif.isPointToPoint());
97+
}
98+
99+
@Test
100+
public void givenInterface_whenChecksIfVirtual_thenCorrect() throws SocketException, UnknownHostException {
101+
NetworkInterface nif = NetworkInterface.getByName("lo");
102+
assertFalse(nif.isVirtual());
103+
}
104+
105+
@Test
106+
public void givenInterface_whenChecksMulticastSupport_thenCorrect() throws SocketException, UnknownHostException {
107+
NetworkInterface nif = NetworkInterface.getByName("lo");
108+
assertTrue(nif.supportsMulticast());
109+
}
110+
111+
@Test
112+
public void givenInterface_whenGetsMacAddress_thenCorrect() throws SocketException, UnknownHostException {
113+
NetworkInterface nif = NetworkInterface.getByName("lo");
114+
byte[] bytes = nif.getHardwareAddress();
115+
assertFalse(bytes == null);
116+
}
117+
118+
@Test
119+
public void givenInterface_whenGetsMTU_thenCorrect() throws SocketException, UnknownHostException {
120+
NetworkInterface nif = NetworkInterface.getByName("lo");
121+
int mtu = nif.getMTU();
122+
}
123+
}

‎core-java/src/test/java/com/baeldung/java/nio2/FileTest.java‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package com.baeldung.java.nio2;
22

3-
import org.junit.Test;
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
45

56
import java.io.IOException;
6-
import java.nio.file.*;
7+
import java.nio.file.DirectoryNotEmptyException;
8+
import java.nio.file.FileAlreadyExistsException;
9+
import java.nio.file.Files;
10+
import java.nio.file.NoSuchFileException;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
import java.nio.file.StandardCopyOption;
714
import java.util.UUID;
815

9-
import static org.junit.Assert.assertFalse;
10-
import static org.junit.Assert.assertTrue;
16+
import org.junit.Test;
1117

1218
public class FileTest {
1319
private static final String HOME = System.getProperty("user.home");
@@ -86,6 +92,7 @@ public void givenDirPath_whenFailsToCreateRecursively_thenCorrect() throws IOExc
8692
Path p = Paths.get(HOME + "/" + dirName);
8793
assertFalse(Files.exists(p));
8894
Files.createDirectory(p);
95+
8996
}
9097

9198
@Test
@@ -151,11 +158,16 @@ public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOExcept
151158

152159
}
153160

154-
@Test(expected = NoSuchFileException.class)
161+
@Test
155162
public void givenInexistentFile_whenDeleteFails_thenCorrect() throws IOException {
156163
Path p = Paths.get(HOME + "/inexistentFile.txt");
157164
assertFalse(Files.exists(p));
158-
Files.delete(p);
165+
try {
166+
Files.delete(p);
167+
} catch (IOException e) {
168+
assertTrue(e instanceof NoSuchFileException);
169+
}
170+
159171
}
160172

161173
@Test
@@ -234,4 +246,5 @@ public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect() throws IO
234246
assertTrue(Files.exists(file2));
235247
assertFalse(Files.exists(file1));
236248
}
249+
237250
}

0 commit comments

Comments
 (0)