task1/solution06/test/org/apidesign/apifest08/test/Task1Test.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
child 21 61e4c4c120fd
permissions -rw-r--r--
Adding solutions received for task1
     1 package org.apidesign.apifest08.test;
     2 
     3 import static org.apidesign.apifest08.currency.Currencies.CZK;
     4 import static org.apidesign.apifest08.currency.Currencies.SKK;
     5 import static org.apidesign.apifest08.currency.Currencies.USD;
     6 
     7 import java.math.BigDecimal;
     8 
     9 import junit.framework.TestCase;
    10 
    11 import org.apidesign.apifest08.currency.Amount;
    12 import org.apidesign.apifest08.currency.Convertor;
    13 import org.apidesign.apifest08.currency.ConvertorFactory;
    14 import org.apidesign.apifest08.currency.UnsupportedConversionException;
    15 
    16 /** Finish the Convertor API, and then write bodies of methods inside
    17  * of this class to match the given tasks. To fullfil your task, use the
    18  * API define in the <code>org.apidesign.apifest08.currency</code> package.
    19  * Do not you reflection, or other hacks as your code
    20  * shall run without any runtime permissions.
    21  */
    22 public class Task1Test extends TestCase {
    23     public Task1Test(String testName) {
    24         super(testName);
    25     }
    26 
    27     @Override
    28     protected void setUp() throws Exception {
    29     }
    30 
    31     @Override
    32     protected void tearDown() throws Exception {
    33     }
    34 
    35     /** Create convertor that understands two currencies, CZK and
    36      *  USD. Make 1 USD == 17 CZK.
    37      *
    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() {
    44         return ConvertorFactory.newInstance();
    45     }
    46 
    47     /** Create convertor that understands two currencies, CZK and
    48      *  SKK. Make 100 SKK == 80 CZK.
    49      *
    50      * Creation of the convertor shall not require subclassing of any class
    51      * or interface on the client side.
    52      * 
    53      * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    54      */
    55     public static Convertor createSKKtoCZK() {
    56     	return ConvertorFactory.newInstance();
    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         // convert $5 to CZK using c:
    65         Amount result =  c.convert(new BigDecimal(5), USD, CZK);
    66         assertEquals("Result is 85 CZK", 85, result.getValue().intValue());
    67 
    68         // convert $8 to CZK
    69         result =  c.convert(new BigDecimal(8), USD, CZK);        
    70         assertEquals("Result is 136 CZK", 136, result.getValue().intValue());
    71 
    72         // convert 1003CZK to USD
    73         result =  c.convert(new BigDecimal(1003), CZK, USD);
    74         assertEquals("Result is 59 USD", 59, result.getValue().intValue());
    75     }
    76 
    77     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    78      * with it.
    79      */
    80     public void testCurrencySKKCZK() throws Exception {
    81         Convertor c = createSKKtoCZK();
    82         // convert 16CZK using c:
    83         Amount result =  c.convert(new BigDecimal(16), CZK, SKK);
    84         assertEquals("Result is 20 SKK", 20, result.getValue().intValue());
    85 
    86         // convert 500SKK to CZK
    87         result =  c.convert(new BigDecimal(500), SKK, CZK);
    88         assertEquals("Result is 400 CZK", 400, result.getValue().intValue());
    89     }
    90     
    91     public void testUnssuportedConversion(){
    92     	Convertor c = ConvertorFactory.newInstance();
    93     	try {
    94     		c.convert(new BigDecimal(5), USD, SKK);
    95     		fail();
    96     	} catch(UnsupportedConversionException e) {
    97     		//expected    		
    98     	}    	
    99     }
   100 }
   101