task4/solution12/test/org/apidesign/apifest08/test/Task4Test.java
changeset 68 4de3a4b5445a
parent 62 f711ecd374f3
     1.1 --- a/task4/solution12/test/org/apidesign/apifest08/test/Task4Test.java	Sat Oct 11 23:46:05 2008 +0200
     1.2 +++ b/task4/solution12/test/org/apidesign/apifest08/test/Task4Test.java	Fri Oct 17 17:39:18 2008 +0200
     1.3 @@ -1,8 +1,11 @@
     1.4  package org.apidesign.apifest08.test;
     1.5  
     1.6 +import java.text.SimpleDateFormat;
     1.7 +import java.util.Currency;
     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.exceptions.UnknownConvertorException;
    1.12  
    1.13  /** The exchange rates are not always the same. They are changing. However
    1.14   * as in order to predict the future, one needs to understand own past. That is
    1.15 @@ -45,57 +48,104 @@
    1.16       * @return new convertor
    1.17       */
    1.18      public static Convertor limitTo(Convertor old, Date from, Date till) {
    1.19 -        return null;
    1.20 +        return Convertor.limitExchangeRatesValidity(old, from, till);
    1.21      }
    1.22  
    1.23  
    1.24      public void testCompositionOfLimitedConvertors() throws Exception {
    1.25 -        if (Boolean.getBoolean("ignore.failing")) {
    1.26 -            // implement me! then delete this if statement
    1.27 -            return;
    1.28 -        }
    1.29 -
    1.30 -        Date d1 = null; // 2008-10-01 0:00 GMT
    1.31 -        Date d2 = null; // 2008-10-02 0:00 GMT
    1.32 -        Date d3 = null; // 2008-10-03 0:00 GMT
    1.33 +        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz");
    1.34 +        Date d1 = format.parse("2008-10-01 00:00 GMT"); // 2008-10-01 0:00 GMT
    1.35 +        Date d2 = format.parse("2008-10-02 00:00 GMT"); // 2008-10-02 0:00 GMT
    1.36 +        Date d3 = format.parse("2008-10-03 00:00 GMT"); // 2008-10-03 0:00 GMT
    1.37          
    1.38          Convertor c = Task2Test.merge(
    1.39              limitTo(Task1Test.createCZKtoUSD(), d1, d2),
    1.40              limitTo(Task1Test.createSKKtoCZK(), d2, d3)
    1.41          );
    1.42 +        
    1.43 +        Date now = new Date();
    1.44  
    1.45          // convert $5 to CZK using c:
    1.46 -        // cannot convert as no rate is applicable to current date
    1.47 +        double result = 0;
    1.48 +        boolean exceptionThrown = false;
    1.49 +        try {
    1.50 +          result = c.convert(5, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    1.51 +        } catch (Exception e) {
    1.52 +          exceptionThrown = true;
    1.53 +        }
    1.54 +        
    1.55 +        if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
    1.56 +          System.out.println("Result.");
    1.57 +          assertEquals("Result is not 85 CZK.", 85d, result);          
    1.58 +        } else {
    1.59 +          System.out.println("Exception.");
    1.60 +          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
    1.61 +        }
    1.62  
    1.63          // convert $8 to CZK using c:
    1.64 -        // cannot convert as no rate is applicable to current date
    1.65 +        exceptionThrown = false;
    1.66 +        try {
    1.67 +          result = c.convert(8, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    1.68 +        } catch (Exception e) {
    1.69 +          exceptionThrown = true;
    1.70 +        }
    1.71 +        
    1.72 +        if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
    1.73 +          System.out.println("Result.");
    1.74 +          assertEquals("Result is not 136 CZK.", 136d, result);          
    1.75 +        } else {
    1.76 +          System.out.println("Exception.");
    1.77 +          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
    1.78 +        }
    1.79  
    1.80          // convert 1003CZK to USD using c:
    1.81 -        // cannot convert as no rate is applicable to current date
    1.82 +        exceptionThrown = false;
    1.83 +        try {
    1.84 +          result = c.convert(1003, Currency.getInstance("CZK"), Currency.getInstance("USD"));
    1.85 +        } catch (Exception e) {
    1.86 +          exceptionThrown = true;
    1.87 +        }
    1.88 +        
    1.89 +        if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
    1.90 +          System.out.println("Result.");
    1.91 +          assertEquals("Result is not 59 USD.", 59d, result);
    1.92 +        } else {
    1.93 +          System.out.println("Exception.");
    1.94 +          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
    1.95 +        }
    1.96  
    1.97          // convert 16CZK using c:
    1.98 -        // cannot convert as no rate is applicable to current date
    1.99 +        exceptionThrown = false;
   1.100 +        try {
   1.101 +          result = c.convert(16, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
   1.102 +        } catch (Exception e) {
   1.103 +          exceptionThrown = true;
   1.104 +        }
   1.105 +        
   1.106 +        if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
   1.107 +          System.out.println("Result.");
   1.108 +          assertEquals("Result is not 20 SKK.", 20d, result);          
   1.109 +        } else {
   1.110 +          System.out.println("Exception.");
   1.111 +          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   1.112 +        }
   1.113  
   1.114          // convert 500SKK to CZK using c:
   1.115 -        // cannot convert as no rate is applicable to current date
   1.116 +        exceptionThrown = false;
   1.117 +        try {
   1.118 +          result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   1.119 +        } catch (Exception e) {
   1.120 +          exceptionThrown = true;
   1.121 +        }
   1.122 +        
   1.123 +        if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
   1.124 +          System.out.println("Result.");
   1.125 +          assertEquals("Result is not 400 CZK.", 400d, result);          
   1.126 +        } else {
   1.127 +          System.out.println("Exception.");
   1.128 +          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   1.129 +        }
   1.130  
   1.131 -        // convert $5 to CZK using c at 2008-10-01 6:00 GMT:
   1.132 -        // assertEquals("Result is 85 CZK");
   1.133 -
   1.134 -        // convert $8 to CZK using c at 2008-10-01 6:00 GMT:
   1.135 -        // assertEquals("Result is 136 CZK");
   1.136 -
   1.137 -        // convert 1003CZK to USD using c at 2008-10-01 6:00 GMT:
   1.138 -        // assertEquals("Result is 59 USD");
   1.139 -
   1.140 -        // convert 16CZK using c at 2008-10-02 9:00 GMT:
   1.141 -        // assertEquals("Result is 20 SKK");
   1.142 -
   1.143 -        // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
   1.144 -        // assertEquals("Result is 400 CZK");
   1.145 -
   1.146 -        // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   1.147 -        // cannot convert as no rate is applicable to current date
   1.148      }
   1.149  
   1.150      /** Create convertor that understands two currencies, CZK and
   1.151 @@ -104,18 +154,26 @@
   1.152       * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
   1.153       */
   1.154      public static Convertor createSKKtoCZK2() {
   1.155 -        return null;
   1.156 +      // set exchange rates
   1.157 +      Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 90d, 100d);
   1.158 +
   1.159 +      // create new instance
   1.160 +      Convertor convertor = null;
   1.161 +      try {
   1.162 +        convertor = Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   1.163 +      } catch (UnknownConvertorException e) {
   1.164 +        e.printStackTrace();
   1.165 +      }
   1.166 +
   1.167 +      return convertor;
   1.168      }
   1.169  
   1.170      public void testDateConvetorWithTwoDifferentRates() throws Exception {
   1.171 -        if (Boolean.getBoolean("ignore.failing")) {
   1.172 -            // implement me! then delete this if statement
   1.173 -            return;
   1.174 -        }
   1.175  
   1.176 -        Date d1 = null; // 2008-10-01 0:00 GMT
   1.177 -        Date d2 = null; // 2008-10-02 0:00 GMT
   1.178 -        Date d3 = null; // 2008-10-03 0:00 GMT
   1.179 +        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz");
   1.180 +        Date d1 = format.parse("2008-10-01 00:00 GMT"); // 2008-10-01 0:00 GMT
   1.181 +        Date d2 = format.parse("2008-10-02 00:00 GMT"); // 2008-10-02 0:00 GMT
   1.182 +        Date d3 = format.parse("2008-10-03 00:00 GMT"); // 2008-10-03 0:00 GMT
   1.183  
   1.184          Convertor c = Task2Test.merge(
   1.185              limitTo(createSKKtoCZK2(), d1, d2),
   1.186 @@ -127,6 +185,27 @@
   1.187  
   1.188          // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   1.189          // assertEquals("Result is 450 CZK");
   1.190 +        Date now = new Date();
   1.191 +        
   1.192 +        // convert 500SKK to CZK using c:
   1.193 +        double result = 0;
   1.194 +        boolean exceptionThrown = false;
   1.195 +        try {
   1.196 +          result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   1.197 +        } catch (Exception e) {
   1.198 +          exceptionThrown = true;
   1.199 +        }
   1.200 +        
   1.201 +        if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
   1.202 +          System.out.println("\nResult");
   1.203 +          assertEquals("Result is not 450 CZK.", 450d, result);
   1.204 +        } else if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
   1.205 +          System.out.println("\nResult");
   1.206 +          assertEquals("Result is not 400 CZK.", 400d, result);
   1.207 +        } else {
   1.208 +          System.out.println("\nException");
   1.209 +          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   1.210 +        }
   1.211      }
   1.212  
   1.213  }