You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2022/07/16 21:11:08 UTC

[sis] 03/03: Better error message for invalid sexagesimal angles.

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 51cfa9cbab075c91455e9d7188f79f9e92132a2a
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Sat Jul 16 19:04:36 2022 +0200

    Better error message for invalid sexagesimal angles.
---
 .../org/apache/sis/measure/SexagesimalConverter.java   | 14 ++++++--------
 .../java/org/apache/sis/util/resources/Errors.java     | 12 ++++++------
 .../org/apache/sis/util/resources/Errors.properties    |  2 +-
 .../org/apache/sis/util/resources/Errors_fr.properties |  2 +-
 .../apache/sis/measure/SexagesimalConverterTest.java   | 18 +++++++++++++++++-
 5 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/core/sis-utility/src/main/java/org/apache/sis/measure/SexagesimalConverter.java b/core/sis-utility/src/main/java/org/apache/sis/measure/SexagesimalConverter.java
index bb87fd296a..0e9122189e 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/measure/SexagesimalConverter.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/measure/SexagesimalConverter.java
@@ -20,7 +20,6 @@ import javax.measure.Unit;
 import javax.measure.quantity.Angle;
 import javax.measure.UnitConverter;
 import org.apache.sis.util.resources.Errors;
-import org.apache.sis.util.resources.Vocabulary;
 import org.apache.sis.internal.util.Numerics;
 import org.apache.sis.math.MathFunctions;
 
@@ -40,7 +39,7 @@ import static org.apache.sis.math.MathFunctions.truncate;
  * This class and all inner classes are immutable, and thus inherently thread-safe.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
- * @version 1.0
+ * @version 1.2
  * @since   0.3
  * @module
  */
@@ -280,7 +279,7 @@ class SexagesimalConverter extends AbstractConverter {
                     if (min >= 0) deg++; else deg--;
                     min = 0;
                 } else {
-                    throw illegalField(angle, min, Vocabulary.Keys.AngularMinutes);
+                    throw illegalField(angle, min, 0);
                 }
             }
             if (sec <= -60 || sec >= 60) {                              // Do not enter for NaN
@@ -288,7 +287,7 @@ class SexagesimalConverter extends AbstractConverter {
                     if (sec >= 0) min++; else min--;
                     sec = 0;
                 } else {
-                    throw illegalField(angle, sec, Vocabulary.Keys.AngularSeconds);
+                    throw illegalField(angle, sec, 1);
                 }
             }
             return (sec/60 + min)/60 + deg;
@@ -299,12 +298,11 @@ class SexagesimalConverter extends AbstractConverter {
          *
          * @param  value  the user-supplied angle value.
          * @param  field  the value of the illegal field.
-         * @param  unit   the vocabulary key for the field (minutes or seconds).
+         * @param  unit   0 for minutes or 1 for seconds.
          * @return the exception to throw.
          */
-        private static IllegalArgumentException illegalField(final double value, final double field, final short unit) {
-            return new IllegalArgumentException(Errors.format(Errors.Keys.IllegalArgumentField_4,
-                    "angle", value, Vocabulary.format(unit), field));
+        private static IllegalArgumentException illegalField(final double value, final double field, final int unit) {
+            return new IllegalArgumentException(Errors.format(Errors.Keys.IllegalSexagesimalField_3, value, unit, field));
         }
     }
 }
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
index 739c255be8..7b3d1dfe90 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
@@ -339,12 +339,6 @@ public final class Errors extends IndexedResourceBundle {
          */
         public static final short IllegalArgumentClass_3 = 43;
 
-        /**
-         * Argument ‘{0}’ can not take the “{1}” value, because the ‘{2}’ field can not take the “{3}”
-         * value.
-         */
-        public static final short IllegalArgumentField_4 = 44;
-
         /**
          * Argument ‘{0}’ can not take the “{1}” value.
          */
@@ -435,6 +429,12 @@ public final class Errors extends IndexedResourceBundle {
          */
         public static final short IllegalRange_2 = 60;
 
+        /**
+         * Sexagesimal angle {0,number} is illegal because the {1,choice,0#minutes|1#seconds} field can
+         * not take the {2,number} value.
+         */
+        public static final short IllegalSexagesimalField_3 = 44;
+
         /**
          * Value {1} for “{0}” is not a valid Unicode code point.
          */
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
index c0b3168576..8ea926426d 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
@@ -79,7 +79,6 @@ ForbiddenAttribute_2              = Attribute \u201c{0}\u201d is not allowed for
 ForbiddenProperty_1               = Property \u201c{0}\u201d is not allowed.
 IllegalArgumentClass_2            = Argument \u2018{0}\u2019 can not be an instance of \u2018{1}\u2019.
 IllegalArgumentClass_3            = Argument \u2018{0}\u2019 can not be an instance of \u2018{2}\u2019. Expected an instance of \u2018{1}\u2019 or derived type.
-IllegalArgumentField_4            = Argument \u2018{0}\u2019 can not take the \u201c{1}\u201d value, because the \u2018{2}\u2019 field can not take the \u201c{3}\u201d value.
 IllegalArgumentValue_2            = Argument \u2018{0}\u2019 can not take the \u201c{1}\u201d value.
 IllegalBitsPattern_1              = Illegal bits pattern: {0}.
 IllegalCharacter_2                = The \u201c{1}\u201d character can not be used for \u201c{0}\u201d.
@@ -98,6 +97,7 @@ IllegalPropertyValue_2            = Property \u201c{0}\u201d can not take the \u
 IllegalPropertyValueClass_2       = Property \u201c{0}\u201d does not accept instances of \u2018{1}\u2019.
 IllegalPropertyValueClass_3       = Expected an instance of \u2018{1}\u2019 for the \u201c{0}\u201d property, but got an instance of \u2018{2}\u2019.
 IllegalRange_2                    = Range [{0} \u2026 {1}] is not valid.
+IllegalSexagesimalField_3         = Sexagesimal angle {0,number} is illegal because the {1,choice,0#minutes|1#seconds} field can not take the {2,number} value.
 IllegalUnicodeCodePoint_2         = Value {1} for \u201c{0}\u201d is not a valid Unicode code point.
 IllegalValueForProperty_2         = Illegal value for property \u201c{1}\u201d in \u201c{0}\u201d.
 IncompatibleFormat_2              = Can not use the {1} format with \u201c{0}\u201d.
diff --git a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
index b93ab77358..673dd4df77 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
+++ b/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
@@ -76,7 +76,6 @@ ForbiddenAttribute_2              = L\u2019attribut \u00ab\u202f{0}\u202f\u00bb
 ForbiddenProperty_1               = La propri\u00e9t\u00e9 \u00ab\u202f{0}\u202f\u00bb n\u2019est pas autoris\u00e9e.
 IllegalArgumentClass_2            = L\u2019argument \u2018{0}\u2019 ne peut pas \u00eatre de type \u2018{1}\u2019.
 IllegalArgumentClass_3            = L\u2019argument \u2018{0}\u2019 ne peut pas \u00eatre de type \u2018{2}\u2019. Une instance de \u2018{1}\u2019 ou d\u2019un type d\u00e9riv\u00e9 \u00e9tait attendue.
-IllegalArgumentField_4            = L\u2019argument \u2018{0}\u2019 n\u2019accepte pas la valeur \u00ab\u202f{1}\u202f\u00bb parce que le champs \u2018{2}\u2019 ne peut pas prendre la valeur \u00ab\u202f{3}\u202f\u00bb.
 IllegalArgumentValue_2            = L\u2019argument \u2018{0}\u2019 n\u2019accepte pas la valeur \u00ab\u202f{1}\u202f\u00bb.
 IllegalBitsPattern_1              = Pattern de bits invalide\u00a0: {0}.
 IllegalClass_2                    = La classe \u2018{1}\u2019 est ill\u00e9gale. Il doit s\u2019agir d\u2019une classe \u2018{0}\u2019 ou d\u00e9riv\u00e9e.
@@ -95,6 +94,7 @@ IllegalPropertyValue_2            = La propri\u00e9t\u00e9 \u00ab\u202f{0}\u202f
 IllegalPropertyValueClass_2       = La propri\u00e9t\u00e9 \u00ab\u202f{0}\u202f\u00bb n\u2019accepte pas les valeurs de type \u2018{1}\u2019.
 IllegalPropertyValueClass_3       = Une instance \u2018{1}\u2019 \u00e9tait attendue pour la propri\u00e9t\u00e9 \u00ab\u202f{0}\u202f\u00bb, mais la valeur donn\u00e9e est une instance de \u2018{2}\u2019.
 IllegalRange_2                    = La plage [{0} \u2026 {1}] n\u2019est pas valide.
+IllegalSexagesimalField_3         = L\u2019angle sexag\u00e9simal {0,number} est invalide parce que le champs des {1,choice,0#minutes|1#secondes} ne peut pas prendre la valeur {2,number}.
 IllegalUnicodeCodePoint_2         = La valeur {1} de \u00ab\u202f{0}\u202f\u00bb n\u2019est pas un code Unicode valide.
 IllegalValueForProperty_2         = Valeur ill\u00e9gale pour la propri\u00e9t\u00e9 \u00ab\u202f{1}\u202f\u00bb dans \u00ab\u202f{0}\u202f\u00bb.
 IncompatibleFormat_2              = Le format {1} ne s\u2019applique pas \u00e0 \u00ab\u202f{0}\u202f\u00bb.
diff --git a/core/sis-utility/src/test/java/org/apache/sis/measure/SexagesimalConverterTest.java b/core/sis-utility/src/test/java/org/apache/sis/measure/SexagesimalConverterTest.java
index 041dfd50ef..fa5d2f3cbb 100644
--- a/core/sis-utility/src/test/java/org/apache/sis/measure/SexagesimalConverterTest.java
+++ b/core/sis-utility/src/test/java/org/apache/sis/measure/SexagesimalConverterTest.java
@@ -30,7 +30,7 @@ import static org.junit.Assert.*;
  * Test the {@link SexagesimalConverter} class.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.0
+ * @version 1.3
  * @since   0.3
  * @module
  */
@@ -92,6 +92,22 @@ public final strictfp class SexagesimalConverterTest extends TestCase {
         checkConversion(44.505590277777777, Units.DEGREE, 443020.125, DMS_SCALED);
     }
 
+    /**
+     * Tests the error message on attempt to convert an illegal value.
+     */
+    @Test
+    public void testErrorMessage() {
+        final UnitConverter converter = DMS.getConverterTo(Units.DEGREE);
+        assertEquals(10.5, converter.convert(10.3), STRICT);
+        try {
+            converter.convert(10.7);
+            fail("Conversion of illegal value should not be allowed.");
+        } catch (IllegalArgumentException e) {
+            final String message = e.getMessage();
+            assertNotNull(message);     // Can not test message content because it is locale-sensitive.
+        }
+    }
+
     /**
      * Tests the fix for rounding error in conversion of 46°57'8.66".
      * This fix is necessary for avoiding a 4 cm error with Apache SIS