1 package org.apidesign.apifest08.test;
3 import junit.framework.TestCase;
4 import org.apidesign.apifest08.currency.Convertor;
6 /** The exchange rates are not always the same. They are changing. Day by day,
7 * hour by hour, minute by minute. For every bank it is important to always
8 * have the actual exchange rate available in the system. That is why let's
9 * create a pluggable convertor that will always have up to date value of its
12 * The quest for today is to allow 3rd party developer to write a convertor
13 * that adjusts its exchange rate everytime it is queried. This convertor is
14 * written by independent vendor, the vendor knows only your Convertor API,
15 * he does not know how the whole system looks and how the convertor is supposed
18 public class Task3Test extends TestCase {
19 public Task3Test(String testName) {
24 protected void setUp() throws Exception {
28 protected void tearDown() throws Exception {
31 // Backward compatibly enhance your existing API to support following
36 /** Without knowing anything about the surrounding system, write an
37 * implementation of convertor that will return different rates everytime
38 * it is queried. Convert USD to CZK and vice versa. Start with the rate of
39 * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
40 * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
41 * until you reach 1USD = 16CZK
43 * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
45 public static Convertor createOnlineCZKUSDConvertor() {
46 // initial rate: 1USD = 16CZK
47 // 2nd query 1USD = 15.99CZK
48 // 3rd query 1USD = 15.98CZK
49 // until 1USD = 15.00CZK
50 // then 1USD = 15.01CZK
51 // then 1USD = 15.02CZK
52 // and so on and on up to 1USD = 16CZK
53 // and then another round to 15, etc.
57 public void testFewQueriesForOnlineConvertor() {
58 if (Boolean.getBoolean("ignore.failing")) {
63 Convertor c = createOnlineCZKUSDConvertor();
64 doFewQueriesForOnlineConvertor(c);
67 static void doFewQueriesForOnlineConvertor(Convertor c) {
68 // convert $5 to CZK using c:
69 //assertEquals("Result is 80 CZK");
71 // convert $8 to CZK using c:
72 //assertEquals("Result is 127.92 CZK");
74 // convert $1 to CZK using c:
75 //assertEquals("Result is 15.98 CZK");
77 // convert 15.97CZK to USD using c:
78 //assertEquals("Result is 1$");
80 fail("Implement me!");
83 /** Join the convertors and show they behave sane.
85 public void testOnlineConvertorComposition() throws Exception {
86 if (Boolean.getBoolean("ignore.failing")) {
91 Convertor c = Task2Test.merge(
92 createOnlineCZKUSDConvertor(),
93 Task1Test.createSKKtoCZK()
96 // convert 16CZK to SKK using c:
97 // assertEquals("Result is 20 SKK");
99 // convert 500SKK to CZK using c:
100 // assertEquals("Result is 400 CZK");
102 doFewQueriesForOnlineConvertor(c);