Exemple #1
0
        public void TestBraceAccess()
        {
            var rootObject = new ServiceExpressionContext(serviceProvider.GetService <IApplicationContext>());
            var context    = new StandardEvaluationContext(rootObject);

            context.AddPropertyAccessor(new ServiceExpressionContextAccessor());
            context.AddPropertyAccessor(new ConfigurationAccessor());
            var sep = new SpelExpressionParser();

            // basic
            var ex = sep.ParseExpression("configuration['my.name']");

            Assert.Equal("myservice", ex.GetValue <string>(context));
        }
        public void TestAddingSpecificPropertyAccessor()
        {
            var parser = new SpelExpressionParser();
            var ctx    = new StandardEvaluationContext();

            // Even though this property accessor is added after the reflection one, it specifically
            // names the String class as the type it is interested in so is chosen in preference to
            // any 'default' ones
            ctx.AddPropertyAccessor(new StringyPropertyAccessor());
            var expr = parser.ParseRaw("new String('hello').flibbles");
            var i    = expr.GetValue(ctx, typeof(int));

            Assert.Equal(7, (int)i);

            // The reflection one will be used for other properties...
            expr = parser.ParseRaw("new String('hello').Length");
            var o = expr.GetValue(ctx);

            Assert.NotNull(o);

            var flibbleexpr = parser.ParseRaw("new String('hello').flibbles");

            flibbleexpr.SetValue(ctx, 99);
            i = flibbleexpr.GetValue(ctx, typeof(int));
            Assert.Equal(99, (int)i);

            // Cannot set it to a string value
            Assert.Throws <SpelEvaluationException>(() => flibbleexpr.SetValue(ctx, "not allowed"));

            // message will be: EL1063E:(pos 20): A problem occurred whilst attempting to set the property
            // 'flibbles': 'Cannot set flibbles to an object of type 'class java.lang.String''
            // System.out.println(e.getMessage());
        }
        public void TestScenario02_ComparingNames()
        {
            var parser = new SpelExpressionParser();
            var ctx    = new StandardEvaluationContext();

            ctx.AddPropertyAccessor(new SecurityPrincipalAccessor());

            // Multiple options for supporting this expression: "p.name == principal.name"
            // (1) If the right person is the root context object then "name==principal.name" is good enough
            var expr = parser.ParseRaw("Name == Principal.Name");

            ctx.SetRootObject(new Person("Andy"));
            var value = expr.GetValue(ctx, typeof(bool));

            Assert.True((bool)value);

            ctx.SetRootObject(new Person("Christian"));
            value = expr.GetValue(ctx, typeof(bool));
            Assert.False((bool)value);

            // (2) Or register an accessor that can understand 'p' and return the right person
            expr = parser.ParseRaw("P.Name == Principal.Name");

            var pAccessor = new PersonAccessor();

            ctx.AddPropertyAccessor(pAccessor);
            ctx.SetRootObject(null);

            pAccessor.ActivePerson = new Person("Andy");
            value = expr.GetValue(ctx, typeof(bool));
            Assert.True((bool)value);

            pAccessor.ActivePerson = new Person("Christian");
            value = expr.GetValue(ctx, typeof(bool));
            Assert.False((bool)value);
        }
Exemple #4
0
        public void TestScenario_AddingYourOwnPropertyResolvers_2()
        {
            // Create a parser
            var parser = new SpelExpressionParser();

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

            ctx.AddPropertyAccessor(new VegetableColourAccessor());
            var expr  = parser.ParseRaw("Pea");
            var value = expr.GetValue(ctx);

            Assert.Equal(Color.Green, value);
            var ex = Assert.Throws <SpelEvaluationException>(() => expr.SetValue(ctx, Color.Blue));

            Assert.Equal(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE_ON_NULL, ex.MessageCode);
        }
        public void ShouldAlwaysUsePropertyAccessorFromEvaluationContext()
        {
            var parser     = new SpelExpressionParser();
            var expression = parser.ParseExpression("name");

            var context = new StandardEvaluationContext();

            context.AddPropertyAccessor(new ConfigurablePropertyAccessor(new Dictionary <string, object>()
            {
                { "name", "Ollie" }
            }));
            Assert.Equal("Ollie", expression.GetValue(context));

            context = new StandardEvaluationContext();
            context.AddPropertyAccessor(new ConfigurablePropertyAccessor(new Dictionary <string, object>()
            {
                { "name", "Jens" }
            }));
            Assert.Equal("Jens", expression.GetValue(context));
        }
Exemple #6
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));
        }
        public void MapAccessorCompilable()
        {
            var testMap = GetSimpleTestDictionary();
            var sec     = new StandardEvaluationContext();

            sec.AddPropertyAccessor(new DictionaryAccessor());
            var sep = new SpelExpressionParser();

            // basic
            var ex = sep.ParseExpression("foo");

            Assert.Equal("bar", ex.GetValue(sec, testMap));

            // assertThat(SpelCompiler.compile(ex)).isTrue();
            // assertThat(ex.getValue(sec, testMap)).isEqualTo("bar");

            // compound expression
            ex = sep.ParseExpression("foo.ToUpper()");
            Assert.Equal("BAR", ex.GetValue(sec, testMap));

            // assertThat(SpelCompiler.compile(ex)).isTrue();
            // assertThat(ex.getValue(sec, testMap)).isEqualTo("BAR");

            // nested map
            var nestedMap = GetNestedTestDictionary();

            ex = sep.ParseExpression("aaa.foo.ToUpper()");
            Assert.Equal("BAR", ex.GetValue(sec, nestedMap));

            // assertThat(SpelCompiler.compile(ex)).isTrue();
            // assertThat(ex.getValue(sec, nestedMap)).isEqualTo("BAR");

            // avoiding inserting checkcast because first part of expression returns a Map
            ex = sep.ParseExpression("Map.foo");
            MapGetter mapGetter = new MapGetter();

            Assert.Equal("bar", ex.GetValue(sec, mapGetter));

            // assertThat(SpelCompiler.compile(ex)).isTrue();
            // assertThat(ex.getValue(sec, mapGetter)).isEqualTo("bar");
        }
        public void TestAddingRemovingAccessors()
        {
            var ctx = new StandardEvaluationContext();

            // reflective property accessor is the only one by default
            var propertyAccessors = ctx.PropertyAccessors;

            Assert.Single(propertyAccessors);

            var spa = new StringyPropertyAccessor();

            ctx.AddPropertyAccessor(spa);
            Assert.Equal(2, ctx.PropertyAccessors.Count);

            var copy = new List <IPropertyAccessor>(ctx.PropertyAccessors);

            Assert.True(ctx.RemovePropertyAccessor(spa));
            Assert.False(ctx.RemovePropertyAccessor(spa));
            Assert.Single(ctx.PropertyAccessors);

            ctx.PropertyAccessors = copy;
            Assert.Equal(2, ctx.PropertyAccessors.Count);
        }