public void TestScenario01_Roles() { var parser = new SpelExpressionParser(); var ctx = new StandardEvaluationContext(); var expr = parser.ParseRaw("HasAnyRole('MANAGER','TELLER')"); ctx.SetRootObject(new Person("Ben")); var value = expr.GetValue(ctx, typeof(bool)); Assert.False((bool)value); ctx.SetRootObject(new Manager("Luke")); value = expr.GetValue(ctx, typeof(bool)); Assert.True((bool)value); }
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 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 TestDictionaryAccess() { var societyContext = new StandardEvaluationContext(); societyContext.SetRootObject(new IEEE()); // Officer's Dictionary var pupin = parser.ParseExpression("Officers['president']").GetValue <Inventor>(societyContext); Assert.NotNull(pupin); // Evaluates to "Idvor" var city = parser.ParseExpression("Officers['president'].PlaceOfBirth.City").GetValue <string>(societyContext); Assert.NotNull(city); // setting values var i = parser.ParseExpression("Officers['advisors'][0]").GetValue <Inventor>(societyContext); Assert.Equal("Nikola Tesla", i.Name); parser.ParseExpression("Officers['advisors'][0].PlaceOfBirth.Country").SetValue(societyContext, "Croatia"); var i2 = parser.ParseExpression("Reverse[0]['advisors'][0]").GetValue <Inventor>(societyContext); Assert.Equal("Nikola Tesla", i2.Name); }
public void TestSelection() { var societyContext = new StandardEvaluationContext(); societyContext.SetRootObject(new IEEE()); var list = (List <object>)parser.ParseExpression("Members2.?[Nationality == 'Serbian']").GetValue(societyContext); Assert.Single(list); Assert.Equal("Nikola Tesla", ((Inventor)list[0]).Name); }
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); }
public void TestConstructors() { var societyContext = new StandardEvaluationContext(); societyContext.SetRootObject(new IEEE()); var einstein = parser.ParseExpression("new Steeltoe.Common.Expression.Internal.Spring.TestResources.Inventor('Albert Einstein',new DateTime(1879, 3, 14), 'German')").GetValue <Inventor>(); Assert.Equal("Albert Einstein", einstein.Name); // create new inventor instance within add method of List parser.ParseExpression("Members2.Add(new Steeltoe.Common.Expression.Internal.Spring.TestResources.Inventor('Albert Einstein', 'German'))").GetValue(societyContext); }
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); }
public void TestEqualityCheck() { var parser = new SpelExpressionParser(); var context = new StandardEvaluationContext(); context.SetRootObject(Tesla); var exp = parser.ParseExpression("Name == 'Nikola Tesla'"); var isEqual = exp.GetValue <bool>(context); Assert.True(isEqual); }
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); }
private static void SetupRootContextObject(StandardEvaluationContext testContext) { var c = new GregorianCalendar(); var tesla = new Inventor("Nikola Tesla", c.ToDateTime(1856, 7, 9, 0, 0, 0, GregorianCalendar.CurrentEra), "Serbian"); tesla.PlaceOfBirth = new PlaceOfBirth("SmilJan"); tesla.Inventions = new string[] { "Telephone repeater", "Rotating magnetic field principle", "Polyphase alternating-current system", "Induction motor", "Alternating-current power transmission", "Tesla coil transformer", "Wireless communication", "Radio", "Fluorescent lights" }; testContext.SetRootObject(tesla); }
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); }
public void TestMethodInvocation2() { // string literal, Evaluates to "bc" var c = parser.ParseExpression("'abc'.Substring(1, 2)").GetValue <string>(); Assert.Equal("bc", c); var societyContext = new StandardEvaluationContext(); societyContext.SetRootObject(new IEEE()); // Evaluates to true var isMember = parser.ParseExpression("IsMember('Mihajlo Pupin')").GetValue <bool>(societyContext); Assert.True(isMember); }
public void TestRootObject() { // The constructor arguments are name, birthday, and nationaltiy. var tesla = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), "Serbian"); var parser = new SpelExpressionParser(); var exp = parser.ParseExpression("Name"); var context = new StandardEvaluationContext(); context.SetRootObject(tesla); var name = (string)exp.GetValue(context); Assert.Equal("Nikola Tesla", name); }
public void TestScenario_UsingADifferentRootContextobject() { // Create a parser var parser = new SpelExpressionParser(); // Use the standard evaluation context var ctx = new StandardEvaluationContext(); var tc = new TestClass { Property = 42, Str = "wibble" }; ctx.SetRootObject(tc); // read it, set it, read it again var expr = parser.ParseRaw("Str"); var value = expr.GetValue(ctx); Assert.Equal("wibble", value); expr = parser.ParseRaw("Str"); expr.SetValue(ctx, "wobble"); expr = parser.ParseRaw("Str"); value = expr.GetValue(ctx); Assert.Equal("wobble", value); // or using assignment within the expression expr = parser.ParseRaw("Str='wabble'"); value = expr.GetValue(ctx); expr = parser.ParseRaw("Str"); value = expr.GetValue(ctx); Assert.Equal("wabble", value); // private property will be accessed through getter() expr = parser.ParseRaw("Property"); value = expr.GetValue(ctx); Assert.Equal(42, value); // ... and set through setter expr = parser.ParseRaw("Property=4"); value = expr.GetValue(ctx); expr = parser.ParseRaw("Property"); value = expr.GetValue(ctx); Assert.Equal(4, value); }
public void TestAssignment() { var inventor = new Inventor(); var inventorContext = new StandardEvaluationContext(); inventorContext.SetRootObject(inventor); parser.ParseExpression("Foo").SetValue(inventorContext, "Alexander Seovic2"); Assert.Equal("Alexander Seovic2", parser.ParseExpression("Foo").GetValue <string>(inventorContext)); // alternatively var aleks = parser.ParseExpression("Foo = 'Alexandar Seovic'").GetValue <string>(inventorContext); Assert.Equal("Alexandar Seovic", parser.ParseExpression("Foo").GetValue <string>(inventorContext)); Assert.Equal("Alexandar Seovic", aleks); }
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 TestLogicalOperators() { var societyContext = new StandardEvaluationContext(); societyContext.SetRootObject(new IEEE()); // -- AND -- // Evaluates to false var falseValue = parser.ParseExpression("true and false").GetValue <bool>(); Assert.False(falseValue); // Evaluates to true var expression = "IsMember('Nikola Tesla') and IsMember('Mihajlo Pupin')"; var trueValue = parser.ParseExpression(expression).GetValue <bool>(societyContext); // -- OR -- // Evaluates to true trueValue = parser.ParseExpression("true or false").GetValue <bool>(); Assert.True(trueValue); // Evaluates to true expression = "IsMember('Nikola Tesla') or IsMember('Albert Einstien')"; trueValue = parser.ParseExpression(expression).GetValue <bool>(societyContext); Assert.True(trueValue); // -- NOT -- // Evaluates to false falseValue = parser.ParseExpression("!true").GetValue <bool>(); Assert.False(falseValue); // -- AND and NOT -- expression = "IsMember('Nikola Tesla') and !IsMember('Mihajlo Pupin')"; falseValue = parser.ParseExpression(expression).GetValue <bool>(societyContext); Assert.False(falseValue); }
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" }