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.assertEquals; 033import static org.testng.AssertJUnit.assertNotNull; 034import static tech.units.tck.TCKRunner.SPEC_ID; 035import static tech.units.tck.TCKRunner.SPEC_VERSION; 036 037import java.util.Set; 038import javax.measure.Quantity; 039import javax.measure.Quantity.Scale; 040import javax.measure.Unit; 041import javax.measure.quantity.Length; 042import javax.measure.spi.QuantityFactory; 043import javax.measure.spi.ServiceProvider; 044import org.jboss.test.audit.annotations.SpecAssertion; 045import org.jboss.test.audit.annotations.SpecVersion; 046import org.reflections.Reflections; 047import org.testng.annotations.Test; 048 049/** 050 * Tests for QuantityFactory 051 * 052 * @author <a href="mailto:[email protected]">Werner Keil</a> 053 * @version 1.1, December 18, 2018 054 * @since 1.0 055 */ 056@SpecVersion(spec = SPEC_ID, version = SPEC_VERSION) 057public class QuantityFactoryTest { 058 private static final String MEASURE_PACKAGE = "javax.measure"; 059 private static final String SECTION = "5.1"; 060 061 /** 062 * Ensure at least one QuantityFactory implementation exists 063 */ 064 @SuppressWarnings({ "unchecked", "rawtypes" }) 065 @Test(groups = { "spi" }, description = SECTION + " Ensure a QuantityFactory implementation exists for every ServiceProvider") 066 @SpecAssertion(section = SECTION, id = "51-A1") 067 public void testQuantityFactoryExists() { 068 for (ServiceProvider provider : ServiceProvider.available()) { 069 assertNotNull("Section " + SECTION + ": ServiceProvider is null", provider); 070 Reflections reflections = new Reflections(MEASURE_PACKAGE); 071 Set<Class<? extends Quantity>> subTypes = reflections.getSubTypesOf(Quantity.class); 072 for (Class clazz : subTypes) { 073 QuantityFactory<?> factory = provider.getQuantityFactory(clazz); 074 assertNotNull("Section " + SECTION + ": No QuantityFactory available for " + clazz.getSimpleName() + " in " + provider, factory); 075 } 076 } 077 } 078 079 /** 080 * Ensure every QuantityFactory creates quantities with the correct scale 081 * @since 2.0 082 */ 083 @Test(groups = { "spi" }, description = SECTION + " Ensure a QuantityFactory implementation returns the correct scale") 084 @SpecAssertion(section = SECTION, id = "51-A2") 085 public void testQuantityFactoryScale() { 086 for (ServiceProvider provider : ServiceProvider.available()) { 087 QuantityFactory<Length> factory = provider.getQuantityFactory(Length.class); 088 Unit<Length> systemUnit = ServiceProvider.current().getSystemOfUnitsService().getSystemOfUnits().getUnit(Length.class); 089 for (Scale scale : Scale.values()) { 090 Quantity<Length> result = factory.create(10d, systemUnit, scale); 091 assertEquals(scale, result.getScale()); 092 } 093 } 094 } 095}