001/* 002 * Units of Measurement TCK 003 * Copyright © 2005-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 tech.units.tck.tests.unit; 031 032import static org.hamcrest.MatcherAssert.assertThat; 033import static org.hamcrest.Matchers.empty; 034import static org.hamcrest.core.Is.is; 035import static org.hamcrest.core.IsNot.not; 036import static org.testng.Assert.assertNotNull; 037import static tech.units.tck.TCKRunner.SECTION_PREFIX; 038import static tech.units.tck.TCKRunner.SPEC_ID; 039import static tech.units.tck.TCKRunner.SPEC_VERSION; 040import static tech.units.tck.util.TestGroups.CORE; 041import static tech.units.tck.util.TestUtils.MSG_NO_TCK_CONFIG; 042import static tech.units.tck.util.TestUtils.testHasPublicMethod; 043 044import java.util.List; 045import javax.measure.UnitConverter; 046import org.jboss.test.audit.annotations.SpecAssertion; 047import org.jboss.test.audit.annotations.SpecVersion; 048import org.testng.annotations.Test; 049 050import tech.units.tck.TCKSetup; 051 052/** 053 * Tests for Unit Conversion 054 * 055 * @author Werner Keil 056 * @author Almas Shaikh 057 * @version 2.2, October 4, 2023 058 * @since 1.0 059 */ 060@SpecVersion(spec = SPEC_ID, version = SPEC_VERSION) 061public class UnitConversionTest { 062 private static final String SECTION = "4.2.3"; 063 064 /** 065 * Ensure at least one UnitConverter implementation is available/registered. 066 */ 067 @SpecAssertion(section = SECTION, id = "423-A1") 068 @Test(groups = { CORE }, description = SECTION + " Ensure at least one UnitConverter implementation is available/registered.") 069 public void testEnsureGotConverters() { 070 assertNotNull(TCKSetup.getConfiguration(), MSG_NO_TCK_CONFIG); 071 assertThat("No UnitConverter implementation found.", TCKSetup.getConfiguration().getUnitConverters4Test(), is(not(empty()))); 072 } 073 074 /** 075 * Test that UnitConverter implementations override equals. 076 */ 077 @SpecAssertion(section = SECTION, id = "423-A2") 078 @Test(groups = { CORE }, description = SECTION + " Ensure registered UnitConverter classes override equals.") 079 public void testUnitConverterEquals() { 080 for (UnitConverter converter : TCKSetup.getConfiguration().getUnitConverters4Test()) { 081 Class<?> type = converter.getClass(); 082 testHasPublicMethod(SECTION_PREFIX + SECTION, type, true, boolean.class,"equals", Object.class); 083 } 084 } 085 086 /** 087 * Test that UnitConverter implementations override hashCode. 088 */ 089 @SpecAssertion(section = SECTION, id = "423-A3") 090 @Test(groups = { CORE }, description = SECTION + " Ensure registered UnitConverter classes override hashCode.") 091 public void testUnitConverterHashcode() { 092 for (UnitConverter converter : TCKSetup.getConfiguration().getUnitConverters4Test()) { 093 Class<?> type = converter.getClass(); 094 testHasPublicMethod(SECTION_PREFIX + SECTION, type, true, int.class,"hashCode"); 095 } 096 } 097 098 /** 099 * Ensure the inverse() method is implemented. 100 */ 101 @SpecAssertion(section = SECTION, id = "423-A4") 102 @Test(groups = { CORE }, description = SECTION + " Ensure the inverse() method is implemented.") 103 public void testUnitConverterInvert() { 104 for (UnitConverter converter : TCKSetup.getConfiguration().getUnitConverters4Test()) { 105 Class<?> type = converter.getClass(); 106 testHasPublicMethod(SECTION_PREFIX + SECTION, type, "inverse"); 107 } 108 } 109 110 /** 111 * Ensure the isIdentity() operation is implemented. 112 */ 113 @SpecAssertion(section = SECTION, id = "423-A5") 114 @Test(groups = { CORE }, description = SECTION + " Ensure the isIdentity() method is implemented.") 115 public void testUnitConverterIsIdentity() { 116 for (UnitConverter converter : TCKSetup.getConfiguration().getUnitConverters4Test()) { 117 Class<?> type = converter.getClass(); 118 testHasPublicMethod(SECTION_PREFIX + SECTION, type, "isIdentity"); 119 } 120 } 121 122 /** 123 * Ensure the isLinear() operation is implemented. 124 */ 125 @SpecAssertion(section = SECTION, id = "423-A6") 126 @Test(groups = { CORE }, description = SECTION + " Ensure the isLinear() method is implemented.") 127 public void testUnitConverterIsLinear() { 128 for (UnitConverter converter : TCKSetup.getConfiguration().getUnitConverters4Test()) { 129 Class<?> type = converter.getClass(); 130 testHasPublicMethod(SECTION_PREFIX + SECTION, type, "isLinear"); 131 } 132 } 133 134 /** 135 * Ensure the convert() operation is implemented. 136 */ 137 @SpecAssertion(section = SECTION, id = "423-A7") 138 @Test(groups = { CORE }, description = SECTION + " Ensure the convert() method is implemented.") 139 public void testUnitConverterConvert() { 140 for (UnitConverter converter : TCKSetup.getConfiguration().getUnitConverters4Test()) { 141 Class<?> type = converter.getClass(); 142 testHasPublicMethod(SECTION_PREFIX + SECTION, type, Number.class, "convert", Number.class); 143 } 144 } 145 146 /** 147 * Ensure the convert() operation is implemented. 148 */ 149 @SpecAssertion(section = SECTION, id = "423-A8") 150 @Test(groups = { CORE }, description = SECTION + " Ensure the convert() method is implemented.") 151 public void testUnitConverterConvertWithDouble() { 152 for (UnitConverter converter : TCKSetup.getConfiguration().getUnitConverters4Test()) { 153 Class<?> type = converter.getClass(); 154 testHasPublicMethod(SECTION_PREFIX + SECTION, type, double.class, "convert", double.class); 155 } 156 } 157 158 /** 159 * Ensure the concatenate() operation is implemented. 160 */ 161 @SpecAssertion(section = SECTION, id = "423-A9") 162 @Test(groups = { CORE }, description = SECTION + " Ensure the concatenate() method is implemented.") 163 public void testUnitConverterConcatenate() { 164 for (UnitConverter converter : TCKSetup.getConfiguration().getUnitConverters4Test()) { 165 Class<?> type = converter.getClass(); 166 testHasPublicMethod(SECTION_PREFIX + SECTION, type, UnitConverter.class, "concatenate", UnitConverter.class); 167 } 168 } 169 170 /** 171 * Ensure the getConversionSteps() operation is implemented. 172 */ 173 @SpecAssertion(section = SECTION, id = "423-A10") 174 @Test(groups = { CORE }, description = SECTION + " Ensure the getConversionSteps() method is implemented.") 175 public void testUnitConverterGetConversionSteps() { 176 for (UnitConverter converter : TCKSetup.getConfiguration().getUnitConverters4Test()) { 177 Class<?> type = converter.getClass(); 178 testHasPublicMethod(SECTION_PREFIX + SECTION, type, List.class, "getConversionSteps"); 179 } 180 } 181}