task2/solution13/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 23 task1/solution13/test/org/apidesign/apifest08/test/Task1Test.java@f4b4f95ae1bd
child 41 a7e6f84fb078
permissions -rw-r--r--
Getting ready for task2: copying all solutions to new locations
     1 package org.apidesign.apifest08.test;
     2 
     3 import java.math.BigDecimal;
     4 import junit.framework.TestCase;
     5 import org.apidesign.apifest08.currency.ConversionResult;
     6 import org.apidesign.apifest08.currency.Convertor;
     7 import org.apidesign.apifest08.currency.ConvertorCurrency;
     8 import org.apidesign.apifest08.currency.ExchangeRateProvider;
     9 
    10 /** Finish the Convertor API, and then write bodies of methods inside
    11  * of this class to match the given tasks. To fullfil your task, use the
    12  * API define in the <code>org.apidesign.apifest08.currency</code> package.
    13  * Do not you reflection, or other hacks as your code
    14  * shall run without any runtime permissions.
    15  */
    16 public class Task1Test extends TestCase {
    17     public Task1Test(String testName) {
    18         super(testName);
    19     }
    20 
    21     @Override
    22     protected void setUp() throws Exception {
    23     }
    24 
    25     @Override
    26     protected void tearDown() throws Exception {
    27     }
    28 
    29     /** Create convertor that understands two currencies, CZK and
    30      *  USD. Make 1 USD == 17 CZK.
    31      *
    32      * Creation of the convertor shall not require subclassing of any class
    33      * or interface on the client side.
    34      *
    35      * @return prepared convertor ready for converting USD to CZK and CZK to USD
    36      */
    37     public Convertor createCZKtoUSD() {
    38         ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK");
    39         ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("USD");
    40         ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(17), fromCurrency, new BigDecimal(1), toCurrency);
    41         
    42         return Convertor.createConvertor(exchangeRateProvider);
    43     }
    44 
    45     /** Create convertor that understands two currencies, CZK and
    46      *  SKK. Make 100 SKK == 80 CZK.
    47      *
    48      * Creation of the convertor shall not require subclassing of any class
    49      * or interface on the client side.
    50      * 
    51      * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    52      */
    53     public Convertor createSKKtoCZK() {
    54         ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("SKK");
    55         ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("CZK");
    56         ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(100), fromCurrency, new BigDecimal(80), toCurrency);
    57         
    58         return Convertor.createConvertor(exchangeRateProvider);
    59     }
    60     
    61     
    62     public Convertor createCZKtoYEN() {
    63         ConvertorCurrency fromCurrency = ConvertorCurrency.getInstance("CZK");
    64         ConvertorCurrency toCurrency = ConvertorCurrency.getInstance("JPY");
    65         ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(1), fromCurrency, new BigDecimal(1), toCurrency);
    66         
    67         return Convertor.createConvertor(exchangeRateProvider);
    68     }
    69     
    70     /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    71      * with it.
    72      */
    73     public void testCurrencyCZKUSD() throws Exception {
    74         Convertor convertCzkUsd = createCZKtoUSD();
    75 
    76         {
    77             // convert $1 to CZK using c:
    78             ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(1));
    79             assertEquals("Result is 17 CZK", new BigDecimal("17.00"), result.getConverted());
    80             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
    81         }
    82 
    83         {
    84             // convert 17CKZ to $ using c:
    85             ConversionResult result = convertCzkUsd.convert(new BigDecimal(17));
    86             assertEquals("Result is 1 $", new BigDecimal("1.00"), result.getConverted());
    87             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
    88         }
    89 
    90         {
    91             // convert $5 to CZK using c:
    92             ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(5));
    93             assertEquals("Result is 85 CZK", new BigDecimal("85.00"), result.getConverted());
    94             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
    95         }
    96         
    97         {
    98             // convert $8 to CZK
    99             ConversionResult result = convertCzkUsd.convertBack(new BigDecimal(8));
   100             assertEquals("Result is 136 CZK", new BigDecimal("136.00"), result.getConverted());
   101             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
   102         }
   103 
   104         {
   105             // convert 1003CZK to USD
   106             ConversionResult result = convertCzkUsd.convert(new BigDecimal(1003));
   107             assertEquals("Result is 59 USD", new BigDecimal("59.00"), result.getConverted());
   108             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
   109         }
   110     }
   111 
   112     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
   113      * with it.
   114      */
   115     public void testCurrencySKKCZK() throws Exception {
   116         Convertor convertSkkCzk = createSKKtoCZK();
   117         {
   118             // convert 100SKK using c:
   119             ConversionResult result = convertSkkCzk.convert(new BigDecimal(100));
   120             assertEquals("Result is 80 CZK", new BigDecimal("80.00"), result.getConverted());
   121         }
   122         {
   123             // convert 80CZK using c:
   124             ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(80));
   125             assertEquals("Result is 100 SKK", new BigDecimal("100.00"), result.getConverted());
   126         }
   127         
   128         {
   129             // convert 16CZK using c:
   130             ConversionResult result = convertSkkCzk.convertBack(new BigDecimal(16));
   131             assertEquals("Result is 20 SKK", new BigDecimal("20.00"), result.getConverted());
   132         }
   133 
   134         {
   135             // convert 500SKK to CZK
   136             ConversionResult result = convertSkkCzk.convert(new BigDecimal(500));
   137             assertEquals("Result is 400 CZK", new BigDecimal("400.00"), result.getConverted());
   138             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());            
   139         }
   140         
   141         {
   142             // convert 501SKK to CZK
   143             ConversionResult result = convertSkkCzk.convert(new BigDecimal(501));
   144             assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted());
   145             assertEquals("No Remainer", BigDecimal.ZERO, result.getRemainder());
   146             
   147         }
   148     }
   149     
   150     /**
   151      * Convert SKK to CZK. Convertor can't convert whole amout (can't convert one SKK cent to CZK). Remaining
   152      * amount is stored in remainder result.
   153      * 
   154      * Test is currently failing, because implementation can't handle this case.
   155      */
   156 //    public void testConvertSmallUnits_failing() {
   157 //        Convertor convertSkkCzk = createSKKtoCZK();
   158 //        {
   159 //            // convert 501SKK to CZK
   160 //            ConversionResult result = convertSkkCzk.convert(new BigDecimal("501.01"));
   161 //            assertEquals("Result is 400 CZK", new BigDecimal("400.80"), result.getConverted());
   162 //            assertEquals("No Remainer", new BigDecimal("0.01"), result.getRemainder());
   163 //            
   164 //        }
   165 //        
   166 //    }
   167     
   168     /**
   169      * Test converting from CZK to JPY. Remained has scale of CZK.
   170      * 
   171      * This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency.
   172      */
   173 //    public void testConvertCzkToJpy_failing() {
   174 //        Convertor convertSkkCzk = createCZKtoYEN();
   175 //        {
   176 //            // convert 501SKK to CZK
   177 //            ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00"));
   178 //            assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted());
   179 //            assertEquals("No Remainer", new BigDecimal("0.00"), result.getRemainder());
   180 //            
   181 //        }
   182 //    }
   183     
   184     /**
   185      * Test converting from JPY to CZK. Remained has scale of JPY.
   186      * 
   187      * This test is currently failing, because converter implementation currently can't handle conversion from "cent" to "no-cent" currency.
   188      */
   189 //    public void testConvertJpyToCzk_failing() {
   190 //        Convertor convertSkkCzk = createCZKtoYEN();
   191 //        {
   192 //            // convert 501SKK to CZK
   193 //            ConversionResult result = convertSkkCzk.convert(new BigDecimal("120.00"));
   194 //            assertEquals("Result is 120 YEN", new BigDecimal("120"), result.getConverted());
   195 //            assertEquals("No Remainer", new BigDecimal("0"), result.getRemainder());
   196 //            
   197 //        }
   198 //    }
   199     
   200     public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
   201             Convertor c = createCZKtoUSD();
   202             // convert $5 to SKK, the API shall say this is not possible
   203             // convert 500 SKK to CZK, the API shall say this is not possible
   204             // ... no api for required call, should be here?
   205     }
   206     
   207  
   208 }
   209