1 package org.apidesign.apifest08.test;
3 import junit.framework.TestCase;
4 import org.apidesign.apifest08.currency.*;
6 import java.math.BigDecimal;
7 import java.math.RoundingMode;
8 import java.util.Currency;
11 * Finish the Convertor API, and then write bodies of methods inside
12 * of this class to match the given tasks. To fullfil your task, use the
13 * API define in the <code>org.apidesign.apifest08.currency</code> package.
14 * Do not you reflection, or other hacks as your code
15 * shall run without any runtime permissions.
17 public class Task1Test extends TestCase {
18 private static CurrencyConvertorFactory currencyConvertorFactoryInstance;
20 public Task1Test(String testName) {
25 protected void setUp() throws Exception {
26 currencyConvertorFactoryInstance = ConvertorsFactory.getCurrencyConvertorFactoryInstance();
30 protected void tearDown() throws Exception {
31 currencyConvertorFactoryInstance = null;
35 * Create convertor that understands two currencies, CZK and
36 * USD. Make 1 USD == 17 CZK.
38 * Creation of the convertor shall not require subclassing of any class
39 * or interface on the client side.
41 * @return prepared convertor ready for converting USD to CZK and CZK to USD
43 public static Convertor createCZKtoUSD() throws ConvertorNotAvailableException {
44 final Currency czk = Currency.getInstance("CZK");
45 final Currency usd = Currency.getInstance("USD");
46 final BigDecimal constant = BigDecimal.ONE.divide(BigDecimal.valueOf(17), 10, RoundingMode.HALF_EVEN);
47 return currencyConvertorFactoryInstance.createConvertor(czk, usd, ConversionProperties.create(constant, RoundingMode.HALF_EVEN));
51 * Create convertor that understands two currencies, CZK and
52 * SKK. Make 100 SKK == 80 CZK.
54 * Creation of the convertor shall not require subclassing of any class
55 * or interface on the client side.
57 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
59 public static Convertor createSKKtoCZK() throws ConvertorNotAvailableException {
60 final Currency skk = Currency.getInstance("SKK");
61 final Currency czk = Currency.getInstance("CZK");
62 final BigDecimal constant = new BigDecimal("0.8");
63 return currencyConvertorFactoryInstance.createConvertor(skk, czk, ConversionProperties.create(constant, RoundingMode.HALF_EVEN));
67 * Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
70 public void testCurrencyCZKUSD() throws Exception {
71 Convertor c = createCZKtoUSD();
72 // convert $5 to CZK using c:
74 final int czkDigitsScale = c.getCurrency1().getDefaultFractionDigits();
75 final int usdDigitsScale = c.getCurrency2().getDefaultFractionDigits();
77 assertEquals("Result is 85 CZK", BigDecimal.valueOf(85).setScale(czkDigitsScale), c.convertCurrency2ToCurrency1(BigDecimal.valueOf(5)));
80 assertEquals("Result is 136 CZK", BigDecimal.valueOf(136).setScale(czkDigitsScale), c.convertCurrency2ToCurrency1(BigDecimal.valueOf(8)));
82 // convert 1003CZK to USD
83 assertEquals("Result is 59 USD", BigDecimal.valueOf(59).setScale(usdDigitsScale), c.convertCurrency1ToCurrency2(BigDecimal.valueOf(1003)));
87 * Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
90 public void testCurrencySKKCZK() throws Exception {
91 Convertor c = createSKKtoCZK();
92 // convert 16CZK using c:
94 final int skkDigitsScale = c.getCurrency1().getDefaultFractionDigits();
95 final int czkDigitsScale = c.getCurrency2().getDefaultFractionDigits();
97 assertEquals("Result is 20 SKK", BigDecimal.valueOf(20).setScale(skkDigitsScale), c.convertCurrency2ToCurrency1(BigDecimal.valueOf(16)));
99 // convert 500SKK to CZK
100 assertEquals("Result is 400 CZK", BigDecimal.valueOf(400).setScale(czkDigitsScale), c.convertCurrency1ToCurrency2(BigDecimal.valueOf(500)));