sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1688996 - in /sis/branches/JDK8/core: sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTFormatTest.java
Date Fri, 03 Jul 2015 10:59:41 GMT
Author: desruisseaux
Date: Fri Jul  3 10:59:41 2015
New Revision: 1688996

URL: http://svn.apache.org/r1688996
Log:
WKT 2: add a test about the warning in case of invalid character.

Modified:
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTFormatTest.java

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java?rev=1688996&r1=1688995&r2=1688996&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Formatter.java
[UTF-8] Fri Jul  3 10:59:41 2015
@@ -973,16 +973,26 @@ public class Formatter implements Locali
         final int base = buffer.appendCodePoint(symbols.getOpeningQuote(0)).length();
         if (type != ElementKind.REMARKS) {
             text = transliterator.filter(text);
+            int startAt = 0; // Index of the last space character.
             final int length = text.length();
             for (int i = 0; i < length;) {
-                final int c = text.codePointAt(i);
-                final int n = Character.charCount(c);
+                int c = text.codePointAt(i);
+                int n = Character.charCount(c);
                 if (!Characters.isValidWKT(c)) {
+                    final String illegal = text.substring(i, i+n);
+                    while ((i += n) < length) {
+                        c = text.codePointAt(i);
+                        n = Character.charCount(c);
+                        if (c == ' ' || c == '_') break;
+                    }
                     warnings().add(Errors.formatInternational(Errors.Keys.IllegalCharacterForFormat_3,
-                            "Well-Known Text", text, text.substring(i, i+n)), null, null);
+                            "Well-Known Text", text.substring(startAt, i), illegal), null,
null);
                     break;
                 }
                 i += n;
+                if (c == ' ' || c == '_') {
+                    startAt = i;
+                }
             }
         }
         buffer.append(text);

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTFormatTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTFormatTest.java?rev=1688996&r1=1688995&r2=1688996&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTFormatTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/WKTFormatTest.java
[UTF-8] Fri Jul  3 10:59:41 2015
@@ -16,8 +16,11 @@
  */
 package org.apache.sis.io.wkt;
 
+import java.util.Collections;
 import java.text.ParseException;
+import javax.measure.unit.NonSI;
 import org.opengis.referencing.crs.VerticalCRS;
+import org.apache.sis.referencing.datum.DefaultPrimeMeridian;
 import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
@@ -227,4 +230,38 @@ public final strictfp class WKTFormatTes
         final Object reparsed = format.parseObject(reformat);
         assertEqualsIgnoreMetadata(expected, reparsed);
     }
+
+    /**
+     * Tests the production of a warning messages when the WKT contains unformattable elements.
+     *
+     * @throws ParseException if the parsing (tested after formatting) failed.
+     */
+    @Test
+    public void testWarnings() throws ParseException {
+        DefaultPrimeMeridian pm = new DefaultPrimeMeridian(Collections.singletonMap(
+                DefaultPrimeMeridian.NAME_KEY, "Invalid “$name” here"), -10, NonSI.DEGREE_ANGLE);
+        format = new WKTFormat(null, null);
+        final String   wkt      = format.format(pm);
+        final Warnings warnings = format.getWarnings();
+        assertNotNull("warnings", warnings);
+        assertEquals ("warnings.numMessages", 1, warnings.getNumMessages());
+        assertEquals ("PrimeMeridian[\"Invalid \"\"$name\"\" here\", -10.0, AngleUnit[\"degree\",
0.017453292519943295]]", wkt);
+        assertEquals ("The “$” character in “\"$name\"” is not permitted by the “Well-Known
Text” format.", warnings.getMessage(0));
+        assertNull   (warnings.getException(0));
+        /*
+         * Verify that FormattableObject.toWKT() reports that the WKT is invalid.
+         */
+        try {
+            pm.toWKT();
+            fail("Expected UnformattableObjectException.");
+        } catch (UnformattableObjectException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("$name"));
+        }
+        /*
+         * Verify that the WKT is still parseable despite the warning.
+         */
+        pm = (DefaultPrimeMeridian) format.parseObject(wkt);
+        assertEquals("Invalid \"$name\" here", pm.getName().getCode());
+    }
 }



Mime
View raw message