task1/solution12/test/org/apidesign/apifest08/test/Task1Test.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
child 20 7ca97f802b5a
permissions -rw-r--r--
Adding solutions received for task1
     1 package org.apidesign.apifest08.test;
     2 
     3 import java.util.Currency;
     4 
     5 import junit.framework.TestCase;
     6 
     7 import org.apidesign.apifest08.currency.Convertor;
     8 
     9 /**
    10  * Finish the Convertor API, and then write bodies of methods inside of this class to match the given tasks. To fullfil
    11  * your task, use the API define in the <code>org.apidesign.apifest08.currency</code> package. Do not you reflection, or
    12  * other hacks as your code shall run without any runtime permissions.
    13  */
    14 public class Task1Test extends TestCase {
    15   public Task1Test(String testName) {
    16     super(testName);
    17   }
    18 
    19   @Override
    20   protected void setUp() throws Exception {
    21   }
    22 
    23   @Override
    24   protected void tearDown() throws Exception {
    25   }
    26 
    27   /**
    28    * Create convertor that understands two currencies, CZK and USD. Make 1 USD == 17 CZK. Creation of the convertor
    29    * shall not require subclassing of any class or interface on the client side.
    30    * @return prepared convertor ready for converting USD to CZK and CZK to USD
    31    */
    32   public static Convertor createCZKtoUSD() {
    33     return Convertor.getConvertorInstance(Currency.getInstance("CZK"), Currency.getInstance("USD"));
    34   }
    35 
    36   /**
    37    * Create convertor that understands two currencies, CZK and SKK. Make 100 SKK == 80 CZK. Creation of the convertor
    38    * shall not require subclassing of any class or interface on the client side.
    39    * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    40    */
    41   public static Convertor createSKKtoCZK() {
    42     return Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
    43   }
    44 
    45   /**
    46    * Use the convertor from <code>createCZKtoUSD</code> method and do few conversions with it.
    47    */
    48   public void testCurrencyCZKUSD() throws Exception {
    49     Convertor c = createCZKtoUSD();
    50 
    51     // convert $5 to CZK using c:
    52     double result = c.convert(5, Currency.getInstance("USD"));
    53     assertEquals("Result is not 85 CZK", 85.0, result);
    54 
    55     // convert $8 to CZK
    56     result = c.convert(8, Currency.getInstance("USD"));
    57     assertEquals("Result is not 136 CZK", 136.0, result);
    58 
    59     // convert 1003CZK to USD
    60     result = c.convert(1003, Currency.getInstance("CZK"));
    61     assertEquals("Result is not 59 USD", 59.0, result);
    62   }
    63 
    64   /**
    65    * Use the convertor from <code>createSKKtoCZK</code> method and do few conversions with it.
    66    */
    67   public void testCurrencySKKCZK() throws Exception {
    68     Convertor c = createSKKtoCZK();
    69 
    70     // convert 16CZK using c:
    71     double result = c.convert(16, Currency.getInstance("CZK"));
    72     assertEquals("Result is not 20 SKK", 20.0, result);
    73 
    74     // convert 500SKK to CZK
    75     result = c.convert(500, Currency.getInstance("SKK"));
    76     assertEquals("Result is not 400 CZK", 400.0, result);
    77   }
    78 }