Class AbsQuantityTest
AbsQuantity API using Position and its nested
Position.Reference.
Goals and coverage:
- Reference-aware comparisons and
Comparablerules - Reference transformations via
AbsQuantity.relativeTo(Reference) - Parsing helpers:
AbsQuantity.valueOf(String, AbsQuantity, Reference)andAbsQuantity.of(double, String, AbsQuantity, Reference) - Stringification and SI-prefix formatting
- Static helpers:
AbsQuantity.interpolate(AbsQuantity, AbsQuantity, double),AbsQuantity.max(AbsQuantity, AbsQuantity[]),AbsQuantity.min(AbsQuantity, AbsQuantity[]),AbsQuantity.sum(AbsQuantity, AbsQuantity[]),AbsQuantity.mean(AbsQuantity, AbsQuantity[]) - Arithmetic with relative quantities:
AbsQuantity.add(Quantity)andAbsQuantity.subtract(Quantity)andAbsQuantity.subtract(AbsQuantity) 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.(package private) static classRelative quantity class for test. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) voidVerifiesAbsQuantity.add(Quantity)andAbsQuantity.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 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 ofAbsQuantity) rejectsnullfor either the relative quantity or the reference.(package private) voidVerifies thatAbsQuantity.getDisplayUnit()andAbsQuantity.setDisplayUnit(org.djunits.unit.Unit)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) void(package private) voidTestsAbsQuantity.getName()usingPositionas the representative absolute quantity.(package private) void(package private) voidmaxMin()VerifiesAbsQuantity.max(AbsQuantity, AbsQuantity[])andAbsQuantity.min(AbsQuantity, AbsQuantity[])for identical references and that mixing references raises anIllegalArgumentException.(package private) voidofParses()Verifies successful parsing viaAbsQuantity.of(double, String, AbsQuantity, Reference)and error branches fornullparameters, empty unit string, and unknown units.(package private) static voidPin the defaultLocalefor predictable formatting/parsing behavior.(package private) voidVerifies the accessorsAbsQuantity.getQuantity()andAbsQuantity.getReference().(package private) voidVerifiesAbsQuantity.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).(package private) static voidRestore the defaultLocalefor the FORMAT category.(package private) voidsetup()Establishes a small reference graph for tests.(package private) voidsiUnit()Verifies correct working ofAbsQuantity.siUnit().(package private) voidVerifiesAbsQuantity.subtract(AbsQuantity)aligns references and returns the relative difference.(package private) voidsumMean()VerifiesAbsQuantity.sum(AbsQuantity, AbsQuantity[])andAbsQuantity.mean(AbsQuantity, AbsQuantity[])for identical references, and ensures that the reference and display unit of the first argument are preserved.voidTest multiple format settings combined.voidTest defaulttoString()for a simple direction.voidTest magnitude change with decimals and width.voidTest locale influence on decimal separator.voidTesttoString(Unit)converts the unit for display.(package private) voidVerifies successful parsing viaAbsQuantity.valueOf(String, AbsQuantity, Reference)and error branches fornullparameters, empty text, and unknown units.(package private) voidVerifies zero-comparison helpers (lt0/le0/gt0/ge0/eq0/ne0) forAbsQuantity.
-
Constructor Details
-
AbsQuantityTest
public AbsQuantityTest()
-
-
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 rootY: related indirect rootZ: 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 ofPosition(as a representative ofAbsQuantity) rejectsnullfor either the relative quantity or the reference. -
displayUnitAccessors
@Test void displayUnitAccessors()Verifies thatAbsQuantity.getDisplayUnit()andAbsQuantity.setDisplayUnit(org.djunits.unit.Unit)delegate correctly to the inner relative quantity and that the setter is fluent. -
quantityAndReferenceAccessors
@Test void quantityAndReferenceAccessors()Verifies the accessorsAbsQuantity.getQuantity()andAbsQuantity.getReference(). -
getInUnitVariants
@Test void getInUnitVariants() -
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) forAbsQuantity. -
valueOfParses
@Test void valueOfParses()Verifies successful parsing viaAbsQuantity.valueOf(String, AbsQuantity, Reference)and error branches fornullparameters, empty text, and unknown units. -
ofParses
@Test void ofParses()Verifies successful parsing viaAbsQuantity.of(double, String, AbsQuantity, Reference)and error branches fornullparameters, empty unit string, and unknown units. -
relativeToVariants
@Test void relativeToVariants()VerifiesAbsQuantity.relativeTo(Reference)for:- identity transform (same reference)
- direct parent/child transform (A ⇄ B)
- multi-level transform (C ⇄ A via B)
- unrelated roots (should throw)
-
testDefaultToString
@Test @DisplayName("Default toString() uses default number and display unit") public void testDefaultToString()Test defaulttoString()for a simple direction. -
testToStringWithTargetUnit
@Test @DisplayName("toString(Unit) converts value and displays target unit") public void testToStringWithTargetUnit()TesttoString(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()VerifiesAbsQuantity.subtract(AbsQuantity)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()VerifiesAbsQuantity.add(Quantity)andAbsQuantity.subtract(Quantity)adjust the inner relative quantity while preserving the absolute reference and the display unit. -
interpolateAbsolute
@Test void interpolateAbsolute()VerifiesAbsQuantity.interpolate(AbsQuantity, AbsQuantity, double). For:- valid ratio in [0, 1]
- ratio out of bounds raising
IllegalArgumentException - reference mismatch raising
IllegalArgumentException
-
maxMin
@Test void maxMin()VerifiesAbsQuantity.max(AbsQuantity, AbsQuantity[])andAbsQuantity.min(AbsQuantity, AbsQuantity[])for identical references and that mixing references raises anIllegalArgumentException. -
sumMean
@Test void sumMean()VerifiesAbsQuantity.sum(AbsQuantity, AbsQuantity[])andAbsQuantity.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 ofAbsQuantity.siUnit(). -
equalsHashCode
@Test void equalsHashCode()Verifiesequals/hashCodeconsider both the inner relative quantity and the reference identity. -
getNameFormatsPrettyForAbsoluteQuantity
@Test void getNameFormatsPrettyForAbsoluteQuantity()TestsAbsQuantity.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".
-