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.hamcrest.MatcherAssert.assertThat;
033import static org.hamcrest.Matchers.empty;
034import static org.hamcrest.core.Is.is;
035import static org.hamcrest.core.IsNot.not;
036import static org.testng.Assert.assertNotNull;
037import static tech.units.tck.TCKRunner.SECTION_PREFIX;
038import static tech.units.tck.TCKRunner.SPEC_ID;
039import static tech.units.tck.TCKRunner.SPEC_VERSION;
040import static tech.units.tck.util.TestGroups.SPI;
041
042import java.util.Set;
043
044import javax.measure.Dimension;
045import javax.measure.Unit;
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.testng.annotations.Test;
052
053import tech.units.tck.util.TestUtils;
054
055/**
056 * Tests for SystemOfUnits
057 *
058 * @author <a href="mailto:[email protected]">Werner Keil</a>
059 * @version 2.3, October 4, 2023
060 * @since 1.0
061 */
062@SpecVersion(spec = SPEC_ID, version = SPEC_VERSION)
063public class SystemOfUnitsTest {
064    private static final String SECTION_NUM = "5.2";
065
066    /**
067     * Ensure at least one SystemOfUnits implementation exists.
068     */
069    @SpecAssertion(section = SECTION_NUM, id = "52-A1")
070    @Test(groups = { SPI }, description = SECTION_NUM
071            + " Ensure a SystemOfUnits implementation exists for every ServiceProvider")
072    public void testEnsureGotSystemOfUnits() {
073        for (ServiceProvider provider : ServiceProvider.available()) {
074            assertNotNull(provider,     SECTION_PREFIX + SECTION_NUM + ": ServiceProvider is null");
075            assertNotNull(provider.getSystemOfUnitsService(), String.format("SystemOfUnitsService is null for %s", provider));
076            assertNotNull(provider.getSystemOfUnitsService().getAvailableSystemsOfUnits(), 
077                        String.format("Available SystemOfUnits is null for %s", provider));
078            assertThat(String.format("SystemOfUnits not found in %s", provider), 
079                        provider.getSystemOfUnitsService().getAvailableSystemsOfUnits(), is(not(empty())));
080        }
081    }
082
083    /**
084     * Ensure the getName() method is implemented.
085     */
086    @SpecAssertion(section = SECTION_NUM, id = "52-A2")
087    @Test(groups = { SPI }, description = SECTION_NUM
088            + " Ensure the getName() method is implemented.")
089    public void testSystemOfUnitsGetName() {
090        for (SystemOfUnits suo : ServiceProvider.current()
091                .getSystemOfUnitsService().getAvailableSystemsOfUnits()) {
092            Class<?> type = suo.getClass();
093            TestUtils
094                    .testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, "getName");
095        }
096    }
097
098    /**
099     * Ensure the getUnit() method is implemented.
100     */
101    @SpecAssertion(section = SECTION_NUM, id = "52-A3")
102    @Test(groups = { SPI }, description = SECTION_NUM
103            + " Ensure the getUnit() method is implemented.")
104    public void testSystemOfUnitsGetUnit() {
105        for (SystemOfUnits suo : ServiceProvider.current()
106                .getSystemOfUnitsService().getAvailableSystemsOfUnits()) {
107            Class<?> type = suo.getClass();
108            TestUtils.testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, true,
109                    Unit.class, "getUnit", Class.class);
110        }
111    }
112    
113    /**
114     * Ensure the getUnit() method is implemented.
115     * @since 2.0
116     */
117    @SpecAssertion(section = SECTION_NUM, id = "52-A4")
118    @Test(groups = { SPI }, description = SECTION_NUM
119        + " Ensure the getUnit() method is implemented.")
120    public void testSystemOfUnitsGetUnitForString() {
121        for (SystemOfUnits suo : ServiceProvider.current()
122            .getSystemOfUnitsService().getAvailableSystemsOfUnits()) {
123            Class<?> type = suo.getClass();
124            TestUtils.testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, true,
125                Unit.class, "getUnit", String.class);
126        }
127    }
128
129    /**
130     * Ensure the getUnits() method is implemented.
131     */
132    @SpecAssertion(section = SECTION_NUM, id = "52-A5")
133    @Test(groups = { SPI }, description = SECTION_NUM
134            + " Ensure the getUnits() method is implemented.")
135    public void testSystemOfUnitsGetUnits() {
136        for (SystemOfUnits suo : ServiceProvider.current()
137                .getSystemOfUnitsService().getAvailableSystemsOfUnits()) {
138            Class<?> type = suo.getClass();
139            TestUtils.testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type,
140                    "getUnits", false);
141        }
142    }
143
144    /**
145     * Ensure the getUnits() method is implemented.
146     */
147    @SpecAssertion(section = SECTION_NUM, id = "52-A6")
148    @Test(groups = { SPI }, description = "5.2 Ensure the getUnits() method is implemented.")
149    public void testSystemOfUnitsGetUnitsForDimension() {
150        for (SystemOfUnits suo : ServiceProvider.current()
151                .getSystemOfUnitsService().getAvailableSystemsOfUnits()) {
152            Class<?> type = suo.getClass();
153            TestUtils.testHasPublicMethod(SECTION_PREFIX + SECTION_NUM, type, true, Set.class,
154                    "getUnits", Dimension.class);
155        }
156    }
157}