1 package org.apidesign.apifest08.test;
3 import junit.framework.TestCase;
4 import org.apidesign.apifest08.currency.Convertor;
6 /** Finish the Convertor API, and then write bodies of methods inside
7 * of this class to match the given tasks. To fullfil your task, use the
8 * API define in the <code>org.apidesign.apifest08.currency</code> package.
9 * Do not you reflection, or other hacks as your code
10 * shall run without any runtime permissions.
12 public class Task1Test extends TestCase {
13 public Task1Test(String testName) {
18 protected void setUp() throws Exception {
22 protected void tearDown() throws Exception {
26 // Imagine that there are three parts of the whole system:
27 // 1. there is someone who knows the current exchange rate
28 // 2. there is someone who wants to do the conversion
29 // 3. there is the API between 1. and 2. which allows them to communicate
30 // Please design such API
33 /** Create convertor that understands two currencies, CZK and
34 * USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
35 * e.g. those that know the exchange rate. They somehow need to create
36 * the objects from the API and tell them the exchange rate. The API itself
37 * knows nothing about any rates, before the createCZKtoUSD method is called.
39 * Creation of the convertor shall not require subclassing of any class
40 * or interface on the client side.
42 * @return prepared convertor ready for converting USD to CZK and CZK to USD
44 public static Convertor createCZKtoUSD() {
48 /** Create convertor that understands two currencies, CZK and
49 * SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
50 * it knows the exchange rate, and needs to use the API to create objects
51 * with the exchange rate. Anyone shall be ready to call this method without
52 * any other method being called previously. The API itself shall know
53 * nothing about any rates, before this method is called.
55 * Creation of the convertor shall not require subclassing of any class
56 * or interface on the client side.
58 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
60 public static Convertor createSKKtoCZK() {
65 // now the methods for group #2 follow:
66 // this group knows nothing about exchange rates, but knows how to use
67 // the API to do conversions. It somehow (by calling one of the factory
68 // methods) gets objects from the API and uses them to do the conversions.
71 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
74 public void testCurrencyCZKUSD() throws Exception {
75 Convertor c = createCZKtoUSD();
76 // convert $5 to CZK using c:
77 // assertEquals("Result is 85 CZK");
80 // assertEquals("Result is 136 CZK");
82 // convert 1003CZK to USD
83 // assertEquals("Result is 59 USD");
86 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
89 public void testCurrencySKKCZK() throws Exception {
90 Convertor c = createSKKtoCZK();
91 // convert 16CZK using c:
92 // assertEquals("Result is 20 SKK");
94 // convert 500SKK to CZK
95 // assertEquals("Result is 400 CZK");
98 /** Verify that the CZK to USD convertor knows nothing about SKK.
100 public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
101 Convertor c = createCZKtoUSD();
102 // convert $5 to SKK, the API shall say this is not possible
103 // convert 500 SKK to CZK, the API shall say this is not possible
106 /** Verify that the CZK to SKK convertor knows nothing about USD.
108 public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception {
109 Convertor c = createSKKtoCZK();
110 // convert $5 to SKK, the API shall say this is not possible
111 // convert 500 CZK to USD, the API shall say this is not possible