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; 011 012import org.javamoney.tck.TCKTestSetup; 013import org.javamoney.tck.TestUtils; 014import org.jboss.test.audit.annotations.SpecAssertion; 015import org.jboss.test.audit.annotations.SpecVersion; 016import org.testng.AssertJUnit; 017import org.testng.annotations.Test; 018 019import javax.money.CurrencyUnit; 020import javax.money.Monetary; 021import java.util.ArrayList; 022import java.util.Currency; 023import java.util.List; 024 025 026@SpecVersion(spec = "JSR 354", version = "1.0.0") 027public class ModellingCurrenciesTest { 028 029 /** 030 * Ensure at least one CurrencyUnit implementation 031 * is available and registered/accessible from Monetary. 032 */ 033 @SpecAssertion(section = "4.2.1", id = "421-A1") 034 @Test(description = "4.2.1 Ensure TCK has CurrencyUnit classes configured.") 035 public void testEnsureCurrencyUnit() { 036 AssertJUnit.assertTrue("TCK Configuration not available.", TCKTestSetup.getTestConfiguration() != null); 037 AssertJUnit.assertTrue(!TCKTestSetup.getTestConfiguration().getCurrencyClasses().isEmpty()); 038 } 039 040 /** 041 * Tests that currencies returned for same ISO currency code are 042 * equal, ensure when listing all available currencies, that the 043 * code 044 * is unique. 045 */ 046 @SpecAssertion(section = "4.2.1", id = "421-A2") 047 @Test(description = "4.2.1 Test currencies provided equal at least currencies from java.util.Currency.") 048 public void testEqualISOCurrencies() { 049 for (Currency currency : Currency.getAvailableCurrencies()) { 050 CurrencyUnit unit = Monetary.getCurrency(currency.getCurrencyCode()); 051 AssertJUnit.assertNotNull(unit); 052 CurrencyUnit unit2 = Monetary.getCurrency(currency.getCurrencyCode()); 053 AssertJUnit.assertNotNull(unit2); 054 AssertJUnit.assertEquals(unit, unit2); 055 } 056 } 057 058 /** 059 * Ensure all ISO 3-letters codes as defined by the JDK are also 060 * available from Monetary. 061 */ 062 @SpecAssertion(section = "4.2.1", id = "421-A3") 063 @Test(description = "4.2.1 Test currencies provided have correct ISO 3-letter currency codes.") 064 public void testEnforce3LetterCode4ISO() { 065 for (Currency currency : Currency.getAvailableCurrencies()) { 066 CurrencyUnit unit = Monetary.getCurrency(currency.getCurrencyCode()); 067 AssertJUnit.assertNotNull(unit); 068 AssertJUnit.assertEquals(currency.getCurrencyCode(), unit.getCurrencyCode()); 069 } 070 } 071 072 /** 073 * Test that JDK currencies returned match the values of corresponding JDK Currency (code, numeric code, 074 * default fraction digits). 075 */ 076 @SpecAssertion(section = "4.2.1", id = "421-A4") 077 @Test(description = "4.2.1 Test currencies provided have correct default fraction digits and numeric code.") 078 public void testISOCodes() { 079 for (Currency currency : Currency.getAvailableCurrencies()) { 080 CurrencyUnit unit = Monetary.getCurrency(currency.getCurrencyCode()); 081 AssertJUnit.assertEquals(currency.getCurrencyCode(), unit.getCurrencyCode()); 082 AssertJUnit.assertEquals(currency.getDefaultFractionDigits(), unit.getDefaultFractionDigits()); 083 AssertJUnit.assertEquals(currency.getNumericCode(), unit.getNumericCode()); 084 } 085 } 086 087 /** 088 * Test that CurrencyUnit implementations implement hashCode. 089 */ 090 @SpecAssertion(section = "4.2.1", id = "421-B1") 091 @Test(description = "4.2.1 Ensure registered CurrencyUnit classes implement hashCode.") 092 public void testCurrencyClassesEqualsHashcode() { 093 for (Class type : TCKTestSetup.getTestConfiguration().getCurrencyClasses()) { 094 TestUtils.testHasPublicMethod("Section 4.2.1", type, int.class, "hashCode"); 095 } 096 for (String code : new String[]{"CHF", "USD", "EUR", "GBP", "USS"}) { 097 CurrencyUnit unit = Monetary.getCurrency(code); 098 TestUtils.testHasPublicMethod("Section 4.2.1", unit.getClass(), int.class, "hashCode"); 099 } 100 } 101 102 /** 103 * Test that CurrencyUnit implementations implement equals. 104 */ 105 @SpecAssertion(section = "4.2.1", id = "421-B2") 106 @Test(description = "4.2.1 Ensure registered CurrencyUnit classes implement equals.") 107 public void testImplementsEquals() { 108 List<CurrencyUnit> firstUnits = new ArrayList<>(); 109 List<CurrencyUnit> secondUnits = new ArrayList<>(); 110 for (String code : new String[]{"CHF", "USD", "EUR", "GBP", "USS"}) { 111 CurrencyUnit unit = Monetary.getCurrency(code); 112 AssertJUnit.assertNotNull(unit); 113 TestUtils.testHasPublicMethod("Section 4.2.1", unit.getClass(), boolean.class, "equals", Object.class); 114 firstUnits.add(unit); 115 CurrencyUnit unit2 = Monetary.getCurrency(code); 116 AssertJUnit.assertNotNull(unit2); 117 secondUnits.add(unit2); 118 } 119 for (String code : new String[]{"CHF", "USD", "EUR", "GBP", "USS"}) { 120 CurrencyUnit unit = Monetary.getCurrency(code); 121 TestUtils.testHasPublicMethod("Section 4.2.1", unit.getClass(), boolean.class, "equals", Object.class); 122 } 123 for (int i = 0; i < firstUnits.size(); i++) { 124 AssertJUnit.assertEquals(firstUnits.get(i), secondUnits.get(i)); 125 } 126 } 127 128 /** 129 * Test that CurrencyUnit implementations are comparable. 130 */ 131 @SpecAssertion(section = "4.2.1", id = "421-B3") 132 @Test(description = "4.2.1 Ensure registered CurrencyUnit classes are Comparable.") 133 public void testCurrencyClassesComparable() { 134 for (Class type : TCKTestSetup.getTestConfiguration().getCurrencyClasses()) { 135 TestUtils.testComparable("Section 4.2.1", type); 136 } 137 for (String code : new String[]{"CHF", "USD", "EUR", "GBP", "USS"}) { 138 CurrencyUnit unit = Monetary.getCurrency(code); 139 TestUtils.testComparable("Section 4.2.1", unit.getClass()); 140 } 141 } 142 143 /** 144 * Test that CurrencyUnit implementations are immutable. 145 */ 146 @SpecAssertion(section = "4.2.1", id = "421-B4") 147 @Test(description = "4.2.1 Ensure registered CurrencyUnit classes are immutable.") 148 public void testIsImmutable() { 149 for (Class type : TCKTestSetup.getTestConfiguration().getCurrencyClasses()) { 150 TestUtils.testImmutable("Section 4.2.1", type); 151 } 152 for (String code : new String[]{"CHF", "USD", "EUR", "GBP", "USS"}) { 153 CurrencyUnit unit = Monetary.getCurrency(code); 154 TestUtils.testImmutable("Section 4.2.1", unit.getClass()); 155 } 156 } 157 158 /** 159 * Test that CurrencyUnit implementations are serializable. 160 */ 161 @SpecAssertion(section = "4.2.1", id = "421-B6") 162 @Test(description = "4.2.1 Ensure registered CurrencyUnit classes are serializable.") 163 public void testImplementsSerializable() { 164 for (Class type : TCKTestSetup.getTestConfiguration().getCurrencyClasses()) { 165 TestUtils.testSerializable("Section 4.2.1", type); 166 } 167 for (String code : new String[]{"CHF", "USD", "EUR", "GBP", "USS"}) { 168 CurrencyUnit unit = Monetary.getCurrency(code); 169 TestUtils.testSerializable("Section 4.2.1", unit); 170 } 171 } 172 173}