Skip to content

Commit 927c923

Browse files
committed
Modify and fail-fast GeoSearchParam (#3827)
1 parent 72073c5 commit 927c923

File tree

4 files changed

+50
-33
lines changed

4 files changed

+50
-33
lines changed

Diff for: src/main/java/redis/clients/jedis/params/GeoSearchParam.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,25 @@ public GeoSearchParam any() {
115115

116116
@Override
117117
public void addParams(CommandArguments args) {
118-
if (this.fromMember) {
119-
args.add(Keyword.FROMMEMBER).add(this.member);
120-
} else if (this.fromLonLat) {
118+
if (fromMember && fromLonLat) {
119+
throw new IllegalArgumentException("Both FROMMEMBER and FROMLONLAT cannot be used.");
120+
} else if (fromMember) {
121+
args.add(Keyword.FROMMEMBER).add(member);
122+
} else if (fromLonLat) {
121123
args.add(Keyword.FROMLONLAT).add(coord.getLongitude()).add(coord.getLatitude());
124+
} else {
125+
throw new IllegalArgumentException("Either FROMMEMBER or FROMLONLAT must be used.");
122126
}
123127

124-
if (this.byRadius) {
125-
args.add(Keyword.BYRADIUS).add(this.radius);
126-
} else if (this.byBox) {
127-
args.add(Keyword.BYBOX).add(this.width).add(this.height);
128+
if (byRadius && byBox) {
129+
throw new IllegalArgumentException("Both BYRADIUS and BYBOX cannot be used.");
130+
} else if (byRadius) {
131+
args.add(Keyword.BYRADIUS).add(radius).add(unit);
132+
} else if (byBox) {
133+
args.add(Keyword.BYBOX).add(width).add(height).add(unit);
134+
} else {
135+
throw new IllegalArgumentException("Either BYRADIUS or BYBOX must be used.");
128136
}
129-
args.add(this.unit);
130137

131138
if (withCoord) {
132139
args.add(Keyword.WITHCOORD);

Diff for: src/test/java/redis/clients/jedis/commands/jedis/GeoCommandsTest.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -532,30 +532,30 @@ public void geosearchNegative() {
532532
// combine byradius and bybox
533533
try {
534534
jedis.geosearch("barcelona", new GeoSearchParam()
535-
.byRadius(3000, GeoUnit.M).byBox(300, 300, GeoUnit.M));
535+
.byRadius(3000, GeoUnit.M)
536+
.byBox(300, 300, GeoUnit.M));
536537
fail();
537-
} catch (Exception ignored) { }
538+
} catch (IllegalArgumentException ignored) { }
538539

539540
// without byradius and without bybox
540541
try {
541-
jedis.geosearch("barcelona", new GeoSearchParam()
542-
.fromMember("foobar"));
542+
jedis.geosearch("barcelona", new GeoSearchParam().fromMember("foobar"));
543543
fail();
544-
} catch (Exception ignored) { }
544+
} catch (IllegalArgumentException ignored) { }
545545

546546
// combine frommember and fromlonlat
547547
try {
548548
jedis.geosearch("barcelona", new GeoSearchParam()
549-
.fromMember("foobar").fromLonLat(10,10));
549+
.fromMember("foobar")
550+
.fromLonLat(10,10));
550551
fail();
551-
} catch (Exception ignored) { }
552+
} catch (IllegalArgumentException ignored) { }
552553

553554
// without frommember and without fromlonlat
554555
try {
555-
jedis.geosearch("barcelona", new GeoSearchParam()
556-
.byRadius(10, GeoUnit.MI));
556+
jedis.geosearch("barcelona", new GeoSearchParam().byRadius(10, GeoUnit.MI));
557557
fail();
558-
} catch (Exception ignored) { }
558+
} catch (IllegalArgumentException ignored) { }
559559
}
560560

561561
@Test

Diff for: src/test/java/redis/clients/jedis/commands/unified/GeoCommandsTestBase.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -532,30 +532,30 @@ public void geosearchNegative() {
532532
// combine byradius and bybox
533533
try {
534534
jedis.geosearch("barcelona", new GeoSearchParam()
535-
.byRadius(3000, GeoUnit.M).byBox(300, 300, GeoUnit.M));
535+
.byRadius(3000, GeoUnit.M)
536+
.byBox(300, 300, GeoUnit.M));
536537
fail();
537-
} catch (redis.clients.jedis.exceptions.JedisDataException ignored) { }
538+
} catch (IllegalArgumentException ignored) { }
538539

539540
// without byradius and without bybox
540541
try {
541-
jedis.geosearch("barcelona", new GeoSearchParam()
542-
.fromMember("foobar"));
542+
jedis.geosearch("barcelona", new GeoSearchParam().fromMember("foobar"));
543543
fail();
544-
} catch (java.lang.IllegalArgumentException ignored) { }
544+
} catch (IllegalArgumentException ignored) { }
545545

546546
// combine frommember and fromlonlat
547547
try {
548548
jedis.geosearch("barcelona", new GeoSearchParam()
549-
.fromMember("foobar").fromLonLat(10,10));
549+
.fromMember("foobar")
550+
.fromLonLat(10,10));
550551
fail();
551-
} catch (java.lang.IllegalArgumentException ignored) { }
552+
} catch (IllegalArgumentException ignored) { }
552553

553554
// without frommember and without fromlonlat
554555
try {
555-
jedis.geosearch("barcelona", new GeoSearchParam()
556-
.byRadius(10, GeoUnit.MI));
556+
jedis.geosearch("barcelona", new GeoSearchParam().byRadius(10, GeoUnit.MI));
557557
fail();
558-
} catch (redis.clients.jedis.exceptions.JedisDataException ignored) { }
558+
} catch (IllegalArgumentException ignored) { }
559559
}
560560

561561
@Test

Diff for: src/test/java/redis/clients/jedis/commands/unified/cluster/ClusterGeoCommandsTest.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
//
1616
//import redis.clients.jedis.GeoCoordinate;
1717
//import redis.clients.jedis.args.GeoUnit;
18+
//import redis.clients.jedis.commands.unified.GeoCommandsTestBase;
1819
//import redis.clients.jedis.params.GeoRadiusParam;
1920
//import redis.clients.jedis.params.GeoRadiusStoreParam;
20-
//import redis.clients.jedis.commands.unified.GeoCommandsTestBase;
2121
//
2222
//public class ClusterGeoCommandsTest extends GeoCommandsTestBase {
2323
//
@@ -51,8 +51,8 @@
5151
// jedis.geoadd("Sicily {ITA}", coordinateMap);
5252
//
5353
// long size = jedis.georadiusStore("Sicily {ITA}", 15, 37, 200, GeoUnit.KM,
54-
// GeoRadiusParam.geoRadiusParam(),
55-
// GeoRadiusStoreParam.geoRadiusStoreParam().store("{ITA} SicilyStore"));
54+
// GeoRadiusParam.geoRadiusParam(),
55+
// GeoRadiusStoreParam.geoRadiusStoreParam().store("{ITA} SicilyStore"));
5656
// assertEquals(2, size);
5757
// List<String> expected = new ArrayList<>();
5858
// expected.add("Palermo");
@@ -73,8 +73,8 @@
7373
// jedis.geoadd("Sicily {ITA}", 15.087269, 37.502669, "Catania");
7474
//
7575
// long size = jedis.georadiusByMemberStore("Sicily {ITA}", "Agrigento", 100, GeoUnit.KM,
76-
// GeoRadiusParam.geoRadiusParam(),
77-
// GeoRadiusStoreParam.geoRadiusStoreParam().store("{ITA} SicilyStore"));
76+
// GeoRadiusParam.geoRadiusParam(),
77+
// GeoRadiusStoreParam.geoRadiusStoreParam().store("{ITA} SicilyStore"));
7878
// assertEquals(2, size);
7979
// List<String> expected = new ArrayList<>();
8080
// expected.add("Agrigento");
@@ -86,4 +86,14 @@
8686
// @Override
8787
// public void georadiusByMemberStoreBinary() {
8888
// }
89+
//
90+
// @Ignore
91+
// @Override
92+
// public void geosearchstore() {
93+
// }
94+
//
95+
// @Ignore
96+
// @Override
97+
// public void geosearchstoreWithdist() {
98+
// }
8999
//}

0 commit comments

Comments
 (0)