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