001/* 002 * Units of Measurement API 003 * Copyright (c) 2014-2023, Jean-Marie Dautelle, Werner Keil, Otavio Santana. 004 * 005 * All rights reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without modification, 008 * are permitted provided that the following conditions are met: 009 * 010 * 1. Redistributions of source code must retain the above copyright notice, 011 * this list of conditions and the following disclaimer. 012 * 013 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions 014 * and the following disclaimer in the documentation and/or other materials provided with the distribution. 015 * 016 * 3. Neither the name of JSR-385 nor the names of its contributors may be used to endorse or promote products 017 * derived from this software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 021 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 022 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 023 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 028 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package javax.measure.test; 031 032import static org.junit.jupiter.api.Assertions.*; 033import static javax.measure.test.EnumUnit.TEST; 034 035import javax.measure.Dimension; 036import javax.measure.UnconvertibleException; 037import javax.measure.Unit; 038import javax.measure.UnitConverter; 039import javax.measure.test.unit.BaseUnit; 040import javax.measure.test.unit.DistanceUnit; 041 042import org.junit.jupiter.api.BeforeEach; 043import org.junit.jupiter.api.Test; 044 045/** 046 * @author Werner 047 * 048 */ 049public class UnitTest { 050 private static final Number NULL_NUMBER = null; 051 052 @SuppressWarnings("rawtypes") 053 private Unit sut; 054 055 @BeforeEach 056 public void init() { 057 sut = TEST; 058 } 059 060 /** 061 * Test method for {@link javax.measure.test.EnumUnit#getSymbol()}. 062 */ 063 @Test 064 public void testGetSymbol() { 065 assertNotNull(sut.getSymbol()); 066 assertEquals("t", sut.getSymbol()); 067 } 068 069 /** 070 * Test method for {@link javax.measure.test.EnumUnit#getDimension()}. 071 */ 072 @Test 073 public void testGetDimension() { 074 final Dimension dim = TestDimension.getInstance(); 075 assertEquals(dim, sut.getDimension()); 076 } 077 078 /** 079 * Test method for {@link javax.measure.test.EnumUnit#isCompatible(javax.measure.Unit)}. 080 */ 081 @SuppressWarnings("unchecked") 082 @Test 083 public void testIsCompatible() { 084 assertTrue(sut.isCompatible(TEST)); 085 } 086 087 /** 088 * Test method for {@link javax.measure.test.EnumUnit#isEquivalentTo(javax.measure.Unit)}. 089 */ 090 @SuppressWarnings("unchecked") 091 @Test 092 public void testIsEquivalentTo() { 093 assertTrue(sut.isEquivalentTo(TEST)); 094 } 095 096 @Test 097 public void testGetConverterTo() { 098 assertThrows(UnconvertibleException.class, () -> { 099 sut = DistanceUnit.m; 100 @SuppressWarnings("unchecked") 101 UnitConverter converter = sut.getConverterTo(BaseUnit.ONE); 102 assertNotNull(converter); 103 }); 104 } 105 106 @Test 107 public void testDivideNull() { 108 assertThrows(NullPointerException.class, () -> { 109 sut = DistanceUnit.m; 110 @SuppressWarnings({ "rawtypes", "unused" }) 111 Unit result = sut.divide(NULL_NUMBER); 112 }); 113 } 114 115 @Test 116 public void testMultiplyNull() { 117 assertThrows(NullPointerException.class, () -> { 118 sut = DistanceUnit.m; 119 @SuppressWarnings({ "unused", "rawtypes" }) 120 Unit result = sut.multiply(NULL_NUMBER); 121 }); 122 } 123 124 @Test 125 public void testShiftNull() { 126 assertThrows(NullPointerException.class, () -> { 127 sut = DistanceUnit.m; 128 @SuppressWarnings({ "unused", "rawtypes" }) 129 Unit result = sut.shift(NULL_NUMBER); 130 }); 131 } 132}