4040import org .elasticsearch .common .xcontent .XContentBuilder ;
4141import org .elasticsearch .common .xcontent .XContentFactory ;
4242import org .elasticsearch .common .xcontent .XContentParser ;
43+ import org .elasticsearch .geo .geometry .Geometry ;
4344import org .elasticsearch .geo .geometry .Line ;
4445import org .elasticsearch .geo .geometry .MultiLine ;
4546import org .elasticsearch .geo .geometry .MultiPoint ;
@@ -112,27 +113,39 @@ public void testParsePoint() throws IOException {
112113
113114 @ Override
114115 public void testParseMultiPoint () throws IOException {
115- int numPoints = randomIntBetween (2 , 100 );
116+ int numPoints = randomIntBetween (0 , 100 );
116117 List <Coordinate > coordinates = new ArrayList <>(numPoints );
117118 for (int i = 0 ; i < numPoints ; ++i ) {
118119 coordinates .add (new Coordinate (GeoTestUtil .nextLongitude (), GeoTestUtil .nextLatitude ()));
119120 }
120121
121- Shape [] shapes = new Shape [ numPoints ] ;
122+ List < org . elasticsearch . geo . geometry . Point > points = new ArrayList <>( numPoints ) ;
122123 for (int i = 0 ; i < numPoints ; ++i ) {
123124 Coordinate c = coordinates .get (i );
124- shapes [ i ] = SPATIAL_CONTEXT . makePoint (c .x , c .y );
125+ points . add ( new org . elasticsearch . geo . geometry . Point (c .y , c .x ) );
125126 }
126- ShapeCollection <?> expected = shapeCollection (shapes );
127- assertExpected (expected , new MultiPointBuilder (coordinates ), true );
128127
129- List <org .elasticsearch .geo .geometry .Point > points = new ArrayList <>(numPoints );
128+ Geometry expectedGeom ;
129+ MultiPointBuilder actual ;
130+ if (numPoints == 0 ) {
131+ expectedGeom = MultiPoint .EMPTY ;
132+ actual = new MultiPointBuilder ();
133+ } else {
134+ expectedGeom = new MultiPoint (points );
135+ actual = new MultiPointBuilder (coordinates );
136+ }
137+
138+ assertExpected (expectedGeom , actual , false );
139+ assertMalformed (actual );
140+
141+ assumeTrue ("JTS test path cannot handle empty multipoints" , numPoints > 1 );
142+ Shape [] shapes = new Shape [numPoints ];
130143 for (int i = 0 ; i < numPoints ; ++i ) {
131144 Coordinate c = coordinates .get (i );
132- points . add ( new org . elasticsearch . geo . geometry . Point (c .y , c .x ) );
145+ shapes [ i ] = SPATIAL_CONTEXT . makePoint (c .x , c .y );
133146 }
134- assertExpected ( new MultiPoint ( points ), new MultiPointBuilder ( coordinates ), false );
135- assertMalformed ( new MultiPointBuilder (coordinates ));
147+ ShapeCollection <?> expected = shapeCollection ( shapes );
148+ assertExpected ( expected , new MultiPointBuilder (coordinates ), true );
136149 }
137150
138151 private List <Coordinate > randomLineStringCoords () {
@@ -163,7 +176,7 @@ public void testParseLineString() throws IOException {
163176
164177 @ Override
165178 public void testParseMultiLineString () throws IOException {
166- int numLineStrings = randomIntBetween (2 , 8 );
179+ int numLineStrings = randomIntBetween (0 , 8 );
167180 List <LineString > lineStrings = new ArrayList <>(numLineStrings );
168181 MultiLineStringBuilder builder = new MultiLineStringBuilder ();
169182 for (int j = 0 ; j < numLineStrings ; ++j ) {
@@ -173,18 +186,27 @@ public void testParseMultiLineString() throws IOException {
173186 builder .linestring (new LineStringBuilder (lsc ));
174187 }
175188
176- MultiLineString expected = GEOMETRY_FACTORY .createMultiLineString (
177- lineStrings .toArray (new LineString [lineStrings .size ()]));
178- assertExpected (jtsGeom (expected ), builder , true );
179-
180189 List <Line > lines = new ArrayList <>(lineStrings .size ());
181190 for (int j = 0 ; j < lineStrings .size (); ++j ) {
182191 Coordinate [] c = lineStrings .get (j ).getCoordinates ();
183192 lines .add (new Line (Arrays .stream (c ).mapToDouble (i ->i .y ).toArray (),
184193 Arrays .stream (c ).mapToDouble (i ->i .x ).toArray ()));
185194 }
186- assertExpected (new MultiLine (lines ), builder , false );
195+ Geometry expectedGeom ;
196+ if (lines .isEmpty ()) {
197+ expectedGeom = MultiLine .EMPTY ;
198+ } else if (lines .size () == 1 ) {
199+ expectedGeom = new Line (lines .get (0 ).getLats (), lines .get (0 ).getLons ());
200+ } else {
201+ expectedGeom = new MultiLine (lines );
202+ }
203+ assertExpected (expectedGeom , builder , false );
187204 assertMalformed (builder );
205+
206+ MultiLineString expected = GEOMETRY_FACTORY .createMultiLineString (
207+ lineStrings .toArray (new LineString [lineStrings .size ()]));
208+ assumeTrue ("JTS test path cannot handle empty multilinestrings" , numLineStrings > 1 );
209+ assertExpected (jtsGeom (expected ), builder , true );
188210 }
189211
190212 @ Override
@@ -201,7 +223,7 @@ public void testParsePolygon() throws IOException {
201223
202224 @ Override
203225 public void testParseMultiPolygon () throws IOException {
204- int numPolys = randomIntBetween (2 , 8 );
226+ int numPolys = randomIntBetween (0 , 8 );
205227 MultiPolygonBuilder builder = new MultiPolygonBuilder ();
206228 PolygonBuilder pb ;
207229 Coordinate [] coordinates ;
@@ -214,7 +236,7 @@ public void testParseMultiPolygon() throws IOException {
214236 shell = GEOMETRY_FACTORY .createLinearRing (coordinates );
215237 shapes [i ] = GEOMETRY_FACTORY .createPolygon (shell , null );
216238 }
217-
239+ assumeTrue ( "JTS test path cannot handle empty multipolygon" , numPolys > 1 );
218240 Shape expected = shapeCollection (shapes );
219241 assertExpected (expected , builder , true );
220242 assertMalformed (builder );
@@ -429,7 +451,6 @@ public void testInvalidGeometryType() throws IOException {
429451 assertValidException (builder , IllegalArgumentException .class );
430452 }
431453
432- @ AwaitsFix (bugUrl = "https://github.com/elastic/elasticsearch/issues/37894" )
433454 @ Override
434455 public void testParseGeometryCollection () throws IOException {
435456 if (rarely ()) {
0 commit comments