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 Convertor c = createOnlineCZKUSDConvertor();
71 doFewQueriesForOnlineConvertor(c);
74 static void doFewQueriesForOnlineConvertor( final Convertor c ) {
75 // convert $5 to CZK using c:
76 final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) );
77 final MonetaryAmount a1 = r1.getNetAmount();
78 //assertEquals("Result is 80 CZK");
80 assertEquals( 80.0, a1.getAmount().doubleValue() );
81 assertEquals( CZK, a1.getCurrency() );
83 // convert $8 to CZK using c:
84 final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) );
85 final MonetaryAmount a2 = r2.getNetAmount();
86 //assertEquals("Result is 127.92 CZK");
88 assertEquals( 12792.0, a2.getAmount().movePointRight( 2 ).doubleValue() );
89 assertEquals( CZK, a2.getCurrency() );
91 // convert $1 to CZK using c:
92 final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1, USD ), CZK ) );
93 final MonetaryAmount a3 = r3.getNetAmount();
94 //assertEquals("Result is 15.98 CZK");
96 assertEquals( 1598.0, a3.getAmount().movePointRight( 2 ).doubleValue() );
97 assertEquals( CZK, a3.getCurrency() );
99 // convert 15.97CZK to USD using c:
100 final BigDecimal s4 = new BigDecimal( 1597 ).movePointLeft( 2 );
101 final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( s4, CZK ), USD ) );
102 final MonetaryAmount a4 = r4.getNetAmount();
103 //assertEquals("Result is 1$");
105 assertEquals( 1.0, a4.getAmount().doubleValue() );
106 assertEquals( USD, a4.getCurrency() );
109 /** Join the convertors and show they behave sane.
111 public void testOnlineConvertorComposition() throws Exception {
112 final Convertor c = Task2Test.merge(
113 createOnlineCZKUSDConvertor(),
114 Task1Test.createSKKtoCZK()
117 // convert 16CZK using c:
118 final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) );
119 final MonetaryAmount a4 = r4.getNetAmount();
120 // assertEquals("Result is 20 SKK");
122 assertEquals( 20.0, a4.getAmount().doubleValue() );
123 assertEquals( SKK, a4.getCurrency() );
125 // convert 500SKK to CZK using c:
126 final Convertor.ConversionResult r5 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) );
127 final MonetaryAmount a5 = r5.getNetAmount();
128 // assertEquals("Result is 400 CZK");
130 assertEquals( 400.0, a5.getAmount().doubleValue() );
131 assertEquals( CZK, a5.getCurrency() );
133 doFewQueriesForOnlineConvertor(c);