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