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