diff -r 251d0ed461fb -r 58ec6da75f6f task4/solution04/test/org/apidesign/apifest08/test/Task2Test.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task4/solution04/test/org/apidesign/apifest08/test/Task2Test.java Sat Oct 11 23:38:46 2008 +0200 @@ -0,0 +1,179 @@ +package org.apidesign.apifest08.test; + +import java.math.BigDecimal; +import java.util.Currency; +import java.util.Set; +import junit.framework.TestCase; +import org.apidesign.apifest08.currency.Convertor; +import org.apidesign.apifest08.currency.ConvertorFactory; +import org.apidesign.apifest08.currency.InvalidConversionException; + + +/** There are many currencies around the world and many banks manipulate + * with more than one or two at the same time. As banks are usually the + * best paying clients, which is true even in case of your Convertor API, + * it is reasonable to listen to their requests. + *

+ * The quest for today is to enhance your existing convertor API to hold + * information about many currencies and allow conversions between any of them. + * Also, as conversion rates for diferent currencies usually arise from various + * bank departments, there is another important need. There is a need to + * compose two convertors into one by merging all the information about + * currencies they know about. + */ +public class Task2Test extends TestCase +{ + private final static Currency CZK; + private final static Currency SKK; + private final static Currency USD; + + static + { + CZK = Currency.getInstance("CZK"); + SKK = Currency.getInstance("SKK"); + USD = Currency.getInstance("USD"); + } + + public Task2Test(String testName) + { + super(testName); + } + + @Override + protected void setUp() + throws Exception + { + } + + @Override + protected void tearDown() + throws Exception + { + } + + // As in Task1Test, keep in mind, that there are three parts + // of the whole system: + // 1. there is someone who knows the current exchange rate + // 2. there is someone who wants to do the conversion + // 3. there is the API between 1. and 2. which allows them to communicate + // + // Please backward compatibly enhance your existing API to support following + // usecases: + // + + /** Create convertor that understands two currencies, CZK and + * SKK. Make 100 SKK == 75 CZK. This is method for the group of users that + * knows the exchange rate, and needs to use the API to create objects + * with the exchange rate. Anyone shall be ready to call this method without + * any other method being called previously. The API itself shall know + * nothing about any rates, before this method is called. + */ + public static Convertor createTripleConvertor() { + // Rates: 1USD = 15CZK + // Rates: 1USD = 20SKK + // Rates: 75CZK = 100SKK + Convertor c = ConvertorFactory.mergeConvertors( + ConvertorFactory.getConvertor(USD, BigDecimal.ONE, CZK, BigDecimal.valueOf(15.00)), + ConvertorFactory.getConvertor(USD, BigDecimal.ONE, SKK, BigDecimal.valueOf(20.00)) + ); + + return c; + } + + /** Define convertor that understands three currencies. Use it. + */ + public void testConvertorForUSDandCZKandSKK() throws Exception { + Convertor c = createTripleConvertor(); + + // convert $5 to CZK using c: + // assertEquals("Result is 75 CZK"); + assertEquals(new BigDecimal("75.00"), c.convert(USD, CZK, BigDecimal.valueOf(5.00))); + + // convert $5 to SKK using c: + // assertEquals("Result is 100 SKK"); + assertEquals(new BigDecimal("100.00"), c.convert(USD, SKK, BigDecimal.valueOf(5.00))); + + // convert 200SKK to CZK using c: + // assertEquals("Result is 150 CZK"); + assertEquals(new BigDecimal("150.00"), c.convert(SKK, CZK, BigDecimal.valueOf(200.00))); + + // convert 200SKK to USK using c: + // assertEquals("Result is 10 USD"); + assertEquals(new BigDecimal("10.00"), c.convert(SKK, USD, BigDecimal.valueOf(200.00))); + } + + /** Merge all currency rates of convertor 1 with convertor 2. + * Implement this using your API, preferably this method just delegates + * into some API method which does the actual work, without requiring + * API clients to code anything complex. + */ + public static Convertor merge(Convertor one, Convertor two) { + return ConvertorFactory.mergeConvertors(one, two); + } + + /** Join the convertors from previous task, Task1Test and show that it + * can be used to do reasonable conversions. + */ + public void testConvertorComposition() throws Exception { + Convertor c = merge( + Task1Test.createCZKtoUSD(), + Task1Test.createSKKtoCZK() + ); + + // convert $5 to CZK using c: + // assertEquals("Result is 85 CZK"); + assertEquals(new BigDecimal("85.00"), c.convert(USD, CZK, BigDecimal.valueOf(5.00))); + + // convert $8 to CZK using c: + // assertEquals("Result is 136 CZK"); + assertEquals(new BigDecimal("136.00"), c.convert(USD, CZK, BigDecimal.valueOf(8.00))); + + // convert 1003CZK to USD using c: + // assertEquals("Result is 59 USD"); + assertEquals(new BigDecimal("59.00"), c.convert(CZK, USD, BigDecimal.valueOf(1003.00))); + + // convert 16CZK using c: + // assertEquals("Result is 20 SKK"); + assertEquals(new BigDecimal("20.00"), c.convert(CZK, SKK, BigDecimal.valueOf(16.00))); + + // convert 500SKK to CZK using c: + // assertEquals("Result is 400 CZK"); + assertEquals(new BigDecimal("400.00"), c.convert(SKK, CZK, BigDecimal.valueOf(500.00))); + } + + public void testGetCurrencies() + { + Convertor c = merge( + Task1Test.createCZKtoUSD(), + Task1Test.createSKKtoCZK() + ); + Set currencies; + + currencies = c.getCurrencies(); + assertEquals(3, currencies.size()); + assertTrue(currencies.contains(Currency.getInstance("SKK"))); + assertTrue(currencies.contains(Currency.getInstance("CZK"))); + assertTrue(currencies.contains(Currency.getInstance("USD"))); + } + + public void testGetConverstionRate() + throws InvalidConversionException + { + Convertor c = merge( + Task1Test.createCZKtoUSD(), + Task1Test.createSKKtoCZK() + ); + + assertEquals(1.0, c.getConversionRate(USD, USD).doubleValue(), 0.0000000000000001); + assertEquals(17.0, c.getConversionRate(USD, CZK).doubleValue(), 0.0000000000000001); + assertEquals(21.25, c.getConversionRate(USD, SKK).doubleValue(), 0.0000000000000001); + + assertEquals(1.0 / 17.0, c.getConversionRate(CZK, USD).doubleValue(), 0.0000000000000001); + assertEquals(1.0, c.getConversionRate(CZK, CZK).doubleValue(), 0.0000000000000001); + assertEquals(1.25, c.getConversionRate(CZK, SKK).doubleValue(), 0.0000000000000001); + + assertEquals(0.04705882352941176, c.getConversionRate(SKK, USD).doubleValue(), 0.0000000000000001); + assertEquals(0.8, c.getConversionRate(SKK, CZK).doubleValue(), 0.0000000000000001); + assertEquals(1.0, c.getConversionRate(SKK, SKK).doubleValue(), 0.0000000000000001); + } +}