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.CurrencyProviderSpi;
019import javax.money.spi.MonetaryAmountFactoryProviderSpi;
020import javax.money.spi.MonetaryAmountsSingletonSpi;
021import javax.money.spi.MonetaryConversionsSingletonSpi;
022import javax.money.spi.RoundingProviderSpi;
023import java.util.ServiceLoader;
024
025/**
026 * Tests for the core SPI implementation.
027 * Created by Anatole on 10.03.14.
028 */
029@SpecVersion(spec = "JSR 354", version = "1.0.0")
030public class CoreSPITests {
031
032
033    // ***************************************** A. Registering Currencies ***********************************
034
035    /**
036     * Test registered CurrencyProviderSpi (at least one instance
037     * required). Test behaviour,
038     * especially bad case behaviour for invalid
039     * input.
040     */
041    @Test(description = "4.5.1 Test if a CurrencyProviderSpi is registered.")
042    @SpecAssertion(id = "451-A1", section = "4.5.1")
043    public void testCurrencyProviderSpi() {
044        ServiceLoader l = null;
045        try {
046            l = ServiceLoader.load(CurrencyProviderSpi.class);
047        } catch (Exception e) {
048            Assert.fail("Failure during check for loaded CurrencyProviderSpi.", e);
049        }
050        Assert.assertTrue(l.iterator().hasNext(), "No instance of CurrencyProviderSpi provided by implementation.");
051    }
052
053    /**
054     * Test registered MonetarySingletonSpi (at least one instance
055     * required). Test behaviour,
056     * especially bad case behaviour for invalid
057     * input.
058     */
059    @Test(description = "4.5.1 Test if a MonetaryAmountsSingletonSpi is registered.")
060    @SpecAssertion(id = "451-A2", section = "4.5.1")
061    public void testMonetaryAmountsSingletonSpi2() {
062        // Duplicate test.
063    }
064
065
066    // ***************************************** A. Registering Monetary Amount Factories **************************
067
068    /**
069     * Test registered MonetarySpi (at least one instance
070     * required). Test behaviour,
071     * especially bad case behaviour for invalid
072     * input.
073     */
074    @Test(description = "4.5.1 Test if a MonetaryAmountFactoryProviderSpi is registered.")
075    @SpecAssertion(id = "451-B1", section = "4.5.1")
076    public void testMonetaryAmountFactoryProviderSpis() {
077        ServiceLoader l = null;
078        try {
079            l = ServiceLoader.load(MonetaryAmountFactoryProviderSpi.class);
080        } catch (Exception e) {
081            Assert.fail("Failure during check for loaded MonetaryAmountFactoryProviderSpi.", e);
082        }
083        Assert.assertTrue(l.iterator().hasNext(),
084                "No instance of MonetaryAmountFactoryProviderSpi provided by implementation.");
085    }
086
087    // ************************************ C. Backing the Monetary Singleton ******************************
088
089    /**
090     * Test registered MonetarySingletonSpi (at least one instance
091     * required). Test behaviour,
092     * especially bad case behaviour for invalid
093     * input.
094     */
095    @Test(description = "4.5.1 Test if a MonetaryAmountsSingletonSpi is registered.")
096    @SpecAssertion(id = "451-C1", section = "4.5.1")
097    public void testMonetaryAmountsSingletonSpi() {
098        ServiceLoader l = null;
099        try {
100            l = ServiceLoader.load(MonetaryAmountsSingletonSpi.class);
101        } catch (Exception e) {
102            Assert.fail("Failure during check for loaded MonetaryAmountsSingletonSpi.", e);
103        }
104        Assert.assertTrue(l.iterator().hasNext(),
105                "No instance of MonetaryAmountsSingletonSpi provided by implementation.");
106    }
107
108    // ************************************ D. Registering Roundings ******************************
109
110    /**
111     * Test registered RoundingProviderSpi (at least one instance
112     * required). Test behaviour,
113     * especially bad case behaviour for invalid
114     * input.
115     */
116    @Test(description = "4.5.1 Test if a RoundingProviderSpi is registered.")
117    @SpecAssertion(id = "451-D1", section = "4.5.1")
118    public void testRoundingProviderSpi() {
119        ServiceLoader l = null;
120        try {
121            l = ServiceLoader.load(RoundingProviderSpi.class);
122        } catch (Exception e) {
123            Assert.fail("Failure during check for loaded RoundingProviderSpi.", e);
124        }
125        Assert.assertTrue(l.iterator().hasNext(), "No instance of RoundingProviderSpi provided by implementation.");
126    }
127
128    // ************************************ E. Adapting Currency Conversion ******************************
129
130    /**
131     * Test registered ConversionProviderSpi (at least one instance
132     * required). Test behaviour,
133     * especially bad case behaviour for invalid
134     * input.
135     */
136    @Test(description = "4.5.2 Test if any ExchangeRateProvider instances are registered.")
137    @SpecAssertion(id = "452-A1", section = "4.5.2")
138    public void testExchangeRateProviderSpi() {
139        ServiceLoader l = null;
140        try {
141            l = ServiceLoader.load(ExchangeRateProvider.class);
142        } catch (Exception e) {
143            Assert.fail("Failure during check for loaded ExchangeRateProvider.", e);
144        }
145        Assert.assertTrue(l.iterator().hasNext(), "No instance of ExchangeRateProvider provided by implementation.");
146    }
147
148    /**
149     * Test registered MonetaryConversionsSingletonSpi (at least one instance
150     * required). Test behaviour,
151     * especially bad case behaviour for invalid
152     * input.
153     */
154    @Test(description = "4.5.2 Test if a MonetaryConversionsSingletonSpi instance is registered.")
155    @SpecAssertion(id = "452-A2", section = "4.5.2")
156    public void testMonetaryConversionsSingletonSpi() {
157        ServiceLoader l = null;
158        try {
159            l = ServiceLoader.load(MonetaryConversionsSingletonSpi.class);
160        } catch (Exception e) {
161            Assert.fail("Failure during check for loaded MonetaryConversionsSingletonSpi.", e);
162        }
163        Assert.assertTrue(l.iterator().hasNext(),
164                "No instance of MonetaryConversionsSingletonSpi provided by implementation.");
165    }
166
167}