task4/solution06/test/org/apidesign/apifest08/test/Task4Test.java
changeset 64 51f7b894eba6
parent 62 f711ecd374f3
child 71 978f6a78c22e
     1.1 --- a/task4/solution06/test/org/apidesign/apifest08/test/Task4Test.java	Sat Oct 11 23:46:05 2008 +0200
     1.2 +++ b/task4/solution06/test/org/apidesign/apifest08/test/Task4Test.java	Fri Oct 17 17:32:34 2008 +0200
     1.3 @@ -1,8 +1,13 @@
     1.4  package org.apidesign.apifest08.test;
     1.5  
     1.6 +import java.math.BigDecimal;
     1.7 +import java.text.SimpleDateFormat;
     1.8  import java.util.Date;
     1.9  import junit.framework.TestCase;
    1.10  import org.apidesign.apifest08.currency.Convertor;
    1.11 +import org.apidesign.apifest08.currency.UnsupportedConversionException;
    1.12 +
    1.13 +import static org.apidesign.apifest08.test.Currencies.*;
    1.14  
    1.15  /** The exchange rates are not always the same. They are changing. However
    1.16   * as in order to predict the future, one needs to understand own past. That is
    1.17 @@ -45,7 +50,7 @@
    1.18       * @return new convertor
    1.19       */
    1.20      public static Convertor limitTo(Convertor old, Date from, Date till) {
    1.21 -        return null;
    1.22 +        return new Convertor(old, from, till);
    1.23      }
    1.24  
    1.25  
    1.26 @@ -54,10 +59,11 @@
    1.27              // implement me! then delete this if statement
    1.28              return;
    1.29          }
    1.30 -
    1.31 -        Date d1 = null; // 2008-10-01 0:00 GMT
    1.32 -        Date d2 = null; // 2008-10-02 0:00 GMT
    1.33 -        Date d3 = null; // 2008-10-03 0:00 GMT
    1.34 +        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm zzzz");
    1.35 +        
    1.36 +        Date d1 = df.parse("2008-10-01 0:00 GMT"); 
    1.37 +        Date d2 = df.parse("2008-10-02 0:00 GMT"); 
    1.38 +        Date d3 = df.parse("2008-10-03 0:00 GMT");
    1.39          
    1.40          Convertor c = Task2Test.merge(
    1.41              limitTo(Task1Test.createCZKtoUSD(), d1, d2),
    1.42 @@ -65,37 +71,67 @@
    1.43          );
    1.44  
    1.45          // convert $5 to CZK using c:
    1.46 -        // cannot convert as no rate is applicable to current date
    1.47 +        try {
    1.48 +        	c.convert(new BigDecimal(5), USD , CZK);
    1.49 +        	fail("cannot convert as no rate is applicable to current date");
    1.50 +        } catch(UnsupportedConversionException e) {
    1.51 +        	//expected
    1.52 +        }
    1.53  
    1.54          // convert $8 to CZK using c:
    1.55 -        // cannot convert as no rate is applicable to current date
    1.56 +        try {
    1.57 +        	c.convert(new BigDecimal(8), USD , CZK);
    1.58 +        	fail("cannot convert as no rate is applicable to current date");
    1.59 +        } catch(UnsupportedConversionException e) {
    1.60 +        	//expected
    1.61 +        }
    1.62  
    1.63          // convert 1003CZK to USD using c:
    1.64 -        // cannot convert as no rate is applicable to current date
    1.65 +        try {
    1.66 +        	c.convert(new BigDecimal(1003), CZK, USD);
    1.67 +        	fail("cannot convert as no rate is applicable to current date");
    1.68 +        } catch(UnsupportedConversionException e) {
    1.69 +        	//expected
    1.70 +        }
    1.71  
    1.72          // convert 16CZK using c:
    1.73 -        // cannot convert as no rate is applicable to current date
    1.74 +        try {
    1.75 +        	c.convert(new BigDecimal(16), CZK, USD);
    1.76 +        	fail("cannot convert as no rate is applicable to current date");
    1.77 +        } catch(UnsupportedConversionException e) {
    1.78 +        	//expected
    1.79 +        }
    1.80  
    1.81          // convert 500SKK to CZK using c:
    1.82 -        // cannot convert as no rate is applicable to current date
    1.83 +        try {
    1.84 +        	c.convert(new BigDecimal(500), SKK, CZK);
    1.85 +        	fail("cannot convert as no rate is applicable to current date");
    1.86 +        } catch(UnsupportedConversionException e) {
    1.87 +        	//expected
    1.88 +        }
    1.89  
    1.90 -        // convert $5 to CZK using c at 2008-10-01 6:00 GMT:
    1.91 -        // assertEquals("Result is 85 CZK");
    1.92 +        // convert $5 to CZK using c at 2008-10-01 6:00 GMT:         
    1.93 +        assertEquals("Result is 85 CZK", 85, c.convert(new BigDecimal(5), USD, CZK, df.parse("2008-10-01 6:00 GMT")).getValue().intValue());
    1.94  
    1.95          // convert $8 to CZK using c at 2008-10-01 6:00 GMT:
    1.96 -        // assertEquals("Result is 136 CZK");
    1.97 -
    1.98 +        assertEquals("Result is 136 CZK", 136, c.convert(new BigDecimal(8), USD, CZK, df.parse("2008-10-01 6:00 GMT")).getValue().intValue());
    1.99 +     
   1.100          // convert 1003CZK to USD using c at 2008-10-01 6:00 GMT:
   1.101 -        // assertEquals("Result is 59 USD");
   1.102 +        assertEquals("Result is 59 USD", 59, c.convert(new BigDecimal(1003), CZK, USD, df.parse("2008-10-01 6:00 GMT")).getValue().intValue());
   1.103  
   1.104          // convert 16CZK using c at 2008-10-02 9:00 GMT:
   1.105 -        // assertEquals("Result is 20 SKK");
   1.106 +        assertEquals("Result is 20 SKK", 20, c.convert(new BigDecimal(16), CZK, SKK, df.parse("2008-10-02 9:00 GMT")).getValue().intValue());
   1.107  
   1.108          // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
   1.109 -        // assertEquals("Result is 400 CZK");
   1.110 +        assertEquals("Result is 400 CZK", 400, c.convert(new BigDecimal(500), SKK, CZK, df.parse("2008-10-02 9:00 GMT")).getValue().intValue());
   1.111  
   1.112          // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   1.113 -        // cannot convert as no rate is applicable to current date
   1.114 +        try {
   1.115 +        	c.convert(new BigDecimal(500), SKK, CZK, df.parse("2008-10-01 6:00 GMT"));
   1.116 +        	fail("cannot convert as no rate is applicable to current date");
   1.117 +        } catch(UnsupportedConversionException e) {
   1.118 +        	//expected
   1.119 +        }
   1.120      }
   1.121  
   1.122      /** Create convertor that understands two currencies, CZK and
   1.123 @@ -104,7 +140,7 @@
   1.124       * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
   1.125       */
   1.126      public static Convertor createSKKtoCZK2() {
   1.127 -        return null;
   1.128 +    	return new Convertor(new BigDecimal("0.9"), SKK, CZK);
   1.129      }
   1.130  
   1.131      public void testDateConvetorWithTwoDifferentRates() throws Exception {
   1.132 @@ -112,10 +148,11 @@
   1.133              // implement me! then delete this if statement
   1.134              return;
   1.135          }
   1.136 -
   1.137 -        Date d1 = null; // 2008-10-01 0:00 GMT
   1.138 -        Date d2 = null; // 2008-10-02 0:00 GMT
   1.139 -        Date d3 = null; // 2008-10-03 0:00 GMT
   1.140 +        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm zzzz");
   1.141 +        
   1.142 +        Date d1 = df.parse("2008-10-01 0:00 GMT"); 
   1.143 +        Date d2 = df.parse("2008-10-02 0:00 GMT"); 
   1.144 +        Date d3 = df.parse("2008-10-03 0:00 GMT");
   1.145  
   1.146          Convertor c = Task2Test.merge(
   1.147              limitTo(createSKKtoCZK2(), d1, d2),
   1.148 @@ -123,10 +160,10 @@
   1.149          );
   1.150  
   1.151          // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
   1.152 -        // assertEquals("Result is 400 CZK");
   1.153 +        assertEquals("Result is 400 CZK", 400, c.convert(new BigDecimal(500), SKK, CZK, df.parse("2008-10-02 9:00 GMT")).getValue().intValue());
   1.154  
   1.155          // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   1.156 -        // assertEquals("Result is 450 CZK");
   1.157 +        assertEquals("Result is 450 CZK", 450, c.convert(new BigDecimal(500), SKK, CZK, df.parse("2008-10-01 6:00 GMT")).getValue().intValue());
   1.158      }
   1.159  
   1.160  }