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 static org.testng.Assert.assertEquals;
039import static org.testng.AssertJUnit.assertFalse;
040
041import javax.measure.BinaryPrefix;
042import javax.measure.MetricPrefix;
043import javax.measure.spi.FormatService;
044import javax.measure.spi.ServiceProvider;
045import javax.measure.spi.SystemOfUnitsService;
046import javax.measure.spi.UnitFormatService;
047
048import org.jboss.test.audit.annotations.SpecAssertion;
049import org.jboss.test.audit.annotations.SpecVersion;
050import org.testng.annotations.Test;
051
052/**
053 * Test class for Services.
054 * 
055 * @author Werner Keil
056 * @version 1.2, January 16, 2019
057 * @since 1.0
058 */
059@SpecVersion(spec = SPEC_ID, version = SPEC_VERSION)
060public class ServicesTest {
061        private static final String SECTION = "5.4";
062        private static final String DESCRIPTION = SECTION + " Services";
063
064        // ************************ 5.4 Services
065        // ************************
066        /**
067         * Access available UnitFormatServices.
068         */
069        @Test(groups = { "spi" }, description = DESCRIPTION)
070        @SpecAssertion(section = SECTION, id = "54-A1")
071        public void testFormatService() {
072                for (ServiceProvider provider : ServiceProvider.available()) {
073                        assertNotNull("Section " + SECTION + ": ServiceProvider is null", provider);
074                        UnitFormatService service = provider.getFormatService();
075                        assertNotNull("Section " + SECTION + ": FormatService is null", service);
076                }
077        }
078
079        // ************************ 5.4 Services
080        // ************************
081        /**
082         * Access default UnitFormats in UnitFormatServices.
083         */
084        @Test(groups = { "spi" }, description = DESCRIPTION)
085        @SpecAssertion(section = SECTION, id = "54-A2")
086        public void testFormatServiceDefaultFormat() {
087                for (ServiceProvider provider : ServiceProvider.available()) {
088                        assertNotNull("Section " + SECTION + ": ServiceProvider is null", provider);
089                        FormatService service = provider.getFormatService();
090                        assertNotNull("Section " + SECTION + ": UnitFormatService is null", service);
091                        assertNotNull("Section " + SECTION + ": Default UnitFormat is null", service.getUnitFormat());
092                        assertNotNull("Section " + SECTION + ": Available UnitFormat names are null",
093                                        service.getAvailableFormatNames());
094                        assertFalse("Section " + SECTION + " No available UnitFormat names found",
095                                        service.getAvailableFormatNames().isEmpty());
096                }
097        }
098
099        // ************************ 5.4 Services
100        // ************************
101        /**
102         * Access available UnitFormats in UnitFormatServices.
103         */
104        @Test(groups = { "spi" }, description = DESCRIPTION)
105        @SpecAssertion(section = SECTION, id = "54-A3")
106        public void testFormatServiceAvailableFormats() {
107                for (ServiceProvider provider : ServiceProvider.available()) {
108                        assertNotNull("Section " + SECTION + ": ServiceProvider is null", provider);
109                        FormatService service = provider.getFormatService();
110                        assertNotNull("Section " + SECTION + ": FormatService is null", service);
111                        assertNotNull("Section " + SECTION + ": Available UnitFormat names are null",
112                                        service.getAvailableFormatNames());
113                        assertFalse("Section " + SECTION + " No available UnitFormat names found",
114                                        service.getAvailableFormatNames().isEmpty());
115                }
116        }
117
118        // ************************ 5.4 Services
119        // ************************
120        /**
121         * Access available SystemOfUnitsServices.
122         */
123        @Test(groups = { "spi" }, description = DESCRIPTION)
124        @SpecAssertion(section = SECTION, id = "54-A4")
125        public void testSystemOfUnitsService() {
126                for (ServiceProvider provider : ServiceProvider.available()) {
127                        assertNotNull("Section " + SECTION + ": ServiceProvider is null", provider);
128                        SystemOfUnitsService service = provider.getSystemOfUnitsService();
129                        assertNotNull("Section " + SECTION + ": SystemOfUnitsService is null", service);
130                }
131        }
132
133        // ************************ 5.4 Services
134        // ************************
135        /**
136         * Access default SystemOfUnits in SystemOfUnitsService.
137         */
138        @Test(groups = { "spi" }, description = DESCRIPTION)
139        @SpecAssertion(section = SECTION, id = "54-A5")
140        public void testSystemOfUnitsServiceDefaultSystem() {
141                for (ServiceProvider provider : ServiceProvider.available()) {
142                        assertNotNull("Section " + SECTION + ": ServiceProvider is null", provider);
143                        SystemOfUnitsService service = provider.getSystemOfUnitsService();
144                        assertNotNull("Section " + SECTION + ": SystemOfUnitsService is null", service);
145                        assertNotNull("Section " + SECTION + ": Default SystemOfUnits is null", service.getSystemOfUnits());
146                }
147        }
148
149        // ************************ 5.4 Services
150        // ************************
151        /**
152         * Access available Systems OfUnits in SystemOfUnitsService.
153         */
154        @Test(groups = { "spi" }, description = DESCRIPTION)
155        @SpecAssertion(section = SECTION, id = "54-A6")
156        public void testSystemOfUnitsServiceAvailableSystems() {
157                for (ServiceProvider provider : ServiceProvider.available()) {
158                        assertNotNull("Section " + SECTION + ": ServiceProvider is null", provider);
159                        SystemOfUnitsService service = provider.getSystemOfUnitsService();
160                        assertNotNull("Section " + SECTION + ": SystemOfUnitsService is null", service);
161                        assertNotNull("Section " + SECTION + ": Available SystemOfUnits are null",
162                                        service.getAvailableSystemsOfUnits());
163                        assertFalse("Section " + SECTION + " No available SystemOfUnits found",
164                                        service.getAvailableSystemsOfUnits().isEmpty());
165                }
166        }
167
168        /**
169         * Access Binary Prefixes in SystemOfUnitsService.
170         * @since 2.0
171         */
172        @Test(groups = { "spi" }, description = DESCRIPTION)
173        @SpecAssertion(section = SECTION, id = "54-A7")
174        public void testSystemOfUnitsServicePrefixBinary() {
175                for (ServiceProvider provider : ServiceProvider.available()) {
176                        assertNotNull("Section " + SECTION + ": ServiceProvider is null", provider);
177                        final SystemOfUnitsService service = provider.getSystemOfUnitsService();
178                        assertNotNull("Section " + SECTION + ": SystemOfUnitsService is null", service);
179                        Set<BinaryPrefix> prefixes = service.getPrefixes(BinaryPrefix.class);
180                        assertNotNull("Section " + SECTION + ": Binary Prefixes are null", prefixes);
181                        assertFalse("Section " + SECTION + " No Binary Prefixes found", prefixes.isEmpty());
182                        assertEquals(8, prefixes.size(), "Section " + SECTION + " Wrong Number of Binary Prefixes");
183                }
184        }
185        
186        /**
187         * Access Metric Prefixes in SystemOfUnitsService.
188         * @since 2.0
189         */
190        @Test(groups = { "spi" }, description = DESCRIPTION)
191        @SpecAssertion(section = SECTION, id = "54-A8")
192        public void testSystemOfUnitsServicePrefixMetric() {
193                for (ServiceProvider provider : ServiceProvider.available()) {
194                        assertNotNull("Section " + SECTION + ": ServiceProvider is null", provider);
195                        final SystemOfUnitsService service = provider.getSystemOfUnitsService();
196                        assertNotNull("Section " + SECTION + ": SystemOfUnitsService is null", service);
197                        Set<MetricPrefix> prefixes = service.getPrefixes(MetricPrefix.class);
198                        assertNotNull("Section " + SECTION + ": Metric Prefixes are null", prefixes);
199                        assertFalse("Section " + SECTION + " No Metric Prefixes found", prefixes.isEmpty());
200                        assertEquals(20, prefixes.size(), "Section " + SECTION + " Wrong Number of Metric Prefixes");
201                }
202        }
203}