task4/solution04/test/org/apidesign/apifest08/test/Task1Test.java
changeset 61 58ec6da75f6f
parent 45 251d0ed461fb
child 69 420baec87dc5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution04/test/org/apidesign/apifest08/test/Task1Test.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,223 @@
     1.4 +package org.apidesign.apifest08.test;
     1.5 +
     1.6 +
     1.7 +import java.math.BigDecimal;
     1.8 +import java.util.Currency;
     1.9 +import java.util.Set;
    1.10 +import junit.framework.TestCase;
    1.11 +import org.apidesign.apifest08.currency.Convertor;
    1.12 +import org.apidesign.apifest08.currency.ConvertorFactory;
    1.13 +import org.apidesign.apifest08.currency.InvalidConversionException;
    1.14 +
    1.15 +
    1.16 +/** Finish the Convertor API, and then write bodies of methods inside
    1.17 + * of this class to match the given tasks. To fullfil your task, use the
    1.18 + * API define in the <code>org.apidesign.apifest08.currency</code> package.
    1.19 + * Do not you reflection, or other hacks as your code
    1.20 + * shall run without any runtime permissions.
    1.21 + */
    1.22 +public class Task1Test extends TestCase {
    1.23 +    
    1.24 +    private final static Currency CZK;
    1.25 +    private final static Currency SKK;
    1.26 +    private final static Currency USD;
    1.27 +
    1.28 +    static
    1.29 +    {
    1.30 +        CZK = Currency.getInstance("CZK");
    1.31 +        SKK = Currency.getInstance("SKK");
    1.32 +        USD = Currency.getInstance("USD");
    1.33 +    }
    1.34 +    
    1.35 +    public Task1Test(String testName) {
    1.36 +        super(testName);
    1.37 +    }
    1.38 +
    1.39 +    @Override
    1.40 +    protected void setUp() throws Exception {
    1.41 +    }
    1.42 +
    1.43 +    @Override
    1.44 +    protected void tearDown() throws Exception {
    1.45 +    }
    1.46 +
    1.47 +    /** Create convertor that understands two currencies, CZK and
    1.48 +     *  USD. Make 1 USD == 17 CZK.
    1.49 +     *
    1.50 +     * Creation of the convertor shall not require subclassing of any class
    1.51 +     * or interface on the client side.
    1.52 +     *
    1.53 +     * @return prepared convertor ready for converting USD to CZK and CZK to USD
    1.54 +     */
    1.55 +    public static Convertor createCZKtoUSD()
    1.56 +    {
    1.57 +        return (ConvertorFactory.getConvertor("CZK", BigDecimal.valueOf(17.0),
    1.58 +                                              "USD", BigDecimal.valueOf(1)));
    1.59 +    }
    1.60 +
    1.61 +    /** Create convertor that understands two currencies, CZK and
    1.62 +     *  SKK. Make 100 SKK == 80 CZK.
    1.63 +     *
    1.64 +     * Creation of the convertor shall not require subclassing of any class
    1.65 +     * or interface on the client side.
    1.66 +     * 
    1.67 +     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    1.68 +     */
    1.69 +    public static Convertor createSKKtoCZK()
    1.70 +    {
    1.71 +        return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), BigDecimal.valueOf(100),
    1.72 +                                              Currency.getInstance("CZK"), BigDecimal.valueOf(80)));
    1.73 +    }
    1.74 +    
    1.75 +    /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    1.76 +     * with it.
    1.77 +     */
    1.78 +    public void testCurrencyCZKUSD() throws Exception {
    1.79 +        Convertor  c = createCZKtoUSD();
    1.80 +        BigDecimal result;
    1.81 +        
    1.82 +        // convert $5 to CZK using c:
    1.83 +        // assertEquals("Result is 85 CZK");
    1.84 +        result = c.convert(USD, CZK, BigDecimal.valueOf(5));
    1.85 +        assertEquals(new BigDecimal("85.00"), result);
    1.86 +
    1.87 +        // convert $8 to CZK
    1.88 +        // assertEquals("Result is 136 CZK");
    1.89 +        result = c.convert(USD, CZK, BigDecimal.valueOf(8));
    1.90 +        assertEquals(new BigDecimal("136.00"), result);
    1.91 +
    1.92 +        // convert 1003CZK to USD
    1.93 +        // assertEquals("Result is 59 USD");
    1.94 +        result = c.convert(CZK, USD, BigDecimal.valueOf(1003));
    1.95 +        assertEquals(new BigDecimal("59.00"), result);
    1.96 +    }
    1.97 +
    1.98 +    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    1.99 +     * with it.
   1.100 +     */
   1.101 +    public void testCurrencySKKCZK() throws Exception {
   1.102 +        Convertor c = createSKKtoCZK();
   1.103 +        BigDecimal result;
   1.104 +        
   1.105 +        // convert 16CZK using c:
   1.106 +        // assertEquals("Result is 20 SKK");
   1.107 +        result = c.convert(CZK, SKK, BigDecimal.valueOf(16));
   1.108 +        assertEquals(new BigDecimal("20.00"), result);
   1.109 +                        
   1.110 +        // convert 500SKK to CZK
   1.111 +        // assertEquals("Result is 400 CZK");
   1.112 +        result = c.convert(SKK, CZK, BigDecimal.valueOf(500));
   1.113 +        assertEquals(new BigDecimal("400.00"), result);
   1.114 +   }
   1.115 +
   1.116 +    /** 
   1.117 +     * Verify that the CZK to USD convertor knows nothing about SKK.
   1.118 +     */
   1.119 +    public void testCannotConvertToSKKwithCZKUSDConvertor() 
   1.120 +        throws Exception 
   1.121 +    {
   1.122 +        Convertor c = createCZKtoUSD();
   1.123 +        
   1.124 +        try
   1.125 +        {
   1.126 +            // convert $5 to SKK, the API shall say this is not possible
   1.127 +            c.convert(USD, SKK, BigDecimal.valueOf(5));
   1.128 +            fail("cannot use the CZKtoUSD converter to convert to SKK");
   1.129 +        }
   1.130 +        catch(InvalidConversionException ex)
   1.131 +        {       
   1.132 +            assertEquals("cannot convert to: SKK", ex.getMessage());
   1.133 +            assertEquals(SKK, ex.getBadCurrency());
   1.134 +            assertEquals(CZK, ex.getCurrencyA());
   1.135 +            assertEquals(USD, ex.getCurrencyB());
   1.136 +        }
   1.137 +
   1.138 +        try
   1.139 +        {
   1.140 +            // convert 500 SKK to CZK, the API shall say this is not possible
   1.141 +            c.convert(SKK, CZK, BigDecimal.valueOf(5));
   1.142 +            fail("cannot use the CZKtoUSD converter to convert from SKK");
   1.143 +        }
   1.144 +        catch(InvalidConversionException ex)
   1.145 +        {            
   1.146 +            assertEquals("cannot convert from: SKK", ex.getMessage());
   1.147 +            assertEquals(SKK, ex.getBadCurrency());
   1.148 +            assertEquals(CZK, ex.getCurrencyA());
   1.149 +            assertEquals(USD, ex.getCurrencyB());
   1.150 +        }
   1.151 +    }
   1.152 +    
   1.153 +    /** 
   1.154 +     * Verify that the CZK to SKK convertor knows nothing about USD.
   1.155 +     */
   1.156 +    public void testCannotConvertToUSDwithSKKCZKConvertor() 
   1.157 +        throws Exception 
   1.158 +    {
   1.159 +        Convertor c = createSKKtoCZK();
   1.160 +        
   1.161 +        try
   1.162 +        {
   1.163 +            // convert $5 to SKK, the API shall say this is not possible
   1.164 +            c.convert(USD, SKK, BigDecimal.valueOf(5));
   1.165 +            fail("cannot use the CZKtoUSD converter to convert to SKK");
   1.166 +        }
   1.167 +        catch(InvalidConversionException ex)
   1.168 +        {       
   1.169 +            assertEquals("cannot convert from: USD", ex.getMessage());
   1.170 +            assertEquals(USD, ex.getBadCurrency());
   1.171 +            assertEquals(SKK, ex.getCurrencyA());
   1.172 +            assertEquals(CZK, ex.getCurrencyB());
   1.173 +        }
   1.174 +
   1.175 +        try
   1.176 +        {
   1.177 +            // convert 500 CZK to USD, the API shall say this is not possible
   1.178 +            c.convert(CZK, USD, BigDecimal.valueOf(500));
   1.179 +            fail("cannot use the CZKtoUSD converter to convert from SKK");
   1.180 +        }
   1.181 +        catch(InvalidConversionException ex)
   1.182 +        {            
   1.183 +            assertEquals("cannot convert to: USD", ex.getMessage());
   1.184 +            assertEquals(USD, ex.getBadCurrency());
   1.185 +            assertEquals(SKK, ex.getCurrencyA());
   1.186 +            assertEquals(CZK, ex.getCurrencyB());
   1.187 +        }
   1.188 +    }
   1.189 +    
   1.190 +    public void testGetCurrencies()
   1.191 +    {
   1.192 +        Convertor           c;
   1.193 +        Set<Currency> currencies;
   1.194 +        
   1.195 +        c          = createSKKtoCZK();
   1.196 +        currencies = c.getCurrencies();        
   1.197 +        assertEquals(2, currencies.size());
   1.198 +        assertTrue(currencies.contains(Currency.getInstance("SKK")));
   1.199 +        assertTrue(currencies.contains(Currency.getInstance("CZK")));
   1.200 +
   1.201 +        c          = createCZKtoUSD();
   1.202 +        currencies = c.getCurrencies();        
   1.203 +        assertEquals(2, currencies.size());
   1.204 +        assertTrue(currencies.contains(Currency.getInstance("USD")));
   1.205 +        assertTrue(currencies.contains(Currency.getInstance("CZK")));
   1.206 +    }
   1.207 +    
   1.208 +    public void testGetConverstionRate()
   1.209 +        throws InvalidConversionException
   1.210 +    {
   1.211 +        Convertor c;
   1.212 +       
   1.213 +        c = createSKKtoCZK();
   1.214 +        assertEquals(1.0,  c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("CZK")).doubleValue());
   1.215 +        assertEquals(1.0,  c.getConversionRate(Currency.getInstance("SKK"), Currency.getInstance("SKK")).doubleValue());
   1.216 +        assertEquals(0.80, c.getConversionRate(Currency.getInstance("SKK"), Currency.getInstance("CZK")).doubleValue());
   1.217 +        assertEquals(1.25, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("SKK")).doubleValue());
   1.218 +
   1.219 +        c = createCZKtoUSD();
   1.220 +        assertEquals(1.0,      c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("CZK")).doubleValue());
   1.221 +        assertEquals(1.0,      c.getConversionRate(Currency.getInstance("USD"), Currency.getInstance("USD")).doubleValue());
   1.222 +        assertEquals(1.0/17.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("USD")).doubleValue(), 0.00000000000000001);
   1.223 +        assertEquals(17.0,     c.getConversionRate(Currency.getInstance("USD"), Currency.getInstance("CZK")).doubleValue(), 0.00000000000000001);
   1.224 +    }
   1.225 +}
   1.226 +