1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task4/solution06/test/org/apidesign/apifest08/test/Task3Test.java Sat Oct 11 23:38:46 2008 +0200
1.3 @@ -0,0 +1,111 @@
1.4 +package org.apidesign.apifest08.test;
1.5 +
1.6 +import java.math.BigDecimal;
1.7 +import java.math.RoundingMode;
1.8 +
1.9 +import junit.framework.TestCase;
1.10 +import org.apidesign.apifest08.currency.Convertor;
1.11 +import static org.apidesign.apifest08.test.Currencies.*;
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 extends TestCase {
1.26 + public Task3Test(String testName) {
1.27 + super(testName);
1.28 + }
1.29 +
1.30 + @Override
1.31 + protected void setUp() throws Exception {
1.32 + }
1.33 +
1.34 + @Override
1.35 + protected void tearDown() throws Exception {
1.36 + }
1.37 +
1.38 + // Backward compatibly enhance your existing API to support following
1.39 + // usecases:
1.40 + //
1.41 +
1.42 +
1.43 + /** Without knowing anything about the surrounding system, write an
1.44 + * implementation of convertor that will return different rates everytime
1.45 + * it is queried. Convert USD to CZK and vice versa. Start with the rate of
1.46 + * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
1.47 + * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
1.48 + * until you reach 1USD = 16CZK
1.49 + *
1.50 + * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
1.51 + */
1.52 + public static Convertor createOnlineCZKUSDConvertor() {
1.53 + Convertor.RateProvider rateProvider = new Convertor.RateProvider() {
1.54 + private BigDecimal seed = new BigDecimal("16.01").setScale(2);
1.55 +
1.56 + @Override
1.57 + public BigDecimal getRate() {
1.58 + if(seed.equals(new BigDecimal("15").setScale(2))) {
1.59 + seed = seed.add(new BigDecimal("0.01")).setScale(2);
1.60 + } else {
1.61 + seed = seed.subtract(new BigDecimal("0.01")).setScale(2);
1.62 + }
1.63 + return seed;
1.64 + }
1.65 +
1.66 + };
1.67 +
1.68 + // initial rate: 1USD = 16CZK
1.69 + // 2nd query 1USD = 15.99CZK
1.70 + // 3rd query 1USD = 15.98CZK
1.71 + // until 1USD = 15.00CZK
1.72 + // then 1USD = 15.01CZK
1.73 + // then 1USD = 15.02CZK
1.74 + // and so on and on up to 1USD = 16CZK
1.75 + // and then another round to 15, etc.
1.76 + return new Convertor(rateProvider, USD, CZK);
1.77 + }
1.78 +
1.79 + public void testFewQueriesForOnlineConvertor() {
1.80 + Convertor c = createOnlineCZKUSDConvertor();
1.81 + doFewQueriesForOnlineConvertor(c);
1.82 + }
1.83 +
1.84 + static void doFewQueriesForOnlineConvertor(Convertor c) {
1.85 + // convert $5 to CZK using c:
1.86 + assertEquals("Result is 80 CZK",80 ,c.convert(new BigDecimal(5), USD, CZK).getValue().intValue());
1.87 +
1.88 + // convert $8 to CZK using c:
1.89 + assertEquals("Result is 127.92 CZK", 127.92d , c.convert(new BigDecimal(8), USD, CZK).getValue().doubleValue());
1.90 +
1.91 + // convert $1 to CZK using c:
1.92 + assertEquals("Result is 15.98 CZK", 15.98d, c.convert(new BigDecimal(1), USD, CZK).getValue().doubleValue());
1.93 +
1.94 + // convert 15.97CZK to USD using c:
1.95 + assertEquals("Result is 1$", 1, c.convert(new BigDecimal("15.97").setScale(2), CZK, USD).getValue().intValue());
1.96 + }
1.97 +
1.98 + /** Join the convertors and show they behave sane.
1.99 + */
1.100 + public void testOnlineConvertorComposition() throws Exception {
1.101 + Convertor c = Task2Test.merge(
1.102 + createOnlineCZKUSDConvertor(),
1.103 + Task1Test.createSKKtoCZK()
1.104 + );
1.105 +
1.106 + // convert 16CZK to SKK using c:
1.107 + assertEquals("Result is 20 SKK", 20, c.convert(new BigDecimal(16), CZK, SKK).getValue().intValue());
1.108 +
1.109 + // convert 500SKK to CZK using c:
1.110 + assertEquals("Result is 400 CZK", 400, c.convert(new BigDecimal(500), SKK, CZK).getValue().intValue());
1.111 +
1.112 + doFewQueriesForOnlineConvertor(c);
1.113 + }
1.114 +}