Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
postcodes-io-java
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
James Coyle
postcodes-io-java
Commits
5f0392d7
Commit
5f0392d7
authored
Aug 15, 2016
by
Deepak Prabhakar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed memory leaks which were breaking the test.(
http://goo.gl/iA8QWo
)
parent
2a6a49d6
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
16 deletions
+53
-16
src/main/java/com/postcode/io/initializers/Postcode.java
src/main/java/com/postcode/io/initializers/Postcode.java
+28
-6
src/main/java/com/postcode/io/initializers/ReverseGeocoding.java
...n/java/com/postcode/io/initializers/ReverseGeocoding.java
+21
-6
src/test/java/com/postcode/io/initializers/PostcodeLookupTest.java
...java/com/postcode/io/initializers/PostcodeLookupTest.java
+4
-4
No files found.
src/main/java/com/postcode/io/initializers/Postcode.java
View file @
5f0392d7
...
...
@@ -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
;
}
}
src/main/java/com/postcode/io/initializers/ReverseGeocoding.java
View file @
5f0392d7
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
,
IO
Exception
{
public
JSONObject
asjson
()
throws
IOException
,
Unirest
Exception
{
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
;
...
...
src/test/java/com/postcode/io/initializers/PostcodeLookupTest.java
View file @
5f0392d7
...
...
@@ -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
.
LENIEN
T
);
JSONCompareMode
.
STRIC
T
);
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment