task1/solution04/test/org/apidesign/apifest08/test/Task1Test.java
changeset 6 97662396c0fd
child 17 37c9921c653e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task1/solution04/test/org/apidesign/apifest08/test/Task1Test.java	Sun Sep 28 14:12:38 2008 +0200
     1.3 @@ -0,0 +1,118 @@
     1.4 +package org.apidesign.apifest08.test;
     1.5 +
     1.6 +import java.math.BigDecimal;
     1.7 +import java.util.Currency;
     1.8 +import junit.framework.TestCase;
     1.9 +import org.apidesign.apifest08.currency.Convertor;
    1.10 +import org.apidesign.apifest08.currency.ConvertorFactory;
    1.11 +
    1.12 +/** Finish the Convertor API, and then write bodies of methods inside
    1.13 + * of this class to match the given tasks. To fullfil your task, use the
    1.14 + * API define in the <code>org.apidesign.apifest08.currency</code> package.
    1.15 + * Do not you reflection, or other hacks as your code
    1.16 + * shall run without any runtime permissions.
    1.17 + */
    1.18 +public class Task1Test extends TestCase {
    1.19 +    public Task1Test(String testName) {
    1.20 +        super(testName);
    1.21 +    }
    1.22 +
    1.23 +    @Override
    1.24 +    protected void setUp() throws Exception {
    1.25 +    }
    1.26 +
    1.27 +    @Override
    1.28 +    protected void tearDown() throws Exception {
    1.29 +    }
    1.30 +
    1.31 +    /** Create convertor that understands two currencies, CZK and
    1.32 +     *  USD. Make 1 USD == 17 CZK.
    1.33 +     *
    1.34 +     * Creation of the convertor shall not require subclassing of any class
    1.35 +     * or interface on the client side.
    1.36 +     *
    1.37 +     * @return prepared convertor ready for converting USD to CZK and CZK to USD
    1.38 +     */
    1.39 +    public static Convertor createCZKtoUSD()
    1.40 +    {
    1.41 +        return (ConvertorFactory.getConvertor("CZK", "USD"));
    1.42 +    }
    1.43 +
    1.44 +    /** Create convertor that understands two currencies, CZK and
    1.45 +     *  SKK. Make 100 SKK == 80 CZK.
    1.46 +     *
    1.47 +     * Creation of the convertor shall not require subclassing of any class
    1.48 +     * or interface on the client side.
    1.49 +     * 
    1.50 +     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    1.51 +     */
    1.52 +    public static Convertor createSKKtoCZK()
    1.53 +    {
    1.54 +        return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), Currency.getInstance("CZK")));
    1.55 +    }
    1.56 +    
    1.57 +    public static Convertor createUSDtoUSD()
    1.58 +    {
    1.59 +        return (ConvertorFactory.getConvertor(Currency.getInstance("USD"), Currency.getInstance("USD")));
    1.60 +    }
    1.61 +    
    1.62 +    /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    1.63 +     * with it.
    1.64 +     */
    1.65 +    public void testCurrencyCZKUSD() throws Exception {
    1.66 +        Convertor  c = createCZKtoUSD();
    1.67 +        BigDecimal result;
    1.68 +        
    1.69 +        // convert $5 to CZK using c:
    1.70 +        // assertEquals("Result is 85 CZK");
    1.71 +        result = c.convertFrom(BigDecimal.valueOf(5));
    1.72 +        assertEquals(new BigDecimal("85.00"), result);
    1.73 +
    1.74 +        // convert $8 to CZK
    1.75 +        // assertEquals("Result is 136 CZK");
    1.76 +        result = c.convertFrom(BigDecimal.valueOf(8));
    1.77 +        assertEquals(new BigDecimal("136.00"), result);
    1.78 +
    1.79 +        // convert 1003CZK to USD
    1.80 +        // assertEquals("Result is 59 USD");
    1.81 +        result = c.convertTo(BigDecimal.valueOf(1003));
    1.82 +        assertEquals(new BigDecimal("59.00"), result);
    1.83 +    }
    1.84 +
    1.85 +    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    1.86 +     * with it.
    1.87 +     */
    1.88 +    public void testCurrencySKKCZK() throws Exception {
    1.89 +        Convertor c = createSKKtoCZK();
    1.90 +        BigDecimal result;
    1.91 +        
    1.92 +        // convert 16CZK using c:
    1.93 +        // assertEquals("Result is 20 SKK");
    1.94 +        result = c.convertFrom(BigDecimal.valueOf(16));
    1.95 +        assertEquals(new BigDecimal("20.00"), result);
    1.96 +                        
    1.97 +        // convert 500SKK to CZK
    1.98 +        // assertEquals("Result is 400 CZK");
    1.99 +        result = c.convertTo(BigDecimal.valueOf(500));
   1.100 +        assertEquals(new BigDecimal("400.00"), result);
   1.101 +   }
   1.102 +
   1.103 +    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
   1.104 +     * with it.
   1.105 +     */
   1.106 +    public void testCurrencyUSDUSD() throws Exception {
   1.107 +        Convertor c = createUSDtoUSD();
   1.108 +        BigDecimal result;
   1.109 +        
   1.110 +        // convert 1USD using c:
   1.111 +        // assertEquals("Result is 1 USD");
   1.112 +        result = c.convertFrom(BigDecimal.valueOf(1));
   1.113 +        assertEquals(new BigDecimal("1.00"), result);
   1.114 +                        
   1.115 +        // convert 500USD to USD
   1.116 +        // assertEquals("Result is 500 USD");
   1.117 +        result = c.convertTo(BigDecimal.valueOf(500));
   1.118 +        assertEquals(new BigDecimal("500.00"), result);
   1.119 +   }
   1.120 +}
   1.121 +