Package org.djunits.unit
Class UnitTest
java.lang.Object
org.djunits.unit.UnitTest
Tests for
UnitInterface, AbstractUnit, and representative concrete units.
The goal is to validate the shared unit contracts:
- Public getters and default conversion methods of
UnitInterface. - Construction guards, equality/hashCode/toString, SI-prefix generation, and derivation in
AbstractUnit. - Integration with existing concrete units (
Length.Unit,Mass.Unit,Frequency.Unit). - Defining a new unit inside the test (Jerk: m/s^3) using
AbstractUnitto verify extensibility.
- Author:
- Alexander Verbraeck (specifications); Test implementation by Copilot.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static final classMinimal Jerk quantity to exerciseAbstractUnitextensibility inside the test.(package private) static final classA minimal test-local quantity representing a "per mass" value (unit: 1/kg), used to verify correct behavior of per-kilo SI-prefix generation onAbstractUnit. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) voidConstructor validation inAbstractUnit: null or empty fields must be rejected.(package private) void(package private) voidVerify that a newly defined unit (Jerk) behaves correctly: SI-prefix generation on a base-scale unit, Derivation and Units registry lookups, UnitInterface.ofSi(...) creates the correct quantity, quantityInUnit(...) sets SI value and display unit.(package private) voidVerify thatAbstractUnit.deriveUnit(String, String, String, double, UnitSystem)multiplies linear factors and throws for non-linear scales.(package private) voidSI-prefix generation: guard paths and positive paths.(package private) voidSmoke-tests thatUnitInterface.ofSi(double)on concrete units produces the right quantity type and SI value.(package private) voidVerify per-kilo prefix generation and semantics for a test-local quantityPerMass(unit 1/kg).(package private) voidVerifyUnitInterface.setSiPrefix(SIPrefix),UnitInterface.setSiPrefix(String),UnitInterface.setSiPrefixKilo(String), andUnitInterface.setSiPrefixPer(String)on a fresh, test-only unit instance (to avoid mutating shared singletons).(package private) voidSanity: stored vs (potentially localized) getters return non-null strings; stored is stable.(package private) voidVerifyUnitInterface.toBaseValue(double)andUnitInterface.fromBaseValue(double)forward to the underlyingScaleand work for base and derived units.(package private) voidVerify basic metadata and identity of units (id/name/abbreviations/system/scale) without assuming localization.(package private) voidVerifyUnitInterface.quantityInUnit(double)constructs a quantity with correct SI value and display unit.
-
Constructor Details
-
UnitTest
UnitTest()
-
-
Method Details
-
testUnitInterfaceDefaultConversions
@Test @DisplayName("UnitInterface default conversions: base and derived") void testUnitInterfaceDefaultConversions()VerifyUnitInterface.toBaseValue(double)andUnitInterface.fromBaseValue(double)forward to the underlyingScaleand work for base and derived units. -
testUnitInterfaceQuantityInUnit
@Test @DisplayName("UnitInterface.quantityInUnit: SI value and display unit are set") void testUnitInterfaceQuantityInUnit()VerifyUnitInterface.quantityInUnit(double)constructs a quantity with correct SI value and display unit. -
testUnitInterfaceMetadata
@Test @DisplayName("UnitInterface metadata: id/name/abbreviations/system/scale") void testUnitInterfaceMetadata()Verify basic metadata and identity of units (id/name/abbreviations/system/scale) without assuming localization. -
testAbstractUnitConstructorValidation
@Test @DisplayName("AbstractUnit: constructor validation") void testAbstractUnitConstructorValidation()Constructor validation inAbstractUnit: null or empty fields must be rejected. -
testAbstractUnitEqualsHashCodeToString
@Test @DisplayName("AbstractUnit: equals/hashCode and toString") void testAbstractUnitEqualsHashCodeToString() -
testGenerateSiPrefixes
@Test @DisplayName("AbstractUnit.generateSiPrefixes: guard paths and positives") void testGenerateSiPrefixes()SI-prefix generation: guard paths and positive paths. -
testSetGetSiPrefix
@Test @DisplayName("UnitInterface: set/get SI prefix on a fresh unit") void testSetGetSiPrefix()VerifyUnitInterface.setSiPrefix(SIPrefix),UnitInterface.setSiPrefix(String),UnitInterface.setSiPrefixKilo(String), andUnitInterface.setSiPrefixPer(String)on a fresh, test-only unit instance (to avoid mutating shared singletons). -
testCustomUnitJerk
@Test @DisplayName("New unit (Jerk): prefixes, derivation, ofSi, quantityInUnit") void testCustomUnitJerk()Verify that a newly defined unit (Jerk) behaves correctly:- SI-prefix generation on a base-scale unit,
- Derivation and Units registry lookups,
- UnitInterface.ofSi(...) creates the correct quantity,
- quantityInUnit(...) sets SI value and display unit.
-
testDeriveUnitLinearAndGuard
@Test @DisplayName("deriveUnit: linear factor multiplication and non-linear guard") void testDeriveUnitLinearAndGuard()Verify thatAbstractUnit.deriveUnit(String, String, String, double, UnitSystem)multiplies linear factors and throws for non-linear scales. -
testOfSiConcreteSmoke
@Test @DisplayName("UnitInterface.ofSi: concrete units produce correct quantity type/SI value") void testOfSiConcreteSmoke()Smoke-tests thatUnitInterface.ofSi(double)on concrete units produces the right quantity type and SI value. -
testStoredVsLocalizedGetters
@Test @DisplayName("Stored vs localized getters are sane and stable") void testStoredVsLocalizedGetters()Sanity: stored vs (potentially localized) getters return non-null strings; stored is stable. -
testPerKiloUnitGenerationAndSemantics
@Test @DisplayName("Per-kilo unit generation and semantics (PerMass: 1/kg)") void testPerKiloUnitGenerationAndSemantics()Verify per-kilo prefix generation and semantics for a test-local quantityPerMass(unit 1/kg).- Prefix generation with
(kilo=true, perUnit=true)produces per-gram (/g) and per-megagram (/Mg), etc. - Scale factors match expectations: /kg = 10^0, /g = 10^3, /Mg = 10^-3.
- Derived names are correct: "per gram", "per megagram".
- SIPrefix objects are set on the derived units as created by
generateSiPrefixes. quantityInUnituses the generated scales correctly.
- Prefix generation with
-