Improve documentation for SpEL indexing support

Prior to this commit, the reference manual only documented indexing
support for arrays, lists, and maps.

This commit improves the overall documentation for SpEL's property
navigation and indexing support and introduces additional documentation
for indexing into Strings and Objects.

Closes gh-32355
This commit is contained in:
Sam Brannen
2024-03-01 18:08:10 +01:00
parent bdcd10e228
commit fdbefad59c
2 changed files with 197 additions and 52 deletions

View File

@@ -161,60 +161,99 @@ class SpelDocumentationTests extends AbstractExpressionTests {
class PropertiesArraysListsMapsAndIndexers {
@Test
void propertyAccess() {
void propertyNavigation() {
EvaluationContext context = TestScenarioCreator.getTestEvaluationContext();
// evaluates to 1856
int year = (Integer) parser.parseExpression("Birthdate.Year + 1900").getValue(context); // 1856
assertThat(year).isEqualTo(1856);
// evaluates to "Smiljan"
String city = (String) parser.parseExpression("placeOfBirth.City").getValue(context);
assertThat(city).isEqualTo("Smiljan");
}
@Test
void propertyNavigation() {
void indexingIntoArraysAndCollections() {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext teslaContext = TestScenarioCreator.getTestEvaluationContext();
StandardEvaluationContext societyContext = new StandardEvaluationContext();
societyContext.setRootObject(new IEEE());
// Inventions Array
// evaluates to "Induction motor"
String invention = parser.parseExpression("inventions[3]").getValue(teslaContext, String.class);
assertThat(invention).isEqualTo("Induction motor");
// Members List
StandardEvaluationContext societyContext = new StandardEvaluationContext();
societyContext.setRootObject(new IEEE());
// evaluates to "Nikola Tesla"
String name = parser.parseExpression("members[0].Name").getValue(societyContext, String.class);
assertThat(name).isEqualTo("Nikola Tesla");
// List and Array navigation
// List and Array Indexing
// evaluates to "Wireless communication"
invention = parser.parseExpression("members[0].Inventions[6]").getValue(societyContext, String.class);
assertThat(invention).isEqualTo("Wireless communication");
}
@Test
void maps() {
void indexingIntoStrings() {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext societyContext = new StandardEvaluationContext();
societyContext.setRootObject(new IEEE());
// Officer's map
Inventor pupin = parser.parseExpression("officers['president']").getValue(societyContext, Inventor.class);
// evaluates to "T" (8th letter of "Nikola Tesla")
String character = parser.parseExpression("members[0].name[7]")
.getValue(societyContext, String.class);
assertThat(character).isEqualTo("T");
}
@Test
void indexingIntoMaps() {
StandardEvaluationContext societyContext = new StandardEvaluationContext();
societyContext.setRootObject(new IEEE());
// Officer's Map
// evaluates to Inventor("Pupin")
Inventor pupin = parser.parseExpression("officers['president']")
.getValue(societyContext, Inventor.class);
assertThat(pupin).isNotNull();
assertThat(pupin.getName()).isEqualTo("Pupin");
// evaluates to "Idvor"
String city = parser.parseExpression("officers['president'].PlaceOfBirth.city").getValue(societyContext, String.class);
assertThat(city).isNotNull();
String city = parser.parseExpression("officers['president'].placeOfBirth.city")
.getValue(societyContext, String.class);
assertThat(city).isEqualTo("Idvor");
String countryExpression = "officers['advisors'][0].placeOfBirth.Country";
// setting values
Inventor i = parser.parseExpression("officers['advisors'][0]").getValue(societyContext, Inventor.class);
assertThat(i.getName()).isEqualTo("Nikola Tesla");
parser.parseExpression(countryExpression)
.setValue(societyContext, "Croatia");
parser.parseExpression("officers['advisors'][0].PlaceOfBirth.Country").setValue(societyContext, "Croatia");
Inventor i2 = parser.parseExpression("reverse[0]['advisors'][0]").getValue(societyContext, Inventor.class);
assertThat(i2.getName()).isEqualTo("Nikola Tesla");
// evaluates to "Croatia"
String country = parser.parseExpression(countryExpression)
.getValue(societyContext, String.class);
assertThat(country).isEqualTo("Croatia");
}
@Test
void indexingIntoObjects() {
ExpressionParser parser = new SpelExpressionParser();
// Create an inventor to use as the root context object.
Inventor tesla = new Inventor("Nikola Tesla");
// evaluates to "Nikola Tesla"
String name = parser.parseExpression("#root['name']")
.getValue(context, tesla, String.class);
assertThat(name).isEqualTo("Nikola Tesla");
}
}
@Nested