1 package org.apidesign.apifest08.test;
3 import java.util.Currency;
5 import junit.framework.TestCase;
6 import org.apidesign.apifest08.currency.Convertor;
7 import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
9 /** The exchange rates are not always the same. They are changing. Day by day,
10 * hour by hour, minute by minute. For every bank it is important to always
11 * have the actual exchange rate available in the system. That is why let's
12 * create a pluggable convertor that will always have up to date value of its
15 * The quest for today is to allow 3rd party developer to write a convertor
16 * that adjusts its exchange rate everytime it is queried. This convertor is
17 * written by independent vendor, the vendor knows only your Convertor API,
18 * he does not know how the whole system looks and how the convertor is supposed
21 public class Task3Test extends TestCase {
23 private static double actualRate;
24 private static boolean increasing;
26 private static final double EXCHANGE_RATE_MAX = 16.0;
27 private static final double EXCHANGE_RATE_MIN = 15.0;
29 public Task3Test(String testName) {
34 protected void setUp() throws Exception {
38 protected void tearDown() throws Exception {
41 // Backward compatibly enhance your existing API to support following
46 /** Without knowing anything about the surrounding system, write an
47 * implementation of convertor that will return different rates everytime
48 * it is queried. Convert USD to CZK and vice versa. Start with the rate of
49 * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
50 * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
51 * until you reach 1USD = 16CZK
53 * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
55 public static Convertor createOnlineCZKUSDConvertor() {
59 // sets actual exchange rates
62 // create new instance
63 Convertor convertor = null;
65 convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("CZK"));
66 } catch (UnknownConvertorException e) {
73 public void testFewQueriesForOnlineConvertor() throws Exception {
74 Convertor c = createOnlineCZKUSDConvertor();
75 doFewQueriesForOnlineConvertor(c);
78 static void doFewQueriesForOnlineConvertor(Convertor c) throws Exception {
79 // convert $5 to CZK using c:
80 double result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
81 double expectedResult = actualRate * 5;
82 assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result);
84 // change exchange rates
87 // convert $8 to CZK using c:
88 result = c.convert(8d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
89 expectedResult = actualRate * 8;
90 assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result);
92 // change exchange rates
95 // convert $1 to CZK using c:
96 result = c.convert(1d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
97 expectedResult = actualRate * 1;
98 assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result);
100 // change exchange rates
103 // convert 15.97CZK to USD using c:
104 result = c.convert(15.97d, Currency.getInstance("CZK"), Currency.getInstance("USD"));
105 expectedResult = 15.97 / actualRate ;
106 assertEquals("Result is not " + expectedResult + " USD", expectedResult, result);
109 /** Join the convertors and show they behave sane.
111 public void testOnlineConvertorComposition() throws Exception {
112 Convertor c = Task2Test.merge(
113 createOnlineCZKUSDConvertor(),
114 Task1Test.createSKKtoCZK()
117 // convert 16CZK to SKK using c:
118 double result = c.convert(16d, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
119 assertEquals("Result is not 20 SKK", 20d, result);
121 // convert 500SKK to CZK using c:
122 result = c.convert(500d, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
123 assertEquals("Result is not 400 CZK", 400d, result);
125 doFewQueriesForOnlineConvertor(c);
128 private static void setRates() {
129 // logic for change of actual exchange rate
132 if(actualRate == EXCHANGE_RATE_MAX){
137 if(actualRate == EXCHANGE_RATE_MIN){
142 // set exchange rates
143 Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), actualRate, 1d);