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.spi; 031 032import static org.testng.AssertJUnit.assertNotNull; 033import static tech.units.tck.TCKRunner.SPEC_ID; 034import static tech.units.tck.TCKRunner.SPEC_VERSION; 035 036import java.util.Set; 037 038import javax.measure.Quantity; 039import javax.measure.Unit; 040import javax.measure.spi.QuantityFactory; 041import javax.measure.spi.ServiceProvider; 042 043import org.jboss.test.audit.annotations.SpecAssertion; 044import org.jboss.test.audit.annotations.SpecVersion; 045import org.reflections.Reflections; 046import org.testng.annotations.Test; 047 048import tech.units.tck.util.TestUtils; 049 050/** 051 * Test class for creating new quantities. 052 * @author Werner Keil 053 * @author Almas Shaikh 054 * @version 1.0, August 16, 2016 055 * @since 1.0 056 */ 057@SpecVersion(spec = SPEC_ID, version = SPEC_VERSION) 058public class ObtainingQuantiesTest { 059 060 private static final String MEASURE_PACKAGE = "javax.measure"; 061 private static final String SECTION = "5.6.1"; 062 063 // ************************ 5.6 Obtaining Quantity Instances 064 // ************************ 065 /** 066 * Access a QuantityFactory for each registered type. 067 */ 068 @SuppressWarnings({ "unchecked", "rawtypes" }) 069 @Test(groups = {"spi"}, description = SECTION + " Quantities Obtained from a factory") 070 @SpecAssertion(section = SECTION, id = "561-A1") 071 public void testAccessToQuantityFactory() { 072 Reflections reflections = new Reflections(MEASURE_PACKAGE); 073 Set<Class<? extends Quantity>> subTypes = reflections.getSubTypesOf(Quantity.class); 074 for (Class clazz : subTypes) { 075 QuantityFactory<?> factory = ServiceProvider.current().getQuantityFactory(clazz); 076 assertNotNull("Section " + SECTION + ": No QuantityFactory available for " + clazz.getSimpleName(), factory); 077 } 078 } 079 080 /** 081 * Check a QuantityFactory for each registered type has create method. 082 */ 083 @SuppressWarnings({ "rawtypes", "unchecked" }) 084 @Test(groups = {"spi"}, description = SECTION + " Quantities Obtained from a factory has create method") 085 @SpecAssertion(section = SECTION, id = "561-A2") 086 public void testAccessToQuantityFactoryCreate() { 087 Reflections reflections = new Reflections(MEASURE_PACKAGE); 088 Set<Class<? extends Quantity>> subTypes = reflections.getSubTypesOf(Quantity.class); 089 for (Class clazz : subTypes) { 090 QuantityFactory<?> factory = ServiceProvider.current().getQuantityFactory(clazz); 091 TestUtils.testHasPublicMethod("Section " + SECTION, factory.getClass(), Quantity.class, "create", Number.class, Unit.class); 092 } 093 } 094 095 /** 096 * Check a QuantityFactory for each registered type has getSystemUnit method. 097 */ 098 @SuppressWarnings({ "rawtypes", "unchecked" }) 099 @Test(groups = {"spi"}, description = SECTION + " Quantities Obtained from a factory has getSystemUnit method") 100 @SpecAssertion(section = SECTION, id = "561-A3") 101 public void testAccessToQuantityFactoryGetSystemUnit() { 102 Reflections reflections = new Reflections(MEASURE_PACKAGE); 103 Set<Class<? extends Quantity>> subTypes = reflections.getSubTypesOf(Quantity.class); 104 for (Class clazz : subTypes) { 105 QuantityFactory<?> factory = ServiceProvider.current().getQuantityFactory(clazz); 106 TestUtils.testHasPublicMethod("Section " + SECTION, factory.getClass(), Unit.class, "getSystemUnit"); 107 } 108 } 109}