Ejemplo n.º 1
0
        protected void SetValue(string expression, object value, object expectedValue)
        {
            try
            {
                var e = parser.ParseExpression(expression);
                Assert.NotNull(e);
                if (DEBUG)
                {
                    SpelUtilities.PrintAbstractSyntaxTree(Console.Out, e);
                }

                var lContext = TestScenarioCreator.GetTestEvaluationContext();
                Assert.True(e.IsWritable(lContext));
                e.SetValue(lContext, value);
                var a = expectedValue;
                var b = e.GetValue(lContext);
                Assert.Equal(b, a);
            }
            catch (EvaluationException ex)
            {
                throw new Exception("Unexpected Exception: " + ex.Message, ex);
            }
            catch (ParseException ex)
            {
                throw new Exception("Unexpected Exception: " + ex.Message, ex);
            }
        }
Ejemplo n.º 2
0
        public void TestSetGenericMapElementRequiresCoercion()
        {
            var eContext = TestScenarioCreator.GetTestEvaluationContext();
            var e        = Parse("MapOfstringToBoolean[42]");

            Assert.Null(e.GetValue(eContext));

            // Key should be coerced to string representation of 42
            e.SetValue(eContext, "true");

            // All keys should be strings
            var ks = Parse("MapOfstringToBoolean.Keys").GetValue <ICollection>(eContext);

            foreach (var key in ks)
            {
                Assert.IsType <string>(key);
            }

            // All values should be booleans
            var vs = Parse("MapOfstringToBoolean.Values").GetValue <ICollection>(eContext);

            foreach (var val in vs)
            {
                Assert.IsType <bool>(val);
            }

            // One final Test check coercion on the key for a map lookup
            var o = e.GetValue <bool>(eContext);

            Assert.True(o);
        }
Ejemplo n.º 3
0
        public void TestConstruction()
        {
            var context = TestScenarioCreator.GetTestEvaluationContext();
            var state   = new ExpressionState(context);

            Assert.Equal(context, state.EvaluationContext);
        }
        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);
        }
Ejemplo n.º 5
0
        private ExpressionState GetState()
        {
            var context = TestScenarioCreator.GetTestEvaluationContext();
            var state   = new ExpressionState(context);

            return(state);
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        public void TestAssign()
        {
            var eContext = TestScenarioCreator.GetTestEvaluationContext();
            var e        = Parse("PublicName='Andy'");

            Assert.False(e.IsWritable(eContext));
            Assert.Equal("Andy", e.GetValue(eContext));
        }
Ejemplo n.º 8
0
        public void TestPropertyAccess()
        {
            var context = TestScenarioCreator.GetTestEvaluationContext();
            var year    = parser.ParseExpression("BirthDate.Year + 1900").GetValue <int>(context); // 1856

            Assert.Equal(3756, year);

            var city = (string)parser.ParseExpression("PlaceOfBirth.City").GetValue(context);

            Assert.Equal("SmilJan", city);
        }
Ejemplo n.º 9
0
        public void TestVariableMapAccess()
        {
            var parser = new SpelExpressionParser();
            var ctx    = TestScenarioCreator.GetTestEvaluationContext();

            ctx.SetVariable("day", "saturday");

            var expr  = parser.ParseExpression("TestDictionary[#day]");
            var value = expr.GetValue(ctx, typeof(string));

            Assert.Equal("samstag", value);
        }
Ejemplo n.º 10
0
        public void TestCustomMapAccessor()
        {
            var parser = new SpelExpressionParser();
            var ctx    = TestScenarioCreator.GetTestEvaluationContext();

            ctx.AddPropertyAccessor(new MapAccessor());

            var expr  = parser.ParseExpression("TestDictionary.monday");
            var value = expr.GetValue(ctx, typeof(string));

            Assert.Equal("montag", value);
        }
Ejemplo n.º 11
0
        protected void SetValueExpectError(string expression, object value)
        {
            var e = parser.ParseExpression(expression);

            Assert.NotNull(e);
            if (DEBUG)
            {
                SpelUtilities.PrintAbstractSyntaxTree(Console.Out, e);
            }

            var lContext = TestScenarioCreator.GetTestEvaluationContext();

            Assert.Throws <SpelEvaluationException>(() => e.SetValue(lContext, value));
        }
Ejemplo n.º 12
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));
        }
        public void TestClashingWithSuffixes()
        {
            // Just wanting to use the prefix or suffix within the template:
            var ex = parser.ParseExpression("hello ${3+4} world", DEFAULT_TEMPLATE_PARSER_CONTEXT);
            var s  = ex.GetValue(TestScenarioCreator.GetTestEvaluationContext(), typeof(string));

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

            ex = parser.ParseExpression("hello ${3+4} wo${'${'}rld", DEFAULT_TEMPLATE_PARSER_CONTEXT);
            s  = ex.GetValue(TestScenarioCreator.GetTestEvaluationContext(), typeof(string));
            Assert.Equal("hello 7 wo${rld", s);

            ex = parser.ParseExpression("hello ${3+4} wo}rld", DEFAULT_TEMPLATE_PARSER_CONTEXT);
            s  = ex.GetValue(TestScenarioCreator.GetTestEvaluationContext(), typeof(string));
            Assert.Equal("hello 7 wo}rld", s);
        }
Ejemplo n.º 14
0
        public void TestSetParameterizedList()
        {
            var context = TestScenarioCreator.GetTestEvaluationContext();
            var e       = parser.ParseExpression("ListOfInteger.Count");

            Assert.Equal(0, e.GetValue(context, typeof(int)));
            context.TypeConverter = new TypeConvertorUsingConversionService();

            // Assign a List<String> to the List<Integer> field - the component elements should be converted
            parser.ParseExpression("ListOfInteger").SetValue(context, ListOfString);

            // size now 3
            Assert.Equal(3, e.GetValue(context, typeof(int)));

            // element type correctly Integer
            var clazz = parser.ParseExpression("ListOfInteger[1].GetType()").GetValue(context, typeof(Type));

            Assert.Equal(typeof(int), clazz);
        }
Ejemplo n.º 15
0
        public void TestSimpleOperations()
        {
            // no built in support for this:
            EvaluateAndCheckError("'abc'-true", SpelMessage.OPERATOR_NOT_SUPPORTED_BETWEEN_TYPES);

            var eContext = TestScenarioCreator.GetTestEvaluationContext();

            eContext.OperatorOverloader = new StringAndBooleanAddition();

            var expr = (SpelExpression)parser.ParseExpression("'abc'+true");

            Assert.Equal("abcTrue", expr.GetValue(eContext));

            expr = (SpelExpression)parser.ParseExpression("'abc'-true");
            Assert.Equal("abc", expr.GetValue(eContext));

            expr = (SpelExpression)parser.ParseExpression("'abc'+null");
            Assert.Equal("abcnull", expr.GetValue(eContext));
        }
Ejemplo n.º 16
0
        public void TestIsWritableForInvalidExpressions_SPR10610()
        {
            var lContext = TestScenarioCreator.GetTestEvaluationContext();

            // PROPERTYORFIELDREFERENCE
            // Non existent field (or property):
            var e1 = parser.ParseExpression("ArrayContainer.wibble");

            Assert.False(e1.IsWritable(lContext));

            var e2 = parser.ParseExpression("ArrayContainer.wibble.foo");

            Assert.Throws <SpelEvaluationException>(() => e2.IsWritable(lContext));

            // org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 15): Property or field 'wibble' cannot be found on object of type 'org.springframework.expression.spel.Testresources.ArrayContainer' - maybe not public?
            // at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:225)
            // VARIABLE
            // the variable does not exist (but that is OK, we should be writable)
            var e3 = parser.ParseExpression("#madeup1");

            Assert.True(e3.IsWritable(lContext));

            var e4 = parser.ParseExpression("#madeup2.bar"); // compound expression

            Assert.False(e4.IsWritable(lContext));

            // INDEXER
            // non existent indexer (wibble made up)
            var e5 = parser.ParseExpression("ArrayContainer.wibble[99]");

            Assert.Throws <SpelEvaluationException>(() => e5.IsWritable(lContext));

            // non existent indexer (index via a string)
            var e6 = parser.ParseExpression("ArrayContainer.ints['abc']");

            Assert.Throws <SpelEvaluationException>(() => e6.IsWritable(lContext));
        }
Ejemplo n.º 17
0
        public void TestConstructorThrowingException_SPR6760()
        {
            // Test ctor on inventor:
            // 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 Tester field 'counter' when invoked
            SpelExpressionParser parser = new SpelExpressionParser();
            var expr = parser.ParseExpression("new Steeltoe.Common.Expression.Internal.Spring.ConstructorInvocationTests$Tester(#bar).I");

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

            eContext.SetRootObject(new Tester());
            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(0, 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));

            // 4 will make it throw a checked exception - this will be wrapped by spel on the
            // way out
            eContext.SetVariable("bar", 4);
            var ex = Assert.Throws <SpelEvaluationException>(() => expr.GetValue(eContext));

            Assert.Contains("Tester", ex.Message);

            // A problem occurred whilst attempting to construct an object of type
            // 'org.springframework.expression.spel.ConstructorInvocationTests$Tester'
            // using arguments '(int)'
            // If counter is 4 then the method got called twice!
            Assert.Equal(3, parser.ParseExpression("Counter").GetValue(eContext));

            // 2 will make it throw a SystemException - SpEL will let this through
            eContext.SetVariable("bar", 2);
            var ex2 = Assert.Throws <SystemException>(() => expr.GetValue(eContext));

            Assert.IsNotType <SpelEvaluationException>(ex2);

            // A problem occurred whilst attempting to construct an object of type
            // 'org.springframework.expression.spel.ConstructorInvocationTests$Tester'
            // using arguments '(int)'
            // If counter is 5 then the method got called twice!
            Assert.Equal(4, parser.ParseExpression("Counter").GetValue(eContext));
        }
Ejemplo n.º 18
0
 private IEvaluationContext GetContext()
 {
     return(TestScenarioCreator.GetTestEvaluationContext());
 }