task2/solution01/test/org/apidesign/apifest08/test/Task1Test.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 01 Oct 2008 10:43:05 +0200
changeset 29 f6073056b9fe
parent 6 task1/solution01/test/org/apidesign/apifest08/test/Task1Test.java@97662396c0fd
permissions -rw-r--r--
Getting ready for task2: copying all solutions to new locations
japod@6
     1
package org.apidesign.apifest08.test;
japod@6
     2
japod@6
     3
import junit.framework.TestCase;
japod@6
     4
import org.apidesign.apifest08.currency.*;
japod@6
     5
japod@6
     6
import java.math.BigDecimal;
japod@6
     7
import java.math.RoundingMode;
japod@6
     8
import java.util.Currency;
japod@6
     9
japod@6
    10
/**
japod@6
    11
 * Finish the Convertor API, and then write bodies of methods inside
japod@6
    12
 * of this class to match the given tasks. To fullfil your task, use the
japod@6
    13
 * API define in the <code>org.apidesign.apifest08.currency</code> package.
japod@6
    14
 * Do not you reflection, or other hacks as your code
japod@6
    15
 * shall run without any runtime permissions.
japod@6
    16
 */
japod@6
    17
public class Task1Test extends TestCase {
japod@6
    18
    private static CurrencyConvertorFactory currencyConvertorFactoryInstance;
japod@6
    19
japod@6
    20
    public Task1Test(String testName) {
japod@6
    21
        super(testName);
japod@6
    22
    }
japod@6
    23
japod@6
    24
    @Override
japod@6
    25
    protected void setUp() throws Exception {
japod@6
    26
        currencyConvertorFactoryInstance = ConvertorsFactory.getCurrencyConvertorFactoryInstance();
japod@6
    27
    }
japod@6
    28
japod@6
    29
    @Override
japod@6
    30
    protected void tearDown() throws Exception {
japod@6
    31
        currencyConvertorFactoryInstance = null;
japod@6
    32
    }
japod@6
    33
japod@6
    34
    /**
japod@6
    35
     * Create convertor that understands two currencies, CZK and
japod@6
    36
     * USD. Make 1 USD == 17 CZK.
japod@6
    37
     * <p/>
japod@6
    38
     * Creation of the convertor shall not require subclassing of any class
japod@6
    39
     * or interface on the client side.
japod@6
    40
     *
japod@6
    41
     * @return prepared convertor ready for converting USD to CZK and CZK to USD
japod@6
    42
     */
japod@6
    43
    public static Convertor createCZKtoUSD() throws ConvertorNotAvailableException {
japod@6
    44
        final Currency czk = Currency.getInstance("CZK");
japod@6
    45
        final Currency usd = Currency.getInstance("USD");
japod@6
    46
        final BigDecimal constant = BigDecimal.ONE.divide(BigDecimal.valueOf(17), 10, RoundingMode.HALF_EVEN);
japod@6
    47
        return currencyConvertorFactoryInstance.createConvertor(czk, usd, ConversionProperties.create(constant, RoundingMode.HALF_EVEN));
japod@6
    48
    }
japod@6
    49
japod@6
    50
    /**
japod@6
    51
     * Create convertor that understands two currencies, CZK and
japod@6
    52
     * SKK. Make 100 SKK == 80 CZK.
japod@6
    53
     * <p/>
japod@6
    54
     * Creation of the convertor shall not require subclassing of any class
japod@6
    55
     * or interface on the client side.
japod@6
    56
     *
japod@6
    57
     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
japod@6
    58
     */
japod@6
    59
    public static Convertor createSKKtoCZK() throws ConvertorNotAvailableException {
japod@6
    60
        final Currency skk = Currency.getInstance("SKK");
japod@6
    61
        final Currency czk = Currency.getInstance("CZK");
japod@6
    62
        final BigDecimal constant = new BigDecimal("0.8");
japod@6
    63
        return currencyConvertorFactoryInstance.createConvertor(skk, czk, ConversionProperties.create(constant, RoundingMode.HALF_EVEN));
japod@6
    64
    }
japod@6
    65
japod@6
    66
    /**
japod@6
    67
     * Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
japod@6
    68
     * with it.
japod@6
    69
     */
japod@6
    70
    public void testCurrencyCZKUSD() throws Exception {
japod@6
    71
        Convertor c = createCZKtoUSD();
japod@6
    72
        // convert $5 to CZK using c:
japod@6
    73
japod@6
    74
        final int czkDigitsScale = c.getCurrency1().getDefaultFractionDigits();
japod@6
    75
        final int usdDigitsScale = c.getCurrency2().getDefaultFractionDigits();
japod@6
    76
japod@6
    77
        assertEquals("Result is 85 CZK", BigDecimal.valueOf(85).setScale(czkDigitsScale), c.convertCurrency2ToCurrency1(BigDecimal.valueOf(5)));
japod@6
    78
japod@6
    79
        // convert $8 to CZK
japod@6
    80
        assertEquals("Result is 136 CZK", BigDecimal.valueOf(136).setScale(czkDigitsScale), c.convertCurrency2ToCurrency1(BigDecimal.valueOf(8)));
japod@6
    81
japod@6
    82
        // convert 1003CZK to USD
japod@6
    83
        assertEquals("Result is 59 USD", BigDecimal.valueOf(59).setScale(usdDigitsScale), c.convertCurrency1ToCurrency2(BigDecimal.valueOf(1003)));
japod@6
    84
    }
japod@6
    85
japod@6
    86
    /**
japod@6
    87
     * Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
japod@6
    88
     * with it.
japod@6
    89
     */
japod@6
    90
    public void testCurrencySKKCZK() throws Exception {
japod@6
    91
        Convertor c = createSKKtoCZK();
japod@6
    92
        // convert 16CZK using c:
japod@6
    93
japod@6
    94
        final int skkDigitsScale = c.getCurrency1().getDefaultFractionDigits();
japod@6
    95
        final int czkDigitsScale = c.getCurrency2().getDefaultFractionDigits();
japod@6
    96
        
japod@6
    97
        assertEquals("Result is 20 SKK", BigDecimal.valueOf(20).setScale(skkDigitsScale), c.convertCurrency2ToCurrency1(BigDecimal.valueOf(16)));
japod@6
    98
japod@6
    99
        // convert 500SKK to CZK
japod@6
   100
        assertEquals("Result is 400 CZK", BigDecimal.valueOf(400).setScale(czkDigitsScale), c.convertCurrency1ToCurrency2(BigDecimal.valueOf(500)));
japod@6
   101
    }
japod@6
   102
japod@6
   103
}
japod@6
   104