Skip to content

Commit c9d131f

Browse files
Added unit test
1 parent e853bc5 commit c9d131f

File tree

5 files changed

+212
-6
lines changed

5 files changed

+212
-6
lines changed

com/ip2location/IP2Location.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* <p>
3737
*
3838
* @author IP2Location.com
39-
* @version 8.5.1
39+
* @version 8.5.2
4040
*/
4141
public class IP2Location {
4242
private static final Pattern pattern = Pattern.compile("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"); // IPv4
@@ -148,6 +148,8 @@ public IP2Location() {
148148

149149
/**
150150
* This function can be used to pre-load the BIN file.
151+
* @param DBPath The full path to the IP2Location BIN database file
152+
* @throws IOException If an input or output exception occurred
151153
*/
152154
public void Open(String DBPath) throws IOException {
153155
IPDatabasePath = DBPath;
@@ -156,6 +158,9 @@ public void Open(String DBPath) throws IOException {
156158

157159
/**
158160
* This function can be used to initialized the component with params and pre-load the BIN file.
161+
* @param DBPath The full path to the IP2Location BIN database file
162+
* @param UseMMF Set to true to load the BIN database file into memory mapped file
163+
* @throws IOException If an input or output exception occurred
159164
*/
160165
public void Open(String DBPath, boolean UseMMF) throws IOException {
161166
UseMemoryMappedFile = UseMMF;
@@ -414,6 +419,7 @@ protected void finalize() throws Throwable {
414419
/**
415420
* This function to query IP2Location data.
416421
* @param IPAddress IP Address you wish to query
422+
* @throws IOException If an input or output exception occurred
417423
* @return IP2Location data
418424
*/
419425
public IPResult IPQuery(String IPAddress) throws IOException {

com/ip2location/IP2LocationWebService.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* <p>
2828
*
2929
* @author IP2Location.com
30-
* @version 8.5.1
30+
* @version 8.5.2
3131
*/
3232
public class IP2LocationWebService {
3333
private static final Pattern pattern = Pattern.compile("^[\\dA-Z]{10}$");
@@ -43,13 +43,20 @@ public IP2LocationWebService() {
4343

4444
/**
4545
* This function initializes the params for the web service.
46+
* @param APIKey IP2Location Web Service API key
47+
* @param Package IP2Location Web Service package (WS1 to WS24)
48+
* @throws IllegalArgumentException If an invalid parameter is specified
4649
*/
4750
public void Open(String APIKey, String Package) throws IllegalArgumentException {
4851
Open(APIKey, Package, true);
4952
}
5053

5154
/**
5255
* This function initializes the params for the web service.
56+
* @param APIKey IP2Location Web Service API key
57+
* @param Package IP2Location Web Service package (WS1 to WS24)
58+
* @param UseSSL Set to true to call the web service using SSL
59+
* @throws IllegalArgumentException If an invalid parameter is specified
5360
*/
5461
public void Open(String APIKey, String Package, boolean UseSSL) throws IllegalArgumentException {
5562
_APIKey = APIKey;
@@ -74,6 +81,8 @@ else if (!pattern2.matcher(_Package).matches()) {
7481
/**
7582
* This function to query IP2Location data.
7683
* @param IPAddress IP Address you wish to query
84+
* @throws IllegalArgumentException If an invalid parameter is specified
85+
* @throws RuntimeException If an exception occurred at runtime
7786
* @return IP2Location data
7887
*/
7988
public JsonObject IPQuery(String IPAddress) throws IllegalArgumentException, RuntimeException {
@@ -82,7 +91,10 @@ public JsonObject IPQuery(String IPAddress) throws IllegalArgumentException, Run
8291

8392
/**
8493
* This function to query IP2Location data.
85-
* @param IPAddress IP Address you wish to query and translation language
94+
* @param IPAddress IP Address you wish to query
95+
* @param Language The translation language
96+
* @throws IllegalArgumentException If an invalid parameter is specified
97+
* @throws RuntimeException If an exception occurred at runtime
8698
* @return IP2Location data
8799
*/
88100
public JsonObject IPQuery(String IPAddress, String Language) throws IllegalArgumentException, RuntimeException {
@@ -91,7 +103,11 @@ public JsonObject IPQuery(String IPAddress, String Language) throws IllegalArgum
91103

92104
/**
93105
* This function to query IP2Location data.
94-
* @param IPAddress IP Address you wish to query, Addons and translation language
106+
* @param IPAddress IP Address you wish to query
107+
* @param AddOns The list of AddOns results to return
108+
* @param Language The translation language
109+
* @throws IllegalArgumentException If an invalid parameter is specified
110+
* @throws RuntimeException If an exception occurred at runtime
95111
* @return IP2Location data
96112
*/
97113
public JsonObject IPQuery(String IPAddress, String[] AddOns, String Language) throws IllegalArgumentException, RuntimeException {
@@ -128,6 +144,8 @@ public JsonObject IPQuery(String IPAddress, String[] AddOns, String Language) th
128144

129145
/**
130146
* This function to check web service credit balance.
147+
* @throws IllegalArgumentException If an invalid parameter is specified
148+
* @throws RuntimeException If an exception occurred at runtime
131149
* @return Credit balance
132150
*/
133151
public JsonObject GetCredit() throws IllegalArgumentException, RuntimeException {

com/ip2location/IPResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* <p>
1010
*
1111
* @author IP2Location.com
12-
* @version 8.5.1
12+
* @version 8.5.2
1313
*/
1414
public class IPResult {
1515
static final String NOT_SUPPORTED = "Not_Supported";
@@ -36,7 +36,7 @@ public class IPResult {
3636
String usagetype;
3737
String status;
3838
boolean delay=false;
39-
String version = "Version 8.5.1";
39+
String version = "Version 8.5.2";
4040
IPResult(String ipstring) {
4141
ip_address = ipstring;
4242
}

test/java/IP2LocationTest.java

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import org.junit.jupiter.api.BeforeAll;
2+
import org.junit.jupiter.api.BeforeEach;
3+
import org.junit.jupiter.api.Test;
4+
import org.junit.jupiter.api.AfterEach;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import java.nio.file.*;
8+
import java.io.*;
9+
import com.ip2location.*;
10+
11+
class IP2LocationTest {
12+
private static IP2Location loc;
13+
private static String binfile = "IP2LOCATION-LITE-DB1.BIN";
14+
private static String binfilepath;
15+
private static String ip = "8.8.8.8";
16+
17+
@BeforeAll
18+
static void Setup() {
19+
Path binpath = Paths.get("src","test","resources", binfile);
20+
binfilepath = binpath.toFile().getAbsolutePath();
21+
}
22+
23+
@BeforeEach
24+
void Init() {
25+
loc = new IP2Location();
26+
}
27+
28+
@Test
29+
void TestOpenException() {
30+
assertThrows(IOException.class, () -> {
31+
loc.Open("dummy.bin");
32+
});
33+
}
34+
35+
@Test
36+
void TestQueryCountryCode() throws IOException {
37+
loc.Open(binfilepath);
38+
IPResult rec = loc.IPQuery(ip);
39+
assertEquals(rec.getCountryShort(), "US");
40+
}
41+
42+
@Test
43+
void TestQueryCountryLong() throws IOException {
44+
loc.Open(binfilepath);
45+
IPResult rec = loc.IPQuery(ip);
46+
assertEquals(rec.getCountryLong(), "United States of America");
47+
}
48+
49+
@Test
50+
void TestQueryRegion() throws IOException {
51+
loc.Open(binfilepath);
52+
IPResult rec = loc.IPQuery(ip);
53+
assertEquals(rec.getRegion(), "Not_Supported");
54+
}
55+
56+
@Test
57+
void TestQueryCity() throws IOException {
58+
loc.Open(binfilepath);
59+
IPResult rec = loc.IPQuery(ip);
60+
assertEquals(rec.getCity(), "Not_Supported");
61+
}
62+
63+
@Test
64+
void TestQueryLatitude() throws IOException {
65+
loc.Open(binfilepath);
66+
IPResult rec = loc.IPQuery(ip);
67+
assertEquals(rec.getLatitude(), 0.0);
68+
}
69+
70+
@Test
71+
void TestQueryLongitude() throws IOException {
72+
loc.Open(binfilepath);
73+
IPResult rec = loc.IPQuery(ip);
74+
assertEquals(rec.getLongitude(), 0.0);
75+
}
76+
77+
@Test
78+
void TestQueryZipCode() throws IOException {
79+
loc.Open(binfilepath);
80+
IPResult rec = loc.IPQuery(ip);
81+
assertEquals(rec.getZipCode(), "Not_Supported");
82+
}
83+
84+
@Test
85+
void TestQueryTimeZone() throws IOException {
86+
loc.Open(binfilepath);
87+
IPResult rec = loc.IPQuery(ip);
88+
assertEquals(rec.getTimeZone(), "Not_Supported");
89+
}
90+
91+
@Test
92+
void TestQueryISP() throws IOException {
93+
loc.Open(binfilepath);
94+
IPResult rec = loc.IPQuery(ip);
95+
assertEquals(rec.getISP(), "Not_Supported");
96+
}
97+
98+
@Test
99+
void TestQueryDomain() throws IOException {
100+
loc.Open(binfilepath);
101+
IPResult rec = loc.IPQuery(ip);
102+
assertEquals(rec.getDomain(), "Not_Supported");
103+
}
104+
105+
@Test
106+
void TestQueryNetSpeed() throws IOException {
107+
loc.Open(binfilepath);
108+
IPResult rec = loc.IPQuery(ip);
109+
assertEquals(rec.getNetSpeed(), "Not_Supported");
110+
}
111+
112+
@Test
113+
void TestQueryIDDCode() throws IOException {
114+
loc.Open(binfilepath);
115+
IPResult rec = loc.IPQuery(ip);
116+
assertEquals(rec.getIDDCode(), "Not_Supported");
117+
}
118+
119+
@Test
120+
void TestQueryAreaCode() throws IOException {
121+
loc.Open(binfilepath);
122+
IPResult rec = loc.IPQuery(ip);
123+
assertEquals(rec.getAreaCode(), "Not_Supported");
124+
}
125+
126+
@Test
127+
void TestQueryWeatherStationCode() throws IOException {
128+
loc.Open(binfilepath);
129+
IPResult rec = loc.IPQuery(ip);
130+
assertEquals(rec.getWeatherStationCode(), "Not_Supported");
131+
}
132+
133+
@Test
134+
void TestQueryWeatherStationName() throws IOException {
135+
loc.Open(binfilepath);
136+
IPResult rec = loc.IPQuery(ip);
137+
assertEquals(rec.getWeatherStationName(), "Not_Supported");
138+
}
139+
140+
@Test
141+
void TestQueryMCC() throws IOException {
142+
loc.Open(binfilepath);
143+
IPResult rec = loc.IPQuery(ip);
144+
assertEquals(rec.getMCC(), "Not_Supported");
145+
}
146+
147+
@Test
148+
void TestQueryMNC() throws IOException {
149+
loc.Open(binfilepath);
150+
IPResult rec = loc.IPQuery(ip);
151+
assertEquals(rec.getMNC(), "Not_Supported");
152+
}
153+
154+
@Test
155+
void TestQueryMobileBrand() throws IOException {
156+
loc.Open(binfilepath);
157+
IPResult rec = loc.IPQuery(ip);
158+
assertEquals(rec.getMobileBrand(), "Not_Supported");
159+
}
160+
161+
@Test
162+
void TestQueryElevation() throws IOException {
163+
loc.Open(binfilepath);
164+
IPResult rec = loc.IPQuery(ip);
165+
assertEquals(rec.getElevation(), 0.0);
166+
}
167+
168+
@Test
169+
void TestQueryUsageType() throws IOException {
170+
loc.Open(binfilepath);
171+
IPResult rec = loc.IPQuery(ip);
172+
assertEquals(rec.getUsageType(), "Not_Supported");
173+
}
174+
175+
@AfterEach
176+
void TearDown() {
177+
loc.Close();
178+
loc = null;
179+
}
180+
}
181+
182+
2 MB
Binary file not shown.

0 commit comments

Comments
 (0)