Skip to content

Commit dfb1640

Browse files
authored
Modify and fail-fast GeoSearchParam (#3827)
1 parent 8049b7b commit dfb1640

File tree

5 files changed

+48
-49
lines changed

5 files changed

+48
-49
lines changed

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);

src/test/java/redis/clients/jedis/commands/jedis/GeoCommandsTest.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -540,30 +540,30 @@ public void geosearchNegative() {
540540
// combine byradius and bybox
541541
try {
542542
jedis.geosearch("barcelona", new GeoSearchParam()
543-
.byRadius(3000, GeoUnit.M).byBox(300, 300, GeoUnit.M));
543+
.byRadius(3000, GeoUnit.M)
544+
.byBox(300, 300, GeoUnit.M));
544545
fail();
545-
} catch (Exception ignored) { }
546+
} catch (IllegalArgumentException ignored) { }
546547

547548
// without byradius and without bybox
548549
try {
549-
jedis.geosearch("barcelona", new GeoSearchParam()
550-
.fromMember("foobar"));
550+
jedis.geosearch("barcelona", new GeoSearchParam().fromMember("foobar"));
551551
fail();
552-
} catch (Exception ignored) { }
552+
} catch (IllegalArgumentException ignored) { }
553553

554554
// combine frommember and fromlonlat
555555
try {
556556
jedis.geosearch("barcelona", new GeoSearchParam()
557-
.fromMember("foobar").fromLonLat(10,10));
557+
.fromMember("foobar")
558+
.fromLonLat(10,10));
558559
fail();
559-
} catch (Exception ignored) { }
560+
} catch (IllegalArgumentException ignored) { }
560561

561562
// without frommember and without fromlonlat
562563
try {
563-
jedis.geosearch("barcelona", new GeoSearchParam()
564-
.byRadius(10, GeoUnit.MI));
564+
jedis.geosearch("barcelona", new GeoSearchParam().byRadius(10, GeoUnit.MI));
565565
fail();
566-
} catch (Exception ignored) { }
566+
} catch (IllegalArgumentException ignored) { }
567567
}
568568

569569
@Test

src/test/java/redis/clients/jedis/commands/unified/GeoCommandsTestBase.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -537,30 +537,30 @@ public void geosearchNegative() {
537537
// combine byradius and bybox
538538
try {
539539
jedis.geosearch("barcelona", new GeoSearchParam()
540-
.byRadius(3000, GeoUnit.M).byBox(300, 300, GeoUnit.M));
540+
.byRadius(3000, GeoUnit.M)
541+
.byBox(300, 300, GeoUnit.M));
541542
fail();
542-
} catch (redis.clients.jedis.exceptions.JedisDataException ignored) { }
543+
} catch (IllegalArgumentException ignored) { }
543544

544545
// without byradius and without bybox
545546
try {
546-
jedis.geosearch("barcelona", new GeoSearchParam()
547-
.fromMember("foobar"));
547+
jedis.geosearch("barcelona", new GeoSearchParam().fromMember("foobar"));
548548
fail();
549-
} catch (java.lang.IllegalArgumentException ignored) { }
549+
} catch (IllegalArgumentException ignored) { }
550550

551551
// combine frommember and fromlonlat
552552
try {
553553
jedis.geosearch("barcelona", new GeoSearchParam()
554-
.fromMember("foobar").fromLonLat(10,10));
554+
.fromMember("foobar")
555+
.fromLonLat(10,10));
555556
fail();
556-
} catch (java.lang.IllegalArgumentException ignored) { }
557+
} catch (IllegalArgumentException ignored) { }
557558

558559
// without frommember and without fromlonlat
559560
try {
560-
jedis.geosearch("barcelona", new GeoSearchParam()
561-
.byRadius(10, GeoUnit.MI));
561+
jedis.geosearch("barcelona", new GeoSearchParam().byRadius(10, GeoUnit.MI));
562562
fail();
563-
} catch (redis.clients.jedis.exceptions.JedisDataException ignored) { }
563+
} catch (IllegalArgumentException ignored) { }
564564
}
565565

566566
@Test

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ public void georadiusByMemberStore() {
8484
public void georadiusByMemberStoreBinary() {
8585
}
8686

87-
@Test
8887
@Ignore
88+
@Override
8989
public void geosearchstore() {
9090
}
9191

92-
@Test
9392
@Ignore
93+
@Override
9494
public void geosearchstoreWithdist() {
9595
}
9696
}

src/test/java/redis/clients/jedis/commands/unified/pipeline/GeoPipelineCommandsTest.java

+11-19
Original file line numberDiff line numberDiff line change
@@ -798,32 +798,24 @@ public void geosearch() {
798798
contains(0L));
799799
}
800800

801-
@Test
802-
public void geosearchNegative() {
803-
// combine byradius and bybox
804-
pipe.geosearch("barcelona",
805-
new GeoSearchParam().byRadius(3000, GeoUnit.M).byBox(300, 300, GeoUnit.M));
806-
807-
// without frommember and without fromlonlat
808-
pipe.geosearch("barcelona",
809-
new GeoSearchParam().byRadius(10, GeoUnit.MI));
801+
@Test(expected = IllegalArgumentException.class)
802+
public void geosearchSearchParamCombineFromMemberAndFromLonLat() {
803+
pipe.geosearch("barcelona", new GeoSearchParam().fromMember("foobar").fromLonLat(10, 10));
804+
}
810805

811-
assertThat(pipe.syncAndReturnAll(), contains(
812-
instanceOf(JedisDataException.class),
813-
instanceOf(JedisDataException.class)
814-
));
806+
@Test(expected = IllegalArgumentException.class)
807+
public void geosearchSearchParamWithoutFromMemberAndFromLonLat() {
808+
pipe.geosearch("barcelona", new GeoSearchParam().byRadius(10, GeoUnit.MI));
815809
}
816810

817811
@Test(expected = IllegalArgumentException.class)
818-
public void geosearchSearchParamWithoutRadiousAndWithoutBox() {
819-
pipe.geosearch("barcelona",
820-
new GeoSearchParam().fromMember("foobar"));
812+
public void geosearchSearchParamCombineByRadiousAndByBox() {
813+
pipe.geosearch("barcelona", new GeoSearchParam().byRadius(3000, GeoUnit.M).byBox(300, 300, GeoUnit.M));
821814
}
822815

823816
@Test(expected = IllegalArgumentException.class)
824-
public void geosearchSearchParamCombineMemberAndLonLat() {
825-
pipe.geosearch("barcelona",
826-
new GeoSearchParam().fromMember("foobar").fromLonLat(10, 10));
817+
public void geosearchSearchParamWithoutByRadiousAndByBox() {
818+
pipe.geosearch("barcelona", new GeoSearchParam().fromMember("foobar"));
827819
}
828820

829821
@Test

0 commit comments

Comments
 (0)