1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task1/solution12/test/org/apidesign/apifest08/test/Task1Test.java Sun Sep 28 14:12:38 2008 +0200
1.3 @@ -0,0 +1,78 @@
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 +
1.12 +/**
1.13 + * Finish the Convertor API, and then write bodies of methods inside of this class to match the given tasks. To fullfil
1.14 + * your task, use the API define in the <code>org.apidesign.apifest08.currency</code> package. Do not you reflection, or
1.15 + * other hacks as your code shall run without any runtime permissions.
1.16 + */
1.17 +public class Task1Test extends TestCase {
1.18 + public Task1Test(String testName) {
1.19 + super(testName);
1.20 + }
1.21 +
1.22 + @Override
1.23 + protected void setUp() throws Exception {
1.24 + }
1.25 +
1.26 + @Override
1.27 + protected void tearDown() throws Exception {
1.28 + }
1.29 +
1.30 + /**
1.31 + * Create convertor that understands two currencies, CZK and USD. Make 1 USD == 17 CZK. Creation of the convertor
1.32 + * shall not require subclassing of any class or interface on the client side.
1.33 + * @return prepared convertor ready for converting USD to CZK and CZK to USD
1.34 + */
1.35 + public static Convertor createCZKtoUSD() {
1.36 + return Convertor.getConvertorInstance(Currency.getInstance("CZK"), Currency.getInstance("USD"));
1.37 + }
1.38 +
1.39 + /**
1.40 + * Create convertor that understands two currencies, CZK and SKK. Make 100 SKK == 80 CZK. Creation of the convertor
1.41 + * shall not require subclassing of any class or interface on the client side.
1.42 + * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
1.43 + */
1.44 + public static Convertor createSKKtoCZK() {
1.45 + return Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
1.46 + }
1.47 +
1.48 + /**
1.49 + * Use the convertor from <code>createCZKtoUSD</code> method and do few conversions with it.
1.50 + */
1.51 + public void testCurrencyCZKUSD() throws Exception {
1.52 + Convertor c = createCZKtoUSD();
1.53 +
1.54 + // convert $5 to CZK using c:
1.55 + double result = c.convert(5, Currency.getInstance("USD"));
1.56 + assertEquals("Result is not 85 CZK", 85.0, result);
1.57 +
1.58 + // convert $8 to CZK
1.59 + result = c.convert(8, Currency.getInstance("USD"));
1.60 + assertEquals("Result is not 136 CZK", 136.0, result);
1.61 +
1.62 + // convert 1003CZK to USD
1.63 + result = c.convert(1003, Currency.getInstance("CZK"));
1.64 + assertEquals("Result is not 59 USD", 59.0, result);
1.65 + }
1.66 +
1.67 + /**
1.68 + * Use the convertor from <code>createSKKtoCZK</code> method and do few conversions with it.
1.69 + */
1.70 + public void testCurrencySKKCZK() throws Exception {
1.71 + Convertor c = createSKKtoCZK();
1.72 +
1.73 + // convert 16CZK using c:
1.74 + double result = c.convert(16, Currency.getInstance("CZK"));
1.75 + assertEquals("Result is not 20 SKK", 20.0, result);
1.76 +
1.77 + // convert 500SKK to CZK
1.78 + result = c.convert(500, Currency.getInstance("SKK"));
1.79 + assertEquals("Result is not 400 CZK", 400.0, result);
1.80 + }
1.81 +}