1 package org.apidesign.apifest08.test;
3 import java.math.BigDecimal;
4 import java.util.Currency;
5 import junit.framework.TestCase;
6 import org.apidesign.apifest08.currency.Convertor;
7 import org.apidesign.apifest08.currency.MonetaryAmount;
8 import org.apidesign.apifest08.currency.anothervendor.ZigZaggingBidirectionalConvertor;
10 /** The exchange rates are not always the same. They are changing. Day by day,
11 * hour by hour, minute by minute. For every bank it is important to always
12 * have the actual exchange rate available in the system. That is why let's
13 * create a pluggable convertor that will always have up to date value of its
16 * The quest for today is to allow 3rd party developer to write a convertor
17 * that adjusts its exchange rate everytime it is queried. This convertor is
18 * written by independent vendor, the vendor knows only your Convertor API,
19 * he does not know how the whole system looks and how the convertor is supposed
22 public class Task3Test extends TestCase {
23 public Task3Test(String testName) {
28 protected void setUp() throws Exception {
32 protected void tearDown() throws Exception {
35 protected static final Currency CZK = Currency.getInstance( "CZK" );
36 protected static final Currency SKK = Currency.getInstance( "SKK" );
37 protected static final Currency USD = Currency.getInstance( "USD" );
39 // Backward compatibly enhance your existing API to support following
44 /** Without knowing anything about the surrounding system, write an
45 * implementation of convertor that will return different rates everytime
46 * it is queried. Convert USD to CZK and vice versa. Start with the rate of
47 * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
48 * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
49 * until you reach 1USD = 16CZK
51 * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
53 public static Convertor createOnlineCZKUSDConvertor() {
54 // initial rate: 1USD = 16CZK
55 // 2nd query 1USD = 15.99CZK
56 // 3rd query 1USD = 15.98CZK
57 // until 1USD = 15.00CZK
58 // then 1USD = 15.01CZK
59 // then 1USD = 15.02CZK
60 // and so on and on up to 1USD = 16CZK
61 // and then another round to 15, etc.
62 final MonetaryAmount oneUSD = new MonetaryAmount( 1, USD );
63 final BigDecimal startAmount = new BigDecimal( 16 );
64 final BigDecimal endAmount = new BigDecimal( 15 );
65 final BigDecimal stepAmount = BigDecimal.ONE.movePointLeft( 2 ).negate();
66 return new ContractImposingDelegatingConvertor( new ZigZaggingBidirectionalConvertor( oneUSD, CZK, startAmount, endAmount, stepAmount ) );
69 public void testFewQueriesForOnlineConvertor() {
70 if (Boolean.getBoolean("ignore.failing")) {
75 Convertor c = createOnlineCZKUSDConvertor();
76 doFewQueriesForOnlineConvertor(c);
79 static void doFewQueriesForOnlineConvertor( final Convertor c ) {
80 // convert $5 to CZK using c:
81 final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) );
82 final MonetaryAmount a1 = r1.getNetAmount();
83 //assertEquals("Result is 80 CZK");
85 assertEquals( 80.0, a1.getAmount().doubleValue() );
86 assertEquals( CZK, a1.getCurrency() );
88 // convert $8 to CZK using c:
89 final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) );
90 final MonetaryAmount a2 = r2.getNetAmount();
91 //assertEquals("Result is 127.92 CZK");
93 assertEquals( 12792.0, a2.getAmount().movePointRight( 2 ).doubleValue() );
94 assertEquals( CZK, a2.getCurrency() );
96 // convert $1 to CZK using c:
97 final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1, USD ), CZK ) );
98 final MonetaryAmount a3 = r3.getNetAmount();
99 //assertEquals("Result is 15.98 CZK");
101 assertEquals( 1598.0, a3.getAmount().movePointRight( 2 ).doubleValue() );
102 assertEquals( CZK, a3.getCurrency() );
104 // convert 15.97CZK to USD using c:
105 final BigDecimal s4 = new BigDecimal( 1597 ).movePointLeft( 2 );
106 final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( s4, CZK ), USD ) );
107 final MonetaryAmount a4 = r4.getNetAmount();
108 //assertEquals("Result is 1$");
110 assertEquals( 1.0, a4.getAmount().doubleValue() );
111 assertEquals( USD, a4.getCurrency() );
114 /** Join the convertors and show they behave sane.
116 public void testOnlineConvertorComposition() throws Exception {
117 if (Boolean.getBoolean("ignore.failing")) {
122 final Convertor c = Task2Test.merge(
123 createOnlineCZKUSDConvertor(),
124 Task1Test.createSKKtoCZK()
127 // convert 16CZK using c:
128 final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) );
129 final MonetaryAmount a4 = r4.getNetAmount();
130 // assertEquals("Result is 20 SKK");
132 assertEquals( 20.0, a4.getAmount().doubleValue() );
133 assertEquals( SKK, a4.getCurrency() );
135 // convert 500SKK to CZK using c:
136 final Convertor.ConversionResult r5 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) );
137 final MonetaryAmount a5 = r5.getNetAmount();
138 // assertEquals("Result is 400 CZK");
140 assertEquals( 400.0, a5.getAmount().doubleValue() );
141 assertEquals( CZK, a5.getCurrency() );
143 doFewQueriesForOnlineConvertor(c);