task3/solution14/test/org/apidesign/apifest08/test/Task3Test.java
changeset 54 1b300c79f4ce
parent 50 03c5c5dc94e7
child 59 c1d43bc1e9c0
     1.1 --- a/task3/solution14/test/org/apidesign/apifest08/test/Task3Test.java	Wed Oct 08 13:24:54 2008 +0200
     1.2 +++ b/task3/solution14/test/org/apidesign/apifest08/test/Task3Test.java	Fri Oct 10 22:02:31 2008 +0200
     1.3 @@ -2,6 +2,9 @@
     1.4  
     1.5  import junit.framework.TestCase;
     1.6  import org.apidesign.apifest08.currency.Convertor;
     1.7 +import org.apidesign.apifest08.currency.ConvertorFactory;
     1.8 +import org.apidesign.apifest08.currency.CurrencyRate;
     1.9 +import org.apidesign.apifest08.currency.Rate;
    1.10  
    1.11  /** The exchange rates are not always the same. They are changing. Day by day,
    1.12   * hour by hour, minute by minute. For every bank it is important to always
    1.13 @@ -51,14 +54,47 @@
    1.14          // then 1USD = 15.02CZK
    1.15          // and so on and on up to 1USD = 16CZK
    1.16          // and then another round to 15, etc.
    1.17 -        return null;
    1.18 +        CurrencyRate onlineCurrencyRate = new CurrencyRate() {
    1.19 +            private int usdAmount = 100;
    1.20 +            private int czkAmount = 1600;
    1.21 +            private boolean up = false;
    1.22 +            public String getCurrency1() {
    1.23 +                return "USD";
    1.24 +            }
    1.25 +
    1.26 +            public String getCurrency2() {
    1.27 +                return "CZK";
    1.28 +            }
    1.29 +
    1.30 +            public Rate getRate() { //return Rate according to online status
    1.31 +                Rate rate = new Rate(usdAmount, czkAmount);
    1.32 +                if (up) {
    1.33 +                    if (czkAmount < 1600) {
    1.34 +                        czkAmount++;
    1.35 +                    } else {
    1.36 +                        up = false;
    1.37 +                        czkAmount--;
    1.38 +                    }
    1.39 +                } else { //down
    1.40 +                    if (czkAmount > 1500) {
    1.41 +                        czkAmount--;
    1.42 +                    } else {
    1.43 +                        up = true;
    1.44 +                        czkAmount++;
    1.45 +                    }
    1.46 +                }
    1.47 +                return rate;
    1.48 +            }
    1.49 +        };
    1.50 +
    1.51 +        return ConvertorFactory.newInstance().createConvertor(onlineCurrencyRate);
    1.52      }
    1.53  
    1.54      public void testFewQueriesForOnlineConvertor() {
    1.55 -        if (Boolean.getBoolean("ignore.failing")) {
    1.56 -            // implement me!
    1.57 -            return;
    1.58 -        }
    1.59 +//        if (Boolean.getBoolean("ignore.failing")) {
    1.60 +//            // implement me!
    1.61 +//            return;
    1.62 +//        }
    1.63  
    1.64          Convertor c = createOnlineCZKUSDConvertor();
    1.65          doFewQueriesForOnlineConvertor(c);
    1.66 @@ -67,27 +103,25 @@
    1.67      static void doFewQueriesForOnlineConvertor(Convertor c) {
    1.68          // convert $5 to CZK using c:
    1.69          //assertEquals("Result is 80 CZK");
    1.70 +        assertEquals("Result is 80 CZK", 80.0, c.convert("USD", "CZK", 5), 0.001);
    1.71  
    1.72          // convert $8 to CZK using c:
    1.73          //assertEquals("Result is 127.92 CZK");
    1.74 +        assertEquals("Result is 127.92 CZK", 127.92, c.convert("USD", "CZK", 8), 0.001);
    1.75  
    1.76          // convert $1 to CZK using c:
    1.77          //assertEquals("Result is 15.98 CZK");
    1.78 +        assertEquals("Result is 15.98 CZK", 15.98, c.convert("USD", "CZK", 1), 0.001);
    1.79  
    1.80          // convert 15.97CZK to USD using c:
    1.81          //assertEquals("Result is 1$");
    1.82 -
    1.83 -        fail("Implement me!");
    1.84 +        assertEquals("Result is 1$", 1.0, c.convert("CZK", "USD", 15.97), 0.001);
    1.85 +        
    1.86      }
    1.87  
    1.88      /** Join the convertors and show they behave sane.
    1.89       */
    1.90      public void testOnlineConvertorComposition() throws Exception {
    1.91 -        if (Boolean.getBoolean("ignore.failing")) {
    1.92 -            // implement me!
    1.93 -            return;
    1.94 -        }
    1.95 -        
    1.96          Convertor c = Task2Test.merge(
    1.97              createOnlineCZKUSDConvertor(),
    1.98              Task1Test.createSKKtoCZK()
    1.99 @@ -95,9 +129,11 @@
   1.100  
   1.101          // convert 16CZK to SKK using c:
   1.102          // assertEquals("Result is 20 SKK");
   1.103 +        assertEquals("Result is 20 SKK", 20.0, c.convert("CZK", "SKK", 16), 0.001);
   1.104  
   1.105          // convert 500SKK to CZK using c:
   1.106          // assertEquals("Result is 400 CZK");
   1.107 +        assertEquals("Result is 400 CZK", 400.0, c.convert("SKK", "CZK", 500), 0.001);
   1.108  
   1.109          doFewQueriesForOnlineConvertor(c);
   1.110      }