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.spi;
031
032import static org.testng.AssertJUnit.assertEquals;
033import static org.testng.AssertJUnit.assertNotNull;
034import static tech.units.tck.TCKRunner.MEASURE_PACKAGE;
035import static tech.units.tck.TCKRunner.SECTION_PREFIX;
036import static tech.units.tck.TCKRunner.SPEC_ID;
037import static tech.units.tck.TCKRunner.SPEC_VERSION;
038import static tech.units.tck.util.TestGroups.SPI;
039
040import java.util.Set;
041
042import javax.measure.Quantity;
043import javax.measure.Unit;
044import javax.measure.format.QuantityFormat;
045import javax.measure.spi.QuantityFactory;
046import javax.measure.spi.ServiceProvider;
047import javax.measure.spi.SystemOfUnits;
048
049import org.jboss.test.audit.annotations.SpecAssertion;
050import org.jboss.test.audit.annotations.SpecVersion;
051import org.reflections.Reflections;
052import org.testng.annotations.Test;
053
054import tech.units.tck.util.TestUtils;
055
056/**
057 * Test class for creating new quantities.
058 * @author Werner Keil
059 * @author Almas Shaikh
060 * @version 2.1, November 15, 2020
061 * @since 1.0
062 */
063@SpecVersion(spec = SPEC_ID, version = SPEC_VERSION)
064public class ObtainingQuantiesTest {
065    private static final String SECTION_NUM = "5.6.1";
066    
067    // ************************ 5.6 Obtaining Quantity Instances
068    // ************************
069    /**
070     * Access a QuantityFactory for each registered type.
071     */
072    @SuppressWarnings({ "unchecked", "rawtypes" })
073    @Test(groups = {"spi"}, description = SECTION_NUM + " Quantities Obtained from a factory")
074    @SpecAssertion(section = SECTION_NUM, id = "561-A1")
075    public void testAccessToQuantityFactory() {
076        Reflections reflections = new Reflections(MEASURE_PACKAGE);
077        Set<Class<? extends Quantity>> subTypes = reflections.getSubTypesOf(Quantity.class);
078        for (Class clazz : subTypes) {
079            QuantityFactory<?> factory = ServiceProvider.current().getQuantityFactory(clazz);
080            assertNotNull(SECTION_PREFIX + SECTION_NUM + ": No QuantityFactory available for " + clazz.getSimpleName(), factory);
081        }
082    }
083
084    /**
085     * Check a QuantityFactory for each registered type has create method.
086     */
087    @SuppressWarnings({ "rawtypes", "unchecked" })
088    @Test(groups = { SPI }, description = SECTION_NUM + " Quantities obtained from a factory has create method")
089    @SpecAssertion(section = SECTION_NUM, id = "561-A2")
090    public void testAccessToQuantityFactoryCreate() {
091        Reflections reflections = new Reflections(MEASURE_PACKAGE);
092        Set<Class<? extends Quantity>> subTypes = reflections.getSubTypesOf(Quantity.class);
093        for (Class clazz : subTypes) {
094            QuantityFactory<?> factory = ServiceProvider.current().getQuantityFactory(clazz);
095            TestUtils.testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, factory.getClass(), Quantity.class, "create", Number.class, Unit.class);
096        }
097    }
098
099    /**
100     * Check a QuantityFactory for each registered type has getSystemUnit method.
101     */
102    @SuppressWarnings({ "rawtypes", "unchecked" })
103    @Test(groups = { SPI }, description = SECTION_NUM + " Quantities obtained from a factory has getSystemUnit method")
104    @SpecAssertion(section = SECTION_NUM, id = "561-A3")
105    public void testAccessToQuantityFactoryGetSystemUnit() {
106        Reflections reflections = new Reflections(MEASURE_PACKAGE);
107        Set<Class<? extends Quantity>> subTypes = reflections.getSubTypesOf(Quantity.class);
108        for (Class clazz : subTypes) {
109            QuantityFactory<?> factory = ServiceProvider.current().getQuantityFactory(clazz);
110            TestUtils.testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, factory.getClass(), Unit.class, "getSystemUnit");
111        }
112    }
113    
114    /**
115     * Try parsing a quantity string for each registered unit.
116     * @since 2.1
117     */
118    @SuppressWarnings("rawtypes")
119    @Test(groups = { SPI }, description = SECTION_NUM
120            + " Quantities obtained by string parsing")
121    @SpecAssertion(section = SECTION_NUM, id = "561-A4")
122    public void testGetQuantitiesFromString() {
123        final QuantityFormat format = ServiceProvider.current().getFormatService().getQuantityFormat();
124//              System.out.println("Fmt: " + format.toString()); TODO For debugging, remove or comment out
125                for (SystemOfUnits sou : ServiceProvider.current()
126                        .getSystemOfUnitsService().getAvailableSystemsOfUnits()) {
127//                      int i = 1;
128                    for (Unit u : sou.getUnits()) {
129                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": A Unit is missing from " + sou.getName(), u);
130                                if (u.getSymbol() != null) {
131                                    String s = u.toString();
132//                                  System.out.println("S " + i + ": " + s + "(" + u.getSymbol() + ")");
133                                    Quantity q = format.parse("1 " + s);
134                                    assertEquals(SECTION_PREFIX + SECTION_NUM + ": Quantity unit could not be parsed for '" + s + "'", u, q.getUnit());
135                                    assertEquals(SECTION_PREFIX + SECTION_NUM + ": Quantity value could not be parsed for '" + s + "'", 1, q.getValue());
136//                                  i++;
137                                }
138                    }
139                }
140    }
141}