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 tech.units.tck.TCKRunner.SPEC_ID;
033import static tech.units.tck.TCKRunner.SPEC_VERSION;
034import static tech.units.tck.TCKRunner.SECTION_PREFIX;
035import static tech.units.tck.util.TestGroups.CORE;
036import static tech.units.tck.util.TestUtils.testHasPublicMethod;
037
038import javax.measure.Prefix;
039import javax.measure.Unit;
040import org.jboss.test.audit.annotations.SpecAssertion;
041import org.jboss.test.audit.annotations.SpecVersion;
042import org.testng.annotations.Test;
043
044import tech.units.tck.TCKSetup;
045
046/**
047 * Testing the Unit Interface
048 *
049 * @author <a href="mailto:[email protected]">Werner Keil</a>
050 * @author Almas Shaikh
051 * @version 2.2, February 16, 2023
052 * @since 1.0
053 */
054@SpecVersion(spec = SPEC_ID, version = SPEC_VERSION)
055public class UnitInterfaceTest {
056        /** Section constants */
057        private static final String SECTION_NUM1 = "4.2.1";
058        private static final String SECTION_NUM2 = "4.2.1.2";
059
060    /**
061     * Test that Unit implementations override equals.
062     */
063    @SpecAssertion(section = SECTION_NUM1, id = "421-A1")
064    @Test(groups = { CORE }, description = SECTION_NUM1 + " Ensure registered Unit classes override equals.")
065    public void testEquals() {
066        for (@SuppressWarnings("rawtypes")
067        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
068                        testHasPublicMethod(SECTION_PREFIX + SECTION_NUM1, type, true, boolean.class,"equals", Object.class);
069        }
070    }
071
072    /**
073     * Test that Unit implementations contain getters
074     */
075    @SpecAssertion(section = SECTION_NUM1, id = "421-A2")
076    @Test(groups = { CORE }, description = SECTION_NUM1 + " Ensure registered Unit classes implement getDimension.")
077    public void testGetDimension() {
078        for (@SuppressWarnings("rawtypes")
079        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
080            testHasPublicMethod(SECTION_PREFIX + SECTION_NUM1, type, "getDimension");
081        }
082    }
083
084    /**
085     * Test that Unit implementations contain getSystemUnit
086     */
087    @SpecAssertion(section = SECTION_NUM1, id = "421-A3")
088    @Test(groups = { CORE }, description = SECTION_NUM1 + " Ensure registered Unit classes implement getSystemUnit.")
089    public void testGetSystemUnit() {
090        for (@SuppressWarnings("rawtypes")
091        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
092            testHasPublicMethod(SECTION_PREFIX + SECTION_NUM1, type, "getSystemUnit");
093        }
094    }
095
096    /**
097     * Test that Unit implementations contain getBaseUnits
098     */
099    @SpecAssertion(section = SECTION_NUM1, id = "421-A4")
100    @Test(groups = { CORE }, description = SECTION_NUM1 + " Ensure registered Unit classes implement getBaseUnits.")
101    public void testGetBaseUnits() {
102        for (@SuppressWarnings("rawtypes")
103        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
104            testHasPublicMethod(SECTION_PREFIX + SECTION_NUM1, type, "getBaseUnits");
105        }
106    }
107
108    /**
109     * Test that Unit implementations contain getters
110     */
111    @SuppressWarnings({"rawtypes"})
112    @SpecAssertion(section = SECTION_NUM1, id = "421-A5")
113    @Test(groups = { CORE }, description = SECTION_NUM1 + " Ensure registered Unit classes implement getName.")
114    public void testGetName() {
115        for (Class type : TCKSetup.getConfiguration().getUnitClasses()) {
116            testHasPublicMethod(SECTION_PREFIX + SECTION_NUM1, type, "getName");
117        }
118    }
119
120    /**
121     * Test that Unit implementations contain getters
122     */
123    @SuppressWarnings({"rawtypes"})
124    @SpecAssertion(section = SECTION_NUM1, id = "421-A6")
125    @Test(groups = { CORE }, description = SECTION_NUM1 + " Ensure registered Unit classes implement getSymbol.")
126    public void testGetSymbol() {
127        for (Class type : TCKSetup.getConfiguration().getUnitClasses()) {
128            testHasPublicMethod(SECTION_PREFIX + SECTION_NUM1, type, "getSymbol");
129        }
130    }
131
132    /**
133     * Test that Unit implementations override hashCode.
134     */
135    @SpecAssertion(section = SECTION_NUM1, id = "421-A7")
136    @Test(groups = { CORE }, description = SECTION_NUM1 + " Ensure registered Unit classes override hashCode.")
137    public void testHashcode() {
138        for (@SuppressWarnings("rawtypes")
139        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
140            //testHasPublicMethod(SECTION_PREFIX + SECTION_NUM1, type, "hashCode");
141                        testHasPublicMethod(SECTION_PREFIX + SECTION_NUM1, type, int.class, "hashCode");
142        }
143    }
144
145    /**
146     * Test that Unit implementations implement isEquivalentTo.
147     */
148    @SpecAssertion(section = SECTION_NUM1, id = "421-A8")
149    @Test(groups = { CORE }, description = SECTION_NUM1 + " Ensure registered Unit classes implement isEquivalentTo.")
150    public void testIsEquivalentTo() {
151        for (@SuppressWarnings("rawtypes")
152        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
153                testHasPublicMethod(SECTION_PREFIX + SECTION_NUM1, type, "isEquivalentTo", true);
154        }
155    }
156
157    /**
158     * Test that Unit implementations override toString.
159     */
160    @SpecAssertion(section = SECTION_NUM1, id = "421-A9")
161    @Test(groups = { CORE }, description = SECTION_NUM1 + " Ensure registered Unit classes override toString.")
162    public void testToString() {
163        for (@SuppressWarnings("rawtypes")
164        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
165            //testHasPublicMethod(SECTION_PREFIX + SECTION_NUM1, type, "toString");
166                        testHasPublicMethod(SECTION_PREFIX + SECTION_NUM1, type, false, String.class, "toString");
167        }
168    }
169
170    /**
171     * Ensure the alternate() operation is implemented.
172     */
173    @SpecAssertion(section = SECTION_NUM2, id = "42121-A1")
174    @Test(groups = { CORE }, description = "4.2.1.2.1 Ensure the alternate() operation is implemented.")
175    public void testUnit42121A1_Alternate() {
176        for (@SuppressWarnings("rawtypes")
177        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
178            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.1", type, "alternate", true);
179        }
180    }
181
182    /**
183     * Ensure the divide(Unit) operation is implemented.
184     */
185    @SpecAssertion(section = SECTION_NUM2, id = "42121-A2")
186    @Test(groups = { CORE }, description = "4.2.1.2.1 Ensure the divide(Unit) operation is implemented.")
187    public void testUnit42121A2_Divide() {
188        for (@SuppressWarnings("rawtypes")
189        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
190            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.1", type, Unit.class, "divide", Unit.class);
191        }
192    }
193
194    /**
195     * Ensure the divide(double) operation is implemented.
196     */
197    @SpecAssertion(section = SECTION_NUM2, id = "42121-A3")
198    @Test(groups = { CORE }, description = "4.2.1.2.1 Ensure the divide(double) operation is implemented.")
199    public void testUnit42121A3_DivideByDouble() {
200        for (@SuppressWarnings("rawtypes")
201        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
202            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.1", type, Unit.class, "divide", double.class);
203        }
204    }
205
206        /**
207         * Ensure the divide(Number) operation is implemented.
208         */
209        @SpecAssertion(section = SECTION_NUM2, id = "42121-A4")
210        @Test(groups = { CORE }, description = "4.2.1.2.1 Ensure the divide(Number) operation is implemented.")
211        public void testUnit42121A4_DivideByNumber() {
212                for (@SuppressWarnings("rawtypes")
213                        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
214                        testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.1", type, Unit.class, "divide", Number.class);
215                }
216        }
217
218    /**
219     * Ensure the multiply() operation is implemented.
220     */
221    @SpecAssertion(section = SECTION_NUM2, id = "42121-A5")
222    @Test(groups = { CORE }, description = "4.2.1.2.1 Ensure the multiply() operation is implemented.")
223    public void testUnit42121A5_Multiply() {
224        for (@SuppressWarnings("rawtypes")
225        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
226            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.1", type, Unit.class, "multiply", Unit.class);
227        }
228    }
229
230    /**
231     * Ensure the multiply(double) operation is implemented.
232     */
233    @SpecAssertion(section = SECTION_NUM2, id = "42121-A6")
234    @Test(groups = { CORE }, description = "4.2.1.2.1 Ensure the multiply(double) operation is implemented.")
235    public void testUnit42121A6_MultiplyByDouble() {
236        for (@SuppressWarnings("rawtypes")
237        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
238            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.1", type, Unit.class, "multiply", double.class);
239        }
240    }
241
242        /**
243         * Ensure the multiply(Number) operation is implemented.
244         */
245        @SpecAssertion(section = SECTION_NUM2, id = "42121-A7")
246        @Test(groups = { CORE }, description = "4.2.1.2.1 Ensure the multiply(Number) operation is implemented.")
247        public void testUnit42121A7_MultiplyByNumber() {
248                for (@SuppressWarnings("rawtypes")
249                        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
250                        testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.1", type, Unit.class, "multiply", Number.class);
251                }
252        }
253
254    /**
255     * Ensure the prefix() operation is implemented.
256     * @since 2.0
257     */
258    @SpecAssertion(section = SECTION_NUM2, id = "42121-A8")
259    @Test(groups = { CORE }, description = "4.2.1.2.1 Ensure the prefix() operation is implemented.")
260    public void testUnit42121A8_Prefix() {
261        for (@SuppressWarnings("rawtypes")
262        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
263            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.1", type, false, Unit.class, "prefix", Prefix.class);
264        }
265    }
266
267    /**
268     * Ensure the shift() operation is implemented.
269     */
270    @SpecAssertion(section = SECTION_NUM2, id = "42121-A9")
271    @Test(groups = { CORE }, description = "4.2.1.2.1 Ensure the shift(double) operation is implemented.")
272    public void testUnit42121A9_ShiftByDouble() {
273        for (@SuppressWarnings("rawtypes")
274        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
275                        testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.1", type, Unit.class, "shift", double.class);
276        }
277    }
278
279        /**
280         * Ensure the shift(Number) operation is implemented.
281         */
282        @SpecAssertion(section = SECTION_NUM2, id = "42121-A10")
283        @Test(groups = { CORE }, description = "4.2.1.2.1 Ensure the shift(Number) operation is implemented.")
284        public void testUnit42121A10_ShiftByNumber() {
285                for (@SuppressWarnings("rawtypes")
286                        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
287                        testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.1", type, Unit.class, "shift", Number.class);
288                }
289        }
290
291    /**
292     * Ensure the pow() operation is implemented.
293     */
294    @SpecAssertion(section = SECTION_NUM2, id = "42122-A1")
295    @Test(groups = { CORE }, description = "4.2.1.2.2 Ensure the pow() operation is implemented.")
296    public void testUnit42122Pow() {
297        for (@SuppressWarnings("rawtypes")
298        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
299            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.2", type, "pow", true);
300        }
301    }
302
303    /**
304     * Ensure the root() operation is implemented.
305     */
306    @SpecAssertion(section = SECTION_NUM2, id = "42122-A2")
307    @Test(groups = { CORE }, description = "4.2.1.2.2 Ensure the root() operation is implemented.")
308    public void testUnit42122Root() {
309        for (@SuppressWarnings("rawtypes")
310        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
311            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.2", type, "root", true);
312        }
313    }
314
315    /**
316     * Ensure the transform() operation is implemented.
317     */
318    @SpecAssertion(section = SECTION_NUM2, id = "42122-A3")
319    @Test(groups = { CORE }, description = "4.2.1.2.2 Ensure the transform() operation is implemented.")
320    public void testUnit42122Transform() {
321        for (@SuppressWarnings("rawtypes")
322        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
323            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.2", type, "transform", true);
324        }
325    }
326
327    /**
328     * Ensure the inverse() operation is implemented.
329     */
330    @SpecAssertion(section = SECTION_NUM2, id = "42123-A1")
331    @Test(groups = { CORE }, description = "4.2.1.2.3 Ensure the inverse() operation is implemented.")
332    public void testUnit42123Inverse() {
333        for (@SuppressWarnings("rawtypes")
334        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
335            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.3", type, "inverse", false);
336        }
337    }
338
339    /**
340     * Ensure the isCompatible() operation is implemented.
341     */
342    @SpecAssertion(section = SECTION_NUM2, id = "42124-A1")
343    @Test(groups = { CORE }, description = SECTION_NUM1 + " Ensure the isCompatible() operation is implemented.")
344    public void testUnit4221IsCompatible() {
345        for (@SuppressWarnings("rawtypes")
346        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
347            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.4", type, "isCompatible", true);
348        }
349    }
350
351    /**
352     * Ensure the asType() operation is implemented.
353     */
354    @SpecAssertion(section = SECTION_NUM2, id = "42124-A2")
355    @Test(groups = { CORE }, description = SECTION_NUM1 + " Ensure the asType() operation is implemented.")
356    public void testUnit4222AsType() {
357        for (@SuppressWarnings("rawtypes")
358        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
359            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.4", type, "asType", true);
360        }
361    }
362
363    /**
364     * Ensure the getConverterTo() operation is implemented.
365     */
366    @SpecAssertion(section = SECTION_NUM2, id = "42124-A3")
367    @Test(groups = { CORE }, description = SECTION_NUM1 + " Ensure the getConverterTo() operation is implemented.")
368    public void testUnit4223GetConverterTo() {
369        for (@SuppressWarnings("rawtypes")
370        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
371            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.4", type, "getConverterTo", true);
372        }
373    }
374
375    /**
376     * Ensure the getConverterToAny() operation is implemented.
377     */
378    @SpecAssertion(section = SECTION_NUM2, id = "42124-A4")
379    @Test(groups = { CORE }, description = SECTION_NUM1 + " Ensure the getConverterToAny() operation is implemented.")
380    public void testUnit4224GetConverterToAny() {
381        for (@SuppressWarnings("rawtypes")
382        Class type : TCKSetup.getConfiguration().getUnitClasses()) {
383            testHasPublicMethod(SECTION_PREFIX + "4.2.1.2.4", type, "getConverterToAny", true);
384        }
385    }
386}