1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task1/solution08/test/org/apidesign/apifest08/test/Task1Test.java Sun Sep 28 14:12:38 2008 +0200
1.3 @@ -0,0 +1,86 @@
1.4 +package org.apidesign.apifest08.test;
1.5 +
1.6 +import java.util.Currency;
1.7 +import junit.framework.TestCase;
1.8 +import org.apidesign.apifest08.currency.Convertor;
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 createCZKtoUSD() {
1.38 + return Convertor.getInstanceFor(Currency.getInstance("CZK"), Currency.getInstance("USD"));
1.39 + }
1.40 +
1.41 + /** Create convertor that understands two currencies, CZK and
1.42 + * SKK. Make 100 SKK == 80 CZK.
1.43 + *
1.44 + * Creation of the convertor shall not require subclassing of any class
1.45 + * or interface on the client side.
1.46 + *
1.47 + * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
1.48 + */
1.49 + public static Convertor createSKKtoCZK() {
1.50 + return Convertor.getInstanceFor(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
1.51 + }
1.52 +
1.53 + /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
1.54 + * with it.
1.55 + */
1.56 + public void testCurrencyCZKUSD() throws Exception {
1.57 + Convertor c = createCZKtoUSD();
1.58 + Currency usd = Currency.getInstance("USD");
1.59 + Currency czk = Currency.getInstance("CZK");
1.60 + // convert $5 to CZK using c:
1.61 + // assertEquals("Result is 85 CZK");
1.62 + assertEquals(85F, c.convert(5, usd));
1.63 +
1.64 + // convert $8 to CZK
1.65 + // assertEquals("Result is 136 CZK");
1.66 + assertEquals(136F, c.convert(8, usd));
1.67 +
1.68 + // convert 1003CZK to USD
1.69 + // assertEquals("Result is 59 USD");
1.70 + assertEquals(59F, c.convert(1003, czk));
1.71 + }
1.72 +
1.73 + /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
1.74 + * with it.
1.75 + */
1.76 + public void testCurrencySKKCZK() throws Exception {
1.77 + Convertor c = createSKKtoCZK();
1.78 + Currency skk = Currency.getInstance("SKK");
1.79 + Currency czk = Currency.getInstance("CZK");
1.80 + // convert 16CZK using c:
1.81 + // assertEquals("Result is 20 SKK");
1.82 + assertEquals(20F, c.convert(16, skk));
1.83 +
1.84 + // convert 500SKK to CZK
1.85 + // assertEquals("Result is 400 CZK");
1.86 + assertEquals(400F, c.convert(500, czk));
1.87 + }
1.88 +}
1.89 +