task3/solution04/test/org/apidesign/apifest08/test/Task3Test.java
changeset 55 14e78f48ac2b
parent 48 79a576394dd7
child 59 c1d43bc1e9c0
     1.1 --- a/task3/solution04/test/org/apidesign/apifest08/test/Task3Test.java	Tue Oct 07 11:19:36 2008 +0200
     1.2 +++ b/task3/solution04/test/org/apidesign/apifest08/test/Task3Test.java	Fri Oct 10 22:05:15 2008 +0200
     1.3 @@ -1,7 +1,11 @@
     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.InvalidConversionException;
    1.11 +import org.apidesign.apifest08.currency.OnlineConvertor;
    1.12  
    1.13  /** The exchange rates are not always the same. They are changing. Day by day,
    1.14   * hour by hour, minute by minute. For every bank it is important to always
    1.15 @@ -15,17 +19,33 @@
    1.16   * he does not know how the whole system looks and how the convertor is supposed
    1.17   * to be used.
    1.18   */
    1.19 -public class Task3Test extends TestCase {
    1.20 -    public Task3Test(String testName) {
    1.21 +public class Task3Test 
    1.22 +    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 Task3Test(String testName)
    1.36 +    {
    1.37          super(testName);
    1.38      }
    1.39  
    1.40      @Override
    1.41 -    protected void setUp() throws Exception {
    1.42 +    protected void setUp() throws Exception
    1.43 +    {
    1.44      }
    1.45  
    1.46      @Override
    1.47 -    protected void tearDown() throws Exception {
    1.48 +    protected void tearDown() throws Exception
    1.49 +    {
    1.50      }
    1.51  
    1.52      // Backward compatibly enhance your existing API to support following
    1.53 @@ -41,8 +61,21 @@
    1.54       * until you reach 1USD = 16CZK
    1.55       *
    1.56       * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
    1.57 +     * @throws InvalidConversionException 
    1.58       */
    1.59 -    public static Convertor createOnlineCZKUSDConvertor() {
    1.60 +    public static Convertor createOnlineCZKUSDConvertor() 
    1.61 +        throws InvalidConversionException
    1.62 +    {
    1.63 +        final Convertor convertor;
    1.64 +        
    1.65 +        convertor = new OnlineConvertor(USD,
    1.66 +                                        CZK,
    1.67 +                                        new TestExchangeRateFinder(new BigDecimal("15.00"), 
    1.68 +                                                                   new BigDecimal("16.00"),
    1.69 +                                                                   new BigDecimal("16.00"),
    1.70 +                                                                   new BigDecimal("0.01"),
    1.71 +                                                                   new BigDecimal("-0.01")));
    1.72 +
    1.73          // initial rate: 1USD = 16CZK
    1.74          // 2nd query 1USD = 15.99CZK
    1.75          // 3rd query 1USD = 15.98CZK
    1.76 @@ -51,10 +84,12 @@
    1.77          // then 1USD = 15.02CZK
    1.78          // and so on and on up to 1USD = 16CZK
    1.79          // and then another round to 15, etc.
    1.80 -        return null;
    1.81 +        return convertor;
    1.82      }
    1.83  
    1.84 -    public void testFewQueriesForOnlineConvertor() {
    1.85 +    public void testFewQueriesForOnlineConvertor() 
    1.86 +        throws InvalidConversionException
    1.87 +    {
    1.88          if (Boolean.getBoolean("ignore.failing")) {
    1.89              // implement me!
    1.90              return;
    1.91 @@ -64,25 +99,37 @@
    1.92          doFewQueriesForOnlineConvertor(c);
    1.93      }
    1.94  
    1.95 -    static void doFewQueriesForOnlineConvertor(Convertor c) {
    1.96 +    static void doFewQueriesForOnlineConvertor(Convertor c) 
    1.97 +        throws InvalidConversionException            
    1.98 +    {
    1.99 +        BigDecimal amount;
   1.100 +
   1.101          // convert $5 to CZK using c:
   1.102          //assertEquals("Result is 80 CZK");
   1.103 +        amount = c.convert(USD, CZK, new BigDecimal("5.00"));
   1.104 +        assertEquals(new BigDecimal("80.00"), amount);
   1.105  
   1.106          // convert $8 to CZK using c:
   1.107          //assertEquals("Result is 127.92 CZK");
   1.108 +        amount = c.convert(USD, CZK, new BigDecimal("8.00"));
   1.109 +        assertEquals(new BigDecimal("127.92"), amount);
   1.110  
   1.111          // convert $1 to CZK using c:
   1.112          //assertEquals("Result is 15.98 CZK");
   1.113 +        amount = c.convert(USD, CZK, new BigDecimal("1.00"));
   1.114 +        assertEquals(new BigDecimal("15.98"), amount);
   1.115  
   1.116          // convert 15.97CZK to USD using c:
   1.117          //assertEquals("Result is 1$");
   1.118 -
   1.119 -        fail("Implement me!");
   1.120 +        amount = c.convert(CZK, USD, new BigDecimal("15.97"));
   1.121 +        assertEquals(new BigDecimal("1.00"), amount);
   1.122      }
   1.123  
   1.124      /** Join the convertors and show they behave sane.
   1.125       */
   1.126      public void testOnlineConvertorComposition() throws Exception {
   1.127 +        BigDecimal amount;
   1.128 +        
   1.129          if (Boolean.getBoolean("ignore.failing")) {
   1.130              // implement me!
   1.131              return;
   1.132 @@ -95,9 +142,13 @@
   1.133  
   1.134          // convert 16CZK to SKK using c:
   1.135          // assertEquals("Result is 20 SKK");
   1.136 -
   1.137 +        amount = c.convert(CZK, SKK, new BigDecimal("16.00"));
   1.138 +        assertEquals(new BigDecimal("20.00"), amount);
   1.139 +        
   1.140          // convert 500SKK to CZK using c:
   1.141          // assertEquals("Result is 400 CZK");
   1.142 +        amount = c.convert(SKK, CZK, new BigDecimal("500.00"));
   1.143 +        assertEquals(new BigDecimal("400.00"), amount);
   1.144  
   1.145          doFewQueriesForOnlineConvertor(c);
   1.146      }