001/*
002 * Copyright (c) 2012, 2013, Werner Keil, Credit Suisse (Anatole Tresch). Licensed under the Apache
003 * License, Version 2.0 (the "License"); you may not use this file except in compliance with the
004 * License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
005 * Unless required by applicable law or agreed to in writing, software distributed under the License
006 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
007 * or implied. See the License for the specific language governing permissions and limitations under
008 * the License. Contributors: Anatole Tresch - initial version.
009 */
010package org.javamoney.tck.tests.spi;
011
012import org.jboss.test.audit.annotations.SpecAssertion;
013import org.jboss.test.audit.annotations.SpecVersion;
014import org.testng.Assert;
015import org.testng.annotations.Test;
016
017import javax.money.convert.ExchangeRateProvider;
018import javax.money.spi.*;
019import java.util.ServiceLoader;
020
021import static org.testng.Assert.assertTrue;
022import static org.testng.Assert.fail;
023
024/**
025 * Tests for the core SPI implementation.
026 * Created by Anatole on 10.03.14.
027 */
028@SpecVersion(spec = "JSR 354", version = "1.0.0")
029public class CoreSPITests {
030
031
032    // ***************************************** A. Registering Currencies ***********************************
033
034    /**
035     * Test registered CurrencyProviderSpi (at least one instance
036     * required). Test behaviour,
037     * especially bad case behaviour for invalid
038     * input.
039     */
040    @Test(description = "4.5.1 Test if a CurrencyProviderSpi is registered.")
041    @SpecAssertion(id = "451-A1", section = "4.5.1")
042    public void testCurrencyProviderSpi() {
043        ServiceLoader l = null;
044        try {
045            l = ServiceLoader.load(CurrencyProviderSpi.class);
046        } catch (Exception e) {
047            Assert.fail("Failure during check for loaded CurrencyProviderSpi.", e);
048        }
049        Assert.assertTrue(l.iterator().hasNext(), "No instance of CurrencyProviderSpi provided by implementation.");
050    }
051
052    /**
053     * Test registered MonetaryCurrenciesSingletonSpi (at least one instance
054     * required). Test behaviour,
055     * especially bad case behaviour for invalid
056     * input.
057     */
058    @Test(description = "4.5.1 Test if a MonetaryCurrenciesSingletonSpi is registered.")
059    @SpecAssertion(id = "451-A2", section = "4.5.1")
060    public void testMonetaryCurrenciesSingletonSpi() {
061        ServiceLoader l = null;
062        try {
063            l = ServiceLoader.load(MonetaryCurrenciesSingletonSpi.class);
064        } catch (Exception e) {
065            Assert.fail("Failure during check for loadable MonetaryCurrenciesSingletonSpi.", e);
066        }
067        // this spi is optional, a default will be installed if missing, which will cover the currencies bsed
068        // on java.util.Currency.
069    }
070
071
072    // ***************************************** A. Registering Monetary Amount Factories **************************
073
074    /**
075     * Test registered MonetaryAmountsSpi (at least one instance
076     * required). Test behaviour,
077     * especially bad case behaviour for invalid
078     * input.
079     */
080    @Test(description = "4.5.1 Test if a MonetaryAmountFactoryProviderSpi is registered.")
081    @SpecAssertion(id = "451-B1", section = "4.5.1")
082    public void testMonetaryAmountFactoryProviderSpis() {
083        ServiceLoader l = null;
084        try {
085            l = ServiceLoader.load(MonetaryAmountFactoryProviderSpi.class);
086        } catch (Exception e) {
087            Assert.fail("Failure during check for loaded MonetaryAmountFactoryProviderSpi.", e);
088        }
089        Assert.assertTrue(l.iterator().hasNext(),
090                "No instance of MonetaryAmountFactoryProviderSpi provided by implementation.");
091    }
092
093    // ************************************ C. Backing the MonetaryAmounts Singleton ******************************
094
095    /**
096     * Test registered MonetaryAmountsSingletonSpi (at least one instance
097     * required). Test behaviour,
098     * especially bad case behaviour for invalid
099     * input.
100     */
101    @Test(description = "4.5.1 Test if a MonetaryAmountsSingletonSpi is registered.")
102    @SpecAssertion(id = "451-C1", section = "4.5.1")
103    public void testMonetaryAmountsSingletonSpi() {
104        ServiceLoader l = null;
105        try {
106            l = ServiceLoader.load(MonetaryAmountsSingletonSpi.class);
107        } catch (Exception e) {
108            Assert.fail("Failure during check for loaded MonetaryAmountsSingletonSpi.", e);
109        }
110        Assert.assertTrue(l.iterator().hasNext(),
111                "No instance of MonetaryAmountsSingletonSpi provided by implementation.");
112    }
113
114    // ************************************ D. Registering Roundings ******************************
115
116    /**
117     * Test registered RoundingProviderSpi (at least one instance
118     * required). Test behaviour,
119     * especially bad case behaviour for invalid
120     * input.
121     */
122    @Test(description = "4.5.1 Test if a RoundingProviderSpi is registered.")
123    @SpecAssertion(id = "451-D1", section = "4.5.1")
124    public void testRoundingProviderSpi() {
125        ServiceLoader l = null;
126        try {
127            l = ServiceLoader.load(RoundingProviderSpi.class);
128        } catch (Exception e) {
129            Assert.fail("Failure during check for loaded RoundingProviderSpi.", e);
130        }
131        Assert.assertTrue(l.iterator().hasNext(), "No instance of RoundingProviderSpi provided by implementation.");
132    }
133
134    // ************************************ E. Adapting Currency Conversion ******************************
135
136    /**
137     * Test registered ConversionProviderSpi (at least one instance
138     * required). Test behaviour,
139     * especially bad case behaviour for invalid
140     * input.
141     */
142    @Test(description = "4.5.2 Test if any ExchangeRateProvider instances are registered.")
143    @SpecAssertion(id = "452-A1", section = "4.5.2")
144    public void testExchangeRateProviderSpi() {
145        ServiceLoader l = null;
146        try {
147            l = ServiceLoader.load(ExchangeRateProvider.class);
148        } catch (Exception e) {
149            Assert.fail("Failure during check for loaded ExchangeRateProvider.", e);
150        }
151        Assert.assertTrue(l.iterator().hasNext(), "No instance of ExchangeRateProvider provided by implementation.");
152    }
153
154    /**
155     * Test registered MonetaryConversionsSingletonSpi (at least one instance
156     * required). Test behaviour,
157     * especially bad case behaviour for invalid
158     * input.
159     */
160    @Test(description = "4.5.2 Test if a MonetaryConversionsSingletonSpi instance is registered.")
161    @SpecAssertion(id = "452-A2", section = "4.5.2")
162    public void testMonetaryConversionsSingletonSpi() {
163        ServiceLoader l = null;
164        try {
165            l = ServiceLoader.load(MonetaryConversionsSingletonSpi.class);
166        } catch (Exception e) {
167            Assert.fail("Failure during check for loaded MonetaryConversionsSingletonSpi.", e);
168        }
169        Assert.assertTrue(l.iterator().hasNext(),
170                "No instance of MonetaryConversionsSingletonSpi provided by implementation.");
171    }
172
173}