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.conversion; 011 012import org.javamoney.tck.tests.internal.TestCurrencyUnit; 013import org.jboss.test.audit.annotations.SpecAssertion; 014import org.jboss.test.audit.annotations.SpecVersion; 015import org.testng.AssertJUnit; 016import org.testng.annotations.Test; 017 018import javax.money.CurrencyUnit; 019import javax.money.MonetaryAmount; 020import javax.money.MonetaryAmounts; 021import javax.money.MonetaryCurrencies; 022import javax.money.convert.*; 023 024import static org.testng.AssertJUnit.assertEquals; 025 026/** 027 * Test for converting amounts. 028 * Created by Anatole on 10.03.14. 029 */ 030@SpecVersion(spec = "JSR 354", version = "1.0.0") 031public class ConvertingAmountsTest { 032 033 // ******************************* A. Test Basic MonetaryConversions Accessors ****************************** 034 035 /** 036 * Test successful conversion for possible currency pairs.<p> 037 * Hint: you may only check for rate factory, when using a hardcoded ExchangeRateProvider, such a provider 038 * must be also implemented and registered as an SPI. 039 */ 040 @Test(description = "4.3.2 Test successful conversion for CHF -> FOO, using TestRateProvider.") 041 @SpecAssertion(id = "432-A1", section = "4.3.2") 042 public void testConversion() { 043 CurrencyUnit cu = new TestCurrencyUnit("FOO"); 044 CurrencyConversion conv = MonetaryConversions.getConversion(cu, "TestRateProvider"); 045 MonetaryAmount m = MonetaryAmounts.getDefaultAmountFactory().setNumber(10).setCurrency("CHF").create(); 046 MonetaryAmount m2 = m.with(conv); 047 AssertJUnit.assertEquals(m2.getCurrency().getCurrencyCode(), "FOO"); 048 AssertJUnit.assertEquals(20L, m2.getNumber().longValueExact()); 049 m2 = m.with(conv); 050 AssertJUnit.assertEquals(m2.getCurrency().getCurrencyCode(), "FOO"); 051 AssertJUnit.assertEquals(20L, m2.getNumber().longValueExact()); 052 } 053 054 /** 055 * Compare conversions done with exchange rates provided for same currency pair. 056 */ 057 @Test(description = "4.3.2 Test correct ExchangeRate is returned for CHF -> FOO, using TestRateProvider.") 058 @SpecAssertion(id = "432-A2", section = "4.3.2") 059 public void testConversionComparedWithRate() { 060 final CurrencyUnit FOO = new TestCurrencyUnit("FOO"); 061 ExchangeRate rate = MonetaryConversions.getExchangeRateProvider("TestRateProvider") 062 .getExchangeRate(MonetaryCurrencies.getCurrency("CHF"), FOO); 063 AssertJUnit.assertEquals(rate.getBaseCurrency(), MonetaryCurrencies.getCurrency("CHF")); 064 AssertJUnit.assertEquals(rate.getCurrency().getCurrencyCode(), FOO.getCurrencyCode()); 065 AssertJUnit.assertEquals(rate.getFactor().intValueExact(), 2); 066 AssertJUnit.assertEquals("TestRateProvider", rate.getContext().getProviderName()); 067 } 068 069 /** 070 * Bad case: try converting from/to an inconvertible (custom) currency, ensure CurrencyConversionException is 071 * thrown. 072 * 073 * @see org.javamoney.tck.tests.internal.TestCurrencyUnit } for creating a custom currency, 074 * with some fancy non-ISO currency code. 075 */ 076 @Test(description = "4.3.2 Bad case: Try CurrencyConversion to an inconvertible (custom) " + 077 "currency (FOOANY), ensure CurrencyConversionException is thrown.") 078 @SpecAssertion(id = "432-A3", section = "4.3.2") 079 public void testUnsupportedConversion() { 080 MonetaryAmount m = MonetaryAmounts.getDefaultAmountFactory().setNumber(10).setCurrency("CHF").create(); 081 CurrencyUnit cu = new TestCurrencyUnit("FOOANY"); 082 try { 083 CurrencyConversion conv = MonetaryConversions.getConversion(cu); 084 m.with(conv); 085 } catch (CurrencyConversionException e) { 086 // expected 087 } 088 } 089 090 /** 091 * Bad case: try converting from/to a null currency, ensure NullPointerException is thrown. 092 */ 093 @Test(expectedExceptions = NullPointerException.class, 094 description = "4.3.2 Bad case: Access CurrencyConversion " + 095 "with a CurrencyUnit==null, ensure NullPointerException is thrown.") 096 @SpecAssertion(id = "432-A4", section = "4.3.2") 097 public void testNullConversion1() { 098 MonetaryConversions.getConversion((CurrencyUnit) null); 099 } 100 101 /** 102 * Bad case: try converting from/to a null currency, ensure NullPointerException is thrown. 103 */ 104 @Test(expectedExceptions = NullPointerException.class, 105 description = "4.3.2 Bad case: Access CurrencyConversion with a currency code==null, ensure " + 106 "NullPointerException is thrown.") 107 @SpecAssertion(id = "432-A4", section = "4.3.2") 108 public void testNullConversion2() { 109 MonetaryConversions.getConversion((String) null); 110 } 111 112}