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