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