# HG changeset patch # User Jaroslav Tulach # Date 1223761006 -7200 # Node ID a92eff6b426fff2ac04aa3db67ba33511f210117 # Parent c1d43bc1e9c03a8185c9291076ded5b6f068b53b Quest for task4 diff -r c1d43bc1e9c0 -r a92eff6b426f currency/test/org/apidesign/apifest08/test/Task4Test.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/currency/test/org/apidesign/apifest08/test/Task4Test.java Sat Oct 11 23:36:46 2008 +0200 @@ -0,0 +1,132 @@ +package org.apidesign.apifest08.test; + +import java.util.Date; +import junit.framework.TestCase; +import org.apidesign.apifest08.currency.Convertor; + +/** 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 null; + } + + + public void testCompositionOfLimitedConvertors() throws Exception { + if (Boolean.getBoolean("ignore.failing")) { + // implement me! then delete this if statement + return; + } + + Date d1 = null; // 2008-10-01 0:00 GMT + Date d2 = null; // 2008-10-02 0:00 GMT + Date d3 = null; // 2008-10-03 0:00 GMT + + Convertor c = Task2Test.merge( + limitTo(Task1Test.createCZKtoUSD(), d1, d2), + limitTo(Task1Test.createSKKtoCZK(), d2, d3) + ); + + // convert $5 to CZK using c: + // cannot convert as no rate is applicable to current date + + // convert $8 to CZK using c: + // cannot convert as no rate is applicable to current date + + // convert 1003CZK to USD using c: + // cannot convert as no rate is applicable to current date + + // convert 16CZK using c: + // cannot convert as no rate is applicable to current date + + // convert 500SKK to CZK using c: + // cannot convert as no rate is applicable to current date + + // convert $5 to CZK using c at 2008-10-01 6:00 GMT: + // assertEquals("Result is 85 CZK"); + + // convert $8 to CZK using c at 2008-10-01 6:00 GMT: + // assertEquals("Result is 136 CZK"); + + // convert 1003CZK to USD using c at 2008-10-01 6:00 GMT: + // assertEquals("Result is 59 USD"); + + // convert 16CZK using c at 2008-10-02 9:00 GMT: + // assertEquals("Result is 20 SKK"); + + // 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: + // cannot convert as no rate is applicable to current date + } + + /** 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() { + return null; + } + + public void testDateConvetorWithTwoDifferentRates() throws Exception { + if (Boolean.getBoolean("ignore.failing")) { + // implement me! then delete this if statement + return; + } + + Date d1 = null; // 2008-10-01 0:00 GMT + Date d2 = null; // 2008-10-02 0:00 GMT + Date d3 = null; // 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"); + } + +}