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