Class UnitTest

java.lang.Object
org.djunits.unit.UnitTest

final class UnitTest extends Object
Tests for UnitInterface, AbstractUnit, and representative concrete units.

The goal is to validate the shared unit contracts:

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

    • UnitTest

      UnitTest()
  • Method Details

    • testUnitInterfaceDefaultConversions

      @Test @DisplayName("UnitInterface default conversions: base and derived") void testUnitInterfaceDefaultConversions()
      Verify UnitInterface.toBaseValue(double) and UnitInterface.fromBaseValue(double) forward to the underlying Scale and work for base and derived units.
    • testUnitInterfaceQuantityInUnit

      @Test @DisplayName("UnitInterface.quantityInUnit: SI value and display unit are set") void testUnitInterfaceQuantityInUnit()
      Verify UnitInterface.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 in AbstractUnit: 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()
    • 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 that AbstractUnit.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 that UnitInterface.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 quantity PerMass (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.
      • quantityInUnit uses the generated scales correctly.