sis-commits mailing list archives

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

URL: http://svn.apache.org/viewvc?rev=1765690&view=rev
Log:
Add more test for JSR-363 implementation.

Added:
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SystemUnitTest.java
  (with props)
Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.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/UnitDimensionTest.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/measure/SystemUnit.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.java?rev=1765690&r1=1765689&r2=1765690&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/SystemUnit.java
[UTF-8] Wed Oct 19 17:44:54 2016
@@ -237,9 +237,7 @@ final class SystemUnit<Q extends Quantit
              */
             final Class<?> c = ((SystemUnit<Q>) other).quantity;
             if (c != null) {
-                final boolean status = (quantity == c);
-                assert status == dimension.equals(other.getDimension()) : this;
-                return status;
+                return (quantity == c);
             }
         }
         /*
@@ -428,4 +426,12 @@ final class SystemUnit<Q extends Quantit
     public int hashCode() {
         return super.hashCode() + 37 * dimension.hashCode();
     }
+
+    /**
+     * Invoked on deserialization for returning a unique instance of {@code SystemUnit}.
+     */
+    Object readResolve() throws ObjectStreamException {
+        final SystemUnit<Q> u = Units.get(quantity);
+        return (u != null) ? u : this;
+    }
 }

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=1765690&r1=1765689&r2=1765690&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 17:44:54 2016
@@ -90,7 +90,7 @@ public final class Units extends Static
     private static final Map<Object, SystemUnit<?>> SYSTEM = new HashMap<>();
 
     /**
-     * Returns the unit for the given dimension, or {@code null} if none.
+     * Returns the system unit for the given quantity, or {@code null} if none.
      */
     @SuppressWarnings("unchecked")
     static <Q extends Quantity<Q>> SystemUnit<Q> get(final Class<Q>
type) {
@@ -98,7 +98,9 @@ public final class Units extends Static
     }
 
     /**
-     * Returns the unit for the given dimension, or {@code null} if none.
+     * Returns the system unit for the given dimension, or {@code null} if none.
+     * Note that this method can not distinguish the different kinds of dimensionless units.
+     * If the quantity type is known, use {@link #get(Class)} instead.
      */
     static SystemUnit<?> get(final Dimension dim) {
         return SYSTEM.get(dim);

Added: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SystemUnitTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SystemUnitTest.java?rev=1765690&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SystemUnitTest.java
(added)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/SystemUnitTest.java
[UTF-8] Wed Oct 19 17:44:54 2016
@@ -0,0 +1,247 @@
+/*
+ * 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.Map;
+import java.util.HashMap;
+import java.util.Collections;
+import javax.measure.Unit;
+import javax.measure.UnconvertibleException;
+import javax.measure.IncommensurableException;
+import org.apache.sis.test.DependsOnMethod;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.apache.sis.test.Assert.*;
+
+
+/**
+ * Tests the {@link SystemUnit} class.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.8
+ * @version 0.8
+ * @module
+ */
+@DependsOn(UnitDimensionTest.class)
+public final strictfp class SystemUnitTest extends TestCase {
+    /**
+     * Tests {@link SystemUnit#multiply(Unit)}.
+     */
+    @Test
+    @DependsOnMethod("testEqualsAndHashCode")
+    public void testMultiply() {
+        assertSame(Units.METRE,        Units.METRE.multiply(Units.UNITY));
+        assertSame(Units.SQUARE_METRE, Units.METRE.multiply(Units.METRE));
+        assertSame(Units.CUBIC_METRE,  Units.METRE.multiply(Units.SQUARE_METRE));
+        assertSame(Units.CUBIC_METRE,  Units.SQUARE_METRE.multiply(Units.METRE));
+
+        final Map<Unit<?>,Integer> expected = new HashMap<>(4);
+        assertNull(expected.put(Units.METRE,  1));
+        assertNull(expected.put(Units.SECOND, 1));
+        assertMapEquals(expected, Units.METRE.multiply(Units.SECOND).getBaseUnits());
+    }
+
+    /**
+     * Tests {@link SystemUnit#divide(Unit)}.
+     */
+    @Test
+    @DependsOnMethod("testEqualsAndHashCode")
+    public void testDivide() {
+        assertSame(Units.METRE, Units.METRE.divide(Units.UNITY));
+        assertSame(Units.METRE, Units.SQUARE_METRE.divide(Units.METRE));
+        assertSame(Units.METRE, Units.CUBIC_METRE.divide(Units.SQUARE_METRE));
+        assertSame(Units.SQUARE_METRE, Units.CUBIC_METRE.divide(Units.METRE));
+        assertSame(Units.METRES_PER_SECOND, Units.METRE.divide(Units.SECOND));
+    }
+
+    /**
+     * Tests {@link SystemUnit#pow(int)}.
+     */
+    @Test
+    @DependsOnMethod("testEqualsAndHashCode")
+    public void testPow() {
+        assertSame("UNITY",        Units.UNITY,        Units.UNITY.pow(4));
+        assertSame("SQUARE_METRE", Units.SQUARE_METRE, Units.METRE.pow(2));
+        assertSame("CUBIC_METRE",  Units.CUBIC_METRE,  Units.METRE.pow(3));
+    }
+
+    /**
+     * Tests {@link SystemUnit#root(int)}.
+     */
+    @Test
+    @DependsOnMethod("testEqualsAndHashCode")
+    public void testRoot() {
+        assertSame("UNITY",        Units.UNITY, Units.UNITY.root(4));
+        assertSame("SQUARE_METRE", Units.METRE, Units.SQUARE_METRE.root(2));
+        assertSame("CUBIC_METRE",  Units.METRE, Units.CUBIC_METRE.root(3));
+    }
+
+    /**
+     * Tests {@link SystemUnit#getBaseUnits()}. This method indirectly tests the results
of
+     * {@link SystemUnit#multiply(Unit)}, {@link SystemUnit#divide(Unit)} and {@link SystemUnit#pow(int)}
+     * since this test uses constants that were created with above operations.
+     */
+    @Test
+    public void testGetBaseDimensions() {
+        assertNull("METRE",  Units.METRE .getBaseUnits());
+        assertNull("SECOND", Units.SECOND.getBaseUnits());
+        assertTrue("UNITY",  Units.UNITY .getBaseUnits().isEmpty());
+
+        assertMapEquals(Collections.singletonMap(Units.METRE, 3), Units.CUBIC_METRE.getBaseUnits());
+
+        final Map<Unit<?>,Integer> expected = new HashMap<>(4);
+        assertNull(expected.put(Units.KILOGRAM, 1));
+        assertNull(expected.put(Units.METRE,    1));
+        assertNull(expected.put(Units.SECOND,  -2));
+        assertMapEquals(expected, Units.NEWTON.getBaseUnits());
+    }
+
+    /**
+     * Tests the {@link SystemUnit#equals(Object)} and {@link SystemUnit#hashCode()} methods.
+     */
+    @Test
+    public void testEqualsAndHashCode() {
+        verifyEqualsAndHashCode("Base units",     true,  Units.METRE,  Units.METRE);
+        verifyEqualsAndHashCode("Base units",     true,  Units.SECOND, Units.SECOND);
+        verifyEqualsAndHashCode("Base units",     false, Units.METRE,  Units.SECOND);
+        verifyEqualsAndHashCode("Derived units",  true,  Units.NEWTON, Units.NEWTON);
+        verifyEqualsAndHashCode("Derived units",  true,  Units.JOULE,  Units.JOULE);
+        verifyEqualsAndHashCode("Derived units",  false, Units.NEWTON, Units.JOULE);
+        verifyEqualsAndHashCode("Dimensionsless", true,  Units.UNITY,  Units.UNITY);
+        verifyEqualsAndHashCode("Dimensionsless", true,  Units.DEGREE, Units.DEGREE);
+        verifyEqualsAndHashCode("Dimensionsless", false, Units.UNITY,  Units.DEGREE);
+        verifyEqualsAndHashCode("Mixed types",    false, Units.METRE,  Units.UNITY);
+        verifyEqualsAndHashCode("Mixed types",    false, Units.METRE,  Units.NEWTON);
+    }
+
+    /**
+     * Verifies that the test for equality between two units produce the expected result.
+     * This method expects {@link Unit} instances instead than {@link Unit} for convenience,
+     * but only the units will be compared.
+     *
+     * @param message   the message to show in case of failure.
+     * @param expected  the expected result of {@link SystemUnit#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) {
+        assertEquals(message, expected, a.equals(b));
+        assertEquals(message, expected, b.equals(a));
+        assertEquals(message, expected, a.hashCode() == b.hashCode());
+    }
+
+    /**
+     * Serializes some units, deserializes them and verifies that we get the same instance.
+     */
+    @Test
+    @DependsOnMethod("testEqualsAndHashCode")
+    public void testSerialization() {
+        assertSame(Units.METRE,  assertSerializedEquals(Units.METRE));
+        assertSame(Units.SECOND, assertSerializedEquals(Units.SECOND));
+        assertSame(Units.NEWTON, assertSerializedEquals(Units.NEWTON));
+        assertSame(Units.UNITY,  assertSerializedEquals(Units.UNITY));
+    }
+
+    /**
+     * Tests {@link SystemUnit#isCompatible(Unit)}.
+     */
+    @Test
+    public void testIsCompatible() {
+        assertTrue (Units.METRE .isCompatible(Units.METRE ));
+        assertTrue (Units.SECOND.isCompatible(Units.SECOND));
+        assertTrue (Units.RADIAN.isCompatible(Units.RADIAN));
+        assertFalse(Units.RADIAN.isCompatible(Units.METRE ));
+        assertFalse(Units.METRE .isCompatible(Units.RADIAN));
+        assertTrue (Units.UNITY .isCompatible(Units.RADIAN));   // Really 'true', not 'false'.
+        assertTrue (Units.RADIAN.isCompatible(Units.UNITY ));
+    }
+
+    /**
+     * Tests {@link SystemUnit#getConverterTo(Unit)}.
+     */
+    @Test
+    @DependsOnMethod("testIsCompatible")
+    public void testGetConverterTo() {
+        assertTrue(Units.METRE .getConverterTo(Units.METRE ).isIdentity());
+        assertTrue(Units.SECOND.getConverterTo(Units.SECOND).isIdentity());
+        assertTrue(Units.RADIAN.getConverterTo(Units.RADIAN).isIdentity());
+    }
+
+    /**
+     * Tests {@link SystemUnit#getConverterTo(Unit)} with illegal arguments.
+     * Those calls are not allowed by the Java compiler if parameterized types have not been
erased.
+     * But if the users nevertheless erased parameter types, {@link SystemUnit} implementation
should
+     * have some safety nets.
+     */
+    @Test
+    @SuppressWarnings("unchecked")
+    @DependsOnMethod("testGetConverterTo")
+    public void testIllegalGetConverterTo() {
+        try {
+            Units.METRE.getConverterTo((Unit) Units.SECOND);
+            fail("Conversion should not have been allowed.");
+        } catch (UnconvertibleException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("m"));     // metre unit symbol
+            assertTrue(message, message.contains("s"));     // second unit symbol
+        }
+        /*
+         * Following is tricker because "radian" and "unity" are compatible units,
+         * but should nevertheless not be allowed by the 'getConverterTo' method.
+         */
+        try {
+            Units.RADIAN.getConverterTo((Unit) Units.UNITY);
+            fail("Conversion should not have been allowed.");
+        } catch (UnconvertibleException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("rad"));
+        }
+    }
+
+    /**
+     * Tests {@link SystemUnit#getConverterToAny(Unit)}.
+     *
+     * @throws IncommensurableException if two units were not expected to be considered incompatible.
+     */
+    @Test
+    @DependsOnMethod("testGetConverterTo")
+    public void testGetConverterToAny() throws IncommensurableException {
+        assertTrue(Units.METRE .getConverterToAny(Units.METRE ).isIdentity());
+        assertTrue(Units.SECOND.getConverterToAny(Units.SECOND).isIdentity());
+        assertTrue(Units.RADIAN.getConverterToAny(Units.RADIAN).isIdentity());
+        assertTrue(Units.RADIAN.getConverterToAny(Units.UNITY ).isIdentity());
+        assertTrue(Units.UNITY .getConverterToAny(Units.RADIAN).isIdentity());
+        try {
+            Units.METRE.getConverterToAny(Units.SECOND);
+            fail("Conversion should not have been allowed.");
+        } catch (IncommensurableException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("m"));     // metre unit symbol
+            assertTrue(message, message.contains("s"));     // second unit symbol
+        }
+    }
+
+    /**
+     * Tests {@link SystemUnit#alternate(String)}.
+     */
+    @Test
+    public void testAlternate() {
+        assertSame(Units.RADIAN, Units.RADIAN.alternate("rad"));
+    }
+}

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

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

Modified: 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=1765690&r1=1765689&r2=1765690&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitDimensionTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitDimensionTest.java
[UTF-8] Wed Oct 19 17:44:54 2016
@@ -136,15 +136,14 @@ public final strictfp class UnitDimensio
     /**
      * 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
+     * {@link UnitDimension#pow(int)} since this test uses constants that were created with
above
      * operations.
      */
     @Test
     public void testGetBaseDimensions() {
-        assertNull("METRE",  LENGTH       .getBaseDimensions());
-        assertNull("SECOND", TIME         .getBaseDimensions());
-        assertTrue("UNITY",  DIMENSIONLESS.getBaseDimensions().isEmpty());
-
+        assertNull("LENGTH",        LENGTH       .getBaseDimensions());
+        assertNull("TIME",          TIME         .getBaseDimensions());
+        assertTrue("DIMENSIONLESS", DIMENSIONLESS.getBaseDimensions().isEmpty());
         assertMapEquals(Collections.singletonMap(LENGTH, 3), VOLUME.getBaseDimensions());
 
         final Map<Dimension,Integer> expected = new HashMap<>(4);
@@ -196,10 +195,9 @@ public final strictfp class UnitDimensio
     @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));
+        assertSame(LENGTH,        assertSerializedEquals(LENGTH));
+        assertSame(TIME,          assertSerializedEquals(TIME));
+        assertSame(FORCE,         assertSerializedEquals(FORCE));
+        assertSame(DIMENSIONLESS, assertSerializedEquals(DIMENSIONLESS));
     }
 }

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=1765690&r1=1765689&r2=1765690&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 17:44:54 2016
@@ -36,7 +36,7 @@ import static org.junit.Assert.*;
  * @version 0.8
  * @module
  */
-@DependsOn(UnitDimensionTest.class)
+@DependsOn(SystemUnitTest.class)
 public final strictfp class UnitFormatTest extends TestCase {
     /**
      * Verifies all constants defined in {@link Units} class. This method verifies:

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=1765690&r1=1765689&r2=1765690&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 17:44:54 2016
@@ -104,6 +104,7 @@ import org.junit.BeforeClass;
     // Measurements and formatting.
     org.apache.sis.measure.SexagesimalConverterTest.class,
     org.apache.sis.measure.UnitDimensionTest.class,
+    org.apache.sis.measure.SystemUnitTest.class,
     org.apache.sis.measure.UnitFormatTest.class,
     org.apache.sis.measure.UnitsTest.class,
     org.apache.sis.measure.RangeTest.class,



Mime
View raw message