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