Exemple #1
0
        public void TestMethodFiltering_SPR6764()
        {
            var parser  = new SpelExpressionParser();
            var context = new StandardEvaluationContext();

            context.SetRootObject(new TestObject());
            var filter = new LocalFilter();

            context.RegisterMethodFilter(typeof(TestObject), filter);

            // Filter will be called but not do anything, so first doit() will be invoked
            var expr   = (SpelExpression)parser.ParseExpression("DoIt(1)");
            var result = expr.GetValue <string>(context);

            Assert.Equal("1", result);
            Assert.True(filter.FilterCalled);

            // Filter will now remove non @Anno annotated methods
            filter.RemoveIfNotAnnotated = true;
            filter.FilterCalled         = false;
            expr   = (SpelExpression)parser.ParseExpression("DoIt(1)");
            result = expr.GetValue <string>(context);
            Assert.Equal("double 1.0", result);
            Assert.True(filter.FilterCalled);

            // check not called for other types
            filter.FilterCalled = false;
            context.SetRootObject("abc".Clone());
            expr   = (SpelExpression)parser.ParseExpression("[0]");
            result = expr.GetValue <string>(context);
            Assert.Equal("a", result);
            Assert.False(filter.FilterCalled);

            // check de-registration works
            filter.FilterCalled = false;
            context.RegisterMethodFilter(typeof(TestObject), null); // clear filter
            context.SetRootObject(new TestObject());
            expr   = (SpelExpression)parser.ParseExpression("DoIt(1)");
            result = expr.GetValue <string>(context);
            Assert.Equal("1", result);
            Assert.False(filter.FilterCalled);
        }