japod@6
|
1 |
package org.apidesign.apifest08.test;
|
japod@6
|
2 |
|
japod@6
|
3 |
import java.util.Currency;
|
japod@6
|
4 |
|
japod@6
|
5 |
import junit.framework.TestCase;
|
japod@6
|
6 |
|
japod@6
|
7 |
import org.apidesign.apifest08.currency.Convertor;
|
japod@6
|
8 |
import org.apidesign.apifest08.currency.ConvertorFactory;
|
japod@6
|
9 |
import org.apidesign.apifest08.currency.MoneyImpl;
|
japod@6
|
10 |
|
japod@6
|
11 |
/** Finish the Convertor API, and then write bodies of methods inside
|
japod@6
|
12 |
* of this class to match the given tasks. To fullfil your task, use the
|
japod@6
|
13 |
* API define in the <code>org.apidesign.apifest08.currency</code> package.
|
japod@6
|
14 |
* Do not you reflection, or other hacks as your code
|
japod@6
|
15 |
* shall run without any runtime permissions.
|
japod@6
|
16 |
*/
|
japod@6
|
17 |
public class Task1Test extends TestCase {
|
japod@16
|
18 |
|
japod@6
|
19 |
public static final Currency USD = Currency.getInstance("USD");
|
japod@6
|
20 |
public static final Currency CZK = Currency.getInstance("CZK");
|
japod@6
|
21 |
public static final Currency SKK = Currency.getInstance("SKK");
|
japod@16
|
22 |
|
japod@6
|
23 |
public Task1Test(String testName) {
|
japod@6
|
24 |
super(testName);
|
japod@6
|
25 |
}
|
japod@6
|
26 |
|
japod@6
|
27 |
@Override
|
japod@6
|
28 |
protected void setUp() throws Exception {
|
japod@6
|
29 |
}
|
japod@6
|
30 |
|
japod@6
|
31 |
@Override
|
japod@6
|
32 |
protected void tearDown() throws Exception {
|
japod@6
|
33 |
}
|
japod@6
|
34 |
|
japod@16
|
35 |
//
|
japod@16
|
36 |
// Imagine that there are three parts of the whole system:
|
japod@16
|
37 |
// 1. there is someone who knows the current exchange rate
|
japod@16
|
38 |
// 2. there is someone who wants to do the conversion
|
japod@16
|
39 |
// 3. there is the API between 1. and 2. which allows them to communicate
|
japod@16
|
40 |
// Please design such API
|
japod@16
|
41 |
//
|
japod@16
|
42 |
|
japod@6
|
43 |
/** Create convertor that understands two currencies, CZK and
|
japod@16
|
44 |
* USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
|
japod@16
|
45 |
* e.g. those that know the exchange rate. They somehow need to create
|
japod@16
|
46 |
* the objects from the API and tell them the exchange rate. The API itself
|
japod@16
|
47 |
* knows nothing about any rates, before the createCZKtoUSD method is called.
|
japod@6
|
48 |
*
|
japod@6
|
49 |
* Creation of the convertor shall not require subclassing of any class
|
japod@6
|
50 |
* or interface on the client side.
|
japod@6
|
51 |
*
|
japod@6
|
52 |
* @return prepared convertor ready for converting USD to CZK and CZK to USD
|
japod@6
|
53 |
*/
|
japod@6
|
54 |
public static Convertor createCZKtoUSD() {
|
japod@16
|
55 |
return ConvertorFactory.createConvertor(new MoneyImpl(17,CZK), new MoneyImpl(1,USD));
|
japod@6
|
56 |
}
|
japod@6
|
57 |
|
japod@6
|
58 |
/** Create convertor that understands two currencies, CZK and
|
japod@16
|
59 |
* SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
|
japod@16
|
60 |
* it knows the exchange rate, and needs to use the API to create objects
|
japod@16
|
61 |
* with the exchange rate. Anyone shall be ready to call this method without
|
japod@16
|
62 |
* any other method being called previously. The API itself shall know
|
japod@16
|
63 |
* nothing about any rates, before this method is called.
|
japod@6
|
64 |
*
|
japod@6
|
65 |
* Creation of the convertor shall not require subclassing of any class
|
japod@6
|
66 |
* or interface on the client side.
|
japod@6
|
67 |
*
|
japod@6
|
68 |
* @return prepared convertor ready for converting SKK to CZK and CZK to SKK
|
japod@6
|
69 |
*/
|
japod@6
|
70 |
public static Convertor createSKKtoCZK() {
|
japod@16
|
71 |
return ConvertorFactory.createConvertor(new MoneyImpl(100,SKK), new MoneyImpl(80,CZK));
|
japod@6
|
72 |
}
|
japod@16
|
73 |
|
japod@16
|
74 |
//
|
japod@16
|
75 |
// now the methods for group #2 follow:
|
japod@16
|
76 |
// this group knows nothing about exchange rates, but knows how to use
|
japod@16
|
77 |
// the API to do conversions. It somehow (by calling one of the factory
|
japod@16
|
78 |
// methods) gets objects from the API and uses them to do the conversions.
|
japod@16
|
79 |
//
|
japod@6
|
80 |
|
japod@6
|
81 |
/** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
|
japod@6
|
82 |
* with it.
|
japod@6
|
83 |
*/
|
japod@6
|
84 |
public void testCurrencyCZKUSD() throws Exception {
|
japod@16
|
85 |
Convertor c = createCZKtoUSD();
|
japod@6
|
86 |
// convert $5 to CZK using c:
|
japod@16
|
87 |
assertEquals("Result is 85 CZK",new MoneyImpl(85,CZK), c.convert(new MoneyImpl(5,USD),CZK));
|
japod@6
|
88 |
|
japod@6
|
89 |
// convert $8 to CZK
|
japod@16
|
90 |
assertEquals("Result is 136 CZK",new MoneyImpl(136,CZK), c.convert(new MoneyImpl(8,USD),CZK));
|
japod@6
|
91 |
|
japod@6
|
92 |
// convert 1003CZK to USD
|
japod@16
|
93 |
assertEquals("Result is 59 USD", new MoneyImpl(59,USD), c.convert(new MoneyImpl(1003,CZK),USD));
|
japod@16
|
94 |
|
japod@6
|
95 |
}
|
japod@6
|
96 |
|
japod@6
|
97 |
/** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
|
japod@6
|
98 |
* with it.
|
japod@6
|
99 |
*/
|
japod@6
|
100 |
public void testCurrencySKKCZK() throws Exception {
|
japod@16
|
101 |
Convertor c = createSKKtoCZK();
|
japod@6
|
102 |
// convert 16CZK using c:
|
japod@16
|
103 |
assertEquals("Result is 20 SKK", new MoneyImpl(20,SKK), c.convert(new MoneyImpl(16,CZK),SKK));
|
japod@6
|
104 |
|
japod@6
|
105 |
// convert 500SKK to CZK
|
japod@16
|
106 |
assertEquals("Result is 400 CZK", new MoneyImpl(400,CZK), c.convert(new MoneyImpl(500,SKK),CZK));
|
japod@16
|
107 |
}
|
japod@16
|
108 |
|
japod@16
|
109 |
/** Verify that the CZK to USD convertor knows nothing about SKK.
|
japod@16
|
110 |
*/
|
japod@16
|
111 |
public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
|
japod@16
|
112 |
Convertor c = createCZKtoUSD();
|
japod@16
|
113 |
// convert $5 to SKK, the API shall say this is not possible
|
japod@16
|
114 |
try
|
japod@16
|
115 |
{
|
japod@16
|
116 |
c.convert(new MoneyImpl(5, USD), SKK);
|
japod@16
|
117 |
fail("Exception expected");
|
japod@16
|
118 |
}
|
japod@16
|
119 |
catch(IllegalArgumentException e)
|
japod@16
|
120 |
{
|
japod@16
|
121 |
assertTrue("Ok",true);
|
japod@16
|
122 |
}
|
japod@16
|
123 |
// convert 500 SKK to CZK, the API shall say this is not possible
|
japod@16
|
124 |
try
|
japod@16
|
125 |
{
|
japod@16
|
126 |
c.convert(new MoneyImpl(500, SKK), CZK);
|
japod@16
|
127 |
fail("Exception expected");
|
japod@16
|
128 |
}
|
japod@16
|
129 |
catch(IllegalArgumentException e)
|
japod@16
|
130 |
{
|
japod@16
|
131 |
assertTrue("Ok",true);
|
japod@16
|
132 |
}
|
japod@16
|
133 |
}
|
japod@16
|
134 |
|
japod@16
|
135 |
/** Verify that the CZK to SKK convertor knows nothing about USD.
|
japod@16
|
136 |
*/
|
japod@16
|
137 |
public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception {
|
japod@16
|
138 |
Convertor c = createSKKtoCZK();
|
japod@16
|
139 |
// convert $5 to SKK, the API shall say this is not possible
|
japod@16
|
140 |
try
|
japod@16
|
141 |
{
|
japod@16
|
142 |
c.convert(new MoneyImpl(5, USD), SKK);
|
japod@16
|
143 |
fail("Exception expected");
|
japod@16
|
144 |
}
|
japod@16
|
145 |
catch(IllegalArgumentException e)
|
japod@16
|
146 |
{
|
japod@16
|
147 |
assertTrue("Ok",true);
|
japod@16
|
148 |
}
|
japod@16
|
149 |
// convert 500 CZK to USD, the API shall say this is not possible
|
japod@16
|
150 |
try
|
japod@16
|
151 |
{
|
japod@16
|
152 |
c.convert(new MoneyImpl(500, CZK), USD);
|
japod@16
|
153 |
fail("Exception expected");
|
japod@16
|
154 |
}
|
japod@16
|
155 |
catch(IllegalArgumentException e)
|
japod@16
|
156 |
{
|
japod@16
|
157 |
assertTrue("Ok",true);
|
japod@16
|
158 |
}
|
japod@6
|
159 |
}
|
japod@6
|
160 |
}
|