Commit 5f0392d7 authored by Deepak Prabhakar's avatar Deepak Prabhakar

Fixed memory leaks which were breaking the test.(http://goo.gl/iA8QWo)

parent 2a6a49d6
......@@ -49,15 +49,25 @@ public class Postcode {
* @throws Exception
*/
public JSONObject asJson() throws Exception {
if (!StringUtils.isEmpty(postcode)) {
return JsonFetcher.urlToJson(new URL(LOOKUP_URL.toString().concat(postcode)));
} else if (json != null) {
return JsonFetcher.postURLToJson(new URL(LOOKUP_URL.toString()), json);
} else {
throw new IllegalArgumentException("postcode/postcodes are mandatory");
try {
if (!StringUtils.isEmpty(postcode)) {
return JsonFetcher.urlToJson(new URL(LOOKUP_URL.toString().concat(postcode)));
} else if (json != null) {
return JsonFetcher.postURLToJson(new URL(LOOKUP_URL.toString()), json);
} else {
throw new IllegalArgumentException("postcode/postcodes are mandatory");
}
} finally {
clear();
}
}
private void clear() {
setJson(null);
setPostcode(null);
setPostcodes(null);
}
/**
* Use this to create {@link PostCodeDetails}<br/>
* Pass this to {@link PostCodeDetails#generate}
......@@ -75,4 +85,16 @@ public class Postcode {
}
}
private void setPostcode(String postcode) {
this.postcode = postcode;
}
private void setPostcodes(String[] postcodes) {
this.postcodes = postcodes;
}
private void setJson(JSONObject json) {
this.json = json;
}
}
package com.postcode.io.initializers;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
......@@ -9,6 +8,8 @@ import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.postcode.io.json.JsonFetcher;
/**
......@@ -81,7 +82,7 @@ public class ReverseGeocoding {
return this;
}
public JSONObject asjson() throws MalformedURLException, IOException {
public JSONObject asjson() throws IOException, UnirestException {
String url = "";
url = url.concat("lon=").concat(String.valueOf(longitude));
url = url.concat("&lat=").concat(String.valueOf(latitude));
......@@ -94,13 +95,27 @@ public class ReverseGeocoding {
if (isWideSearch()) {
url = url.concat("&widesearch=").concat(String.valueOf(wideSearch));
}
if (json != null) {
return JsonFetcher.postURLToJson(new URL(LOOKUP_URL), json);
} else {
return JsonFetcher.urlToJson(new URL(LOOKUP_URL.concat(url)));
try {
if (json != null) {
return JsonFetcher.postURLToJson(new URL(LOOKUP_URL), json);
} else {
return Unirest.get(LOOKUP_URL.concat(url)).asJson().getBody().getObject();
}
} finally {
clear();
}
}
private void clear() {
ReverseGeocoding.longitude = null;
ReverseGeocoding.latitude = null;
ReverseGeocoding.limit = 0;
ReverseGeocoding.radius = 0;
ReverseGeocoding.wideSearch = false;
ReverseGeocoding.json = null;
ReverseGeocoding.reverses = null;
}
private JSONObject createGeocodings(List<Reverse> reverses) {
JSONObject json = new JSONObject();
JSONObject tempJson;
......
......@@ -6,6 +6,7 @@ import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
......@@ -87,12 +88,11 @@ public class PostcodeLookupTest {
assertTrue(
PostcodeLookup.reverseGeocoding(0.629834723775309, 51.7923246977375).asjson().getInt("status") == 200);
JSONAssert.assertEquals(
JsonFetcher.urlToJson(new File(
System.getProperty("user.dir").concat("/src/test/resources/reverseGeocodingWithAllParams.json"))
.toURI().toURL()),
JsonFetcher.urlToJson(
new URL("https://api.postcodes.io/postcodes?lon=0.629834723775309&lat=51.7923246977375&limit=100&radius=2000&widesearch=true")),
PostcodeLookup.reverseGeocoding(0.629834723775309, 51.7923246977375).limit(100).radius(2000)
.wideSearch(true).asjson(),
JSONCompareMode.LENIENT);
JSONCompareMode.STRICT);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment