sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1765612 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/internal/system/ main/java/org/apache/sis/measure/ test/java/org/apache/sis/measure/ test/java/org/apache/sis/test/suite/
Date Wed, 19 Oct 2016 14:54:44 GMT
Author: desruisseaux
Date: Wed Oct 19 14:54:43 2016
New Revision: 1765612

URL: http://svn.apache.org/viewvc?rev=1765612&view=rev
Log:
More tests and bug fixes in SIS implementation of JSR-363.

Added:
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitDimensionTest.java
  (with props)
Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/DefaultFactories.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitDimension.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/DefaultFactories.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/DefaultFactories.java?rev=1765612&r1=1765611&r2=1765612&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/DefaultFactories.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/system/DefaultFactories.java
[UTF-8] Wed Oct 19 14:54:43 2016
@@ -40,7 +40,7 @@ import org.apache.sis.util.logging.Loggi
  */
 public final class DefaultFactories extends SystemListener {
     /**
-     * Cache of factories which are found by {@code META-INF/services}.
+     * Cache of factories found by {@link ServiceLoader} from {@code META-INF/services} files
content.
      */
     private static final Map<Class<?>, Object> FACTORIES = new IdentityHashMap<>(4);
     static {

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitDimension.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitDimension.java?rev=1765612&r1=1765611&r2=1765612&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitDimension.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitDimension.java
[UTF-8] Wed Oct 19 14:54:43 2016
@@ -79,9 +79,7 @@ final class UnitDimension implements Dim
      * Pseudo-dimension for dimensionless units.
      */
     static final UnitDimension NONE = new UnitDimension(Collections.emptyMap());
-    static {
-        POOL.put(NONE.components, NONE);
-    }
+    // No need to store in the POOL cache since UnitDimension performs special checks for
dimensionless instances.
 
     /**
      * The product of base dimensions that make this dimension. All keys in this map shall
be base dimensions
@@ -114,8 +112,8 @@ final class UnitDimension implements Dim
     }
 
     /**
-     * Creates a new derived dimension. This constructor shall never be invoked directly;
-     * use {@link #create(Map)} instead.
+     * Creates a new derived dimension. This constructor shall never be invoked directly
+     * (except for {@link #NONE}); use {@link #create(Map)} instead.
      *
      * @param  components  the product of base dimensions together with their power.
      */
@@ -135,10 +133,9 @@ final class UnitDimension implements Dim
             case 0: return NONE;
             case 1: {
                 final Map.Entry<UnitDimension,Fraction> entry = components.entrySet().iterator().next();
-                final UnitDimension base = entry.getKey();
                 final Fraction power = entry.getValue();
                 if (power.numerator == 1 && power.denominator == 1) {
-                    return base;
+                    return entry.getKey();
                 }
                 break;
             }
@@ -164,6 +161,7 @@ final class UnitDimension implements Dim
      * Invoked on deserialization for returning a unique instance of {@code UnitDimension}.
      */
     Object readResolve() throws ObjectStreamException {
+        if (isDimensionless()) return NONE;
         final UnitDimension dim = POOL.putIfAbsent(components, this);
         return (dim != null) ? dim : this;
     }
@@ -316,7 +314,7 @@ final class UnitDimension implements Dim
                  * Do not compare 'components' if 'symbols' is non-zero because in such case
                  * the components map contains 'this', which would cause an infinite loop.
                  */
-                return (symbol == 0) || components.equals(that.components);
+                return (symbol != 0) || components.equals(that.components);
             }
         }
         return false;

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java?rev=1765612&r1=1765611&r2=1765612&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitFormat.java
[UTF-8] Wed Oct 19 14:54:43 2016
@@ -277,6 +277,7 @@ public class UnitFormat extends Format i
                 } else {
                     toAppendTo.append("^(").append(t).append(')');
                 }
+                return;
             }
             n = f.numerator;
         } else {

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java?rev=1765612&r1=1765611&r2=1765612&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java [UTF-8]
Wed Oct 19 14:54:43 2016
@@ -53,7 +53,7 @@ import static org.apache.sis.measure.Sex
  * This class focuses on the most commonly used units in the geospatial domain:
  * angular units ({@linkplain #DEGREE degree}, {@linkplain #ARC_SECOND arc-second}, …),
  * linear units ({@linkplain #KILOMETRE kilometre}, {@linkplain #NAUTICAL_MILE nautical mile},
…) and
- * temporal units ({@linkplain #DAY day}, {@linkplain #YEAR year}, …).
+ * temporal units ({@linkplain #DAY day}, {@linkplain #TROPICAL_YEAR year}, …).
  * But some other kind of units are also provided for completeness.
  *
  * <p>All Units of Measurement are based on units from the International System (SI).
@@ -73,7 +73,8 @@ import static org.apache.sis.measure.Sex
  * </table>
  *
  * Unit names and definitions in this class follow the definitions provided in the EPSG geodetic
dataset
- * (when the unit exists in that dataset).
+ * (when the unit exists in that dataset),
+ * except “year” which has been renamed “{@linkplain #TROPICAL_YEAR tropical year}”.
  *
  * @author  Martin Desruisseaux (IRD, Geomatys)
  * @since   0.3
@@ -507,7 +508,7 @@ public final class Units extends Static
     public static final Unit<Speed> METRES_PER_SECOND;
 
     /**
-     * Unit of measurement defined as 60/1000 metres per second (1 “km/h”).
+     * Unit of measurement defined as 60/1000 metres per second (1 km/h).
      * The unlocalized name is “kilometres per hour”.
      *
      * @see #KILOMETRE
@@ -648,17 +649,26 @@ public final class Units extends Static
     public static final Unit<Dimensionless> PIXEL;
 
     static {
-        final UnitDimension L = new UnitDimension('L');     // Length
-        final UnitDimension M = new UnitDimension('M');     // Mass
-        final UnitDimension T = new UnitDimension('T');     // Time
-        final UnitDimension Θ = new UnitDimension('Θ');     // Temperature
-        final UnitDimension Z = UnitDimension.NONE;         // Dimensionless
+        final UnitDimension length        = new UnitDimension('L');
+        final UnitDimension mass          = new UnitDimension('M');
+        final UnitDimension time          = new UnitDimension('T');
+        final UnitDimension temperature   = new UnitDimension('Θ');
+        final UnitDimension area          = length.pow(2);
+        final UnitDimension speed         = length.divide(time);
+        final UnitDimension force         = mass.multiply(speed).divide(time);
+        final UnitDimension energy        = force.multiply(length);
+        final UnitDimension pressure      = force.divide(area);
+        final UnitDimension dimensionless = UnitDimension.NONE;
         /*
-         * All base units except the ones for the Dimensionless quantity.
+         * Base, derived or alternate units that we need to reuse more than once in this
static initializer.
          */
-        final SystemUnit<Angle>  rad = new SystemUnit<>(Angle.class,  Z, "rad",
(short) 9101);
-        final SystemUnit<Length> m   = new SystemUnit<>(Length.class, L, "m",
  Constants.EPSG_METRE);
-        final SystemUnit<Time>   s   = new SystemUnit<>(Time.class,   T, "s",
  (short) 1040);
+        final SystemUnit<Length>        m   = new SystemUnit<>(Length.class,
       length,        "m",   Constants.EPSG_METRE);
+        final SystemUnit<Time>          s   = new SystemUnit<>(Time.class,  
       time,          "s",   (short) 1040);
+        final SystemUnit<Temperature>   K   = new SystemUnit<>(Temperature.class,
  temperature,   "K",   (short) 0);
+        final SystemUnit<Speed>         mps = new SystemUnit<>(Speed.class, 
       speed,         "m∕s", (short) 1026);
+        final SystemUnit<Pressure>      Pa  = new SystemUnit<>(Pressure.class,
     pressure,      "Pa",  (short) 0);
+        final SystemUnit<Angle>         rad = new SystemUnit<>(Angle.class, 
       dimensionless, "rad", (short) 9101);
+        final SystemUnit<Dimensionless> one = new SystemUnit<>(Dimensionless.class,
dimensionless, "",    (short) 9201);
         /*
          * All SI prefix to be used below.
          */
@@ -702,41 +712,30 @@ public final class Units extends Static
         WEEK           = new ConventionalUnit<>(s, LinearConverter.scale( 7*24*60*60,
     1), "wk",  (short) 0);
         TROPICAL_YEAR  = new ConventionalUnit<>(s, LinearConverter.scale(31556925445.0,
1000), "a",   (short) 1029);
         /*
-         * All Unit<Mass>
+         * Other units.
          */
-        KILOGRAM = add(new SystemUnit<>(Mass.class, Z, "kg", (short) 0));
-        /*
-         * Derived units
-         */
-        final UnitDimension area   = L.pow(2);
-        final UnitDimension speed  = L.divide(T);
-        final UnitDimension force  = M.multiply(speed).divide(T);
-        final UnitDimension energy = force.multiply(L);
-        final SystemUnit<Temperature> K;
-        final SystemUnit<Pressure> Pa;
-        final SystemUnit<Speed> mps;
-        PASCAL              = add(Pa =  new SystemUnit<>(Pressure.class,    force.divide(area),
"Pa",   (short) 0));
-        SQUARE_METRE        = add(      new SystemUnit<>(Area.class,        area, 
             "m²",   (short) 0));
-        CUBIC_METRE         = add(      new SystemUnit<>(Volume.class,      L.pow(3),
          "m³",   (short) 0));
-        METRES_PER_SECOND   = add(mps = new SystemUnit<>(Speed.class,       speed,
             "m∕s",  (short) 1026));
-        NEWTON              = add(      new SystemUnit<>(Force.class,       force,
             "N",    (short) 0));
-        JOULE               = add(      new SystemUnit<>(Energy.class,      energy,
            "J",    (short) 0));
-        WATT                = add(      new SystemUnit<>(Power.class,       energy.divide(T),
  "W",    (short) 0));
-        KELVIN              = add(K =   new SystemUnit<>(Temperature.class, Θ,   
              "K",    (short) 0));
-        HERTZ               = add(      new SystemUnit<>(Frequency.class,   T.pow(-1),
         "Hz",   (short) 0));
-        HECTOPASCAL         = new ConventionalUnit<>(Pa,  hecto,                  
             "hPa",  (short) 0);
-        KILOMETRES_PER_HOUR = new ConventionalUnit<>(mps, LinearConverter.scale(6,
100),        "km∕h", (short) 0);
-        CELSIUS             = new ConventionalUnit<>(K,   LinearConverter.create(1,
273.15),    "℃",    (short) 0);
+        KELVIN              = add(K);
+        PASCAL              = add(Pa);
+        METRES_PER_SECOND   = add(mps);
+        KILOGRAM            = add(new SystemUnit<>(Mass.class,      mass,         
          "kg",   (short) 0));
+        SQUARE_METRE        = add(new SystemUnit<>(Area.class,      area,         
          "m²",   (short) 0));
+        CUBIC_METRE         = add(new SystemUnit<>(Volume.class,    length.pow(3),
          "m³",   (short) 0));
+        NEWTON              = add(new SystemUnit<>(Force.class,     force,        
          "N",    (short) 0));
+        JOULE               = add(new SystemUnit<>(Energy.class,    energy,       
          "J",    (short) 0));
+        WATT                = add(new SystemUnit<>(Power.class,     energy.divide(time),
    "W",    (short) 0));
+        HERTZ               = add(new SystemUnit<>(Frequency.class, time.pow(-1), 
          "Hz",   (short) 0));
+        HECTOPASCAL         = new ConventionalUnit<>(Pa, hecto,                   
          "hPa",  (short) 0);
+        KILOMETRES_PER_HOUR = new ConventionalUnit<>(mps, LinearConverter.scale(6,
100),     "km∕h", (short) 0);
+        CELSIUS             = new ConventionalUnit<>(K, LinearConverter.create(1, 273.15),
  "℃",    (short) 0);
         /*
          * All Unit<Dimensionless>
          */
-        final SystemUnit<Dimensionless> one = new SystemUnit<>(Dimensionless.class,
Z, "", (short) 9201);
         PERCENT = new ConventionalUnit<>(one, centi, "%",   (short) 0);
         PPM     = new ConventionalUnit<>(one, micro, "ppm", (short) 9202);
-        PSU     = new SystemUnit<>(Dimensionless.class, Z, "psu",   (short) 0);
-        SIGMA   = new SystemUnit<>(Dimensionless.class, Z, "sigma", (short) 0);
-        PIXEL   = new SystemUnit<>(Dimensionless.class, Z, "px",    (short) 0);
-        UNITY   = add(one);     // Must be last in order to take precedence over all other
units associated to UnitDimension.NONE.
+        PSU     = new SystemUnit<>(Dimensionless.class, dimensionless, "psu",   (short)
0);
+        SIGMA   = new SystemUnit<>(Dimensionless.class, dimensionless, "sigma", (short)
0);
+        PIXEL   = new SystemUnit<>(Dimensionless.class, dimensionless, "px",    (short)
0);
+        UNITY   = add(one);  // Must be last in order to take precedence over all other units
associated to UnitDimension.NONE.
     }
 
     /**

Added: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitDimensionTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitDimensionTest.java?rev=1765612&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitDimensionTest.java
(added)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitDimensionTest.java
[UTF-8] Wed Oct 19 14:54:43 2016
@@ -0,0 +1,205 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.measure;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.HashMap;
+import javax.measure.Unit;
+import javax.measure.Dimension;
+import org.apache.sis.math.Fraction;
+import org.apache.sis.test.TestCase;
+import org.apache.sis.test.DependsOnMethod;
+import org.apache.sis.util.UnconvertibleObjectException;
+import org.junit.Test;
+
+import static org.apache.sis.test.Assert.*;
+
+
+/**
+ * Tests the {@link UnitDimension} class.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.8
+ * @version 0.8
+ * @module
+ */
+public final strictfp class UnitDimensionTest extends TestCase {
+    /**
+     * The dimension declared by the base {@link Units} constant.
+     * We should not create our own instance for avoiding to pollute the {@link UnitDimension}
cache.
+     */
+    static final Dimension LENGTH        = Units.METRE            .getDimension(),
+                           TIME          = Units.SECOND           .getDimension(),
+                           MASS          = Units.KILOGRAM         .getDimension(),
+                           FORCE         = Units.NEWTON           .getDimension(),
+                           SPEED         = Units.METRES_PER_SECOND.getDimension(),
+                           AREA          = Units.SQUARE_METRE     .getDimension(),
+                           VOLUME        = Units.CUBIC_METRE      .getDimension(),
+                           DIMENSIONLESS = Units.UNITY            .getDimension();
+
+    /**
+     * Tests {@link UnitDimension#multiply(Dimension)}.
+     */
+    @Test
+    @DependsOnMethod("testEqualsAndHashCode")
+    public void testMultiply() {
+        assertSame(LENGTH, LENGTH.multiply(DIMENSIONLESS));
+        assertSame(AREA,   LENGTH.multiply(LENGTH));
+        assertSame(VOLUME, LENGTH.multiply(AREA));
+        assertSame(VOLUME, AREA  .multiply(LENGTH));
+
+        final Map<Dimension,Integer> expected = new HashMap<>(4);
+        assertNull(expected.put(LENGTH, 1));
+        assertNull(expected.put(TIME,   1));
+        assertMapEquals(expected, LENGTH.multiply(TIME).getBaseDimensions());
+    }
+
+    /**
+     * Tests {@link UnitDimension#divide(Dimension)}.
+     */
+    @Test
+    @DependsOnMethod("testEqualsAndHashCode")
+    public void testDivide() {
+        assertSame(LENGTH, LENGTH.divide(DIMENSIONLESS));
+        assertSame(LENGTH, AREA  .divide(LENGTH));
+        assertSame(LENGTH, VOLUME.divide(AREA));
+        assertSame(AREA,   VOLUME.divide(LENGTH));
+        assertSame(SPEED,  LENGTH.divide(TIME));
+    }
+
+    /**
+     * Tests {@link UnitDimension#pow(int)}.
+     */
+    @Test
+    @DependsOnMethod("testEqualsAndHashCode")
+    public void testPow() {
+        assertSame("DIMENSIONLESS", DIMENSIONLESS, DIMENSIONLESS.pow(4));
+        assertSame("AREA",          AREA,          LENGTH.pow(2));
+        assertSame("VOLUME",        VOLUME,        LENGTH.pow(3));
+    }
+
+    /**
+     * Tests {@link UnitDimension#root(int)}.
+     */
+    @Test
+    @DependsOnMethod("testEqualsAndHashCode")
+    public void testRoot() {
+        assertSame("DIMENSIONLESS", DIMENSIONLESS, DIMENSIONLESS.root(4));
+        assertSame("AREA",          LENGTH,        AREA.root(2));
+        assertSame("VOLUME",        LENGTH,        VOLUME.root(3));
+    }
+
+    /**
+     * Returns the specific detectivity dimension, which is T^2.5 / (M⋅L).
+     */
+    static Dimension specificDetectivity() {
+        return TIME.pow(2).divide(MASS.multiply(LENGTH)).multiply(TIME.root(2));
+    }
+
+    /**
+     * Tests a dimension with rational power. This tests use the specific detectivity, which
dimension is T^2.5 / (M⋅L).
+     */
+    @Test
+    @DependsOnMethod({"testMultiply", "testDivide", "testPow", "testRoot"})
+    public void testRationalPower() {
+        final Dimension dim = specificDetectivity();
+        final Map<Dimension,Fraction> expected = new HashMap<>(4);
+        assertNull(expected.put(TIME,   new Fraction( 5, 2)));
+        assertNull(expected.put(MASS,   new Fraction(-1, 1)));
+        assertNull(expected.put(LENGTH, new Fraction(-1, 1)));
+        assertMapEquals(expected, ((UnitDimension) dim).components);
+        try {
+            dim.getBaseDimensions().toString();
+            fail("Mapping from Fraction to Integer should not be allowed.");
+        } catch (UnconvertibleObjectException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("Integer"));
+        }
+        // 'toString()' formatting tested in UnitFormatTest.testRationalPower().
+    }
+
+    /**
+     * Tests {@link UnitDimension#getBaseDimensions()}. This method indirectly tests the
results
+     * of {@link UnitDimension#multiply(Dimension)}, {@link UnitDimension#divide(Dimension)}
and
+     * {@link UnitDimension#pow(int)} since this tests use constant that were created with
above
+     * operations.
+     */
+    @Test
+    public void testGetBaseDimensions() {
+        assertNull("METRE",  LENGTH       .getBaseDimensions());
+        assertNull("SECOND", TIME         .getBaseDimensions());
+        assertTrue("UNITY",  DIMENSIONLESS.getBaseDimensions().isEmpty());
+
+        assertMapEquals(Collections.singletonMap(LENGTH, 3), VOLUME.getBaseDimensions());
+
+        final Map<Dimension,Integer> expected = new HashMap<>(4);
+        assertNull(expected.put(MASS,    1));
+        assertNull(expected.put(LENGTH,  1));
+        assertNull(expected.put(TIME,   -2));
+        assertMapEquals(expected, FORCE.getBaseDimensions());
+    }
+
+    /**
+     * Tests the {@link UnitDimension#equals(Object)} and {@link UnitDimension#hashCode()}
methods.
+     */
+    @Test
+    public void testEqualsAndHashCode() {
+        verifyEqualsAndHashCode("Base dimensions",    true,  Units.METRE,  Units.METRE);
+        verifyEqualsAndHashCode("Base dimensions",    true,  Units.SECOND, Units.SECOND);
+        verifyEqualsAndHashCode("Base dimensions",    false, Units.METRE,  Units.SECOND);
+        verifyEqualsAndHashCode("Derived dimensions", true,  Units.NEWTON, Units.NEWTON);
+        verifyEqualsAndHashCode("Derived dimensions", true,  Units.JOULE,  Units.JOULE);
+        verifyEqualsAndHashCode("Derived dimensions", false, Units.NEWTON, Units.JOULE);
+        verifyEqualsAndHashCode("Dimensionsless",     true,  Units.UNITY,  Units.UNITY);
+        verifyEqualsAndHashCode("Dimensionsless",     true,  Units.DEGREE, Units.DEGREE);
+        verifyEqualsAndHashCode("Dimensionsless",     true,  Units.UNITY,  Units.DEGREE);
   // Really true, not false.
+        verifyEqualsAndHashCode("Mixed types",        false, Units.METRE,  Units.UNITY);
+        verifyEqualsAndHashCode("Mixed types",        false, Units.METRE,  Units.NEWTON);
+    }
+
+    /**
+     * Verifies that the test for equality between two dimensions produce the expected result.
+     * This method expects {@link Unit} instances instead than {@link Dimension} for convenience,
+     * but only the dimensions will be compared.
+     *
+     * @param message   the message to show in case of failure.
+     * @param expected  the expected result of {@link UnitDimension#equals(Object)}.
+     * @param a         {@link Units} constant from which to get the first dimension to compare.
+     * @param b         {@link Units} constant from which to get the first dimension to compare.
+     */
+    private static void verifyEqualsAndHashCode(final String message, final boolean expected,
final Unit<?> a, final Unit<?> b) {
+        final Dimension da = a.getDimension();
+        final Dimension db = b.getDimension();
+        assertEquals(message, expected, da.equals(db));
+        assertEquals(message, expected, db.equals(da));
+        assertEquals(message, expected, da.hashCode() == db.hashCode());
+    }
+
+    /**
+     * Serializes some dimensions, deserializes them and verifies that we get the same instance.
+     */
+    @Test
+    @DependsOnMethod("testEqualsAndHashCode")
+    public void testSerialization() {
+        Dimension dim;
+        assertSame(dim = Units.METRE .getDimension(), assertSerializedEquals(dim));
+        assertSame(dim = Units.SECOND.getDimension(), assertSerializedEquals(dim));
+        assertSame(dim = Units.NEWTON.getDimension(), assertSerializedEquals(dim));
+        assertSame(dim = Units.UNITY .getDimension(), assertSerializedEquals(dim));
+    }
+}

Propchange: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitDimensionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitDimensionTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java?rev=1765612&r1=1765611&r2=1765612&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitFormatTest.java
[UTF-8] Wed Oct 19 14:54:43 2016
@@ -16,6 +16,12 @@
  */
 package org.apache.sis.measure;
 
+import java.util.Set;
+import java.util.HashSet;
+import java.lang.reflect.Field;
+import javax.measure.Unit;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
@@ -30,35 +36,91 @@ import static org.junit.Assert.*;
  * @version 0.8
  * @module
  */
+@DependsOn(UnitDimensionTest.class)
 public final strictfp class UnitFormatTest extends TestCase {
     /**
-     * Tests {@link UnitFormat#format(Object)} using the system-wide instance.
+     * Verifies all constants defined in {@link Units} class. This method verifies:
+     *
+     * <ul>
+     *   <li>The string representation of the dimension, which indirectly tests {@link
UnitFormat}
+     *       since {@link UnitDimension} delegates to to {@code UnitFormat}.</li>
+     *   <li>The unit symbol as given by {@link UnitFormat#format(Object)} using the
system-wide instance.</li>
+     * </ul>
      */
     @Test
-    public void testFormat() {
-        final UnitFormat f = UnitFormat.INSTANCE;
-        assertEquals("m",     f.format(Units.METRE));
-        assertEquals("rad",   f.format(Units.RADIAN));
-        assertEquals("s",     f.format(Units.SECOND));
-        assertEquals("min",   f.format(Units.MINUTE));
-        assertEquals("h",     f.format(Units.HOUR));
-        assertEquals("Hz",    f.format(Units.HERTZ));
-        assertEquals("Pa",    f.format(Units.PASCAL));
-        assertEquals("kg",    f.format(Units.KILOGRAM));
-        assertEquals("N",     f.format(Units.NEWTON));
-        assertEquals("J",     f.format(Units.JOULE));
-        assertEquals("W",     f.format(Units.WATT));
-        assertEquals("K",     f.format(Units.KELVIN));
-        assertEquals("℃",     f.format(Units.CELSIUS));
-        assertEquals("m∕s",   f.format(Units.METRES_PER_SECOND));
-        assertEquals("km∕h",  f.format(Units.KILOMETRES_PER_HOUR));
-        assertEquals("m²",    f.format(Units.SQUARE_METRE));
-        assertEquals("m³",    f.format(Units.CUBIC_METRE));
-        assertEquals("",      f.format(Units.UNITY));
-        assertEquals("%",     f.format(Units.PERCENT));
-        assertEquals("psu",   f.format(Units.PSU));
-        assertEquals("px",    f.format(Units.PIXEL));
-        assertEquals("d",     f.format(Units.DAY));
-        assertEquals("wk",    f.format(Units.WEEK));
+    public void verifyUnitConstants() {
+        final Set<String> declared = new HashSet<>(64);
+        for (final Field f : Units.class.getFields()) {
+            if (Unit.class.isAssignableFrom(f.getType())) {
+                declared.add(f.getName());
+            }
+        }
+        verify(declared, "NANOMETRE",           "L",        "nm",    Units.NANOMETRE);
+        verify(declared, "MILLIMETRE",          "L",        "mm",    Units.MILLIMETRE);
+        verify(declared, "CENTIMETRE",          "L",        "cm",    Units.CENTIMETRE);
+        verify(declared, "METRE",               "L",        "m",     Units.METRE);
+        verify(declared, "KILOMETRE",           "L",        "km",    Units.KILOMETRE);
+        verify(declared, "NAUTICAL_MILE",       "L",        "M",     Units.NAUTICAL_MILE);
+        verify(declared, "STATUTE_MILE",        "L",        "mi",    Units.STATUTE_MILE);
+        verify(declared, "US_SURVEY_FOOT",      "L",        "ft_US", Units.US_SURVEY_FOOT);
+        verify(declared, "FOOT",                "L",        "ft",    Units.FOOT);
+        verify(declared, "INCH",                "L",        "in",    Units.INCH);
+        verify(declared, "POINT",               "L",        "pt",    Units.POINT);
+        verify(declared, "RADIAN",              "",         "rad",   Units.RADIAN);
+        verify(declared, "GRAD",                "",         "grad",  Units.GRAD);
+        verify(declared, "DEGREE",              "",         "°",     Units.DEGREE);
+        verify(declared, "ARC_MINUTE",          "",         "′",     Units.ARC_MINUTE);
+        verify(declared, "ARC_SECOND",          "",         "″",     Units.ARC_SECOND);
+        verify(declared, "MICRORADIAN",         "",         "µrad",  Units.MICRORADIAN);
+        verify(declared, "MILLISECOND",         "T",        "ms",    Units.MILLISECOND);
+        verify(declared, "SECOND",              "T",        "s",     Units.SECOND);
+        verify(declared, "MINUTE",              "T",        "min",   Units.MINUTE);
+        verify(declared, "HOUR",                "T",        "h",     Units.HOUR);
+        verify(declared, "DAY",                 "T",        "d",     Units.DAY);
+        verify(declared, "WEEK",                "T",        "wk",    Units.WEEK);
+        verify(declared, "TROPICAL_YEAR",       "T",        "a",     Units.TROPICAL_YEAR);
+        verify(declared, "PASCAL",              "M⋅L⁻¹⋅T⁻²", "Pa",    Units.PASCAL);
+        verify(declared, "HECTOPASCAL",         "M⋅L⁻¹⋅T⁻²", "hPa",   Units.HECTOPASCAL);
+        verify(declared, "SQUARE_METRE",        "L²",       "m²",    Units.SQUARE_METRE);
+        verify(declared, "CUBIC_METRE",         "L³",       "m³",    Units.CUBIC_METRE);
+        verify(declared, "METRES_PER_SECOND",   "L∕T",      "m∕s",   Units.METRES_PER_SECOND);
+        verify(declared, "KILOMETRES_PER_HOUR", "L∕T",      "km∕h",  Units.KILOMETRES_PER_HOUR);
+        verify(declared, "KILOGRAM",            "M",        "kg",    Units.KILOGRAM);
+        verify(declared, "NEWTON",              "M⋅L∕T²",   "N",     Units.NEWTON);
+        verify(declared, "JOULE",               "M⋅L²∕T²",  "J",     Units.JOULE);
+        verify(declared, "WATT",                "M⋅L²∕T³",  "W",     Units.WATT);
+        verify(declared, "KELVIN",              "Θ",        "K",     Units.KELVIN);
+        verify(declared, "CELSIUS",             "Θ",        "℃",     Units.CELSIUS);
+        verify(declared, "HERTZ",               "T⁻¹",      "Hz",    Units.HERTZ);
+        verify(declared, "UNITY",               "",         "",      Units.UNITY);
+        verify(declared, "PERCENT",             "",         "%",     Units.PERCENT);
+        verify(declared, "PPM",                 "",         "ppm",   Units.PPM);
+        verify(declared, "PSU",                 "",         "psu",   Units.PSU);
+        verify(declared, "PIXEL",               "",         "px",    Units.PIXEL);
+        assertTrue("Missing units in test:" + declared, declared.isEmpty());
+    }
+
+    /**
+     * Verifies one of the constants declared in the {@link Unit} class.
+     *
+     * @param declared   a map from which to remove the {@code field} value, for verifying
that we didn't forgot an element.
+     * @param field      the name of the constant to be verified.
+     * @param dimension  the expected string representation of the unit dimension.
+     * @param symbol     the expected string representation of the unit.
+     * @param unit       the unit to verify.
+     */
+    private static void verify(final Set<String> declared, final String field, final
String dimension, final String symbol, final Unit<?> unit) {
+        assertEquals(field, dimension, String.valueOf(unit.getDimension()));
+        assertEquals(field, symbol,    UnitFormat.INSTANCE.format(unit));
+        declared.remove(field);
+    }
+
+    /**
+     * Tests the formatting of a dimension having rational powers.
+     */
+    @Test
+    @DependsOnMethod("verifyUnitConstants")
+    public void testRationalPower() {
+        assertEquals("T^(5⁄2)⋅M⁻¹⋅L⁻¹", UnitDimensionTest.specificDetectivity().toString());
     }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java?rev=1765612&r1=1765611&r2=1765612&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] Wed Oct 19 14:54:43 2016
@@ -103,6 +103,7 @@ import org.junit.BeforeClass;
 
     // Measurements and formatting.
     org.apache.sis.measure.SexagesimalConverterTest.class,
+    org.apache.sis.measure.UnitDimensionTest.class,
     org.apache.sis.measure.UnitFormatTest.class,
     org.apache.sis.measure.UnitsTest.class,
     org.apache.sis.measure.RangeTest.class,



Mime
View raw message