001/*
002 * Units of Measurement TCK
003 * Copyright © 2005-2017, Jean-Marie Dautelle, Werner Keil, V2COM.
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:units@catmedia.us">Werner Keil</a>
054 * @version 1.0, August 16, 2016
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 getUnits() method is implemented.
112     */
113    @SpecAssertion(section = SECTION, id = "52-A4")
114    @Test(groups = { "spi" }, description = SECTION
115            + " Ensure the getUnits() method is implemented.")
116    public void testSystemOfUnitsGetUnits() {
117        for (SystemOfUnits suo : ServiceProvider.current()
118                .getSystemOfUnitsService().getAvailableSystemsOfUnits()) {
119            Class<?> type = suo.getClass();
120            TestUtils.testHasPublicMethod("Section " + SECTION, type,
121                    "getUnits");
122        }
123    }
124
125    /**
126     * Ensure the getUnits() method is implemented.
127     */
128    @SpecAssertion(section = SECTION, id = "52-A5")
129    @Test(groups = { "spi" }, description = "5.2 Ensure the getUnits() method is implemented.")
130    public void testSystemOfUnitsGetUnitsForDimension() {
131        for (SystemOfUnits suo : ServiceProvider.current()
132                .getSystemOfUnitsService().getAvailableSystemsOfUnits()) {
133            Class<?> type = suo.getClass();
134            TestUtils.testHasPublicMethod("Section 5.2", type, true, Set.class,
135                    "getUnits", Dimension.class);
136        }
137    }
138}