1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task1/solution02/test/org/apidesign/apifest08/test/Task1Test.java Sun Sep 28 14:12:38 2008 +0200
1.3 @@ -0,0 +1,91 @@
1.4 +package org.apidesign.apifest08.test;
1.5 +
1.6 +import java.util.Currency;
1.7 +
1.8 +import junit.framework.TestCase;
1.9 +
1.10 +import org.apidesign.apifest08.currency.Convertor;
1.11 +import org.apidesign.apifest08.currency.ConvertorFactory;
1.12 +import org.apidesign.apifest08.currency.MoneyImpl;
1.13 +
1.14 +/** Finish the Convertor API, and then write bodies of methods inside
1.15 + * of this class to match the given tasks. To fullfil your task, use the
1.16 + * API define in the <code>org.apidesign.apifest08.currency</code> package.
1.17 + * Do not you reflection, or other hacks as your code
1.18 + * shall run without any runtime permissions.
1.19 + */
1.20 +public class Task1Test extends TestCase {
1.21 + public static final Currency USD = Currency.getInstance("USD");
1.22 + public static final Currency CZK = Currency.getInstance("CZK");
1.23 + public static final Currency SKK = Currency.getInstance("SKK");
1.24 + public Task1Test(String testName) {
1.25 + super(testName);
1.26 + }
1.27 +
1.28 + @Override
1.29 + protected void setUp() throws Exception {
1.30 + }
1.31 +
1.32 + @Override
1.33 + protected void tearDown() throws Exception {
1.34 + }
1.35 +
1.36 + /** Create convertor that understands two currencies, CZK and
1.37 + * USD. Make 1 USD == 17 CZK.
1.38 + *
1.39 + * Creation of the convertor shall not require subclassing of any class
1.40 + * or interface on the client side.
1.41 + *
1.42 + * @return prepared convertor ready for converting USD to CZK and CZK to USD
1.43 + */
1.44 + public static Convertor createCZKtoUSD() {
1.45 + //You can use both variants. I wrote the first one but than I realized that maybe
1.46 + //you want me to write the second one. So I have written both.
1.47 + return ConvertorFactory.createConvertor(CZK, USD);
1.48 + //return ConvertorFactory.createConvertor(new MoneyImpl(17,CZK), new MoneyImpl(1,USD));
1.49 + }
1.50 +
1.51 + /** Create convertor that understands two currencies, CZK and
1.52 + * SKK. Make 100 SKK == 80 CZK.
1.53 + *
1.54 + * Creation of the convertor shall not require subclassing of any class
1.55 + * or interface on the client side.
1.56 + *
1.57 + * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
1.58 + */
1.59 + public static Convertor createSKKtoCZK() {
1.60 + //You can use both variants. I wrote the first one but than I realized that maybe
1.61 + //you want me to write the second one. So I have written both.
1.62 + //return ConvertorFactory.createConvertor(SKK, CZK);
1.63 + return ConvertorFactory.createConvertor(new MoneyImpl(100,SKK), new MoneyImpl(80,CZK));
1.64 + }
1.65 +
1.66 + /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
1.67 + * with it.
1.68 + */
1.69 + public void testCurrencyCZKUSD() throws Exception {
1.70 + Convertor czkToUsdConvertor = createCZKtoUSD();
1.71 + // convert $5 to CZK using c:
1.72 + Convertor usdToCzkConvertor = czkToUsdConvertor.revert();
1.73 + assertEquals("Result is 85 CZK",new MoneyImpl(85,CZK), usdToCzkConvertor.convert(new MoneyImpl(5,USD)));
1.74 +
1.75 + // convert $8 to CZK
1.76 + assertEquals("Result is 136 CZK",new MoneyImpl(136,CZK), usdToCzkConvertor.convert(new MoneyImpl(8,USD)));
1.77 +
1.78 + // convert 1003CZK to USD
1.79 + assertEquals("Result is 59 USD", new MoneyImpl(59,USD), czkToUsdConvertor.convert(new MoneyImpl(1003,CZK)));
1.80 + }
1.81 +
1.82 + /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
1.83 + * with it.
1.84 + */
1.85 + public void testCurrencySKKCZK() throws Exception {
1.86 + Convertor skkToCzkConvertor = createSKKtoCZK();
1.87 + // convert 16CZK using c:
1.88 + assertEquals("Result is 20 SKK", new MoneyImpl(20,SKK), skkToCzkConvertor.revert().convert(new MoneyImpl(16,CZK)));
1.89 +
1.90 + // convert 500SKK to CZK
1.91 + assertEquals("Result is 400 CZK", new MoneyImpl(400,CZK), skkToCzkConvertor.convert(new MoneyImpl(500,SKK)));
1.92 + }
1.93 +}
1.94 +