task4/solution12/test/org/apidesign/apifest08/test/Task4Test.java
changeset 72 c6b50876b5cf
parent 71 978f6a78c22e
child 73 e8b0f13fd4fb
child 80 067f86d76ac7
     1.1 --- a/task4/solution12/test/org/apidesign/apifest08/test/Task4Test.java	Fri Oct 17 17:54:38 2008 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,211 +0,0 @@
     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 - * why it is important to know the exchange rate as it was at any time during
    1.16 - * the past.
    1.17 - * <p>
    1.18 - * Today's quest is to enhance the convertor API to deal with dates.
    1.19 - * One shall be able to convert a currency at any date. Each currencies rate shall
    1.20 - * be associated with a range between two Date objects. In order
    1.21 - * to keep compatibility with old API that knew nothing about dates, the
    1.22 - * rates associated then are applicable "for eternity". Any use of existing
    1.23 - * convert methods that do not accept a Date argument, uses the current
    1.24 - * System.currentTimeMillis() as default date.
    1.25 - */
    1.26 -public class Task4Test extends TestCase {
    1.27 -    public Task4Test(String testName) {
    1.28 -        super(testName);
    1.29 -    }
    1.30 -
    1.31 -    @Override
    1.32 -    protected void setUp() throws Exception {
    1.33 -    }
    1.34 -
    1.35 -    @Override
    1.36 -    protected void tearDown() throws Exception {
    1.37 -    }
    1.38 -
    1.39 -    // Backward compatibly enhance your existing API to support following
    1.40 -    // usecases:
    1.41 -    //
    1.42 -
    1.43 -    /** Takes a convertor with any rates associated and creates new convertor
    1.44 -     * that returns the same values as the old one for time between from to till.
    1.45 -     * Otherwise it returns no results. This is just a helper method that
    1.46 -     * shall call some real one in the API.
    1.47 -     * 
    1.48 -     * @param old existing convertor
    1.49 -     * @param from initial date (inclusive)
    1.50 -     * @param till final date (exclusive)
    1.51 -     * @return new convertor
    1.52 -     */
    1.53 -    public static Convertor limitTo(Convertor old, Date from, Date till) {
    1.54 -        return Convertor.limitExchangeRatesValidity(old, from, till);
    1.55 -    }
    1.56 -
    1.57 -
    1.58 -    public void testCompositionOfLimitedConvertors() throws Exception {
    1.59 -        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz");
    1.60 -        Date d1 = format.parse("2008-10-01 00:00 GMT"); // 2008-10-01 0:00 GMT
    1.61 -        Date d2 = format.parse("2008-10-02 00:00 GMT"); // 2008-10-02 0:00 GMT
    1.62 -        Date d3 = format.parse("2008-10-03 00:00 GMT"); // 2008-10-03 0:00 GMT
    1.63 -        
    1.64 -        Convertor c = Task2Test.merge(
    1.65 -            limitTo(Task1Test.createCZKtoUSD(), d1, d2),
    1.66 -            limitTo(Task1Test.createSKKtoCZK(), d2, d3)
    1.67 -        );
    1.68 -        
    1.69 -        Date now = new Date();
    1.70 -
    1.71 -        // convert $5 to CZK using c:
    1.72 -        double result = 0;
    1.73 -        boolean exceptionThrown = false;
    1.74 -        try {
    1.75 -          result = c.convert(5, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    1.76 -        } catch (Exception e) {
    1.77 -          exceptionThrown = true;
    1.78 -        }
    1.79 -        
    1.80 -        if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
    1.81 -          System.out.println("Result.");
    1.82 -          assertEquals("Result is not 85 CZK.", 85d, result);          
    1.83 -        } else {
    1.84 -          System.out.println("Exception.");
    1.85 -          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
    1.86 -        }
    1.87 -
    1.88 -        // convert $8 to CZK using c:
    1.89 -        exceptionThrown = false;
    1.90 -        try {
    1.91 -          result = c.convert(8, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    1.92 -        } catch (Exception e) {
    1.93 -          exceptionThrown = true;
    1.94 -        }
    1.95 -        
    1.96 -        if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
    1.97 -          System.out.println("Result.");
    1.98 -          assertEquals("Result is not 136 CZK.", 136d, result);          
    1.99 -        } else {
   1.100 -          System.out.println("Exception.");
   1.101 -          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   1.102 -        }
   1.103 -
   1.104 -        // convert 1003CZK to USD using c:
   1.105 -        exceptionThrown = false;
   1.106 -        try {
   1.107 -          result = c.convert(1003, Currency.getInstance("CZK"), Currency.getInstance("USD"));
   1.108 -        } catch (Exception e) {
   1.109 -          exceptionThrown = true;
   1.110 -        }
   1.111 -        
   1.112 -        if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
   1.113 -          System.out.println("Result.");
   1.114 -          assertEquals("Result is not 59 USD.", 59d, result);
   1.115 -        } else {
   1.116 -          System.out.println("Exception.");
   1.117 -          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   1.118 -        }
   1.119 -
   1.120 -        // convert 16CZK using c:
   1.121 -        exceptionThrown = false;
   1.122 -        try {
   1.123 -          result = c.convert(16, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
   1.124 -        } catch (Exception e) {
   1.125 -          exceptionThrown = true;
   1.126 -        }
   1.127 -        
   1.128 -        if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
   1.129 -          System.out.println("Result.");
   1.130 -          assertEquals("Result is not 20 SKK.", 20d, result);          
   1.131 -        } else {
   1.132 -          System.out.println("Exception.");
   1.133 -          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   1.134 -        }
   1.135 -
   1.136 -        // convert 500SKK to CZK using c:
   1.137 -        exceptionThrown = false;
   1.138 -        try {
   1.139 -          result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   1.140 -        } catch (Exception e) {
   1.141 -          exceptionThrown = true;
   1.142 -        }
   1.143 -        
   1.144 -        if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
   1.145 -          System.out.println("Result.");
   1.146 -          assertEquals("Result is not 400 CZK.", 400d, result);          
   1.147 -        } else {
   1.148 -          System.out.println("Exception.");
   1.149 -          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   1.150 -        }
   1.151 -
   1.152 -    }
   1.153 -
   1.154 -    /** Create convertor that understands two currencies, CZK and
   1.155 -     *  SKK. Make 100 SKK == 90 CZK.
   1.156 -     *
   1.157 -     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
   1.158 -     */
   1.159 -    public static Convertor createSKKtoCZK2() {
   1.160 -      // set exchange rates
   1.161 -      Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 90d, 100d);
   1.162 -
   1.163 -      // create new instance
   1.164 -      Convertor convertor = null;
   1.165 -      try {
   1.166 -        convertor = Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   1.167 -      } catch (UnknownConvertorException e) {
   1.168 -        e.printStackTrace();
   1.169 -      }
   1.170 -
   1.171 -      return convertor;
   1.172 -    }
   1.173 -
   1.174 -    public void testDateConvetorWithTwoDifferentRates() throws Exception {
   1.175 -
   1.176 -        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz");
   1.177 -        Date d1 = format.parse("2008-10-01 00:00 GMT"); // 2008-10-01 0:00 GMT
   1.178 -        Date d2 = format.parse("2008-10-02 00:00 GMT"); // 2008-10-02 0:00 GMT
   1.179 -        Date d3 = format.parse("2008-10-03 00:00 GMT"); // 2008-10-03 0:00 GMT
   1.180 -
   1.181 -        Convertor c = Task2Test.merge(
   1.182 -            limitTo(createSKKtoCZK2(), d1, d2),
   1.183 -            limitTo(Task1Test.createSKKtoCZK(), d2, d3)
   1.184 -        );
   1.185 -
   1.186 -        // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
   1.187 -        // assertEquals("Result is 400 CZK");
   1.188 -
   1.189 -        // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   1.190 -        // assertEquals("Result is 450 CZK");
   1.191 -        Date now = new Date();
   1.192 -        
   1.193 -        // convert 500SKK to CZK using c:
   1.194 -        double result = 0;
   1.195 -        boolean exceptionThrown = false;
   1.196 -        try {
   1.197 -          result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   1.198 -        } catch (Exception e) {
   1.199 -          exceptionThrown = true;
   1.200 -        }
   1.201 -        
   1.202 -        if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
   1.203 -          System.out.println("\nResult");
   1.204 -          assertEquals("Result is not 450 CZK.", 450d, result);
   1.205 -        } else if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
   1.206 -          System.out.println("\nResult");
   1.207 -          assertEquals("Result is not 400 CZK.", 400d, result);
   1.208 -        } else {
   1.209 -          System.out.println("\nException");
   1.210 -          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   1.211 -        }
   1.212 -    }
   1.213 -
   1.214 -}