Skip to content

Commit 809b1d2

Browse files
committed
Added some simple unit tests
1 parent ef5b443 commit 809b1d2

File tree

5 files changed

+98
-16
lines changed

5 files changed

+98
-16
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

library/src/main/java/com/stealthcopter/networktools/ARPInfo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.support.annotation.Nullable;
44
import android.support.v4.util.Pair;
5+
import android.text.TextUtils;
56

67
import java.io.BufferedReader;
78
import java.io.FileReader;
@@ -29,8 +30,9 @@ public class ARPInfo {
2930
*/
3031
@Nullable
3132
public static String getMACFromIPAddress(String ip) {
32-
if (ip == null)
33+
if (ip == null) {
3334
return null;
35+
}
3436

3537
for (String line : getLinesInARPCache()) {
3638
String[] splitted = line.split(" +");
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.stealthcopter.networktools;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
7+
import static junit.framework.Assert.assertNull;
8+
import static org.junit.Assert.assertEquals;
9+
import static org.mockito.Mockito.mock;
10+
11+
/**
12+
* To work on unit tests, switch the Test Artifact in the Build Variants view.
13+
*/
14+
public class ARPInfoUnitTest {
15+
16+
@Test
17+
public void nullIPsandMacsReturnNull() throws Exception {
18+
19+
assertNull(ARPInfo.getMACFromIPAddress(null));
20+
assertNull(ARPInfo.getIPAddressFromMAC(null));
21+
22+
// assertEquals(arpInfo.getMACFromIPAddress("192.168.18.11"), "00:04:20:06:55:1a");
23+
// assertEquals(arpInfo.getIPAddressFromMAC("00:22:43:ab:2a:5b"), "192.168.18.36");
24+
25+
}
26+
27+
@Test(expected = IllegalArgumentException.class)
28+
public void testIllegalArgumentThrownOnInvalidMACaddress(){
29+
ARPInfo.getIPAddressFromMAC("00:00:00:xx");
30+
}
31+
}

library/src/test/java/com/stealthcopter/networktools/ExampleUnitTest.java

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.stealthcopter.networktools;
2+
3+
import org.junit.Test;
4+
5+
import java.io.IOException;
6+
7+
import static junit.framework.Assert.assertNull;
8+
9+
/**
10+
* To work on unit tests, switch the Test Artifact in the Build Variants view.
11+
*/
12+
public class WakeOnLanUnitTest {
13+
14+
@Test(expected = IllegalArgumentException.class)
15+
public void testIllegalArgumentThrownOnInvalidIPaddress(){
16+
try {
17+
WakeOnLan.sendWakeOnLan(null, "00:04:20:06:55:1a", 9, 10000, 5);
18+
} catch (IOException e) {
19+
e.printStackTrace();
20+
}
21+
}
22+
23+
@Test(expected = IllegalArgumentException.class)
24+
public void testIllegalArgumentThrownOnInvalidMACaddress(){
25+
try {
26+
WakeOnLan.sendWakeOnLan("192.168.0.1", null, 9, 10000, 5);
27+
} catch (IOException e) {
28+
e.printStackTrace();
29+
}
30+
}
31+
32+
@Test(expected = IllegalArgumentException.class)
33+
public void testIllegalArgumentThrownOnInvalidPortLow(){
34+
try {
35+
WakeOnLan.sendWakeOnLan("192.168.0.1", "00:04:20:06:55:1a", -1, 10000, 5);
36+
} catch (IOException e) {
37+
e.printStackTrace();
38+
}
39+
}
40+
41+
@Test(expected = IllegalArgumentException.class)
42+
public void testIllegalArgumentThrownOnInvalidPortHigh(){
43+
try {
44+
WakeOnLan.sendWakeOnLan("192.168.0.1", "00:04:20:06:55:1a", 65536, 10000, 5);
45+
} catch (IOException e) {
46+
e.printStackTrace();
47+
}
48+
}
49+
50+
@Test(expected = IllegalArgumentException.class)
51+
public void testIllegalArgumentThrownOnInvalidPackets(){
52+
try {
53+
WakeOnLan.sendWakeOnLan("192.168.0.1", "00:04:20:06:55:1a", 9, 10000, 0);
54+
} catch (IOException e) {
55+
e.printStackTrace();
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)