Beispiel #1
0
        public void TestScenario_DefiningVariablesThatWillBeAccessibleInExpressions()
        {
            // Create a parser
            var parser = new SpelExpressionParser();

            // Use the standard evaluation context
            var ctx = new StandardEvaluationContext();

            ctx.SetVariable("favouriteColour", "blue");
            List <int> primes = new List <int> {
                2, 3, 5, 7, 11, 13, 17
            };

            ctx.SetVariable("primes", primes);

            var expr  = parser.ParseRaw("#favouriteColour");
            var value = expr.GetValue(ctx);

            Assert.Equal("blue", value);

            expr  = parser.ParseRaw("#primes[1]");
            value = expr.GetValue(ctx);
            Assert.Equal(3, value);

            // all prime numbers > 10 from the list (using selection ?{...})
            expr  = parser.ParseRaw("#primes.?[#this>10]");
            value = expr.GetValue(ctx);
            var asList = value as IList;

            Assert.Equal(3, asList.Count);
            Assert.Equal(11, asList[0]);
            Assert.Equal(13, asList[1]);
            Assert.Equal(17, asList[2]);
        }
        public void TestScenario04_ControllingWhichMethodsRun()
        {
            var parser = new SpelExpressionParser();
            var ctx    = new StandardEvaluationContext();

            ctx.SetRootObject(new Supervisor("Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it);

            // NEEDS TO OVERRIDE THE REFLECTION ONE - SHOW REORDERING MECHANISM
            // Might be better with a as a variable although it would work as a property too...
            // Variable references using a '#'
            // SpelExpression expr = parser.parseExpression("(hasRole('SUPERVISOR') or (#a <  1.042)) and hasIpAddress('10.10.0.0/16')");
            ctx.AddMethodResolver(new MyMethodResolver());

            var expr = parser.ParseRaw("(HasRole(3) or (#a <  1.042)) and HasIpAddress('10.10.0.0/16')");

            bool value;

            ctx.SetVariable("a", 1.0d); // referenced as #a in the expression
            value = expr.GetValue <bool>(ctx);
            Assert.True((bool)value);

            // ctx.setRootObject(new Manager("Luke"));
            // ctx.setVariable("a",1.043d);
            // value = (bool)expr.GetValue(ctx,typeof(bool));
            // assertFalse(value);
        }
        public void TestCallingIllegalFunctions()
        {
            SpelExpressionParser      parser = new SpelExpressionParser();
            StandardEvaluationContext ctx    = new StandardEvaluationContext();

            ctx.SetVariable("notStatic", this.GetType().GetMethod("NonStatic"));
            var ex = Assert.Throws <SpelEvaluationException>(() => parser.ParseRaw("#notStatic()").GetValue(ctx));

            Assert.Equal(SpelMessage.FUNCTION_MUST_BE_STATIC, ex.MessageCode);
        }
        public void TestScenario03_Arithmetic()
        {
            var parser = new SpelExpressionParser();
            var ctx    = new StandardEvaluationContext();

            // Might be better with a as a variable although it would work as a property too...
            // Variable references using a '#'
            var expr = parser.ParseRaw("(HasRole('SUPERVISOR') or (#a <  1.042)) and HasIpAddress('10.10.0.0/16')");

            bool value;

            ctx.SetVariable("a", 1.0d);               // referenced as #a in the expression
            ctx.SetRootObject(new Supervisor("Ben")); // so non-qualified references 'hasRole()' 'hasIpAddress()' are invoked against it
            value = expr.GetValue <bool>(ctx);
            Assert.True((bool)value);

            ctx.SetRootObject(new Manager("Luke"));
            ctx.SetVariable("a", 1.043d);
            value = expr.GetValue <bool>(ctx);
            Assert.False((bool)value);
        }
Beispiel #5
0
        public void TestVariables()
        {
            var tesla   = new Inventor("Nikola Tesla", "Serbian");
            var context = new StandardEvaluationContext();

            context.SetVariable("newName", "Mike Tesla");

            context.SetRootObject(tesla);

            parser.ParseExpression("Foo = #newName").GetValue(context);

            Assert.Equal("Mike Tesla", tesla.Foo);
        }
        public void CustomMapWithNonStringValue()
        {
            CustomMap map = new CustomMap();

            map.Add("x", "1");
            map.Add("y", 2);
            map.Add("z", "3");
            var expression = _parser.ParseExpression("Foo(#props)");
            var context    = new StandardEvaluationContext();

            context.SetVariable("props", map);
            var result = expression.GetValue <string>(context, new TestBean());

            Assert.Equal("123", result);
        }
        public void MapWithAllStringValues()
        {
            var map = new Dictionary <string, object>();

            map.Add("x", "1");
            map.Add("y", "2");
            map.Add("z", "3");
            var expression = _parser.ParseExpression("Foo(#props)");
            var context    = new StandardEvaluationContext();

            context.SetVariable("props", map);
            var result = expression.GetValue <string>(context, new TestBean());

            Assert.Equal("123", result);
        }
        public void Props()
        {
            var props = new Dictionary <string, string>();

            props.Add("x", "1");
            props.Add("y", "2");
            props.Add("z", "3");
            var expression = _parser.ParseExpression("Foo(#props)");
            var context    = new StandardEvaluationContext();

            context.SetVariable("props", props);
            var result = expression.GetValue <string>(context, new TestBean());

            Assert.Equal("123", result);
        }
Beispiel #9
0
        public void ProjectionWithIEnumerable()
        {
            var expression = new SpelExpressionParser().ParseRaw("#testList.![Wrapper.Value]");
            var context    = new StandardEvaluationContext();

            context.SetVariable("testList", IntegerTestBean.CreateIterable());
            var value     = expression.GetValue(context);
            var condition = value is List <object>;

            Assert.True(condition);
            var list = (List <object>)value;

            Assert.Equal(3, list.Count);
            Assert.Equal(5, list[0]);
            Assert.Equal(6, list[1]);
            Assert.Equal(7, list[2]);
        }
Beispiel #10
0
        public void ProjectionWithArray()
        {
            var expression = new SpelExpressionParser().ParseRaw("#testArray.![Wrapper.Value]");
            var context    = new StandardEvaluationContext();

            context.SetVariable("testArray", IntegerTestBean.CreateArray());
            var value = expression.GetValue(context);

            Assert.True(value.GetType().IsArray);
            var typedValue = new TypedValue(value);

            Assert.Equal(typeof(ValueType), typedValue.TypeDescriptor.GetElementType());
            var array = (ValueType[])value;

            Assert.Equal(3, array.Length);
            Assert.Equal(5, array[0]);
            Assert.Equal(5.9f, array[1]);
            Assert.Equal(7, array[2]);
        }
Beispiel #11
0
        public void TestSpecialVariables()
        {
            // create an array of integers
            var primes = new List <int> {
                2, 3, 5, 7, 11, 13, 17
            };

            // create parser and set variable 'primes' as the array of integers
            var parser  = new SpelExpressionParser();
            var context = new StandardEvaluationContext();

            context.SetVariable("primes", primes);

            // all prime numbers > 10 from the list (using selection ?{...})
            var primesGreaterThanTen = parser.ParseExpression("#primes.?[#this>10]").GetValue <List <object> >(context);

            Assert.Equal(3, primesGreaterThanTen.Count);
            Assert.Equal(11, primesGreaterThanTen[0]);
            Assert.Equal(13, primesGreaterThanTen[1]);
            Assert.Equal(17, primesGreaterThanTen[2]);
        }
Beispiel #12
0
        public void TestTernary()
        {
            var falsestring = parser.ParseExpression("false ? 'trueExp' : 'falseExp'").GetValue <string>();

            Assert.Equal("falseExp", falsestring);

            var societyContext = new StandardEvaluationContext();

            societyContext.SetRootObject(new IEEE());

            parser.ParseExpression("Name").SetValue(societyContext, "IEEE");
            societyContext.SetVariable("queryName", "Nikola Tesla");

            var expression = "IsMember(#queryName)? #queryName + ' is a member of the ' "
                             + "+ Name + ' Society' : #queryName + ' is not a member of the ' + Name + ' Society'";

            var queryResultstring = parser.ParseExpression(expression).GetValue <string>(societyContext);

            Assert.Equal("Nikola Tesla is a member of the IEEE Society", queryResultstring);

            // queryResultstring = "Nikola Tesla is a member of the IEEE Society"
        }
Beispiel #13
0
        public void TestConvert()
        {
            var root = new Foo("bar");
            ICollection <string> foos = new List <string>()
            {
                "baz"
            };

            var context = new StandardEvaluationContext(root);

            // property access
            var expression = parser.ParseExpression("Foos");

            expression.SetValue(context, foos);
            var baz = root.Foos.Single();

            Assert.Equal("baz", baz.Value);

            // method call
            expression = parser.ParseExpression("Foos=#foos");
            context.SetVariable("foos", foos);
            expression.GetValue(context);
            baz = root.Foos.Single();
            Assert.Equal("baz", baz.Value);

            // method call with result from method call
            expression = parser.ParseExpression("Foos=FoosAsStrings");
            expression.GetValue(context);
            baz = root.Foos.Single();
            Assert.Equal("baz", baz.Value);

            // method call with result from method call
            expression = parser.ParseExpression("Foos=FoosAsObjects");
            expression.GetValue(context);
            baz = root.Foos.Single();
            Assert.Equal("baz", baz.Value);
        }
Beispiel #14
0
        public void TestCoercionToCollectionOfPrimitive()
        {
            var evaluationContext = new StandardEvaluationContext();

            var collectionType = typeof(TestTarget).GetMethod("Sum").GetParameters()[0].ParameterType;

            // The type conversion is possible
            Assert.True(evaluationContext.TypeConverter.CanConvert(typeof(string), collectionType));

            // ... and it can be done successfully
            var result = evaluationContext.TypeConverter.ConvertValue("1,2,3,4", typeof(string), collectionType);
            var asList = result as ICollection <int>;

            Assert.NotNull(asList);
            Assert.Equal(4, asList.Count);

            evaluationContext.SetVariable("target", new TestTarget());

            // OK up to here, so the evaluation should be fine...
            // ... but this fails
            var result2 = (int)parser.ParseExpression("#target.Sum(#root)").GetValue(evaluationContext, "1,2,3,4");

            Assert.Equal(10, result2);
        }
Beispiel #15
0
 private static void PopulateVariables(StandardEvaluationContext testContext)
 {
     testContext.SetVariable("answer", 42);
 }