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;
037import static javax.measure.spi.FormatService.FormatType.UNIT_FORMAT;
038import static javax.measure.spi.FormatService.FormatType.QUANTITY_FORMAT;
039import java.util.Set;
040
041import static org.testng.Assert.assertEquals;
042import static org.testng.AssertJUnit.assertFalse;
043
044import javax.measure.BinaryPrefix;
045import javax.measure.MetricPrefix;
046import javax.measure.spi.FormatService;
047import javax.measure.spi.ServiceProvider;
048import javax.measure.spi.SystemOfUnitsService;
049
050import org.jboss.test.audit.annotations.SpecAssertion;
051import org.jboss.test.audit.annotations.SpecVersion;
052import org.testng.annotations.Test;
053
054/**
055 * Test class for Services.
056 * 
057 * @author Werner Keil
058 * @version 2.1, November 15, 2020
059 * @since 1.0
060 */
061@SpecVersion(spec = SPEC_ID, version = SPEC_VERSION)
062public class ServicesTest {
063        private static final String SECTION_NUM = "5.4";
064        private static final String DESCRIPTION = SECTION_NUM + " Services";
065
066        // ************************ 5.4 Services
067        // ************************
068        /**
069         * Access available FormatServices.
070         */
071        @Test(groups = { SPI }, description = DESCRIPTION)
072        @SpecAssertion(section = SECTION_NUM, id = "54-A01")
073        public void testFormatService() {
074                for (ServiceProvider provider : ServiceProvider.available()) {
075                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": ServiceProvider is null", provider);
076                        FormatService service = provider.getFormatService();
077                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": FormatService is null", service);
078                }
079        }
080
081        // ************************ 5.4 Services
082        // ************************
083        /**
084         * Access available QuantityFormats in FormatServices.
085         * @since 2.1
086         */
087        @Test(groups = { SPI }, description = DESCRIPTION)
088        @SpecAssertion(section = SECTION_NUM, id = "54-A02")
089        public void testFormatServiceQuantityFormatsAvailable() {
090                for (ServiceProvider provider : ServiceProvider.available()) {
091                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": ServiceProvider is null", provider);
092                        FormatService service = provider.getFormatService();
093                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": FormatService is null", service);
094                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": Available QuantityFormat names are null",
095                                        service.getAvailableFormatNames(QUANTITY_FORMAT));
096                        assertFalse(SECTION_PREFIX + SECTION_NUM + " No available QuantityFormat names found",
097                                        service.getAvailableFormatNames(QUANTITY_FORMAT).isEmpty());
098                }
099        }
100        
101        // ************************ 5.4 Services
102        // ************************
103        /**
104         * Access default QuantityFormat in FormatServices.
105         * @since 2.1
106         */
107        @Test(groups = { SPI }, description = DESCRIPTION)
108        @SpecAssertion(section = SECTION_NUM, id = "54-A03")
109        public void testFormatServiceQuantityFormatsDefault() {
110                for (ServiceProvider provider : ServiceProvider.available()) {
111                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": ServiceProvider is null", provider);
112                        FormatService service = provider.getFormatService();
113                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": FormatService is null", service);
114                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": Default QuantityFormat is null", service.getUnitFormat());
115                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": Available QuantityFormat names are null",
116                                        service.getAvailableFormatNames(QUANTITY_FORMAT));
117                        assertFalse(SECTION_PREFIX + SECTION_NUM + " No available QuantityFormat names found",
118                                        service.getAvailableFormatNames(QUANTITY_FORMAT).isEmpty());
119                }
120        }
121
122        // ************************ 5.4 Services
123        // ************************
124        /**
125         * Access available UnitFormats in FormatServices.
126         */
127        @Test(groups = { SPI }, description = DESCRIPTION)
128        @SpecAssertion(section = SECTION_NUM, id = "54-A04")
129        public void testFormatServiceUnitFormatsAvailable() {
130                for (ServiceProvider provider : ServiceProvider.available()) {
131                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": ServiceProvider is null", provider);
132                        FormatService service = provider.getFormatService();
133                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": FormatService is null", service);
134                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": Available UnitFormat names are null",
135                                        service.getAvailableFormatNames(UNIT_FORMAT));
136                        assertFalse(SECTION_PREFIX + SECTION_NUM + " No available UnitFormat names found",
137                                        service.getAvailableFormatNames(UNIT_FORMAT).isEmpty());
138                }
139        }
140        
141        // ************************ 5.4 Services
142        // ************************
143        /**
144         * Access default UnitFormats in FormatServices.
145         */
146        @Test(groups = { SPI }, description = DESCRIPTION)
147        @SpecAssertion(section = SECTION_NUM, id = "54-A05")
148        public void testFormatServiceUnitFormatsDefault() {
149                for (ServiceProvider provider : ServiceProvider.available()) {
150                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": ServiceProvider is null", provider);
151                        FormatService service = provider.getFormatService();
152                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": FormatService is null", service);
153                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": Default UnitFormat is null", service.getUnitFormat());
154                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": Available UnitFormat names are null",
155                                        service.getAvailableFormatNames(UNIT_FORMAT));
156                        assertFalse(SECTION_PREFIX + SECTION_NUM + " No available UnitFormat names found",
157                                        service.getAvailableFormatNames(UNIT_FORMAT).isEmpty());
158                }
159        }
160
161        // ************************ 5.4 Services
162        // ************************
163        /**
164         * Access available SystemOfUnitsServices.
165         */
166        @Test(groups = { SPI }, description = DESCRIPTION)
167        @SpecAssertion(section = SECTION_NUM, id = "54-A06")
168        public void testSystemOfUnitsService() {
169                for (ServiceProvider provider : ServiceProvider.available()) {
170                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": ServiceProvider is null", provider);
171                        SystemOfUnitsService service = provider.getSystemOfUnitsService();
172                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": SystemOfUnitsService is null", service);
173                }
174        }
175
176        // ************************ 5.4 Services
177        // ************************
178        /**
179         * Access default SystemOfUnits in SystemOfUnitsService.
180         */
181        @Test(groups = { SPI }, description = DESCRIPTION)
182        @SpecAssertion(section = SECTION_NUM, id = "54-A07")
183        public void testSystemOfUnitsServiceDefaultSystem() {
184                for (ServiceProvider provider : ServiceProvider.available()) {
185                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": ServiceProvider is null", provider);
186                        SystemOfUnitsService service = provider.getSystemOfUnitsService();
187                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": SystemOfUnitsService is null", service);
188                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": Default SystemOfUnits is null", service.getSystemOfUnits());
189                }
190        }
191
192        // ************************ 5.4 Services
193        // ************************
194        /**
195         * Access available Systems OfUnits in SystemOfUnitsService.
196         */
197        @Test(groups = { SPI }, description = DESCRIPTION)
198        @SpecAssertion(section = SECTION_NUM, id = "54-A08")
199        public void testSystemOfUnitsServiceAvailableSystems() {
200                for (ServiceProvider provider : ServiceProvider.available()) {
201                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": ServiceProvider is null", provider);
202                        SystemOfUnitsService service = provider.getSystemOfUnitsService();
203                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": SystemOfUnitsService is null", service);
204                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": Available SystemOfUnits are null",
205                                        service.getAvailableSystemsOfUnits());
206                        assertFalse(SECTION_PREFIX + SECTION_NUM + " No available SystemOfUnits found",
207                                        service.getAvailableSystemsOfUnits().isEmpty());
208                }
209        }
210
211        /**
212         * Access Binary Prefixes in SystemOfUnitsService.
213         * @since 2.0
214         */
215        @Test(groups = { SPI }, description = DESCRIPTION)
216        @SpecAssertion(section = SECTION_NUM, id = "54-A09")
217        public void testSystemOfUnitsServicePrefixBinary() {
218                for (ServiceProvider provider : ServiceProvider.available()) {
219                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": ServiceProvider is null", provider);
220                        final SystemOfUnitsService service = provider.getSystemOfUnitsService();
221                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": SystemOfUnitsService is null", service);
222                        Set<BinaryPrefix> prefixes = service.getPrefixes(BinaryPrefix.class);
223                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": Binary Prefixes are null", prefixes);
224                        assertFalse(SECTION_PREFIX + SECTION_NUM + " No Binary Prefixes found", prefixes.isEmpty());
225                        assertEquals(8, prefixes.size(), SECTION_PREFIX + SECTION_NUM + " Wrong Number of Binary Prefixes");
226                }
227        }
228        
229        /**
230         * Access Metric Prefixes in SystemOfUnitsService.
231         * @since 2.0
232         */
233        @Test(groups = { SPI }, description = DESCRIPTION)
234        @SpecAssertion(section = SECTION_NUM, id = "54-A10")
235        public void testSystemOfUnitsServicePrefixMetric() {
236                for (ServiceProvider provider : ServiceProvider.available()) {
237                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": ServiceProvider is null", provider);
238                        final SystemOfUnitsService service = provider.getSystemOfUnitsService();
239                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": SystemOfUnitsService is null", service);
240                        Set<MetricPrefix> prefixes = service.getPrefixes(MetricPrefix.class);
241                        assertNotNull(SECTION_PREFIX + SECTION_NUM + ": Metric Prefixes are null", prefixes);
242                        assertFalse(SECTION_PREFIX + SECTION_NUM + " No Metric Prefixes found", prefixes.isEmpty());
243                        assertEquals(20, prefixes.size(), SECTION_PREFIX + SECTION_NUM + " Wrong Number of Metric Prefixes");
244                }
245        }
246}