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
     1 package org.apidesign.apifest08.test;
     2 
     3 import junit.framework.TestCase;
     4 import org.apidesign.apifest08.currency.*;
     5 
     6 import java.math.BigDecimal;
     7 import java.math.RoundingMode;
     8 import java.util.Currency;
     9 
    10 /**
    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.
    16  */
    17 public class Task1Test extends TestCase {
    18     private static CurrencyConvertorFactory currencyConvertorFactoryInstance;
    19 
    20     public Task1Test(String testName) {
    21         super(testName);
    22     }
    23 
    24     @Override
    25     protected void setUp() throws Exception {
    26         currencyConvertorFactoryInstance = ConvertorsFactory.getCurrencyConvertorFactoryInstance();
    27     }
    28 
    29     @Override
    30     protected void tearDown() throws Exception {
    31         currencyConvertorFactoryInstance = null;
    32     }
    33 
    34     /**
    35      * Create convertor that understands two currencies, CZK and
    36      * USD. Make 1 USD == 17 CZK.
    37      * <p/>
    38      * Creation of the convertor shall not require subclassing of any class
    39      * or interface on the client side.
    40      *
    41      * @return prepared convertor ready for converting USD to CZK and CZK to USD
    42      */
    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));
    48     }
    49 
    50     /**
    51      * Create convertor that understands two currencies, CZK and
    52      * SKK. Make 100 SKK == 80 CZK.
    53      * <p/>
    54      * Creation of the convertor shall not require subclassing of any class
    55      * or interface on the client side.
    56      *
    57      * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    58      */
    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));
    64     }
    65 
    66     /**
    67      * Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    68      * with it.
    69      */
    70     public void testCurrencyCZKUSD() throws Exception {
    71         Convertor c = createCZKtoUSD();
    72         // convert $5 to CZK using c:
    73 
    74         final int czkDigitsScale = c.getCurrency1().getDefaultFractionDigits();
    75         final int usdDigitsScale = c.getCurrency2().getDefaultFractionDigits();
    76 
    77         assertEquals("Result is 85 CZK", BigDecimal.valueOf(85).setScale(czkDigitsScale), c.convertCurrency2ToCurrency1(BigDecimal.valueOf(5)));
    78 
    79         // convert $8 to CZK
    80         assertEquals("Result is 136 CZK", BigDecimal.valueOf(136).setScale(czkDigitsScale), c.convertCurrency2ToCurrency1(BigDecimal.valueOf(8)));
    81 
    82         // convert 1003CZK to USD
    83         assertEquals("Result is 59 USD", BigDecimal.valueOf(59).setScale(usdDigitsScale), c.convertCurrency1ToCurrency2(BigDecimal.valueOf(1003)));
    84     }
    85 
    86     /**
    87      * Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    88      * with it.
    89      */
    90     public void testCurrencySKKCZK() throws Exception {
    91         Convertor c = createSKKtoCZK();
    92         // convert 16CZK using c:
    93 
    94         final int skkDigitsScale = c.getCurrency1().getDefaultFractionDigits();
    95         final int czkDigitsScale = c.getCurrency2().getDefaultFractionDigits();
    96         
    97         assertEquals("Result is 20 SKK", BigDecimal.valueOf(20).setScale(skkDigitsScale), c.convertCurrency2ToCurrency1(BigDecimal.valueOf(16)));
    98 
    99         // convert 500SKK to CZK
   100         assertEquals("Result is 400 CZK", BigDecimal.valueOf(400).setScale(czkDigitsScale), c.convertCurrency1ToCurrency2(BigDecimal.valueOf(500)));
   101     }
   102 
   103 }
   104