Class AbsoluteQuantityTest
AbsoluteQuantity API using Position and its nested
Position.Reference.
Goals and coverage:
- Reference-aware comparisons and
Comparablerules - Reference transformations via
AbsoluteQuantity.relativeTo(AbstractReference) - Parsing helpers:
AbsoluteQuantity.valueOf(String, AbsoluteQuantity, AbstractReference)andAbsoluteQuantity.of(double, String, AbsoluteQuantity, AbstractReference) - Stringification and SI-prefix formatting
- Static helpers:
AbsoluteQuantity.interpolate(AbsoluteQuantity, AbsoluteQuantity, double),AbsoluteQuantity.max(AbsoluteQuantity, AbsoluteQuantity[]),AbsoluteQuantity.min(AbsoluteQuantity, AbsoluteQuantity[]),AbsoluteQuantity.sum(AbsoluteQuantity, AbsoluteQuantity[]),AbsoluteQuantity.mean(AbsoluteQuantity, AbsoluteQuantity[]) - Arithmetic with relative quantities:
AbsoluteQuantity.add(Quantity)andAbsoluteQuantity.subtract(Quantity)andAbsoluteQuantity.subtract(AbsoluteQuantity) equals/hashCodecontract
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classAbsolute quantity class for test. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) voidVerifiesAbsoluteQuantity.add(Quantity)andAbsoluteQuantity.subtract(Quantity)adjust the inner relative quantity while preserving the absolute reference and the display unit.(package private) voidcleanup()Unregisters created references from the per-class registry to avoid cross-test pollution.(package private) voidVerifies concise textual and display strings for absolute quantities.(package private) voidVerifies that comparisons (lt/le/gt/ge) andComparable.compareTo(Object)require identical references, otherwise anIllegalArgumentExceptionis thrown.(package private) voidVerifies that the constructor ofPosition(as a representative ofAbsoluteQuantity) rejectsnullfor either the relative quantity or the reference.(package private) voidVerifies thatAbsoluteQuantity.getDisplayUnit()andAbsoluteQuantity.setDisplayUnit(org.djunits.unit.UnitInterface)delegate correctly to the inner relative quantity and that the setter is fluent.(package private) voidVerifiesequals/hashCodeconsider both the inner relative quantity and the reference identity.(package private) voidVerifiesAbsoluteQuantity.format(double)andAbsoluteQuantity.format(double, String)paths: fixed format range and E-notation range.(package private) void(package private) voidTestsAbsoluteQuantity.getName()usingPositionas the representative absolute quantity.(package private) void(package private) voidmaxMin()VerifiesAbsoluteQuantity.max(AbsoluteQuantity, AbsoluteQuantity[])andAbsoluteQuantity.min(AbsoluteQuantity, AbsoluteQuantity[])for identical references and that mixing references raises anIllegalArgumentException.(package private) voidVerifies thatNumber-derived conversions reflect the SI value semantics forAbsoluteQuantity.(package private) voidofParses()Verifies successful parsing viaAbsoluteQuantity.of(double, String, AbsoluteQuantity, AbstractReference)and error branches fornullparameters, empty unit string, and unknown units.(package private) static voidPin the defaultLocalefor predictable formatting/parsing behavior.(package private) voidVerifies the accessorsAbsoluteQuantity.getQuantity()andAbsoluteQuantity.getReference().(package private) voidVerifiesAbsoluteQuantity.relativeTo(AbstractReference)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).(package private) static voidRestore the defaultLocalefor the FORMAT category.(package private) voidsetup()Establishes a small reference graph for tests.(package private) voidVerifiesAbsoluteQuantity.subtract(AbsoluteQuantity)aligns references and returns the relative difference.(package private) voidsumMean()VerifiesAbsoluteQuantity.sum(AbsoluteQuantity, AbsoluteQuantity[])andAbsoluteQuantity.mean(AbsoluteQuantity, AbsoluteQuantity[])for identical references, and ensures that the reference and display unit of the first argument are preserved.(package private) voidVerifies thatAbsoluteQuantity.toStringSIPrefixed()delegates to the inner relative quantity and produces a sensible SI prefix when possible.(package private) voidVerifiesAbsoluteQuantity.toString()variants, ensuring that whenwithUnit=truethe reference id is appended as " (refId)" and that the verbose flag adds the "Abs " prefix.(package private) voidVerifies successful parsing viaAbsoluteQuantity.valueOf(String, AbsoluteQuantity, AbstractReference)and error branches fornullparameters, empty text, and unknown units.(package private) voidVerifies zero-comparison helpers (lt0/le0/gt0/ge0/eq0/ne0) forAbsoluteQuantity.
-
Constructor Details
-
AbsoluteQuantityTest
public AbsoluteQuantityTest()
-
-
Method Details
-
pinLocale
@BeforeAll static void pinLocale()Pin the defaultLocalefor predictable formatting/parsing behavior. -
restoreLocale
@AfterAll static void restoreLocale()Restore the defaultLocalefor the FORMAT category. -
setup
@BeforeEach void setup()Establishes a small reference graph for tests.A: rootB: A + 2 mC: B + 1 m (i.e., A + 3 m)X: unrelated 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 ofPosition(as a representative ofAbsoluteQuantity) rejectsnullfor either the relative quantity or the reference. -
displayUnitAccessors
@Test void displayUnitAccessors()Verifies thatAbsoluteQuantity.getDisplayUnit()andAbsoluteQuantity.setDisplayUnit(org.djunits.unit.UnitInterface)delegate correctly to the inner relative quantity and that the setter is fluent. -
quantityAndReferenceAccessors
@Test void quantityAndReferenceAccessors()Verifies the accessorsAbsoluteQuantity.getQuantity()andAbsoluteQuantity.getReference(). -
getInUnitVariants
@Test void getInUnitVariants() -
numberConversions
@Test void numberConversions()Verifies thatNumber-derived conversions reflect the SI value semantics forAbsoluteQuantity. -
comparisonsAndReferenceConstraints
@Test void comparisonsAndReferenceConstraints()Verifies that comparisons (lt/le/gt/ge) andComparable.compareTo(Object)require identical references, otherwise anIllegalArgumentExceptionis 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) forAbsoluteQuantity. -
valueOfParses
@Test void valueOfParses()Verifies successful parsing viaAbsoluteQuantity.valueOf(String, AbsoluteQuantity, AbstractReference)and error branches fornullparameters, empty text, and unknown units. -
ofParses
@Test void ofParses()Verifies successful parsing viaAbsoluteQuantity.of(double, String, AbsoluteQuantity, AbstractReference)and error branches fornullparameters, empty unit string, and unknown units. -
relativeToVariants
@Test void relativeToVariants()VerifiesAbsoluteQuantity.relativeTo(AbstractReference)for:- identity transform (same reference)
- direct parent/child transform (A ⇄ B)
- multi-level transform (C ⇄ A via B)
- unrelated roots (should throw)
-
formatVariants
@Test void formatVariants()VerifiesAbsoluteQuantity.format(double)andAbsoluteQuantity.format(double, String)paths: fixed format range and E-notation range. -
toStringVariants
@Test void toStringVariants()VerifiesAbsoluteQuantity.toString()variants, ensuring that whenwithUnit=truethe reference id is appended as " (refId)" and that the verbose flag adds the "Abs " prefix. -
toStringSIPrefixed
@Test void toStringSIPrefixed()Verifies thatAbsoluteQuantity.toStringSIPrefixed()delegates to the inner relative quantity and produces a sensible SI prefix when possible. -
compactStrings
@Test void compactStrings()Verifies concise textual and display strings for absolute quantities. -
subtractAbsolute
@Test void subtractAbsolute()VerifiesAbsoluteQuantity.subtract(AbsoluteQuantity)aligns references and returns the relative difference.Case:
pA = 10@A,pB = 8@Brepresent the same absolute point; the difference is 0. -
addSubtractRelative
@Test void addSubtractRelative()VerifiesAbsoluteQuantity.add(Quantity)andAbsoluteQuantity.subtract(Quantity)adjust the inner relative quantity while preserving the absolute reference and the display unit. -
interpolateAbsolute
@Test void interpolateAbsolute()VerifiesAbsoluteQuantity.interpolate(AbsoluteQuantity, AbsoluteQuantity, double). For:- valid ratio in [0, 1]
- ratio out of bounds raising
IllegalArgumentException - reference mismatch raising
IllegalArgumentException
-
maxMin
@Test void maxMin()VerifiesAbsoluteQuantity.max(AbsoluteQuantity, AbsoluteQuantity[])andAbsoluteQuantity.min(AbsoluteQuantity, AbsoluteQuantity[])for identical references and that mixing references raises anIllegalArgumentException. -
sumMean
@Test void sumMean()VerifiesAbsoluteQuantity.sum(AbsoluteQuantity, AbsoluteQuantity[])andAbsoluteQuantity.mean(AbsoluteQuantity, AbsoluteQuantity[])for identical references, and ensures that the reference and display unit of the first argument are preserved. Mixing references must throw. -
equalsHashCode
@Test void equalsHashCode()Verifiesequals/hashCodeconsider both the inner relative quantity and the reference identity. -
getNameFormatsPrettyForAbsoluteQuantity
@Test void getNameFormatsPrettyForAbsoluteQuantity()TestsAbsoluteQuantity.getName()usingPositionas the representative absolute quantity.The "pretty name" algorithm mirrors
Quantity.getName()and:- Keeps the first character of the localized simple name exactly as-is;
- 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;
- Otherwise, appends the character unchanged.
Coverage note: The class name
Positioncontains 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 thatnew TestPositionAbsolute(...).getName()becomes"Test position absolute".Expected:
new Position(...).getName()yields"Position".
-