japod@6
|
1 |
package org.apidesign.apifest08.test;
|
japod@6
|
2 |
|
japod@17
|
3 |
|
japod@6
|
4 |
import java.math.BigDecimal;
|
japod@6
|
5 |
import java.util.Currency;
|
japod@6
|
6 |
import junit.framework.TestCase;
|
japod@6
|
7 |
import org.apidesign.apifest08.currency.Convertor;
|
japod@6
|
8 |
import org.apidesign.apifest08.currency.ConvertorFactory;
|
japod@17
|
9 |
import org.apidesign.apifest08.currency.InvalidConversionException;
|
japod@17
|
10 |
|
japod@6
|
11 |
|
japod@6
|
12 |
/** Finish the Convertor API, and then write bodies of methods inside
|
japod@6
|
13 |
* of this class to match the given tasks. To fullfil your task, use the
|
japod@6
|
14 |
* API define in the <code>org.apidesign.apifest08.currency</code> package.
|
japod@6
|
15 |
* Do not you reflection, or other hacks as your code
|
japod@6
|
16 |
* shall run without any runtime permissions.
|
japod@6
|
17 |
*/
|
japod@6
|
18 |
public class Task1Test extends TestCase {
|
japod@17
|
19 |
|
japod@17
|
20 |
private final static Currency CZK;
|
japod@17
|
21 |
private final static Currency SKK;
|
japod@17
|
22 |
private final static Currency USD;
|
japod@17
|
23 |
|
japod@17
|
24 |
static
|
japod@17
|
25 |
{
|
japod@17
|
26 |
CZK = Currency.getInstance("CZK");
|
japod@17
|
27 |
SKK = Currency.getInstance("SKK");
|
japod@17
|
28 |
USD = Currency.getInstance("USD");
|
japod@17
|
29 |
}
|
japod@17
|
30 |
|
japod@6
|
31 |
public Task1Test(String testName) {
|
japod@6
|
32 |
super(testName);
|
japod@6
|
33 |
}
|
japod@6
|
34 |
|
japod@6
|
35 |
@Override
|
japod@6
|
36 |
protected void setUp() throws Exception {
|
japod@6
|
37 |
}
|
japod@6
|
38 |
|
japod@6
|
39 |
@Override
|
japod@6
|
40 |
protected void tearDown() throws Exception {
|
japod@6
|
41 |
}
|
japod@6
|
42 |
|
japod@6
|
43 |
/** Create convertor that understands two currencies, CZK and
|
japod@6
|
44 |
* USD. Make 1 USD == 17 CZK.
|
japod@6
|
45 |
*
|
japod@6
|
46 |
* Creation of the convertor shall not require subclassing of any class
|
japod@6
|
47 |
* or interface on the client side.
|
japod@6
|
48 |
*
|
japod@6
|
49 |
* @return prepared convertor ready for converting USD to CZK and CZK to USD
|
japod@6
|
50 |
*/
|
japod@6
|
51 |
public static Convertor createCZKtoUSD()
|
japod@6
|
52 |
{
|
japod@17
|
53 |
return (ConvertorFactory.getConvertor("CZK", BigDecimal.valueOf(17.0), "USD", BigDecimal.valueOf(1)));
|
japod@6
|
54 |
}
|
japod@6
|
55 |
|
japod@6
|
56 |
/** Create convertor that understands two currencies, CZK and
|
japod@6
|
57 |
* SKK. Make 100 SKK == 80 CZK.
|
japod@6
|
58 |
*
|
japod@6
|
59 |
* Creation of the convertor shall not require subclassing of any class
|
japod@6
|
60 |
* or interface on the client side.
|
japod@6
|
61 |
*
|
japod@6
|
62 |
* @return prepared convertor ready for converting SKK to CZK and CZK to SKK
|
japod@6
|
63 |
*/
|
japod@6
|
64 |
public static Convertor createSKKtoCZK()
|
japod@6
|
65 |
{
|
japod@17
|
66 |
return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), BigDecimal.valueOf(100), Currency.getInstance("CZK"), BigDecimal.valueOf(80)));
|
japod@6
|
67 |
}
|
japod@6
|
68 |
|
japod@6
|
69 |
/** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
|
japod@6
|
70 |
* with it.
|
japod@6
|
71 |
*/
|
japod@6
|
72 |
public void testCurrencyCZKUSD() throws Exception {
|
japod@6
|
73 |
Convertor c = createCZKtoUSD();
|
japod@6
|
74 |
BigDecimal result;
|
japod@6
|
75 |
|
japod@6
|
76 |
// convert $5 to CZK using c:
|
japod@6
|
77 |
// assertEquals("Result is 85 CZK");
|
japod@17
|
78 |
result = c.convert(USD, CZK, BigDecimal.valueOf(5));
|
japod@6
|
79 |
assertEquals(new BigDecimal("85.00"), result);
|
japod@6
|
80 |
|
japod@6
|
81 |
// convert $8 to CZK
|
japod@6
|
82 |
// assertEquals("Result is 136 CZK");
|
japod@17
|
83 |
result = c.convert(USD, CZK, BigDecimal.valueOf(8));
|
japod@6
|
84 |
assertEquals(new BigDecimal("136.00"), result);
|
japod@6
|
85 |
|
japod@6
|
86 |
// convert 1003CZK to USD
|
japod@6
|
87 |
// assertEquals("Result is 59 USD");
|
japod@17
|
88 |
result = c.convert(CZK, USD, BigDecimal.valueOf(1003));
|
japod@6
|
89 |
assertEquals(new BigDecimal("59.00"), result);
|
japod@6
|
90 |
}
|
japod@6
|
91 |
|
japod@6
|
92 |
/** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
|
japod@6
|
93 |
* with it.
|
japod@6
|
94 |
*/
|
japod@6
|
95 |
public void testCurrencySKKCZK() throws Exception {
|
japod@6
|
96 |
Convertor c = createSKKtoCZK();
|
japod@6
|
97 |
BigDecimal result;
|
japod@6
|
98 |
|
japod@6
|
99 |
// convert 16CZK using c:
|
japod@6
|
100 |
// assertEquals("Result is 20 SKK");
|
japod@17
|
101 |
result = c.convert(CZK, SKK, BigDecimal.valueOf(16));
|
japod@6
|
102 |
assertEquals(new BigDecimal("20.00"), result);
|
japod@6
|
103 |
|
japod@6
|
104 |
// convert 500SKK to CZK
|
japod@6
|
105 |
// assertEquals("Result is 400 CZK");
|
japod@17
|
106 |
result = c.convert(SKK, CZK, BigDecimal.valueOf(500));
|
japod@6
|
107 |
assertEquals(new BigDecimal("400.00"), result);
|
japod@6
|
108 |
}
|
japod@6
|
109 |
|
japod@17
|
110 |
/**
|
japod@17
|
111 |
* Verify that the CZK to USD convertor knows nothing about SKK.
|
japod@6
|
112 |
*/
|
japod@17
|
113 |
public void testCannotConvertToSKKwithCZKUSDConvertor()
|
japod@17
|
114 |
throws Exception
|
japod@17
|
115 |
{
|
japod@17
|
116 |
Convertor c = createCZKtoUSD();
|
japod@6
|
117 |
|
japod@17
|
118 |
try
|
japod@17
|
119 |
{
|
japod@17
|
120 |
// convert $5 to SKK, the API shall say this is not possible
|
japod@17
|
121 |
c.convert(USD, SKK, BigDecimal.valueOf(5));
|
japod@17
|
122 |
fail("cannot use the CZKtoUSD converter to convert to SKK");
|
japod@17
|
123 |
}
|
japod@17
|
124 |
catch(InvalidConversionException ex)
|
japod@17
|
125 |
{
|
japod@17
|
126 |
assertEquals("cannot convert to: SKK", ex.getMessage());
|
japod@17
|
127 |
assertEquals(SKK, ex.getBadCurrency());
|
japod@17
|
128 |
assertEquals(CZK, ex.getCurrencyA());
|
japod@17
|
129 |
assertEquals(USD, ex.getCurrencyB());
|
japod@17
|
130 |
}
|
japod@17
|
131 |
|
japod@17
|
132 |
try
|
japod@17
|
133 |
{
|
japod@17
|
134 |
// convert 500 SKK to CZK, the API shall say this is not possible
|
japod@17
|
135 |
c.convert(SKK, CZK, BigDecimal.valueOf(5));
|
japod@17
|
136 |
fail("cannot use the CZKtoUSD converter to convert from SKK");
|
japod@17
|
137 |
}
|
japod@17
|
138 |
catch(InvalidConversionException ex)
|
japod@17
|
139 |
{
|
japod@17
|
140 |
assertEquals("cannot convert from: SKK", ex.getMessage());
|
japod@17
|
141 |
assertEquals(SKK, ex.getBadCurrency());
|
japod@17
|
142 |
assertEquals(CZK, ex.getCurrencyA());
|
japod@17
|
143 |
assertEquals(USD, ex.getCurrencyB());
|
japod@17
|
144 |
}
|
japod@17
|
145 |
}
|
japod@17
|
146 |
|
japod@17
|
147 |
/**
|
japod@17
|
148 |
* Verify that the CZK to SKK convertor knows nothing about USD.
|
japod@17
|
149 |
*/
|
japod@17
|
150 |
public void testCannotConvertToUSDwithSKKCZKConvertor()
|
japod@17
|
151 |
throws Exception
|
japod@17
|
152 |
{
|
japod@17
|
153 |
Convertor c = createSKKtoCZK();
|
japod@17
|
154 |
|
japod@17
|
155 |
try
|
japod@17
|
156 |
{
|
japod@17
|
157 |
// convert $5 to SKK, the API shall say this is not possible
|
japod@17
|
158 |
c.convert(USD, SKK, BigDecimal.valueOf(5));
|
japod@17
|
159 |
fail("cannot use the CZKtoUSD converter to convert to SKK");
|
japod@17
|
160 |
}
|
japod@17
|
161 |
catch(InvalidConversionException ex)
|
japod@17
|
162 |
{
|
japod@17
|
163 |
assertEquals("cannot convert from: USD", ex.getMessage());
|
japod@17
|
164 |
assertEquals(USD, ex.getBadCurrency());
|
japod@17
|
165 |
assertEquals(SKK, ex.getCurrencyA());
|
japod@17
|
166 |
assertEquals(CZK, ex.getCurrencyB());
|
japod@17
|
167 |
}
|
japod@17
|
168 |
|
japod@17
|
169 |
try
|
japod@17
|
170 |
{
|
japod@17
|
171 |
// convert 500 CZK to USD, the API shall say this is not possible
|
japod@17
|
172 |
c.convert(CZK, USD, BigDecimal.valueOf(500));
|
japod@17
|
173 |
fail("cannot use the CZKtoUSD converter to convert from SKK");
|
japod@17
|
174 |
}
|
japod@17
|
175 |
catch(InvalidConversionException ex)
|
japod@17
|
176 |
{
|
japod@17
|
177 |
assertEquals("cannot convert to: USD", ex.getMessage());
|
japod@17
|
178 |
assertEquals(USD, ex.getBadCurrency());
|
japod@17
|
179 |
assertEquals(SKK, ex.getCurrencyA());
|
japod@17
|
180 |
assertEquals(CZK, ex.getCurrencyB());
|
japod@17
|
181 |
}
|
japod@17
|
182 |
}
|
japod@6
|
183 |
}
|
japod@6
|
184 |
|