This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit 332592305c6cffe5249a739f28fe646014ec11ed Author: Martin Desruisseaux <[email protected]> AuthorDate: Wed Nov 18 19:16:30 2020 +0100 Add more tests. --- .../org/apache/sis/io/wkt/WKTDictionaryTest.java | 92 ++++++++++++++++++++-- .../resources/org/apache/sis/io/wkt/Malformed.txt | 13 +++ 2 files changed, 100 insertions(+), 5 deletions(-) diff --git a/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTDictionaryTest.java b/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTDictionaryTest.java index 76c6dae..cc88abf 100644 --- a/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTDictionaryTest.java +++ b/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTDictionaryTest.java @@ -23,6 +23,7 @@ import java.util.HashSet; import java.util.IdentityHashMap; import java.util.function.Consumer; import java.util.function.BiFunction; +import java.util.stream.Stream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; @@ -33,6 +34,7 @@ import org.opengis.referencing.crs.GeographicCRS; import org.opengis.referencing.crs.GeodeticCRS; import org.opengis.referencing.cs.AxisDirection; import org.opengis.referencing.IdentifiedObject; +import org.opengis.referencing.NoSuchAuthorityCodeException; import org.apache.sis.test.DependsOn; import org.apache.sis.test.TestCase; import org.junit.Test; @@ -51,6 +53,59 @@ import static org.apache.sis.test.Assert.*; @DependsOn(WKTFormatTest.class) public final strictfp class WKTDictionaryTest extends TestCase { /** + * Tests {@link WKTDictionary#addDefinitions(Stream)}. The CRS used in this test are a subset of the + * ones used by {@link #testLoad()}. One of them is intentionally malformed for testing error index. + * + * @throws FactoryException if an error occurred while parsing a WKT. + */ + @Test + public void testAddDefinitions() throws FactoryException { + final WKTDictionary factory = new WKTDictionary(null); + factory.addDefinitions(Arrays.asList( + "GeodCRS[\"Anguilla 1957\",\n" + + " Datum[\"Anguilla 1957\",\n" + + " Ellipsoid[\"Clarke 1880\", 6378249.145, 293.465]],\n" + + " CS[ellipsoidal, 2],\n" + + " Axis[\"Latitude\", north],\n" + + " Axis[\"Longitude\", east],\n" + + " Unit[\"Degree\", 0.0174532925199433],\n" + + " Id[\"TEST\", 21]]", + + "GeodCRS[\"Error index 69 (on Ellipsoid)\", Datum[\"Erroneous\", Ellipsoid[\"Missing axis length\"]],\n" + + " CS[ellipsoidal, 2],\n" + + " Axis[\"Latitude\", north],\n" + + " Axis[\"Longitude\", east],\n" + + " Unit[\"Degree\", 0.0174532925199433],\n" + + " Id[\"TEST\", \"E1\"]]").stream()); + /* + * Codes can be in any order. Code spaces are omitted when there is no ambiguity. + */ + assertArrayEquals("getCodeSpaces()", new String[] {"TEST"}, factory.getCodeSpaces().toArray()); + assertEquals("getAuthority()", "TEST", factory.getAuthority().getTitle().toString()); + Set<String> codes = factory.getAuthorityCodes(IdentifiedObject.class); + assertSame( codes, factory.getAuthorityCodes(SingleCRS.class)); + assertSame( codes, factory.getAuthorityCodes(GeodeticCRS.class)); + assertSame( codes, factory.getAuthorityCodes(GeographicCRS.class)); + assertEquals(0, factory.getAuthorityCodes(ProjectedCRS.class).size()); + assertSetEquals(Arrays.asList("21", "E1"), codes); + /* + * Tests CRS creation, potentially with expected error. + */ + verifyCRS(factory.createGeographicCRS("21"), "Anguilla 1957"); + verifyErroneousCRS(factory, "E1", 69); + /* + * Test non-existing CRS. + */ + try { + factory.createGeographicCRS("84"); + fail("Expected exception for non-existent CRS."); + } catch (NoSuchAuthorityCodeException e) { + final String message = e.getMessage(); + assertTrue(message, message.contains("84")); + } + } + + /** * Tests {@link WKTDictionary#load(BufferedReader)}. * * @throws IOException if an error occurred while reading the test file. @@ -182,9 +237,9 @@ public final strictfp class WKTDictionaryTest extends TestCase { /** * Verifies a projected CRS. * - * @param crs the CRS to verify. - * @param name expected CRS name. - * @param φ0 expected latitude of origin. + * @param crs the CRS to verify. + * @param name expected CRS name. + * @param φ0 expected latitude of origin. */ private static void verifyCRS(final ProjectedCRS crs, final String name, final double φ0) { assertEquals("name", name, crs.getName().getCode()); @@ -197,8 +252,8 @@ public final strictfp class WKTDictionaryTest extends TestCase { /** * Verifies a geographic CRS. * - * @param crs the CRS to verify. - * @param name expected CRS name. + * @param crs the CRS to verify. + * @param name expected CRS name. */ private static void verifyCRS(final GeographicCRS crs, final String name) { assertEquals("name", name, crs.getName().getCode()); @@ -250,4 +305,31 @@ public final strictfp class WKTDictionaryTest extends TestCase { assertNull(e.getCause()); } } + + /** + * Tests {@link WKTDictionary#load(BufferedReader)} with a malformed file. + * + * @throws IOException if an error occurred while reading the test file. + */ + @Test + public void testLoadMalformed() throws IOException { + FactoryException ex; + final WKTDictionary factory = new WKTDictionary(null); + try (BufferedReader source = new BufferedReader(new InputStreamReader( + WKTFormatTest.class.getResourceAsStream("Malformed.txt"), "UTF-8"))) + { + factory.load(source); + fail("Should not have accepted to load the file."); + return; + } catch (FactoryException e) { + ex = e; + } + /* + * Except a message like: Can not read file at line 13. Cause is: missing ']' in "GeodCRS" element. + * The exact message is locale-dependent, so we test for a few keywords only. + */ + final String message = ex.getMessage(); + assertTrue(message, message.contains("GeodCRS")); + assertTrue(message, message.contains("‘]’")); + } } diff --git a/core/sis-referencing/src/test/resources/org/apache/sis/io/wkt/Malformed.txt b/core/sis-referencing/src/test/resources/org/apache/sis/io/wkt/Malformed.txt new file mode 100644 index 0000000..e9150a2 --- /dev/null +++ b/core/sis-referencing/src/test/resources/org/apache/sis/io/wkt/Malformed.txt @@ -0,0 +1,13 @@ +# +# Test file for org.apache.sis.io.wkt.ReferencingFactory. +# See "doc-files/ESRI.txt" for file syntax. +# + +GeodCRS["Anguilla 1957", + Datum["Anguilla 1957", + Ellipsoid["Clarke 1880", 6378249.145, 293.465]], + CS[ellipsoidal, 2], + Axis["Latitude", north], + Axis["Longitude", east], + Unit["Degree", 0.0174532925199433], + # Intentionally missing closing bracket.
