1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| import com.alibaba.fastjson.JSON; import com.imooc.ad.Application; import com.imooc.ad.vo.CityInfo; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.geo.Circle; import org.springframework.data.geo.Distance; import org.springframework.data.geo.Metrics; import org.springframework.data.geo.Point; import org.springframework.data.redis.connection.RedisGeoCommands; import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList; import java.util.Arrays; import java.util.List;
@RunWith(SpringRunner.class) @SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.NONE) public class GeoServiceTest {
private List<CityInfo> cityInfos;
@Autowired private IGeoService geoService;
@Before public void init() {
cityInfos = new ArrayList<>();
cityInfos.add(new CityInfo("hefei", 117.17, 31.52)); cityInfos.add(new CityInfo("anqing", 117.02, 30.31)); cityInfos.add(new CityInfo("huaibei", 116.47, 33.57)); cityInfos.add(new CityInfo("suzhou", 116.58, 33.38)); cityInfos.add(new CityInfo("fuyang", 115.48, 32.54)); cityInfos.add(new CityInfo("bengbu", 117.21, 32.56)); cityInfos.add(new CityInfo("huangshan", 118.18, 29.43)); }
@Test public void testSaveCityInfoToRedis() {
System.out.println(geoService.saveCityInfoToRedis(cityInfos)); }
@Test public void testGetCityPos() {
System.out.println(JSON.toJSONString(geoService.getCityPos( Arrays.asList("anqing", "suzhou", "xxx").toArray(new String[3]) ))); }
@Test public void testGetTwoCityDistance() {
System.out.println(geoService.getTwoCityDistance("anqing", "suzhou", null).getValue()); System.out.println(geoService.getTwoCityDistance("anqing", "suzhou", Metrics.KILOMETERS).getValue()); }
@Test public void testGetPointRadius() {
Point center = new Point(cityInfos.get(0).getLongitude(), cityInfos.get(0).getLatitude()); Distance radius = new Distance(200, Metrics.KILOMETERS); Circle within = new Circle(center, radius);
System.out.println(JSON.toJSONString(geoService.getPointRadius(within, null)));
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().limit(2).sortAscending(); System.out.println(JSON.toJSONString(geoService.getPointRadius(within, args))); }
@Test public void testGetMemberRadius() {
Distance radius = new Distance(200, Metrics.KILOMETERS);
System.out.println(JSON.toJSONString(geoService.getMemberRadius("suzhou", radius, null)));
RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance().limit(2).sortAscending(); System.out.println(JSON.toJSONString(geoService.getMemberRadius("suzhou", radius, args))); }
@Test public void testGetCityGeoHash() {
System.out.println(JSON.toJSONString(geoService.getCityGeoHash( Arrays.asList("anqing", "suzhou", "xxx").toArray(new String[3]) ))); } }
|