task1/solution04/test/org/apidesign/apifest08/test/Task1Test.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
child 17 37c9921c653e
permissions -rw-r--r--
Adding solutions received for task1
     1 package org.apidesign.apifest08.test;
     2 
     3 import java.math.BigDecimal;
     4 import java.util.Currency;
     5 import junit.framework.TestCase;
     6 import org.apidesign.apifest08.currency.Convertor;
     7 import org.apidesign.apifest08.currency.ConvertorFactory;
     8 
     9 /** Finish the Convertor API, and then write bodies of methods inside
    10  * of this class to match the given tasks. To fullfil your task, use the
    11  * API define in the <code>org.apidesign.apifest08.currency</code> package.
    12  * Do not you reflection, or other hacks as your code
    13  * shall run without any runtime permissions.
    14  */
    15 public class Task1Test extends TestCase {
    16     public Task1Test(String testName) {
    17         super(testName);
    18     }
    19 
    20     @Override
    21     protected void setUp() throws Exception {
    22     }
    23 
    24     @Override
    25     protected void tearDown() throws Exception {
    26     }
    27 
    28     /** Create convertor that understands two currencies, CZK and
    29      *  USD. Make 1 USD == 17 CZK.
    30      *
    31      * Creation of the convertor shall not require subclassing of any class
    32      * or interface on the client side.
    33      *
    34      * @return prepared convertor ready for converting USD to CZK and CZK to USD
    35      */
    36     public static Convertor createCZKtoUSD()
    37     {
    38         return (ConvertorFactory.getConvertor("CZK", "USD"));
    39     }
    40 
    41     /** Create convertor that understands two currencies, CZK and
    42      *  SKK. Make 100 SKK == 80 CZK.
    43      *
    44      * Creation of the convertor shall not require subclassing of any class
    45      * or interface on the client side.
    46      * 
    47      * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    48      */
    49     public static Convertor createSKKtoCZK()
    50     {
    51         return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), Currency.getInstance("CZK")));
    52     }
    53     
    54     public static Convertor createUSDtoUSD()
    55     {
    56         return (ConvertorFactory.getConvertor(Currency.getInstance("USD"), Currency.getInstance("USD")));
    57     }
    58     
    59     /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    60      * with it.
    61      */
    62     public void testCurrencyCZKUSD() throws Exception {
    63         Convertor  c = createCZKtoUSD();
    64         BigDecimal result;
    65         
    66         // convert $5 to CZK using c:
    67         // assertEquals("Result is 85 CZK");
    68         result = c.convertFrom(BigDecimal.valueOf(5));
    69         assertEquals(new BigDecimal("85.00"), result);
    70 
    71         // convert $8 to CZK
    72         // assertEquals("Result is 136 CZK");
    73         result = c.convertFrom(BigDecimal.valueOf(8));
    74         assertEquals(new BigDecimal("136.00"), result);
    75 
    76         // convert 1003CZK to USD
    77         // assertEquals("Result is 59 USD");
    78         result = c.convertTo(BigDecimal.valueOf(1003));
    79         assertEquals(new BigDecimal("59.00"), result);
    80     }
    81 
    82     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    83      * with it.
    84      */
    85     public void testCurrencySKKCZK() throws Exception {
    86         Convertor c = createSKKtoCZK();
    87         BigDecimal result;
    88         
    89         // convert 16CZK using c:
    90         // assertEquals("Result is 20 SKK");
    91         result = c.convertFrom(BigDecimal.valueOf(16));
    92         assertEquals(new BigDecimal("20.00"), result);
    93                         
    94         // convert 500SKK to CZK
    95         // assertEquals("Result is 400 CZK");
    96         result = c.convertTo(BigDecimal.valueOf(500));
    97         assertEquals(new BigDecimal("400.00"), result);
    98    }
    99 
   100     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
   101      * with it.
   102      */
   103     public void testCurrencyUSDUSD() throws Exception {
   104         Convertor c = createUSDtoUSD();
   105         BigDecimal result;
   106         
   107         // convert 1USD using c:
   108         // assertEquals("Result is 1 USD");
   109         result = c.convertFrom(BigDecimal.valueOf(1));
   110         assertEquals(new BigDecimal("1.00"), result);
   111                         
   112         // convert 500USD to USD
   113         // assertEquals("Result is 500 USD");
   114         result = c.convertTo(BigDecimal.valueOf(500));
   115         assertEquals(new BigDecimal("500.00"), result);
   116    }
   117 }
   118