|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.sql.types; |
| 19 | + |
| 20 | +import org.apache.spark.sql.internal.types.SpatialReferenceSystemMapper; |
| 21 | +import org.junit.jupiter.api.Assertions; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | + |
| 24 | +import org.apache.spark.SparkIllegalArgumentException; |
| 25 | + |
| 26 | +import java.util.stream.Stream; |
| 27 | + |
| 28 | +public class JavaGeometryTypeSuite { |
| 29 | + |
| 30 | + /* |
| 31 | + * Test cases GeometryType construction based on SRID. |
| 32 | + */ |
| 33 | + |
| 34 | + @Test |
| 35 | + public void geometryTypeWithSpecifiedValidSridTest() { |
| 36 | + // Valid SRID values for GEOMETRY. |
| 37 | + Stream.of(0, 3857, 4326).forEach(srid -> { |
| 38 | + DataType geometryType = DataTypes.createGeometryType(srid); |
| 39 | + Assertions.assertEquals("GEOMETRY(" + srid + ")", geometryType.sql()); |
| 40 | + Assertions.assertEquals("geometry(" + srid + ")", geometryType.typeName()); |
| 41 | + Assertions.assertEquals("geometry(" + srid + ")", geometryType.simpleString()); |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void geometryTypeWithSpecifiedInvalidSridTest() { |
| 47 | + // Invalid SRID values for GEOMETRY. |
| 48 | + Stream.of(-1, -2, 1, 2).forEach(srid -> { |
| 49 | + try { |
| 50 | + DataTypes.createGeometryType(srid); |
| 51 | + Assertions.fail("Expected SparkIllegalArgumentException for SRID: " + srid); |
| 52 | + } catch (SparkIllegalArgumentException e) { |
| 53 | + Assertions.assertEquals("ST_INVALID_SRID_VALUE", e.getCondition()); |
| 54 | + Assertions.assertEquals(String.valueOf(srid), e.getMessageParameters().get("srid")); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + /* |
| 60 | + * Test cases GeometryType construction based on CRS. |
| 61 | + */ |
| 62 | + |
| 63 | + @Test |
| 64 | + public void geometryTypeWithSpecifiedValidCrsTest() { |
| 65 | + // Valid CRS values for GEOMETRY. |
| 66 | + SpatialReferenceSystemMapper srsMapper = SpatialReferenceSystemMapper.get(); |
| 67 | + Stream.of("SRID:0", "EPSG:3857", "OGC:CRS84").forEach(crs -> { |
| 68 | + int srid = srsMapper.getSrid(crs); |
| 69 | + DataType geometryType = DataTypes.createGeometryType(crs); |
| 70 | + Assertions.assertEquals("GEOMETRY(" + srid + ")", geometryType.sql()); |
| 71 | + Assertions.assertEquals("geometry(" + srid + ")", geometryType.typeName()); |
| 72 | + Assertions.assertEquals("geometry(" + srid + ")", geometryType.simpleString()); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void geometryTypeWithSpecifiedInvalidCrsTest() { |
| 78 | + // Invalid CRS values for GEOMETRY. |
| 79 | + Stream.of("0", "SRID", "SRID:-1", "SRID:4326", "CRS84", "").forEach(crs -> { |
| 80 | + try { |
| 81 | + DataTypes.createGeometryType(crs); |
| 82 | + Assertions.fail("Expected SparkIllegalArgumentException for CRS: " + crs); |
| 83 | + } catch (SparkIllegalArgumentException e) { |
| 84 | + Assertions.assertEquals("ST_INVALID_CRS_VALUE", e.getCondition()); |
| 85 | + Assertions.assertEquals(crs, e.getMessageParameters().get("crs")); |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void geometryTypeWithSpecifiedAnyTest() { |
| 92 | + // Special string value "ANY" in place of CRS is used to denote a mixed GEOMETRY type. |
| 93 | + DataType geometryType = DataTypes.createGeometryType("ANY"); |
| 94 | + Assertions.assertEquals("GEOMETRY(ANY)", geometryType.sql()); |
| 95 | + Assertions.assertEquals("geometry(any)", geometryType.typeName()); |
| 96 | + Assertions.assertEquals("geometry(any)", geometryType.simpleString()); |
| 97 | + } |
| 98 | +} |
0 commit comments