Exemple #1
0
        public void TestPropertyNavigation()
        {
            var parser = new SpelExpressionParser();

            // Inventions Array
            var teslaContext = TestScenarioCreator.GetTestEvaluationContext();

            // teslaContext.SetRootObject(tesla);
            // Evaluates to "Induction motor"
            var invention = parser.ParseExpression("Inventions[3]").GetValue <string>(teslaContext);

            Assert.Equal("Induction motor", invention);

            // Members List
            var societyContext = new StandardEvaluationContext();
            var ieee           = new IEEE();

            ieee.Members[0] = Tesla;
            societyContext.SetRootObject(ieee);

            // Evaluates to "Nikola Tesla"
            var name = parser.ParseExpression("Members[0].Name").GetValue <string>(societyContext);

            Assert.Equal("Nikola Tesla", name);

            // List and Array navigation
            // Evaluates to "Wireless communication"
            invention = parser.ParseExpression("Members[0].Inventions[6]").GetValue <string>(societyContext);
            Assert.Equal("Wireless communication", invention);
        }
        public void TestNestedExpressions()
        {
            var parser = new SpelExpressionParser();

            // treat the nested ${..} as a part of the expression
            var ex = parser.ParseExpression("hello ${ListOfNumbersUpToTen.$[#this<5]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
            var s  = ex.GetValue(TestScenarioCreator.GetTestEvaluationContext(), typeof(string));

            Assert.Equal("hello 4 world", s);

            // not a useful expression but Tests nested expression syntax that clashes with template prefix/suffix
            ex = parser.ParseExpression("hello ${ListOfNumbersUpToTen.$[#root.ListOfNumbersUpToTen.$[#this%2==1]==3]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
            Assert.IsType <CompositeStringExpression>(ex);
            var cse   = (CompositeStringExpression)ex;
            var exprs = cse.Expressions;

            Assert.Equal(3, exprs.Count);
            Assert.Equal("ListOfNumbersUpToTen.$[#root.ListOfNumbersUpToTen.$[#this%2==1]==3]", exprs[1].ExpressionString);
            s = ex.GetValue(TestScenarioCreator.GetTestEvaluationContext(), typeof(string));
            Assert.Equal("hello  world", s);

            ex = parser.ParseExpression("hello ${ListOfNumbersUpToTen.$[#this<5]} ${ListOfNumbersUpToTen.$[#this>5]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
            s  = ex.GetValue(TestScenarioCreator.GetTestEvaluationContext(), typeof(string));
            Assert.Equal("hello 4 10 world", s);

            var pex = Assert.Throws <ParseException>(() => parser.ParseExpression("hello ${ListOfNumbersUpToTen.$[#this<5]} ${ListOfNumbersUpToTen.$[#this>5] world", DEFAULT_TEMPLATE_PARSER_CONTEXT));

            Assert.Equal("No ending suffix '}' for expression starting at character 41: ${ListOfNumbersUpToTen.$[#this>5] world", pex.SimpleMessage);

            pex = Assert.Throws <ParseException>(() => parser.ParseExpression("hello ${ListOfNumbersUpToTen.$[#root.ListOfNumbersUpToTen.$[#this%2==1==3]} world", DEFAULT_TEMPLATE_PARSER_CONTEXT));
            Assert.Equal("Found closing '}' at position 74 but most recent opening is '[' at position 30", pex.SimpleMessage);
        }
Exemple #3
0
        public void SetPropertyContainingMapAutoGrow()
        {
            var parser     = new SpelExpressionParser(new SpelParserOptions(true, false));
            var expression = parser.ParseExpression("ParameterizedMap");

            Assert.Equal(typeof(Dictionary <int, int>), expression.GetValueType(this));
            Assert.Equal(Property, expression.GetValue(this));
            expression = parser.ParseExpression("ParameterizedMap['9']");
            Assert.Null(expression.GetValue(this));
            expression.SetValue(this, "37");
            Assert.Equal(37, expression.GetValue(this));
        }
Exemple #4
0
        public void IndexIntoGenericPropertyContainingArray()
        {
            var property = new string[] { "bar" };

            Property = property;
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("Property");

            Assert.Equal(typeof(string[]), expression.GetValueType(this));
            Assert.Equal(property, expression.GetValue(this));
            expression = parser.ParseExpression("Property[0]");
            Assert.Equal("bar", expression.GetValue(this));
        }
Exemple #5
0
        public void IndexIntoPropertyContainingList()
        {
            var property = new List <int>();

            property.Add(3);
            ParameterizedList = property;
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("ParameterizedList");

            Assert.Equal(typeof(List <int>), expression.GetValueType(this));
            Assert.Equal(property, expression.GetValue(this));
            expression = parser.ParseExpression("ParameterizedList[0]");
            Assert.Equal(3, expression.GetValue(this));
        }
Exemple #6
0
        public void IndexIntoGenericPropertyContainingList()
        {
            var property = new List <string>();

            property.Add("bar");
            Property = property;
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("Property");

            Assert.Equal(typeof(List <string>), expression.GetValueType(this));
            Assert.Equal(property, expression.GetValue(this));
            expression = parser.ParseExpression("Property[0]");
            Assert.Equal("bar", expression.GetValue(this));
        }
Exemple #7
0
        public void TestMethodThrowingException_SPR6760()
        {
            // Test method on inventor: throwException()
            // On 1 it will throw an IllegalArgumentException
            // On 2 it will throw a RuntimeException
            // On 3 it will exit normally
            // In each case it increments the Inventor field 'counter' when invoked
            var parser = new SpelExpressionParser();
            var expr   = parser.ParseExpression("ThrowException(#bar)");

            // Normal exit
            var eContext = TestScenarioCreator.GetTestEvaluationContext();

            eContext.SetVariable("bar", 3);
            var o = expr.GetValue(eContext);

            Assert.Equal(3, o);
            Assert.Equal(1, parser.ParseExpression("Counter").GetValue(eContext));

            // Now the expression has cached that throwException(int) is the right thing to call
            // Let's change 'bar' to be a PlaceOfBirth which indicates the cached reference is
            // out of date.
            eContext.SetVariable("bar", new PlaceOfBirth("London"));
            o = expr.GetValue(eContext);
            Assert.Equal("London", o);

            // That confirms the logic to mark the cached reference stale and retry is working
            // Now let's cause the method to exit via exception and ensure it doesn't cause a retry.
            // First, switch back to throwException(int)
            eContext.SetVariable("bar", 3);
            o = expr.GetValue(eContext);
            Assert.Equal(3, o);
            Assert.Equal(2, parser.ParseExpression("Counter").GetValue(eContext));

            // Now cause it to throw an exception:
            eContext.SetVariable("bar", 1);
            var ex = Assert.Throws <ArgumentException>(() => expr.GetValue(eContext));

            Assert.IsNotType <SpelEvaluationException>(ex);

            // If counter is 4 then the method got called twice!
            Assert.Equal(3, parser.ParseExpression("Counter").GetValue(eContext));

            eContext.SetVariable("bar", 4);
            Assert.Throws <ExpressionInvocationTargetException>(() => expr.GetValue(eContext));

            // If counter is 5 then the method got called twice!
            Assert.Equal(4, parser.ParseExpression("Counter").GetValue(eContext));
        }
Exemple #8
0
        public void IndexIntoGenericPropertyContainingMap()
        {
            var property = new Dictionary <string, string>();

            property.Add("foo", "bar");
            Property = property;
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("Property");

            Assert.Equal(property.GetType(), expression.GetValueType(this));
            Assert.Equal(property, expression.GetValue(this));
            Assert.Equal(property, expression.GetValue(this, typeof(IDictionary)));
            expression = parser.ParseExpression("Property['foo']");
            Assert.Equal("bar", expression.GetValue(this));
        }
Exemple #9
0
        public void SetGenericPropertyContainingList()
        {
            var property = new List <int>();

            property.Add(3);
            Property = property;
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("Property");

            Assert.Equal(typeof(List <int>), expression.GetValueType(this));
            Assert.Equal(property, expression.GetValue(this));
            expression = parser.ParseExpression("Property[0]");
            Assert.Equal(3, expression.GetValue(this));
            expression.SetValue(this, "4");
            Assert.Equal(4, expression.GetValue(this));
        }
Exemple #10
0
        public void ResolveCollectionElementTypeNull()
        {
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("ListNotGeneric");

            Assert.Equal(typeof(IList), expression.GetValueType(this));
        }
        public void TestCompositeStringExpression()
        {
            var parser = new SpelExpressionParser();
            var ex     = parser.ParseExpression("hello ${'world'}", DEFAULT_TEMPLATE_PARSER_CONTEXT);

            Assert.Equal("hello world", ex.GetValue());
            Assert.Equal("hello world", ex.GetValue(typeof(string)));
            Assert.Equal("hello world", ex.GetValue((object)null, typeof(string)));
            Assert.Equal("hello world", ex.GetValue(new Rooty()));
            Assert.Equal("hello world", ex.GetValue(new Rooty(), typeof(string)));

            var ctx = new StandardEvaluationContext();

            Assert.Equal("hello world", ex.GetValue(ctx));
            Assert.Equal("hello world", ex.GetValue(ctx, typeof(string)));
            Assert.Equal("hello world", ex.GetValue(ctx, null, typeof(string)));
            Assert.Equal("hello world", ex.GetValue(ctx, new Rooty()));
            Assert.Equal("hello world", ex.GetValue(ctx, new Rooty(), typeof(string)));
            Assert.Equal("hello world", ex.GetValue(ctx, new Rooty(), typeof(string)));
            Assert.Equal("hello ${'world'}", ex.ExpressionString);
            Assert.False(ex.IsWritable(new StandardEvaluationContext()));
            Assert.False(ex.IsWritable(new Rooty()));
            Assert.False(ex.IsWritable(new StandardEvaluationContext(), new Rooty()));

            Assert.Equal(typeof(string), ex.GetValueType());
            Assert.Equal(typeof(string), ex.GetValueType(ctx));
            Assert.Equal(typeof(string), ex.GetValueType(new Rooty()));
            Assert.Equal(typeof(string), ex.GetValueType(ctx, new Rooty()));
            Assert.Throws <EvaluationException>(() => ex.SetValue(ctx, null));
            Assert.Throws <EvaluationException>(() => ex.SetValue((object)null, null));
            Assert.Throws <EvaluationException>(() => ex.SetValue(ctx, null, null));
        }
        public void SpelExpressionListIndexAccessNullVariables()
        {
            var parser         = new SpelExpressionParser();
            var spelExpression = parser.ParseExpression("#aList[0] eq 'one'");

            Assert.Throws <SpelEvaluationException>(() => spelExpression.GetValue());
        }
Exemple #13
0
        public void SetPropertyContainingMap()
        {
            var property = new Dictionary <int, int>();

            property.Add(9, 3);
            ParameterizedMap = property;
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("ParameterizedMap");

            Assert.Equal(typeof(Dictionary <int, int>), expression.GetValueType(this));
            Assert.Equal(property, expression.GetValue(this));
            expression = parser.ParseExpression("ParameterizedMap['9']");
            Assert.Equal(3, expression.GetValue(this));
            expression.SetValue(this, "37");
            Assert.Equal(37, expression.GetValue(this));
        }
        public void SpelExpressionListNullVariables()
        {
            var parser         = new SpelExpressionParser();
            var spelExpression = parser.ParseExpression("#aList.contains('one')");

            Assert.Throws <SpelEvaluationException>(() => spelExpression.GetValue());
        }
        public void TestParsingSimpleTemplateExpression02()
        {
            var parser = new SpelExpressionParser();
            var expr   = parser.ParseExpression("hello ${'to'} you", DEFAULT_TEMPLATE_PARSER_CONTEXT);
            var o      = expr.GetValue();

            Assert.Equal("hello to you", o.ToString());
        }
        public void TestParsingSimpleTemplateExpression03()
        {
            var parser = new SpelExpressionParser();
            var expr   = parser.ParseExpression("The quick ${'brown'} fox jumped over the ${'lazy'} dog", DEFAULT_TEMPLATE_PARSER_CONTEXT);
            var o      = expr.GetValue();

            Assert.Equal("The quick brown fox jumped over the lazy dog", o.ToString());
        }
Exemple #17
0
        public void TestListOfScalar()
        {
            ListOfScalarNotGeneric = new ArrayList(1);
            ListOfScalarNotGeneric.Add("5");
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("ListOfScalarNotGeneric[0]");

            Assert.Equal(5, expression.GetValue(this, typeof(int)));
        }
Exemple #18
0
        public void SetGenericPropertyContainingMap()
        {
            var property = new Dictionary <string, string>
            {
                { "foo", "bar" }
            };

            Property = property;
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("Property");

            Assert.Equal(typeof(Dictionary <string, string>), expression.GetValueType(this));
            Assert.Equal(property, expression.GetValue(this));
            expression = parser.ParseExpression("Property['foo']");
            Assert.Equal("bar", expression.GetValue(this));
            expression.SetValue(this, "baz");
            Assert.Equal("baz", expression.GetValue(this));
        }
Exemple #19
0
        public void IndexIntoGenericPropertyContainingNullList()
        {
            var configuration = new SpelParserOptions(true, true);
            var parser        = new SpelExpressionParser(configuration);
            var expression    = parser.ParseExpression("Property");

            Assert.Equal(typeof(object), expression.GetValueType(this));
            Assert.Equal(Property, expression.GetValue(this));
            expression = parser.ParseExpression("Property[0]");
            try
            {
                Assert.Equal("bar", expression.GetValue(this));
            }
            catch (EvaluationException ex)
            {
                Assert.StartsWith("EL1027E", ex.Message);
            }
        }
Exemple #20
0
        public void SetPropertyContainingList()
        {
            var property = new List <int>
            {
                3
            };

            ParameterizedList = property;
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("ParameterizedList");

            Assert.Equal(typeof(List <int>), expression.GetValueType(this));
            Assert.Equal(property, expression.GetValue(this));
            expression = parser.ParseExpression("ParameterizedList[0]");
            Assert.Equal(3, expression.GetValue(this));
            expression.SetValue(this, "4");
            Assert.Equal(4, expression.GetValue(this));
        }
Exemple #21
0
        public void EmptyList()
        {
            ListOfScalarNotGeneric = new ArrayList();
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("ListOfScalarNotGeneric");

            Assert.Equal(typeof(ArrayList), expression.GetValueType(this));
            Assert.Equal(string.Empty, expression.GetValue(this, typeof(string)));
        }
Exemple #22
0
        public void ResolveMapKeyValueTypes()
        {
            MapNotGeneric = new Hashtable();
            MapNotGeneric.Add("baseAmount", 3.11);
            MapNotGeneric.Add("bonusAmount", 7.17);
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("MapNotGeneric");

            Assert.Equal(typeof(Hashtable), expression.GetValueType(this));
        }
Exemple #23
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);
        }
Exemple #24
0
        public void TestGetValueFromRootMap()
        {
            var map = new Dictionary <string, string>();

            map.Add("key", "value");

            var spelExpressionParser = new SpelExpressionParser();
            var expr = spelExpressionParser.ParseExpression("#root['key']");

            Assert.Equal("value", expr.GetValue(map));
        }
        public void TestParsingSimpleTemplateExpression04()
        {
            var parser = new SpelExpressionParser();
            var expr   = parser.ParseExpression("${'hello'} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
            var o      = expr.GetValue();

            Assert.Equal("hello world", o.ToString());

            expr = parser.ParseExpression(string.Empty, DEFAULT_TEMPLATE_PARSER_CONTEXT);
            o    = expr.GetValue();
            Assert.Equal(string.Empty, o.ToString());

            expr = parser.ParseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT);
            o    = expr.GetValue();
            Assert.Equal("abc", o.ToString());

            expr = parser.ParseExpression("abc", DEFAULT_TEMPLATE_PARSER_CONTEXT);
            o    = expr.GetValue((object)null);
            Assert.Equal("abc", o.ToString());
        }
Exemple #26
0
        public void ResolveCollectionElementType()
        {
            ListNotGeneric = new ArrayList(2);
            ListNotGeneric.Add(5);
            ListNotGeneric.Add(6);
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("ListNotGeneric");

            Assert.Equal(typeof(ArrayList), expression.GetValueType(this));
            Assert.Equal("5,6", expression.GetValue(this, typeof(string)));
        }
Exemple #27
0
        public void SetGenericPropertyContainingListAutogrow()
        {
            var property = new List <int>();

            Property = property;
            var parser     = new SpelExpressionParser(new SpelParserOptions(true, true));
            var expression = parser.ParseExpression("Property");

            Assert.Equal(typeof(List <int>), expression.GetValueType(this));
            Assert.Equal(property, expression.GetValue(this));
            expression = parser.ParseExpression("Property[0]");
            try
            {
                expression.SetValue(this, "4");
            }
            catch (EvaluationException ex)
            {
                Assert.StartsWith("EL1053E", ex.Message);
            }
        }
Exemple #28
0
        public void TestFunctions()
        {
            var parser  = new SpelExpressionParser();
            var context = new StandardEvaluationContext();

            context.RegisterFunction("reversestring", typeof(StringUtils).GetMethod("ReverseString", BindingFlags.Public | BindingFlags.Static));

            var helloWorldReversed = parser.ParseExpression("#reversestring('hello world')").GetValue <string>(context);

            Assert.Equal("dlrow olleh", helloWorldReversed);
        }
Exemple #29
0
        public void SelectLastItemInMap()
        {
            var context = new StandardEvaluationContext(new MapTestBean());
            var parser  = new SpelExpressionParser();

            var exp       = parser.ParseExpression("Colors.$[Key.StartsWith('b')]");
            var colorsMap = (Dictionary <object, object>)exp.GetValue(context);

            Assert.Single(colorsMap);
            Assert.True(colorsMap.ContainsKey("beige"));
        }
Exemple #30
0
        public void IndexIntoGenericPropertyContainingMapobject()
        {
            var property = new Dictionary <string, Dictionary <string, string> >();
            var map      = new Dictionary <string, string>();

            map.Add("foo", "bar");
            property.Add("property", map);
            var parser  = new SpelExpressionParser();
            var context = new StandardEvaluationContext();

            context.AddPropertyAccessor(new MapAccessor());
            context.SetRootObject(property);
            var expression = parser.ParseExpression("property");

            Assert.Equal(typeof(Dictionary <string, string>), expression.GetValueType(context));
            Assert.Equal(map, expression.GetValue(context));
            Assert.Equal(map, expression.GetValue(context, typeof(IDictionary)));
            expression = parser.ParseExpression("property['foo']");
            Assert.Equal("bar", expression.GetValue(context));
        }