22
22
import java .util .ArrayList ;
23
23
import java .util .Date ;
24
24
import java .util .GregorianCalendar ;
25
- import java .util .HashMap ;
26
25
import java .util .List ;
27
26
import java .util .Map ;
28
27
@@ -172,28 +171,24 @@ void propertyAccess() {
172
171
@ Test
173
172
void propertyNavigation () {
174
173
ExpressionParser parser = new SpelExpressionParser ();
175
-
176
- // Inventions Array
177
174
StandardEvaluationContext teslaContext = TestScenarioCreator .getTestEvaluationContext ();
178
- // teslaContext.setRootObject(tesla);
179
175
176
+ // Inventions Array
180
177
// evaluates to "Induction motor"
181
178
String invention = parser .parseExpression ("inventions[3]" ).getValue (teslaContext , String .class );
182
179
assertThat (invention ).isEqualTo ("Induction motor" );
183
180
184
181
// Members List
185
182
StandardEvaluationContext societyContext = new StandardEvaluationContext ();
186
- IEEE ieee = new IEEE ();
187
- ieee .Members [0 ]= tesla ;
188
- societyContext .setRootObject (ieee );
183
+ societyContext .setRootObject (new IEEE ());
189
184
190
185
// evaluates to "Nikola Tesla"
191
- String name = parser .parseExpression ("Members [0].Name" ).getValue (societyContext , String .class );
186
+ String name = parser .parseExpression ("members [0].Name" ).getValue (societyContext , String .class );
192
187
assertThat (name ).isEqualTo ("Nikola Tesla" );
193
188
194
189
// List and Array navigation
195
190
// evaluates to "Wireless communication"
196
- invention = parser .parseExpression ("Members [0].Inventions[6]" ).getValue (societyContext , String .class );
191
+ invention = parser .parseExpression ("members [0].Inventions[6]" ).getValue (societyContext , String .class );
197
192
assertThat (invention ).isEqualTo ("Wireless communication" );
198
193
}
199
194
@@ -210,12 +205,12 @@ void maps() {
210
205
assertThat (city ).isNotNull ();
211
206
212
207
// setting values
213
- Inventor i = parser .parseExpression ("officers['advisors'][0]" ).getValue (societyContext ,Inventor .class );
208
+ Inventor i = parser .parseExpression ("officers['advisors'][0]" ).getValue (societyContext , Inventor .class );
214
209
assertThat (i .getName ()).isEqualTo ("Nikola Tesla" );
215
210
216
211
parser .parseExpression ("officers['advisors'][0].PlaceOfBirth.Country" ).setValue (societyContext , "Croatia" );
217
212
218
- Inventor i2 = parser .parseExpression ("reverse[0]['advisors'][0]" ).getValue (societyContext ,Inventor .class );
213
+ Inventor i2 = parser .parseExpression ("reverse[0]['advisors'][0]" ).getValue (societyContext , Inventor .class );
219
214
assertThat (i2 .getName ()).isEqualTo ("Nikola Tesla" );
220
215
}
221
216
}
@@ -489,7 +484,7 @@ void constructors() {
489
484
parser .parseExpression ("new org.springframework.expression.spel.testresources.Inventor('Albert Einstein',new java.util.Date(), 'German')" ).getValue (Inventor .class );
490
485
assertThat (einstein .getName ()).isEqualTo ("Albert Einstein" );
491
486
//create new inventor instance within add method of List
492
- parser .parseExpression ("Members2 .add(new org.springframework.expression.spel.testresources.Inventor('Albert Einstein', 'German'))" ).getValue (societyContext );
487
+ parser .parseExpression ("members .add(new org.springframework.expression.spel.testresources.Inventor('Albert Einstein', 'German'))" ).getValue (societyContext );
493
488
}
494
489
}
495
490
@@ -632,11 +627,25 @@ class CollectionSelection {
632
627
@ Test
633
628
@ SuppressWarnings ("unchecked" )
634
629
void selection () {
635
- StandardEvaluationContext societyContext = new StandardEvaluationContext ();
636
- societyContext .setRootObject (new IEEE ());
637
- List <Inventor > list = (List <Inventor >) parser .parseExpression ("Members2.?[nationality == 'Serbian']" ).getValue (societyContext );
638
- assertThat (list ).hasSize (1 );
639
- assertThat (list .get (0 ).getName ()).isEqualTo ("Nikola Tesla" );
630
+ StandardEvaluationContext societyContext = new StandardEvaluationContext (new IEEE ());
631
+ // evaluates to ["Nikola Tesla"]
632
+ List <Inventor > list = (List <Inventor >) parser .parseExpression ("members.?[nationality == 'Serbian']" )
633
+ .getValue (societyContext );
634
+ assertThat (list ).map (Inventor ::getName ).containsOnly ("Nikola Tesla" );
635
+ }
636
+ }
637
+
638
+ @ Nested
639
+ class CollectionProjection {
640
+
641
+ @ Test
642
+ @ SuppressWarnings ("unchecked" )
643
+ void projection () {
644
+ StandardEvaluationContext societyContext = new StandardEvaluationContext (new IEEE ());
645
+ // evaluates to ["SmilJan", "Idvor"]
646
+ List placesOfBirth = parser .parseExpression ("members.![placeOfBirth.city]" )
647
+ .getValue (societyContext , List .class );
648
+ assertThat (placesOfBirth ).containsExactly ("SmilJan" , "Idvor" );
640
649
}
641
650
}
642
651
@@ -654,22 +663,20 @@ void templating() {
654
663
655
664
656
665
static class IEEE {
666
+
657
667
private String name ;
658
668
659
- public Inventor [] Members = new Inventor [1 ];
660
- public List Members2 = new ArrayList ();
661
- public Map <String ,Object > officers = new HashMap <>();
669
+ public List <Inventor > members = new ArrayList <>();
670
+
671
+ public Map <String ,Object > officers = Map .of (
672
+ "president" , pupin ,
673
+ "advisors" , List .of (tesla ));
662
674
663
675
public List <Map <String , Object >> reverse = new ArrayList <>();
664
676
665
- @ SuppressWarnings ("unchecked" )
666
677
IEEE () {
667
- officers .put ("president" ,pupin );
668
- List linv = new ArrayList ();
669
- linv .add (tesla );
670
- officers .put ("advisors" ,linv );
671
- Members2 .add (tesla );
672
- Members2 .add (pupin );
678
+ members .add (tesla );
679
+ members .add (pupin );
673
680
674
681
reverse .add (officers );
675
682
}
@@ -678,8 +685,13 @@ public boolean isMember(String name) {
678
685
return true ;
679
686
}
680
687
681
- public String getName () { return name ; }
682
- public void setName (String n ) { this .name = n ; }
688
+ public String getName () {
689
+ return this .name ;
690
+ }
691
+
692
+ public void setName (String n ) {
693
+ this .name = n ;
694
+ }
683
695
}
684
696
685
697
static class StringUtils {
0 commit comments