001    package sysModel.env.tests;
002    
003    import model.ILambda;
004    import sysModel.env.DeactivatableLambda;
005    import junit.framework.TestCase;
006    
007    /**
008     * Test cases for DeactivatableLambda.
009     *
010     * @author Mathias Ricken
011     */
012    public class Test_DeactivatableLambda extends TestCase {
013        /**
014         * Test not deactivating a lambda.
015         */
016        public void testDontDeactivate() {
017            final String msg = "should be thrown";
018            try {
019                ILambda lambda = new ILambda() {
020                    /**
021                     * Execute command.
022                     */
023                    public Object apply(Object param) {
024                        throw new RuntimeException(msg);
025                    }
026                };
027                DeactivatableLambda deactivatable = new DeactivatableLambda(lambda);
028                deactivatable.apply(null);
029                fail("Lambda was not executed.");
030            }
031            catch (RuntimeException e) {
032                if (!e.getMessage().equals(msg)) {
033                    fail("Wrong exception thrown.");
034                }
035            }
036        }
037    
038        /**
039         * Test not deactivating a lambda.
040         */
041        public void testDontDeactivate2() {
042            ILambda lambda = new ILambda() {
043                /**
044                 * Execute command.
045                 */
046                public Object apply(Object param) {
047                    try {
048                        return new Integer(123 * ((Integer) param).intValue());
049                    }
050                    catch (Exception e) {
051                        fail("Failed to cast parameter: " + e.toString());
052                        return null;
053                    }
054                }
055            };
056            DeactivatableLambda deactivatable = new DeactivatableLambda(lambda);
057            try {
058                Integer i = (Integer) deactivatable.apply(new Integer(33));
059                if (33 * 123 != i.intValue()) {
060                    fail("Incorrect return value.");
061                }
062            }
063            catch (Exception e) {
064                fail("Failed to cast return value: " + e.toString());
065            }
066        }
067    
068        /**
069         * Test deactivating a lambda.
070         */
071        public void testDeactivate() {
072            ILambda lambda = new ILambda() {
073                /**
074                 * Execute command.
075                 */
076                public Object apply(Object param) {
077                    fail("Should not be executed.");
078                    return null;
079                }
080            };
081            DeactivatableLambda deactivatable = new DeactivatableLambda(lambda);
082            deactivatable.deactivate();
083            deactivatable.apply(null);
084        }
085    }