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