author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Tue, 07 Oct 2008 10:43:30 +0200 | |
changeset 43 | ba28def25913 |
child 46 | b61dcd332269 |
permissions | -rw-r--r-- |
jaroslav@43 | 1 |
package org.apidesign.apifest08.test; |
jaroslav@43 | 2 |
|
jaroslav@43 | 3 |
import junit.framework.TestCase; |
jaroslav@43 | 4 |
import org.apidesign.apifest08.currency.Convertor; |
jaroslav@43 | 5 |
|
jaroslav@43 | 6 |
/** The exchange rates are not always the same. They are changing. Day by day, |
jaroslav@43 | 7 |
* hour by hour, minute by minute. For every bank it is important to always |
jaroslav@43 | 8 |
* have the actual exchange rate available in the system. That is why let's |
jaroslav@43 | 9 |
* create a pluggable convertor that will always have up to date value of its |
jaroslav@43 | 10 |
* exchange rate. |
jaroslav@43 | 11 |
* <p> |
jaroslav@43 | 12 |
* The quest for today is to allow 3rd party developer to write a convertor |
jaroslav@43 | 13 |
* that adjusts its exchange rate everytime it is queried. This convertor is |
jaroslav@43 | 14 |
* written by independent vendor, the vendor knows only your Convertor API, |
jaroslav@43 | 15 |
* he does not know how the whole system looks and how the convertor is supposed |
jaroslav@43 | 16 |
* to be used. |
jaroslav@43 | 17 |
*/ |
jaroslav@43 | 18 |
public class Task3Test extends TestCase { |
jaroslav@43 | 19 |
public Task3Test(String testName) { |
jaroslav@43 | 20 |
super(testName); |
jaroslav@43 | 21 |
} |
jaroslav@43 | 22 |
|
jaroslav@43 | 23 |
@Override |
jaroslav@43 | 24 |
protected void setUp() throws Exception { |
jaroslav@43 | 25 |
} |
jaroslav@43 | 26 |
|
jaroslav@43 | 27 |
@Override |
jaroslav@43 | 28 |
protected void tearDown() throws Exception { |
jaroslav@43 | 29 |
} |
jaroslav@43 | 30 |
|
jaroslav@43 | 31 |
// Backward compatibly enhance your existing API to support following |
jaroslav@43 | 32 |
// usecases: |
jaroslav@43 | 33 |
// |
jaroslav@43 | 34 |
|
jaroslav@43 | 35 |
|
jaroslav@43 | 36 |
/** Without knowing anything about the surrounding system, write an |
jaroslav@43 | 37 |
* implementation of convertor that will return different rates everytime |
jaroslav@43 | 38 |
* it is queried. Convert USD to CZK and vice versa. Start with the rate of |
jaroslav@43 | 39 |
* 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query. |
jaroslav@43 | 40 |
* As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD |
jaroslav@43 | 41 |
* until you reach 1USD = 16CZK |
jaroslav@43 | 42 |
* |
jaroslav@43 | 43 |
* @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK |
jaroslav@43 | 44 |
*/ |
jaroslav@43 | 45 |
public static Convertor createOnlineCZKUSDConvertor() { |
jaroslav@43 | 46 |
// initial rate: 1USD = 16CZK |
jaroslav@43 | 47 |
// 2nd query 1USD = 15.99CZK |
jaroslav@43 | 48 |
// 3rd query 1USD = 15.98CZK |
jaroslav@43 | 49 |
// until 1USD = 15.00CZK |
jaroslav@43 | 50 |
// then 1USD = 15.01CZK |
jaroslav@43 | 51 |
// then 1USD = 15.02CZK |
jaroslav@43 | 52 |
// and so on and on up to 1USD = 16CZK |
jaroslav@43 | 53 |
// and then another round to 15, etc. |
jaroslav@43 | 54 |
return null; |
jaroslav@43 | 55 |
} |
jaroslav@43 | 56 |
|
jaroslav@43 | 57 |
public void testFewQueriesForOnlineConvertor() { |
jaroslav@43 | 58 |
Convertor c = createOnlineCZKUSDConvertor(); |
jaroslav@43 | 59 |
doFewQueriesForOnlineConvertor(c); |
jaroslav@43 | 60 |
} |
jaroslav@43 | 61 |
|
jaroslav@43 | 62 |
static void doFewQueriesForOnlineConvertor(Convertor c) { |
jaroslav@43 | 63 |
// convert $5 to CZK using c: |
jaroslav@43 | 64 |
//assertEquals("Result is 80 CZK"); |
jaroslav@43 | 65 |
|
jaroslav@43 | 66 |
// convert $8 to CZK using c: |
jaroslav@43 | 67 |
//assertEquals("Result is 127.92 CZK"); |
jaroslav@43 | 68 |
|
jaroslav@43 | 69 |
// convert $1 to CZK using c: |
jaroslav@43 | 70 |
//assertEquals("Result is 15.98 CZK"); |
jaroslav@43 | 71 |
|
jaroslav@43 | 72 |
// convert 15.97CZK to USD using c: |
jaroslav@43 | 73 |
//assertEquals("Result is 1$"); |
jaroslav@43 | 74 |
|
jaroslav@43 | 75 |
fail("Implement me!"); |
jaroslav@43 | 76 |
} |
jaroslav@43 | 77 |
|
jaroslav@43 | 78 |
/** Join the convertors and show they behave sane. |
jaroslav@43 | 79 |
*/ |
jaroslav@43 | 80 |
public void testOnlineConvertorComposition() throws Exception { |
jaroslav@43 | 81 |
Convertor c = Task2Test.merge( |
jaroslav@43 | 82 |
createOnlineCZKUSDConvertor(), |
jaroslav@43 | 83 |
Task1Test.createSKKtoCZK() |
jaroslav@43 | 84 |
); |
jaroslav@43 | 85 |
|
jaroslav@43 | 86 |
// convert 16CZK to SKK using c: |
jaroslav@43 | 87 |
// assertEquals("Result is 20 SKK"); |
jaroslav@43 | 88 |
|
jaroslav@43 | 89 |
// convert 500SKK to CZK using c: |
jaroslav@43 | 90 |
// assertEquals("Result is 400 CZK"); |
jaroslav@43 | 91 |
|
jaroslav@43 | 92 |
doFewQueriesForOnlineConvertor(c); |
jaroslav@43 | 93 |
} |
jaroslav@43 | 94 |
} |