1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/currency/test/org/apidesign/apifest08/test/Task3Test.java Tue Oct 07 11:00:23 2008 +0200
1.3 @@ -0,0 +1,94 @@
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 +
1.9 +/** The exchange rates are not always the same. They are changing. Day by day,
1.10 + * hour by hour, minute by minute. For every bank it is important to always
1.11 + * have the actual exchange rate available in the system. That is why let's
1.12 + * create a pluggable convertor that will always have up to date value of its
1.13 + * exchange rate.
1.14 + * <p>
1.15 + * The quest for today is to allow 3rd party developer to write a convertor
1.16 + * that adjusts its exchange rate everytime it is queried. This convertor is
1.17 + * written by independent vendor, the vendor knows only your Convertor API,
1.18 + * he does not know how the whole system looks and how the convertor is supposed
1.19 + * to be used.
1.20 + */
1.21 +public class Task3Test extends TestCase {
1.22 + public Task3Test(String testName) {
1.23 + super(testName);
1.24 + }
1.25 +
1.26 + @Override
1.27 + protected void setUp() throws Exception {
1.28 + }
1.29 +
1.30 + @Override
1.31 + protected void tearDown() throws Exception {
1.32 + }
1.33 +
1.34 + // Backward compatibly enhance your existing API to support following
1.35 + // usecases:
1.36 + //
1.37 +
1.38 +
1.39 + /** Without knowing anything about the surrounding system, write an
1.40 + * implementation of convertor that will return different rates everytime
1.41 + * it is queried. Convert USD to CZK and vice versa. Start with the rate of
1.42 + * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
1.43 + * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
1.44 + * until you reach 1USD = 16CZK
1.45 + *
1.46 + * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
1.47 + */
1.48 + public static Convertor createOnlineCZKUSDConvertor() {
1.49 + // initial rate: 1USD = 16CZK
1.50 + // 2nd query 1USD = 15.99CZK
1.51 + // 3rd query 1USD = 15.98CZK
1.52 + // until 1USD = 15.00CZK
1.53 + // then 1USD = 15.01CZK
1.54 + // then 1USD = 15.02CZK
1.55 + // and so on and on up to 1USD = 16CZK
1.56 + // and then another round to 15, etc.
1.57 + return null;
1.58 + }
1.59 +
1.60 + public void testFewQueriesForOnlineConvertor() {
1.61 + Convertor c = createOnlineCZKUSDConvertor();
1.62 + doFewQueriesForOnlineConvertor(c);
1.63 + }
1.64 +
1.65 + static void doFewQueriesForOnlineConvertor(Convertor c) {
1.66 + // convert $5 to CZK using c:
1.67 + //assertEquals("Result is 80 CZK");
1.68 +
1.69 + // convert $8 to CZK using c:
1.70 + //assertEquals("Result is 127.92 CZK");
1.71 +
1.72 + // convert $1 to CZK using c:
1.73 + //assertEquals("Result is 15.98 CZK");
1.74 +
1.75 + // convert 15.97CZK to USD using c:
1.76 + //assertEquals("Result is 1$");
1.77 +
1.78 + fail("Implement me!");
1.79 + }
1.80 +
1.81 + /** Join the convertors and show they behave sane.
1.82 + */
1.83 + public void testOnlineConvertorComposition() throws Exception {
1.84 + Convertor c = Task2Test.merge(
1.85 + createOnlineCZKUSDConvertor(),
1.86 + Task1Test.createSKKtoCZK()
1.87 + );
1.88 +
1.89 + // convert 16CZK to SKK using c:
1.90 + // assertEquals("Result is 20 SKK");
1.91 +
1.92 + // convert 500SKK to CZK using c:
1.93 + // assertEquals("Result is 400 CZK");
1.94 +
1.95 + doFewQueriesForOnlineConvertor(c);
1.96 + }
1.97 +}