Skip to content

Commit a3d3bc0

Browse files
committed
Polishing
1 parent a986374 commit a3d3bc0

File tree

1 file changed

+3
-15
lines changed

1 file changed

+3
-15
lines changed

spring-expression/src/test/java/org/springframework/expression/spel/IndexingTests.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,15 @@ static class RootContextWithIndexedProperties {
524524
@Nested
525525
class IndexAccessorTests { // gh-26478
526526

527+
private final StandardEvaluationContext context = new StandardEvaluationContext();
528+
private final SpelExpressionParser parser = new SpelExpressionParser();
529+
527530
@Test
528531
void addingAndRemovingIndexAccessors() {
529532
ObjectMapper objectMapper = new ObjectMapper();
530533
IndexAccessor accessor1 = new JacksonArrayNodeIndexAccessor(objectMapper);
531534
IndexAccessor accessor2 = new JacksonArrayNodeIndexAccessor(objectMapper);
532535

533-
StandardEvaluationContext context = new StandardEvaluationContext();
534536
List<IndexAccessor> indexAccessors = context.getIndexAccessors();
535537
assertThat(indexAccessors).isEmpty();
536538

@@ -551,10 +553,8 @@ void addingAndRemovingIndexAccessors() {
551553

552554
@Test
553555
void noSuitableIndexAccessorResultsInException() {
554-
StandardEvaluationContext context = new StandardEvaluationContext();
555556
assertThat(context.getIndexAccessors()).isEmpty();
556557

557-
SpelExpressionParser parser = new SpelExpressionParser();
558558
Expression expr = parser.parseExpression("[0]");
559559
assertThatExceptionOfType(SpelEvaluationException.class)
560560
.isThrownBy(() -> expr.getValue(context, this))
@@ -564,15 +564,13 @@ void noSuitableIndexAccessorResultsInException() {
564564

565565
@Test
566566
void canReadThrowsException() throws Exception {
567-
StandardEvaluationContext context = new StandardEvaluationContext();
568567
RuntimeException exception = new RuntimeException("Boom!");
569568

570569
IndexAccessor mock = mock();
571570
given(mock.getSpecificTargetClasses()).willReturn(null);
572571
given(mock.canRead(any(), eq(this), any())).willThrow(exception);
573572
context.addIndexAccessor(mock);
574573

575-
SpelExpressionParser parser = new SpelExpressionParser();
576574
Expression expr = parser.parseExpression("[0]");
577575
assertThatExceptionOfType(SpelEvaluationException.class)
578576
.isThrownBy(() -> expr.getValue(context, this))
@@ -588,7 +586,6 @@ void canReadThrowsException() throws Exception {
588586

589587
@Test
590588
void readThrowsException() throws Exception {
591-
StandardEvaluationContext context = new StandardEvaluationContext();
592589
RuntimeException exception = new RuntimeException("Boom!");
593590

594591
IndexAccessor mock = mock();
@@ -597,7 +594,6 @@ void readThrowsException() throws Exception {
597594
given(mock.read(any(), eq(this), any())).willThrow(exception);
598595
context.addIndexAccessor(mock);
599596

600-
SpelExpressionParser parser = new SpelExpressionParser();
601597
Expression expr = parser.parseExpression("[0]");
602598
assertThatExceptionOfType(SpelEvaluationException.class)
603599
.isThrownBy(() -> expr.getValue(context, this))
@@ -614,15 +610,13 @@ void readThrowsException() throws Exception {
614610

615611
@Test
616612
void canWriteThrowsException() throws Exception {
617-
StandardEvaluationContext context = new StandardEvaluationContext();
618613
RuntimeException exception = new RuntimeException("Boom!");
619614

620615
IndexAccessor mock = mock();
621616
given(mock.getSpecificTargetClasses()).willReturn(null);
622617
given(mock.canWrite(eq(context), eq(this), eq(0))).willThrow(exception);
623618
context.addIndexAccessor(mock);
624619

625-
SpelExpressionParser parser = new SpelExpressionParser();
626620
Expression expr = parser.parseExpression("[0]");
627621
assertThatExceptionOfType(SpelEvaluationException.class)
628622
.isThrownBy(() -> expr.setValue(context, this, 999))
@@ -638,7 +632,6 @@ void canWriteThrowsException() throws Exception {
638632

639633
@Test
640634
void writeThrowsException() throws Exception {
641-
StandardEvaluationContext context = new StandardEvaluationContext();
642635
RuntimeException exception = new RuntimeException("Boom!");
643636

644637
IndexAccessor mock = mock();
@@ -647,7 +640,6 @@ void writeThrowsException() throws Exception {
647640
doThrow(exception).when(mock).write(any(), any(), any(), any());
648641
context.addIndexAccessor(mock);
649642

650-
SpelExpressionParser parser = new SpelExpressionParser();
651643
Expression expr = parser.parseExpression("[0]");
652644
assertThatExceptionOfType(SpelEvaluationException.class)
653645
.isThrownBy(() -> expr.setValue(context, this, 999))
@@ -664,8 +656,6 @@ void writeThrowsException() throws Exception {
664656

665657
@Test
666658
void readAndWriteIndex() {
667-
StandardEvaluationContext context = new StandardEvaluationContext();
668-
669659
ObjectMapper objectMapper = new ObjectMapper();
670660
context.addIndexAccessor(new JacksonArrayNodeIndexAccessor(objectMapper));
671661

@@ -674,7 +664,6 @@ void readAndWriteIndex() {
674664
ArrayNode arrayNode = objectMapper.createArrayNode();
675665
arrayNode.addAll(List.of(node0, node1));
676666

677-
SpelExpressionParser parser = new SpelExpressionParser();
678667
Expression expr = parser.parseExpression("[0]");
679668
assertThat(expr.getValue(context, arrayNode)).isSameAs(node0);
680669

@@ -703,7 +692,6 @@ void readAndWriteIndexWithSimpleEvaluationContext() {
703692
SimpleEvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding()
704693
.withIndexAccessors(new JacksonArrayNodeIndexAccessor(objectMapper))
705694
.build();
706-
SpelExpressionParser parser = new SpelExpressionParser();
707695

708696
TextNode node0 = new TextNode("node0");
709697
TextNode node1 = new TextNode("node1");

0 commit comments

Comments
 (0)