1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task4/solution04/test/org/apidesign/apifest08/test/Task3Test.java Sat Oct 11 23:38:46 2008 +0200
1.3 @@ -0,0 +1,145 @@
1.4 +package org.apidesign.apifest08.test;
1.5 +
1.6 +import java.math.BigDecimal;
1.7 +import java.util.Currency;
1.8 +import junit.framework.TestCase;
1.9 +import org.apidesign.apifest08.currency.Convertor;
1.10 +import org.apidesign.apifest08.currency.InvalidConversionException;
1.11 +import org.apidesign.apifest08.currency.OnlineConvertor;
1.12 +
1.13 +/** The exchange rates are not always the same. They are changing. Day by day,
1.14 + * hour by hour, minute by minute. For every bank it is important to always
1.15 + * have the actual exchange rate available in the system. That is why let's
1.16 + * create a pluggable convertor that will always have up to date value of its
1.17 + * exchange rate.
1.18 + * <p>
1.19 + * The quest for today is to allow 3rd party developer to write a convertor
1.20 + * that adjusts its exchange rate everytime it is queried. This convertor is
1.21 + * written by independent vendor, the vendor knows only your Convertor API,
1.22 + * he does not know how the whole system looks and how the convertor is supposed
1.23 + * to be used.
1.24 + */
1.25 +public class Task3Test
1.26 + extends TestCase
1.27 +{
1.28 + private final static Currency CZK;
1.29 + private final static Currency SKK;
1.30 + private final static Currency USD;
1.31 +
1.32 + static
1.33 + {
1.34 + CZK = Currency.getInstance("CZK");
1.35 + SKK = Currency.getInstance("SKK");
1.36 + USD = Currency.getInstance("USD");
1.37 + }
1.38 +
1.39 + public Task3Test(String testName)
1.40 + {
1.41 + super(testName);
1.42 + }
1.43 +
1.44 + @Override
1.45 + protected void setUp() throws Exception
1.46 + {
1.47 + }
1.48 +
1.49 + @Override
1.50 + protected void tearDown() throws Exception
1.51 + {
1.52 + }
1.53 +
1.54 + // Backward compatibly enhance your existing API to support following
1.55 + // usecases:
1.56 + //
1.57 +
1.58 +
1.59 + /** Without knowing anything about the surrounding system, write an
1.60 + * implementation of convertor that will return different rates everytime
1.61 + * it is queried. Convert USD to CZK and vice versa. Start with the rate of
1.62 + * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
1.63 + * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
1.64 + * until you reach 1USD = 16CZK
1.65 + *
1.66 + * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
1.67 + * @throws InvalidConversionException
1.68 + */
1.69 + public static Convertor createOnlineCZKUSDConvertor()
1.70 + throws InvalidConversionException
1.71 + {
1.72 + final Convertor convertor;
1.73 +
1.74 + convertor = new OnlineConvertor(USD,
1.75 + CZK,
1.76 + new TestExchangeRateFinder(new BigDecimal("15.00"),
1.77 + new BigDecimal("16.00"),
1.78 + new BigDecimal("16.00"),
1.79 + new BigDecimal("0.01"),
1.80 + new BigDecimal("-0.01")));
1.81 +
1.82 + // initial rate: 1USD = 16CZK
1.83 + // 2nd query 1USD = 15.99CZK
1.84 + // 3rd query 1USD = 15.98CZK
1.85 + // until 1USD = 15.00CZK
1.86 + // then 1USD = 15.01CZK
1.87 + // then 1USD = 15.02CZK
1.88 + // and so on and on up to 1USD = 16CZK
1.89 + // and then another round to 15, etc.
1.90 + return convertor;
1.91 + }
1.92 +
1.93 + public void testFewQueriesForOnlineConvertor()
1.94 + throws InvalidConversionException
1.95 + {
1.96 + Convertor c = createOnlineCZKUSDConvertor();
1.97 + doFewQueriesForOnlineConvertor(c);
1.98 + }
1.99 +
1.100 + static void doFewQueriesForOnlineConvertor(Convertor c)
1.101 + throws InvalidConversionException
1.102 + {
1.103 + BigDecimal amount;
1.104 +
1.105 + // convert $5 to CZK using c:
1.106 + //assertEquals("Result is 80 CZK");
1.107 + amount = c.convert(USD, CZK, new BigDecimal("5.00"));
1.108 + assertEquals(new BigDecimal("80.00"), amount);
1.109 +
1.110 + // convert $8 to CZK using c:
1.111 + //assertEquals("Result is 127.92 CZK");
1.112 + amount = c.convert(USD, CZK, new BigDecimal("8.00"));
1.113 + assertEquals(new BigDecimal("127.92"), amount);
1.114 +
1.115 + // convert $1 to CZK using c:
1.116 + //assertEquals("Result is 15.98 CZK");
1.117 + amount = c.convert(USD, CZK, new BigDecimal("1.00"));
1.118 + assertEquals(new BigDecimal("15.98"), amount);
1.119 +
1.120 + // convert 15.97CZK to USD using c:
1.121 + //assertEquals("Result is 1$");
1.122 + amount = c.convert(CZK, USD, new BigDecimal("15.97"));
1.123 + assertEquals(new BigDecimal("1.00"), amount);
1.124 + }
1.125 +
1.126 + /** Join the convertors and show they behave sane.
1.127 + */
1.128 + public void testOnlineConvertorComposition() throws Exception {
1.129 + BigDecimal amount;
1.130 +
1.131 + Convertor c = Task2Test.merge(
1.132 + createOnlineCZKUSDConvertor(),
1.133 + Task1Test.createSKKtoCZK()
1.134 + );
1.135 +
1.136 + // convert 16CZK to SKK using c:
1.137 + // assertEquals("Result is 20 SKK");
1.138 + amount = c.convert(CZK, SKK, new BigDecimal("16.00"));
1.139 + assertEquals(new BigDecimal("20.00"), amount);
1.140 +
1.141 + // convert 500SKK to CZK using c:
1.142 + // assertEquals("Result is 400 CZK");
1.143 + amount = c.convert(SKK, CZK, new BigDecimal("500.00"));
1.144 + assertEquals(new BigDecimal("400.00"), amount);
1.145 +
1.146 + doFewQueriesForOnlineConvertor(c);
1.147 + }
1.148 +}