|
1 | 1 | /* |
2 | | - * Copyright 2015 the original author or authors. |
| 2 | + * Copyright 2015-2016 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
29 | 29 | import org.junit.runner.RunWith; |
30 | 30 | import org.springframework.beans.factory.annotation.Autowired; |
31 | 31 | import org.springframework.context.annotation.Configuration; |
| 32 | +import org.springframework.dao.DataAccessException; |
32 | 33 | import org.springframework.data.annotation.Id; |
33 | 34 | import org.springframework.data.annotation.PersistenceConstructor; |
34 | 35 | import org.springframework.data.geo.GeoResults; |
35 | 36 | import org.springframework.data.geo.Metric; |
36 | 37 | import org.springframework.data.geo.Metrics; |
37 | 38 | import org.springframework.data.geo.Point; |
38 | 39 | import org.springframework.data.mongodb.config.AbstractMongoConfiguration; |
| 40 | +import org.springframework.data.mongodb.core.CollectionCallback; |
39 | 41 | import org.springframework.data.mongodb.core.MongoTemplate; |
40 | 42 | import org.springframework.data.mongodb.core.index.GeoSpatialIndexType; |
41 | 43 | import org.springframework.data.mongodb.core.index.GeoSpatialIndexed; |
42 | 44 | import org.springframework.data.mongodb.core.index.GeospatialIndex; |
43 | 45 | import org.springframework.data.mongodb.core.mapping.Document; |
44 | 46 | import org.springframework.data.mongodb.core.query.NearQuery; |
45 | 47 | import org.springframework.data.mongodb.core.query.Query; |
| 48 | +import org.springframework.data.mongodb.test.util.BasicDbListBuilder; |
46 | 49 | import org.springframework.test.context.ContextConfiguration; |
47 | 50 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
48 | 51 |
|
| 52 | +import com.mongodb.BasicDBObject; |
| 53 | +import com.mongodb.DBCollection; |
49 | 54 | import com.mongodb.Mongo; |
50 | 55 | import com.mongodb.MongoClient; |
| 56 | +import com.mongodb.MongoException; |
51 | 57 | import com.mongodb.WriteConcern; |
52 | 58 |
|
53 | 59 | /** |
@@ -317,6 +323,66 @@ public void nearWithMinAndMaxDistance() { |
317 | 323 | assertThat(venues.size(), is(2)); |
318 | 324 | } |
319 | 325 |
|
| 326 | +/** |
| 327 | + * @see DATAMONGO-1453 |
| 328 | + */ |
| 329 | +@Test |
| 330 | +public void shouldConvertPointRepresentationCorrectlyWhenSourceCoordinatesUsesInteger() { |
| 331 | + |
| 332 | +this.template.execute(template.getCollectionName(DocumentWithPropertyUsingGeoJsonType.class), |
| 333 | +new CollectionCallback<Object>() { |
| 334 | + |
| 335 | +@Override |
| 336 | +public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { |
| 337 | + |
| 338 | +BasicDBObject pointRepresentation = new BasicDBObject(); |
| 339 | +pointRepresentation.put("type", "Point"); |
| 340 | +pointRepresentation.put("coordinates", new BasicDbListBuilder().add(0).add(0).get()); |
| 341 | + |
| 342 | +BasicDBObject document = new BasicDBObject(); |
| 343 | +document.append("_id", "datamongo-1453"); |
| 344 | +document.append("geoJsonPoint", pointRepresentation); |
| 345 | + |
| 346 | +return collection.save(document); |
| 347 | +} |
| 348 | +}); |
| 349 | + |
| 350 | +assertThat(template.findOne(query(where("id").is("datamongo-1453")), |
| 351 | +DocumentWithPropertyUsingGeoJsonType.class).geoJsonPoint, is(equalTo(new GeoJsonPoint(0D, 0D)))); |
| 352 | +} |
| 353 | + |
| 354 | +/** |
| 355 | + * @see DATAMONGO-1453 |
| 356 | + */ |
| 357 | +@Test |
| 358 | +public void shouldConvertLineStringRepresentationCorrectlyWhenSourceCoordinatesUsesInteger() { |
| 359 | + |
| 360 | +this.template.execute(template.getCollectionName(DocumentWithPropertyUsingGeoJsonType.class), |
| 361 | +new CollectionCallback<Object>() { |
| 362 | + |
| 363 | +@Override |
| 364 | +public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { |
| 365 | + |
| 366 | +BasicDBObject lineStringRepresentation = new BasicDBObject(); |
| 367 | +lineStringRepresentation.put("type", "LineString"); |
| 368 | +lineStringRepresentation.put("coordinates", |
| 369 | +new BasicDbListBuilder().add(new BasicDbListBuilder().add(0).add(0).get()) |
| 370 | +.add(new BasicDbListBuilder().add(1).add(1).get()).get()); |
| 371 | + |
| 372 | +BasicDBObject document = new BasicDBObject(); |
| 373 | +document.append("_id", "datamongo-1453"); |
| 374 | +document.append("geoJsonLineString", lineStringRepresentation); |
| 375 | + |
| 376 | +return collection.save(document); |
| 377 | +} |
| 378 | +}); |
| 379 | + |
| 380 | +assertThat( |
| 381 | +template.findOne(query(where("id").is("datamongo-1453")), |
| 382 | +DocumentWithPropertyUsingGeoJsonType.class).geoJsonLineString, |
| 383 | +is(equalTo(new GeoJsonLineString(new Point(0D, 0D), new Point(1, 1))))); |
| 384 | +} |
| 385 | + |
320 | 386 | private void addVenues() { |
321 | 387 |
|
322 | 388 | template.insert(new Venue2DSphere("Penn Station", -73.99408, 40.75057)); |
|
0 commit comments