Class AbsQuantityTest

java.lang.Object
org.djunits.quantity.def.AbsQuantityTest

public class AbsQuantityTest extends Object
Unit tests for the abstract AbsQuantity API using Position and its nested Position.Reference.

Goals and coverage:

Deliberately out of scope: We do not re-test domain specifics of Position/Length. Those are covered by their dedicated tests; here, Position provides a concrete vehicle to exercise the abstract API.

Locale pinning: The suite pins Locale.Category.FORMAT to Locale.US for deterministic formatting/parsing behavior; the original locale is restored afterwards.

Reference registry hygiene: Each created Position.Reference is unregistered in cleanup() to avoid cross-test pollution of the static per-class registry. Copyright (c) 2025-2026 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djunits.org. The DJUNITS project is distributed under a three-clause BSD-style license.

Author:
Alexander Verbraeck (specifications); Test implementation by Copilot.
  • Constructor Details

    • AbsQuantityTest

      public AbsQuantityTest()
  • Method Details

    • pinLocale

      @BeforeAll static void pinLocale()
      Pin the default Locale for predictable formatting/parsing behavior.
    • restoreLocale

      @AfterAll static void restoreLocale()
      Restore the default Locale for the FORMAT category.
    • setup

      @BeforeEach void setup()
      Establishes a small reference graph for tests.
      • A: root
      • B: A + 2 m
      • C: B + 1 m (i.e., A + 3 m)
      • X: unrelated root
      • Y: related indirect root
      • Z: unrelated indirect root
    • cleanup

      @AfterEach void cleanup()
      Unregisters created references from the per-class registry to avoid cross-test pollution.
    • constructorNulls

      @Test void constructorNulls()
      Verifies that the constructor of Position (as a representative of AbsQuantity) rejects null for either the relative quantity or the reference.
    • displayUnitAccessors

      @Test void displayUnitAccessors()
      Verifies that AbsQuantity.getDisplayUnit() and AbsQuantity.setDisplayUnit(org.djunits.unit.Unit) delegate correctly to the inner relative quantity and that the setter is fluent.
    • quantityAndReferenceAccessors

      @Test void quantityAndReferenceAccessors()
    • getInUnitVariants

      @Test void getInUnitVariants()
    • comparisonsAndReferenceConstraints

      @Test void comparisonsAndReferenceConstraints()
      Verifies that comparisons (lt/le/gt/ge) and Comparable.compareTo(Object) require identical references, otherwise an IllegalArgumentException is thrown.

      Also verifies eq/ne logic (which includes reference equality for absolute quantities).

    • zeroComparisons

      @Test void zeroComparisons()
      Verifies zero-comparison helpers (lt0/le0/gt0/ge0/eq0/ne0) for AbsQuantity.
    • valueOfParses

      @Test void valueOfParses()
      Verifies successful parsing via AbsQuantity.valueOf(String, AbsQuantity, Reference) and error branches for null parameters, empty text, and unknown units.
    • ofParses

      @Test void ofParses()
      Verifies successful parsing via AbsQuantity.of(double, String, AbsQuantity, Reference) and error branches for null parameters, empty unit string, and unknown units.
    • relativeToVariants

      @Test void relativeToVariants()
      Verifies AbsQuantity.relativeTo(Reference) for:
      • identity transform (same reference)
      • direct parent/child transform (A ⇄ B)
      • multi-level transform (C ⇄ A via B)
      • unrelated roots (should throw)
      Graph (SI offsets): A=0, B=A+2, C=B+1 (=A+3).
    • testDefaultToString

      @Test @DisplayName("Default toString() uses default number and display unit") public void testDefaultToString()
      Test default toString() for a simple direction.
    • testToStringWithTargetUnit

      @Test @DisplayName("toString(Unit) converts value and displays target unit") public void testToStringWithTargetUnit()
      Test toString(Unit) converts the unit for display.
    • testLargeMagnitudeFixed

      @Test @DisplayName("Large magnitude with explicit number format") public void testLargeMagnitudeFixed()
      Test magnitude change with decimals and width.
    • testCombinedFormats

      @Test @DisplayName("Combined number format, unit format and quantity format") public void testCombinedFormats()
      Test multiple format settings combined.
    • testLocaleFormat

      @Test @DisplayName("Locale format affects decimal separator") public void testLocaleFormat()
      Test locale influence on decimal separator.
    • subtractAbsolute

      @Test void subtractAbsolute()
      Verifies AbsQuantity.subtract(AbsQuantity) aligns references and returns the relative difference.

      Case: pA = 10@A, pB = 8@B represent the same absolute point; the difference is 0.

    • addSubtractRelative

      @Test void addSubtractRelative()
      Verifies AbsQuantity.add(Quantity) and AbsQuantity.subtract(Quantity) adjust the inner relative quantity while preserving the absolute reference and the display unit.
    • interpolateAbsolute

      @Test void interpolateAbsolute()
      Verifies AbsQuantity.interpolate(AbsQuantity, AbsQuantity, double). For:
    • maxMin

      @Test void maxMin()
      Verifies AbsQuantity.max(AbsQuantity, AbsQuantity[]) and AbsQuantity.min(AbsQuantity, AbsQuantity[]) for identical references and that mixing references raises an IllegalArgumentException.
    • sumMean

      @Test void sumMean()
      Verifies AbsQuantity.sum(AbsQuantity, AbsQuantity[]) and AbsQuantity.mean(AbsQuantity, AbsQuantity[]) for identical references, and ensures that the reference and display unit of the first argument are preserved. Mixing references must throw.
    • siUnit

      @Test void siUnit()
      Verifies correct working of AbsQuantity.siUnit().
    • equalsHashCode

      @Test void equalsHashCode()
      Verifies equals/hashCode consider both the inner relative quantity and the reference identity.
    • getNameFormatsPrettyForAbsoluteQuantity

      @Test void getNameFormatsPrettyForAbsoluteQuantity()
      Tests AbsQuantity.getName() using Position as the representative absolute quantity.

      The "pretty name" algorithm mirrors Quantity.getName() and:

      1. Keeps the first character of the localized simple name exactly as-is;
      2. For each subsequent uppercase character, inserts a space if the previous character is not a space, and appends the lower-cased version of that uppercase character;
      3. Otherwise, appends the character unchanged.

      Coverage note: The class name Position contains only an initial uppercase, so this test covers the "no internal uppercase" path. If you want to also cover the branch that inserts spaces for internal capitals for absolute quantities, add a trivial test-only subclass, e.g., TestPositionAbsolute extends Position, and assert that new TestPositionAbsolute(...).getName() becomes "Test position absolute".

      Expected: new Position(...).getName() yields "Position".