18
18
import static org .hamcrest .CoreMatchers .*;
19
19
import static org .junit .Assert .*;
20
20
21
- import java .util .List ;
21
+ import java .util .Arrays ;
22
22
23
23
import org .junit .Test ;
24
24
import org .springframework .data .geo .Box ;
25
25
import org .springframework .data .geo .Circle ;
26
+ import org .springframework .data .geo .Distance ;
27
+ import org .springframework .data .geo .Metrics ;
26
28
import org .springframework .data .geo .Point ;
27
29
import org .springframework .data .geo .Polygon ;
28
30
import org .springframework .data .mongodb .core .convert .GeoConverters .BoxToDbObjectConverter ;
29
31
import org .springframework .data .mongodb .core .convert .GeoConverters .CircleToDbObjectConverter ;
30
32
import org .springframework .data .mongodb .core .convert .GeoConverters .DbObjectToBoxConverter ;
31
33
import org .springframework .data .mongodb .core .convert .GeoConverters .DbObjectToCircleConverter ;
32
34
import org .springframework .data .mongodb .core .convert .GeoConverters .DbObjectToLegacyCircleConverter ;
35
+ import org .springframework .data .mongodb .core .convert .GeoConverters .DbObjectToPointConverter ;
33
36
import org .springframework .data .mongodb .core .convert .GeoConverters .DbObjectToPolygonConverter ;
34
37
import org .springframework .data .mongodb .core .convert .GeoConverters .DbObjectToSphereConverter ;
38
+ import org .springframework .data .mongodb .core .convert .GeoConverters .GeoCommandToDbObjectConverter ;
35
39
import org .springframework .data .mongodb .core .convert .GeoConverters .LegacyCircleToDbObjectConverter ;
36
- import org .springframework .data .mongodb .core .convert .GeoConverters .ListToPointConverter ;
37
- import org .springframework .data .mongodb .core .convert .GeoConverters .PointToListConverter ;
40
+ import org .springframework .data .mongodb .core .convert .GeoConverters .PointToDbObjectConverter ;
38
41
import org .springframework .data .mongodb .core .convert .GeoConverters .PolygonToDbObjectConverter ;
39
42
import org .springframework .data .mongodb .core .convert .GeoConverters .SphereToDbObjectConverter ;
40
43
import org .springframework .data .mongodb .core .geo .Sphere ;
44
+ import org .springframework .data .mongodb .core .query .GeoCommand ;
41
45
42
- import com .mongodb .BasicDBList ;
46
+ import com .mongodb .DBObject ;
43
47
44
48
/**
45
49
* Unit tests for {@link GeoConverters}.
46
50
*
47
51
* @author Thomas Darimont
52
+ * @author Oliver Gierke
48
53
* @since 1.5
49
54
*/
50
55
@ SuppressWarnings ("deprecation" )
@@ -58,7 +63,7 @@ public void convertsBoxToDbObjectAndBackCorrectly() {
58
63
59
64
Box box = new Box (new Point (1 , 2 ), new Point (3 , 4 ));
60
65
61
- BasicDBList dbo = BoxToDbObjectConverter .INSTANCE .convert (box );
66
+ DBObject dbo = BoxToDbObjectConverter .INSTANCE .convert (box );
62
67
Box result = DbObjectToBoxConverter .INSTANCE .convert (dbo );
63
68
64
69
assertThat (result , is (box ));
@@ -69,16 +74,32 @@ public void convertsBoxToDbObjectAndBackCorrectly() {
69
74
* @see DATAMONGO-858
70
75
*/
71
76
@ Test
72
- public void convertsCircleToDbObjectAndBackCorrectly () {
77
+ public void convertsCircleToDbObjectAndBackCorrectlyNeutralDistance () {
73
78
74
79
Circle circle = new Circle (new Point (1 , 2 ), 3 );
75
80
76
- BasicDBList dbo = CircleToDbObjectConverter .INSTANCE .convert (circle );
81
+ DBObject dbo = CircleToDbObjectConverter .INSTANCE .convert (circle );
77
82
Circle result = DbObjectToCircleConverter .INSTANCE .convert (dbo );
78
83
79
84
assertThat (result , is (circle ));
80
85
}
81
86
87
+ /**
88
+ * @see DATAMONGO-858
89
+ */
90
+ @ Test
91
+ public void convertsCircleToDbObjectAndBackCorrectlyMilesDistance () {
92
+
93
+ Distance radius = new Distance (3 , Metrics .MILES );
94
+ Circle circle = new Circle (new Point (1 , 2 ), radius );
95
+
96
+ DBObject dbo = CircleToDbObjectConverter .INSTANCE .convert (circle );
97
+ Circle result = DbObjectToCircleConverter .INSTANCE .convert (dbo );
98
+
99
+ assertThat (result , is (circle ));
100
+ assertThat (result .getRadius (), is (radius ));
101
+ }
102
+
82
103
/**
83
104
* @see DATAMONGO-858
84
105
*/
@@ -88,7 +109,7 @@ public void convertsLegacyCircleToDbObjectAndBackCorrectly() {
88
109
org .springframework .data .mongodb .core .geo .Circle circle = new org .springframework .data .mongodb .core .geo .Circle (
89
110
new Point (1 , 2 ), 3 );
90
111
91
- BasicDBList dbo = LegacyCircleToDbObjectConverter .INSTANCE .convert (circle );
112
+ DBObject dbo = LegacyCircleToDbObjectConverter .INSTANCE .convert (circle );
92
113
org .springframework .data .mongodb .core .geo .Circle result = DbObjectToLegacyCircleConverter .INSTANCE .convert (dbo );
93
114
94
115
assertThat (result , is (circle ));
@@ -102,7 +123,7 @@ public void convertsPolygonToDbObjectAndBackCorrectly() {
102
123
103
124
Polygon polygon = new Polygon (new Point (1 , 2 ), new Point (2 , 3 ), new Point (3 , 4 ), new Point (5 , 6 ));
104
125
105
- BasicDBList dbo = PolygonToDbObjectConverter .INSTANCE .convert (polygon );
126
+ DBObject dbo = PolygonToDbObjectConverter .INSTANCE .convert (polygon );
106
127
Polygon result = DbObjectToPolygonConverter .INSTANCE .convert (dbo );
107
128
108
129
assertThat (result , is (polygon ));
@@ -113,14 +134,31 @@ public void convertsPolygonToDbObjectAndBackCorrectly() {
113
134
* @see DATAMONGO-858
114
135
*/
115
136
@ Test
116
- public void convertsSphereToDbObjectAndBackCorrectly () {
137
+ public void convertsSphereToDbObjectAndBackCorrectlyWithNeutralDistance () {
117
138
118
139
Sphere sphere = new Sphere (new Point (1 , 2 ), 3 );
119
140
120
- BasicDBList dbo = SphereToDbObjectConverter .INSTANCE .convert (sphere );
141
+ DBObject dbo = SphereToDbObjectConverter .INSTANCE .convert (sphere );
142
+ Sphere result = DbObjectToSphereConverter .INSTANCE .convert (dbo );
143
+
144
+ assertThat (result , is (sphere ));
145
+ assertThat (result .getClass ().equals (org .springframework .data .mongodb .core .geo .Sphere .class ), is (true ));
146
+ }
147
+
148
+ /**
149
+ * @see DATAMONGO-858
150
+ */
151
+ @ Test
152
+ public void convertsSphereToDbObjectAndBackCorrectlyWithKilometerDistance () {
153
+
154
+ Distance radius = new Distance (3 , Metrics .KILOMETERS );
155
+ Sphere sphere = new Sphere (new Point (1 , 2 ), radius );
156
+
157
+ DBObject dbo = SphereToDbObjectConverter .INSTANCE .convert (sphere );
121
158
Sphere result = DbObjectToSphereConverter .INSTANCE .convert (dbo );
122
159
123
160
assertThat (result , is (sphere ));
161
+ assertThat (result .getRadius (), is (radius ));
124
162
assertThat (result .getClass ().equals (org .springframework .data .mongodb .core .geo .Sphere .class ), is (true ));
125
163
}
126
164
@@ -132,10 +170,30 @@ public void convertsPointToListAndBackCorrectly() {
132
170
133
171
Point point = new Point (1 , 2 );
134
172
135
- List < Double > list = PointToListConverter .INSTANCE .convert (point );
136
- Point result = ListToPointConverter .INSTANCE .convert (list );
173
+ DBObject dbo = PointToDbObjectConverter .INSTANCE .convert (point );
174
+ Point result = DbObjectToPointConverter .INSTANCE .convert (dbo );
137
175
138
176
assertThat (result , is (point ));
139
177
assertThat (result .getClass ().equals (org .springframework .data .mongodb .core .geo .Point .class ), is (true ));
140
178
}
179
+
180
+ /**
181
+ * @see DATAMONGO-858
182
+ */
183
+ @ Test
184
+ @ SuppressWarnings ("unchecked" )
185
+ public void convertsGeoCommandToDbObjectCorrectly () {
186
+
187
+ Box box = new Box (new double [] { 1 , 2 }, new double [] { 3 , 4 });
188
+ GeoCommand cmd = new GeoCommand (box );
189
+
190
+ DBObject dbo = GeoCommandToDbObjectConverter .INSTANCE .convert (cmd );
191
+
192
+ assertThat (dbo , is (notNullValue ()));
193
+
194
+ DBObject boxObject = (DBObject ) dbo .get ("$box" );
195
+
196
+ assertThat (boxObject ,
197
+ is ((Object ) Arrays .asList (GeoConverters .toList (box .getFirst ()), GeoConverters .toList (box .getSecond ()))));
198
+ }
141
199
}
0 commit comments