diff -r 978f6a78c22e -r c6b50876b5cf task4/solution12/test/org/apidesign/apifest08/test/Task4Test.java --- a/task4/solution12/test/org/apidesign/apifest08/test/Task4Test.java Fri Oct 17 17:54:38 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,211 +0,0 @@ -package org.apidesign.apifest08.test; - -import java.text.SimpleDateFormat; -import java.util.Currency; -import java.util.Date; -import junit.framework.TestCase; -import org.apidesign.apifest08.currency.Convertor; -import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException; - -/** The exchange rates are not always the same. They are changing. However - * as in order to predict the future, one needs to understand own past. That is - * why it is important to know the exchange rate as it was at any time during - * the past. - *

- * Today's quest is to enhance the convertor API to deal with dates. - * One shall be able to convert a currency at any date. Each currencies rate shall - * be associated with a range between two Date objects. In order - * to keep compatibility with old API that knew nothing about dates, the - * rates associated then are applicable "for eternity". Any use of existing - * convert methods that do not accept a Date argument, uses the current - * System.currentTimeMillis() as default date. - */ -public class Task4Test extends TestCase { - public Task4Test(String testName) { - super(testName); - } - - @Override - protected void setUp() throws Exception { - } - - @Override - protected void tearDown() throws Exception { - } - - // Backward compatibly enhance your existing API to support following - // usecases: - // - - /** Takes a convertor with any rates associated and creates new convertor - * that returns the same values as the old one for time between from to till. - * Otherwise it returns no results. This is just a helper method that - * shall call some real one in the API. - * - * @param old existing convertor - * @param from initial date (inclusive) - * @param till final date (exclusive) - * @return new convertor - */ - public static Convertor limitTo(Convertor old, Date from, Date till) { - return Convertor.limitExchangeRatesValidity(old, from, till); - } - - - public void testCompositionOfLimitedConvertors() throws Exception { - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz"); - Date d1 = format.parse("2008-10-01 00:00 GMT"); // 2008-10-01 0:00 GMT - Date d2 = format.parse("2008-10-02 00:00 GMT"); // 2008-10-02 0:00 GMT - Date d3 = format.parse("2008-10-03 00:00 GMT"); // 2008-10-03 0:00 GMT - - Convertor c = Task2Test.merge( - limitTo(Task1Test.createCZKtoUSD(), d1, d2), - limitTo(Task1Test.createSKKtoCZK(), d2, d3) - ); - - Date now = new Date(); - - // convert $5 to CZK using c: - double result = 0; - boolean exceptionThrown = false; - try { - result = c.convert(5, Currency.getInstance("USD"), Currency.getInstance("CZK")); - } catch (Exception e) { - exceptionThrown = true; - } - - if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) { - System.out.println("Result."); - assertEquals("Result is not 85 CZK.", 85d, result); - } else { - System.out.println("Exception."); - assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown); - } - - // convert $8 to CZK using c: - exceptionThrown = false; - try { - result = c.convert(8, Currency.getInstance("USD"), Currency.getInstance("CZK")); - } catch (Exception e) { - exceptionThrown = true; - } - - if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) { - System.out.println("Result."); - assertEquals("Result is not 136 CZK.", 136d, result); - } else { - System.out.println("Exception."); - assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown); - } - - // convert 1003CZK to USD using c: - exceptionThrown = false; - try { - result = c.convert(1003, Currency.getInstance("CZK"), Currency.getInstance("USD")); - } catch (Exception e) { - exceptionThrown = true; - } - - if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) { - System.out.println("Result."); - assertEquals("Result is not 59 USD.", 59d, result); - } else { - System.out.println("Exception."); - assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown); - } - - // convert 16CZK using c: - exceptionThrown = false; - try { - result = c.convert(16, Currency.getInstance("CZK"), Currency.getInstance("SKK")); - } catch (Exception e) { - exceptionThrown = true; - } - - if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) { - System.out.println("Result."); - assertEquals("Result is not 20 SKK.", 20d, result); - } else { - System.out.println("Exception."); - assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown); - } - - // convert 500SKK to CZK using c: - exceptionThrown = false; - try { - result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK")); - } catch (Exception e) { - exceptionThrown = true; - } - - if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) { - System.out.println("Result."); - assertEquals("Result is not 400 CZK.", 400d, result); - } else { - System.out.println("Exception."); - assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown); - } - - } - - /** Create convertor that understands two currencies, CZK and - * SKK. Make 100 SKK == 90 CZK. - * - * @return prepared convertor ready for converting SKK to CZK and CZK to SKK - */ - public static Convertor createSKKtoCZK2() { - // set exchange rates - Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 90d, 100d); - - // create new instance - Convertor convertor = null; - try { - convertor = Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK")); - } catch (UnknownConvertorException e) { - e.printStackTrace(); - } - - return convertor; - } - - public void testDateConvetorWithTwoDifferentRates() throws Exception { - - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz"); - Date d1 = format.parse("2008-10-01 00:00 GMT"); // 2008-10-01 0:00 GMT - Date d2 = format.parse("2008-10-02 00:00 GMT"); // 2008-10-02 0:00 GMT - Date d3 = format.parse("2008-10-03 00:00 GMT"); // 2008-10-03 0:00 GMT - - Convertor c = Task2Test.merge( - limitTo(createSKKtoCZK2(), d1, d2), - limitTo(Task1Test.createSKKtoCZK(), d2, d3) - ); - - // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT: - // assertEquals("Result is 400 CZK"); - - // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT: - // assertEquals("Result is 450 CZK"); - Date now = new Date(); - - // convert 500SKK to CZK using c: - double result = 0; - boolean exceptionThrown = false; - try { - result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK")); - } catch (Exception e) { - exceptionThrown = true; - } - - if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) { - System.out.println("\nResult"); - assertEquals("Result is not 450 CZK.", 450d, result); - } else if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) { - System.out.println("\nResult"); - assertEquals("Result is not 400 CZK.", 400d, result); - } else { - System.out.println("\nException"); - assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown); - } - } - -}