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.quantity;
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;
036
037import javax.measure.Quantity;
038import javax.measure.Unit;
039
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;
045import tech.units.tck.util.TestUtils;
046
047/**
048 * Tests for The Quantity Interface
049 *
050 * @author <a href="mailto:[email protected]">Werner Keil</a>
051 * @author Almas Shaikh
052 * @version 2.0, November 15, 2020
053 * @since 1.0
054 */
055@SpecVersion(spec = SPEC_ID, version = SPEC_VERSION)
056public class QuantityInterfaceTest {
057    private static final String SECTION_NUM = "4.3.1";
058    
059    /**
060     * Test that Quantity implementations override asType method.
061     */
062    @SpecAssertion(section = SECTION_NUM, id = "431-A1")
063    @Test(groups = { CORE }, description = SECTION_NUM + " Ensure registered Quantity classes implement asType method.")
064    public void testQuantityCastAsType() {
065        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
066            TestUtils.testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, false, Quantity.class, "asType", Class.class);
067        }
068    }
069    /**
070     * Test that Quantity implementations override equals.
071     */
072    @SpecAssertion(section = SECTION_NUM, id = "431-A2")
073    @Test(groups = { CORE }, description = SECTION_NUM + " Ensure registered Quantity classes override equals.")
074    public void testQuantityEquals() {
075        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
076            TestUtils.testHasPublicMethod(SECTION_PREFIX +  SECTION_NUM, type, "equals", true);
077        }
078    }
079    
080    /**
081     * Test that Quantity implementations override getScale.
082     */
083    @SpecAssertion(section = SECTION_NUM, id = "431-A3")
084    @Test(groups = { CORE }, description = SECTION_NUM + " Ensure registered Quantity classes implement getScale method.")
085    public void testQuantityGetScale() {
086        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
087            TestUtils.testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, "getScale");
088        }
089    }
090
091
092    /**
093     * Test that Quantity implementations override getUnit.
094     */
095    @SpecAssertion(section = SECTION_NUM, id = "431-A4")
096    @Test(groups = { CORE }, description = SECTION_NUM + " Ensure registered Quantity classes implement getUnit.")
097    public void testQuantityGetUnit() {
098        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
099            TestUtils.testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, "getUnit");
100        }
101    }
102
103    /**
104     * Test that Quantity implementations override getValue.
105     */
106    @SpecAssertion(section = SECTION_NUM, id = "431-A5")
107    @Test(groups = { CORE }, description = SECTION_NUM + " Ensure registered Quantity classes implement getValue.")
108    public void testQuantityGetValue() {
109        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
110            TestUtils.testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, "getValue");
111        }
112    }
113
114    /**
115     * Test that Quantity implementations override hashCode.
116     */
117    @SpecAssertion(section = SECTION_NUM, id = "431-A6")
118    @Test(groups = { CORE }, description = SECTION_NUM + " Ensure registered Quantity classes override hashCode.")
119    public void testQuantityHashCode() {
120        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
121            TestUtils.testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, "hashCode");
122        }
123    }
124    
125    /**
126     * Test that Quantity implementations override to method.
127     */
128    @SpecAssertion(section = SECTION_NUM, id = "431-A7")
129    @Test(groups = { CORE }, description = SECTION_NUM + " Ensure registered Quantity classes implement isEquivalentTo method.")
130    public void testQuantityIsEquivalentTo() {
131        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
132                TestUtils.testHasPublicMethod(SECTION_PREFIX +  SECTION_NUM, type, "isEquivalentTo", true);
133        }
134    }
135    
136    /**
137     * Test that Quantity implementations override inverse.
138     */
139    @SpecAssertion(section = SECTION_NUM, id = "431-A8")
140    @Test(groups = { CORE }, description = SECTION_NUM + " Ensure registered Quantity classes implement inverse method.")
141    public void testQuantityOp0Inverse() {
142        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
143            TestUtils.testHasPublicMethod(SECTION_PREFIX +  SECTION_NUM, type, "inverse");
144        }
145    }
146    
147    /**
148     * Test that Quantity implementations override add.
149     */
150    @SpecAssertion(section = "4.3.1.1", id = "4311-A1")
151    @Test(groups = { CORE }, description = "4.3.1.1 Ensure registered Quantity classes implement add.")
152    public void testQuantityOp1Add() {
153        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
154            TestUtils.testHasPublicMethod(SECTION_PREFIX + "4.3.1.1", type, Quantity.class, "add", Quantity.class);
155        }
156    }
157
158    /**
159     * Test that Quantity implementations override multiply with number as argument.
160     */
161    @SpecAssertion(section = "4.3.1.1", id = "4311-A4")
162    @Test(groups = { CORE }, description = "4.3.1.1 Ensure registered Quantity classes implement multiply by number.")
163    public void testQuantityOp1MultiplyByNumber() {
164        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
165            TestUtils.testHasPublicMethod(SECTION_PREFIX + "4.3.1.1", type, Quantity.class, "multiply", Number.class);
166        }
167    }
168
169    /**
170     * Test that Quantity implementations override divide with number as argument.
171     */
172    @SpecAssertion(section = "4.3.1.1", id = "4311-A5")
173    @Test(groups = { CORE }, description = "4.3.1.1 Ensure registered Quantity classes implement divide by number.")
174    public void testQuantityOp1DivideByNumber() {
175        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
176            TestUtils.testHasPublicMethod(SECTION_PREFIX + "4.3.1.1", type, Quantity.class, "divide", Number.class);
177        }
178    }
179
180    /**
181     * Test that Quantity implementations override subtract.
182     */
183    @SpecAssertion(section = "4.3.1.1", id = "4311-A6")
184    @Test(groups = { CORE }, description = "4.3.1.1 Ensure registered Quantity classes implement subtract.")
185    public void testQuantityOp1Subtract() {
186        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
187            TestUtils.testHasPublicMethod(SECTION_PREFIX + "4.3.1.1", type, Quantity.class, "subtract", Quantity.class);
188        }
189    }
190
191    /**
192     * Test that Quantity implementations override divide.
193     */
194    @SpecAssertion(section = "4.3.1.2", id = "4312-A1")
195    @Test(groups = { CORE }, description = "4.3.1.2 Ensure registered Quantity classes implement divide.")
196    public void testQuantityOp2Divide() {
197        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
198            TestUtils.testHasPublicMethod(SECTION_PREFIX + "4.3.1.2", type, Quantity.class, "divide", Quantity.class);
199        }
200    }
201
202    /**
203     * Test that Quantity implementations override multiply.
204     */
205    @SpecAssertion(section = "4.3.1.2", id = "4312-A2")
206    @Test(groups = { CORE }, description = "4.3.1.2 Ensure registered Quantity classes implement multiply.")
207    public void testQuantityOp2Multiply() {
208        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
209            TestUtils.testHasPublicMethod(SECTION_PREFIX + "4.3.1.2", type, Quantity.class, "multiply", Quantity.class);
210        }
211    }
212    
213    /**
214     * Test that Quantity implementations override to method.
215     */
216    @SpecAssertion(section = SECTION_NUM, id = "431-A9")
217    @Test(groups = { CORE }, description = SECTION_NUM + " Ensure registered Quantity classes implement to method.")
218    public void testQuantityOp0To() {
219        for (Class<?> type : TCKSetup.getConfiguration().getQuantityClasses()) {
220            TestUtils.testHasPublicMethod(SECTION_PREFIX +  SECTION_NUM, type, Quantity.class, "to", Unit.class);
221        }
222    }        
223}