Search in sources :

Example 1 with Inventor

use of org.springframework.expression.spel.testresources.Inventor in project spring-framework by spring-projects.

the class TestScenarioCreator method setupRootContextObject.

/**
	 * Create the root context object, an Inventor instance. Non-qualified property
	 * and method references will be resolved against this context object.
	 * @param testContext the evaluation context in which to set the root object
	 */
private static void setupRootContextObject(StandardEvaluationContext testContext) {
    GregorianCalendar c = new GregorianCalendar();
    c.set(1856, 7, 9);
    Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");
    tesla.setPlaceOfBirth(new PlaceOfBirth("SmilJan"));
    tesla.setInventions(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);
}
Also used : GregorianCalendar(java.util.GregorianCalendar) Inventor(org.springframework.expression.spel.testresources.Inventor) PlaceOfBirth(org.springframework.expression.spel.testresources.PlaceOfBirth)

Example 2 with Inventor

use of org.springframework.expression.spel.testresources.Inventor in project spring-framework by spring-projects.

the class SpelDocumentationTests method testDictionaryAccess.

@Test
public void testDictionaryAccess() throws Exception {
    StandardEvaluationContext societyContext = new StandardEvaluationContext();
    societyContext.setRootObject(new IEEE());
    // Officer's Dictionary
    Inventor pupin = parser.parseExpression("officers['president']").getValue(societyContext, Inventor.class);
    assertNotNull(pupin);
    // evaluates to "Idvor"
    String city = parser.parseExpression("officers['president'].PlaceOfBirth.city").getValue(societyContext, String.class);
    assertNotNull(city);
    // setting values
    Inventor i = parser.parseExpression("officers['advisors'][0]").getValue(societyContext, Inventor.class);
    assertEquals("Nikola Tesla", i.getName());
    parser.parseExpression("officers['advisors'][0].PlaceOfBirth.Country").setValue(societyContext, "Croatia");
    Inventor i2 = parser.parseExpression("reverse[0]['advisors'][0]").getValue(societyContext, Inventor.class);
    assertEquals("Nikola Tesla", i2.getName());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Inventor(org.springframework.expression.spel.testresources.Inventor) Test(org.junit.Test)

Example 3 with Inventor

use of org.springframework.expression.spel.testresources.Inventor in project spring-framework by spring-projects.

the class SpelDocumentationTests method testVariables.

// 7.5.8
@Test
public void testVariables() throws Exception {
    Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setVariable("newName", "Mike Tesla");
    context.setRootObject(tesla);
    parser.parseExpression("foo = #newName").getValue(context);
    assertEquals("Mike Tesla", tesla.getFoo());
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Inventor(org.springframework.expression.spel.testresources.Inventor) Test(org.junit.Test)

Example 4 with Inventor

use of org.springframework.expression.spel.testresources.Inventor in project spring-framework by spring-projects.

the class SpelDocumentationTests method testConstructors.

// 7.5.7
@Test
public void testConstructors() throws Exception {
    StandardEvaluationContext societyContext = new StandardEvaluationContext();
    societyContext.setRootObject(new IEEE());
    Inventor einstein = parser.parseExpression("new org.springframework.expression.spel.testresources.Inventor('Albert Einstein',new java.util.Date(), 'German')").getValue(Inventor.class);
    assertEquals("Albert Einstein", einstein.getName());
    //create new inventor instance within add method of List
    parser.parseExpression("Members2.add(new org.springframework.expression.spel.testresources.Inventor('Albert Einstein', 'German'))").getValue(societyContext);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Inventor(org.springframework.expression.spel.testresources.Inventor) Test(org.junit.Test)

Example 5 with Inventor

use of org.springframework.expression.spel.testresources.Inventor in project spring-framework by spring-projects.

the class SpelDocumentationTests method testAssignment.

// 7.5.5
@Test
public void testAssignment() throws Exception {
    Inventor inventor = new Inventor();
    StandardEvaluationContext inventorContext = new StandardEvaluationContext();
    inventorContext.setRootObject(inventor);
    parser.parseExpression("foo").setValue(inventorContext, "Alexander Seovic2");
    assertEquals("Alexander Seovic2", parser.parseExpression("foo").getValue(inventorContext, String.class));
    // alternatively
    String aleks = parser.parseExpression("foo = 'Alexandar Seovic'").getValue(inventorContext, String.class);
    assertEquals("Alexandar Seovic", parser.parseExpression("foo").getValue(inventorContext, String.class));
    assertEquals("Alexandar Seovic", aleks);
}
Also used : StandardEvaluationContext(org.springframework.expression.spel.support.StandardEvaluationContext) Inventor(org.springframework.expression.spel.testresources.Inventor) Test(org.junit.Test)

Aggregations

Inventor (org.springframework.expression.spel.testresources.Inventor)7 Test (org.junit.Test)6 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)6 GregorianCalendar (java.util.GregorianCalendar)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Expression (org.springframework.expression.Expression)1 ExpressionParser (org.springframework.expression.ExpressionParser)1 SpelExpressionParser (org.springframework.expression.spel.standard.SpelExpressionParser)1 PlaceOfBirth (org.springframework.expression.spel.testresources.PlaceOfBirth)1