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