task1/solution04/test/org/apidesign/apifest08/test/Task1Test.java
changeset 17 37c9921c653e
parent 6 97662396c0fd
     1.1 --- a/task1/solution04/test/org/apidesign/apifest08/test/Task1Test.java	Sun Sep 28 14:12:38 2008 +0200
     1.2 +++ b/task1/solution04/test/org/apidesign/apifest08/test/Task1Test.java	Tue Sep 30 11:50:09 2008 +0200
     1.3 @@ -1,10 +1,13 @@
     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 junit.framework.TestCase;
    1.10  import org.apidesign.apifest08.currency.Convertor;
    1.11  import org.apidesign.apifest08.currency.ConvertorFactory;
    1.12 +import org.apidesign.apifest08.currency.InvalidConversionException;
    1.13 +
    1.14  
    1.15  /** Finish the Convertor API, and then write bodies of methods inside
    1.16   * of this class to match the given tasks. To fullfil your task, use the
    1.17 @@ -13,6 +16,18 @@
    1.18   * shall run without any runtime permissions.
    1.19   */
    1.20  public class Task1Test extends TestCase {
    1.21 +    
    1.22 +    private final static Currency CZK;
    1.23 +    private final static Currency SKK;
    1.24 +    private final static Currency USD;
    1.25 +
    1.26 +    static
    1.27 +    {
    1.28 +        CZK = Currency.getInstance("CZK");
    1.29 +        SKK = Currency.getInstance("SKK");
    1.30 +        USD = Currency.getInstance("USD");
    1.31 +    }
    1.32 +    
    1.33      public Task1Test(String testName) {
    1.34          super(testName);
    1.35      }
    1.36 @@ -35,7 +50,7 @@
    1.37       */
    1.38      public static Convertor createCZKtoUSD()
    1.39      {
    1.40 -        return (ConvertorFactory.getConvertor("CZK", "USD"));
    1.41 +        return (ConvertorFactory.getConvertor("CZK", BigDecimal.valueOf(17.0), "USD", BigDecimal.valueOf(1)));
    1.42      }
    1.43  
    1.44      /** Create convertor that understands two currencies, CZK and
    1.45 @@ -48,12 +63,7 @@
    1.46       */
    1.47      public static Convertor createSKKtoCZK()
    1.48      {
    1.49 -        return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), Currency.getInstance("CZK")));
    1.50 -    }
    1.51 -    
    1.52 -    public static Convertor createUSDtoUSD()
    1.53 -    {
    1.54 -        return (ConvertorFactory.getConvertor(Currency.getInstance("USD"), Currency.getInstance("USD")));
    1.55 +        return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), BigDecimal.valueOf(100), Currency.getInstance("CZK"), BigDecimal.valueOf(80)));
    1.56      }
    1.57      
    1.58      /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    1.59 @@ -65,17 +75,17 @@
    1.60          
    1.61          // convert $5 to CZK using c:
    1.62          // assertEquals("Result is 85 CZK");
    1.63 -        result = c.convertFrom(BigDecimal.valueOf(5));
    1.64 +        result = c.convert(USD, CZK, BigDecimal.valueOf(5));
    1.65          assertEquals(new BigDecimal("85.00"), result);
    1.66  
    1.67          // convert $8 to CZK
    1.68          // assertEquals("Result is 136 CZK");
    1.69 -        result = c.convertFrom(BigDecimal.valueOf(8));
    1.70 +        result = c.convert(USD, CZK, BigDecimal.valueOf(8));
    1.71          assertEquals(new BigDecimal("136.00"), result);
    1.72  
    1.73          // convert 1003CZK to USD
    1.74          // assertEquals("Result is 59 USD");
    1.75 -        result = c.convertTo(BigDecimal.valueOf(1003));
    1.76 +        result = c.convert(CZK, USD, BigDecimal.valueOf(1003));
    1.77          assertEquals(new BigDecimal("59.00"), result);
    1.78      }
    1.79  
    1.80 @@ -88,31 +98,87 @@
    1.81          
    1.82          // convert 16CZK using c:
    1.83          // assertEquals("Result is 20 SKK");
    1.84 -        result = c.convertFrom(BigDecimal.valueOf(16));
    1.85 +        result = c.convert(CZK, SKK, BigDecimal.valueOf(16));
    1.86          assertEquals(new BigDecimal("20.00"), result);
    1.87                          
    1.88          // convert 500SKK to CZK
    1.89          // assertEquals("Result is 400 CZK");
    1.90 -        result = c.convertTo(BigDecimal.valueOf(500));
    1.91 +        result = c.convert(SKK, CZK, BigDecimal.valueOf(500));
    1.92          assertEquals(new BigDecimal("400.00"), result);
    1.93     }
    1.94  
    1.95 -    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    1.96 -     * with it.
    1.97 +    /** 
    1.98 +     * Verify that the CZK to USD convertor knows nothing about SKK.
    1.99       */
   1.100 -    public void testCurrencyUSDUSD() throws Exception {
   1.101 -        Convertor c = createUSDtoUSD();
   1.102 -        BigDecimal result;
   1.103 +    public void testCannotConvertToSKKwithCZKUSDConvertor() 
   1.104 +        throws Exception 
   1.105 +    {
   1.106 +        Convertor c = createCZKtoUSD();
   1.107          
   1.108 -        // convert 1USD using c:
   1.109 -        // assertEquals("Result is 1 USD");
   1.110 -        result = c.convertFrom(BigDecimal.valueOf(1));
   1.111 -        assertEquals(new BigDecimal("1.00"), result);
   1.112 -                        
   1.113 -        // convert 500USD to USD
   1.114 -        // assertEquals("Result is 500 USD");
   1.115 -        result = c.convertTo(BigDecimal.valueOf(500));
   1.116 -        assertEquals(new BigDecimal("500.00"), result);
   1.117 -   }
   1.118 +        try
   1.119 +        {
   1.120 +            // convert $5 to SKK, the API shall say this is not possible
   1.121 +            c.convert(USD, SKK, BigDecimal.valueOf(5));
   1.122 +            fail("cannot use the CZKtoUSD converter to convert to SKK");
   1.123 +        }
   1.124 +        catch(InvalidConversionException ex)
   1.125 +        {       
   1.126 +            assertEquals("cannot convert to: SKK", ex.getMessage());
   1.127 +            assertEquals(SKK, ex.getBadCurrency());
   1.128 +            assertEquals(CZK, ex.getCurrencyA());
   1.129 +            assertEquals(USD, ex.getCurrencyB());
   1.130 +        }
   1.131 +
   1.132 +        try
   1.133 +        {
   1.134 +            // convert 500 SKK to CZK, the API shall say this is not possible
   1.135 +            c.convert(SKK, CZK, BigDecimal.valueOf(5));
   1.136 +            fail("cannot use the CZKtoUSD converter to convert from SKK");
   1.137 +        }
   1.138 +        catch(InvalidConversionException ex)
   1.139 +        {            
   1.140 +            assertEquals("cannot convert from: SKK", ex.getMessage());
   1.141 +            assertEquals(SKK, ex.getBadCurrency());
   1.142 +            assertEquals(CZK, ex.getCurrencyA());
   1.143 +            assertEquals(USD, ex.getCurrencyB());
   1.144 +        }
   1.145 +    }
   1.146 +    
   1.147 +    /** 
   1.148 +     * Verify that the CZK to SKK convertor knows nothing about USD.
   1.149 +     */
   1.150 +    public void testCannotConvertToUSDwithSKKCZKConvertor() 
   1.151 +        throws Exception 
   1.152 +    {
   1.153 +        Convertor c = createSKKtoCZK();
   1.154 +        
   1.155 +        try
   1.156 +        {
   1.157 +            // convert $5 to SKK, the API shall say this is not possible
   1.158 +            c.convert(USD, SKK, BigDecimal.valueOf(5));
   1.159 +            fail("cannot use the CZKtoUSD converter to convert to SKK");
   1.160 +        }
   1.161 +        catch(InvalidConversionException ex)
   1.162 +        {       
   1.163 +            assertEquals("cannot convert from: USD", ex.getMessage());
   1.164 +            assertEquals(USD, ex.getBadCurrency());
   1.165 +            assertEquals(SKK, ex.getCurrencyA());
   1.166 +            assertEquals(CZK, ex.getCurrencyB());
   1.167 +        }
   1.168 +
   1.169 +        try
   1.170 +        {
   1.171 +            // convert 500 CZK to USD, the API shall say this is not possible
   1.172 +            c.convert(CZK, USD, BigDecimal.valueOf(500));
   1.173 +            fail("cannot use the CZKtoUSD converter to convert from SKK");
   1.174 +        }
   1.175 +        catch(InvalidConversionException ex)
   1.176 +        {            
   1.177 +            assertEquals("cannot convert to: USD", ex.getMessage());
   1.178 +            assertEquals(USD, ex.getBadCurrency());
   1.179 +            assertEquals(SKK, ex.getCurrencyA());
   1.180 +            assertEquals(CZK, ex.getCurrencyB());
   1.181 +        }
   1.182 +    }
   1.183  }
   1.184