Polishing

This commit is contained in:
Sam Brannen 2024-01-28 18:29:49 +01:00
parent 62fa3f11c1
commit dc6ce30663
2 changed files with 26 additions and 19 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -149,7 +149,7 @@ public abstract class SpelNodeImpl implements SpelNode, Opcodes {
public TypedValue setValueInternal(ExpressionState expressionState, Supplier<TypedValue> valueSupplier) public TypedValue setValueInternal(ExpressionState expressionState, Supplier<TypedValue> valueSupplier)
throws EvaluationException { throws EvaluationException {
throw new SpelEvaluationException(getStartPosition(), SpelMessage.SETVALUE_NOT_SUPPORTED, getClass()); throw new SpelEvaluationException(getStartPosition(), SpelMessage.SETVALUE_NOT_SUPPORTED, getClass().getName());
} }
@Override @Override

View File

@ -28,11 +28,16 @@ import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.expression.EvaluationException; import org.springframework.expression.EvaluationException;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.ParseException; import org.springframework.expression.ParseException;
import org.springframework.expression.spel.ast.Assign;
import org.springframework.expression.spel.testresources.PlaceOfBirth; import org.springframework.expression.spel.testresources.PlaceOfBirth;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.springframework.expression.spel.SpelMessage.ARRAY_INDEX_OUT_OF_BOUNDS;
import static org.springframework.expression.spel.SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS;
import static org.springframework.expression.spel.SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE;
import static org.springframework.expression.spel.SpelMessage.SETVALUE_NOT_SUPPORTED;
import static org.springframework.expression.spel.SpelMessage.TYPE_CONVERSION_ERROR; import static org.springframework.expression.spel.SpelMessage.TYPE_CONVERSION_ERROR;
/** /**
@ -56,22 +61,22 @@ class SetValueTests extends AbstractExpressionTests {
@Test @Test
void setValueFailsWhenLeftOperandIsNotAssignable() { void setValueFailsWhenLeftOperandIsNotAssignable() {
setValueAndExpectError("3=4", "enigma"); setValueAndExpectError("3=4", "enigma", SETVALUE_NOT_SUPPORTED, 1, Assign.class.getName());
} }
@Test @Test
void setValueFailsWhenLeftOperandCannotBeIndexed() { void setValueFailsWhenLeftOperandCannotBeIndexed() {
setValueAndExpectError("'hello'[3]", 'p'); setValueAndExpectError("'hello'[3]", 'p', INDEXING_NOT_SUPPORTED_FOR_TYPE, 7, String.class.getName());
} }
@Test @Test
void setArrayElementFailsWhenIndexIsOutOfBounds() { void setArrayElementFailsWhenIndexIsOutOfBounds() {
setValueAndExpectError("placesLived[23]", "Wien"); setValueAndExpectError("placesLived[23]", "Wien", ARRAY_INDEX_OUT_OF_BOUNDS, 11, 1, 23);
} }
@Test @Test
void setListElementFailsWhenIndexIsOutOfBounds() { void setListElementFailsWhenIndexIsOutOfBounds() {
setValueAndExpectError("placesLivedList[23]", "Wien"); setValueAndExpectError("placesLivedList[23]", "Wien", COLLECTION_INDEX_OUT_OF_BOUNDS, 15, 1, 23);
} }
@Test @Test
@ -118,19 +123,6 @@ class SetValueTests extends AbstractExpressionTests {
setValue("arrayContainer.doubles[1]", 3.4d); setValue("arrayContainer.doubles[1]", 3.4d);
} }
@Test
void setArrayElementToPrimitiveFromStringFails() {
String notPrimitiveOrWrapper = "not primitive or wrapper";
setValueAndExpectError("arrayContainer.booleans[1]", notPrimitiveOrWrapper);
setValueAndExpectError("arrayContainer.chars[1]", notPrimitiveOrWrapper);
setValueAndExpectError("arrayContainer.shorts[1]", notPrimitiveOrWrapper);
setValueAndExpectError("arrayContainer.bytes[1]", notPrimitiveOrWrapper);
setValueAndExpectError("arrayContainer.ints[1]", notPrimitiveOrWrapper);
setValueAndExpectError("arrayContainer.longs[1]", notPrimitiveOrWrapper);
setValueAndExpectError("arrayContainer.floats[1]", notPrimitiveOrWrapper);
setValueAndExpectError("arrayContainer.doubles[1]", notPrimitiveOrWrapper);
}
@Test @Test
void setArrayElementToPrimitiveFromSingleElementPrimitiveArray() { void setArrayElementToPrimitiveFromSingleElementPrimitiveArray() {
setValue("arrayContainer.booleans[1]", new boolean[] { false }, false); setValue("arrayContainer.booleans[1]", new boolean[] { false }, false);
@ -167,6 +159,21 @@ class SetValueTests extends AbstractExpressionTests {
setValue("arrayContainer.doubles[1]", List.of(42D), 42D); setValue("arrayContainer.doubles[1]", List.of(42D), 42D);
} }
@ParameterizedTest
@ValueSource(strings = {
"arrayContainer.booleans[1]",
"arrayContainer.chars[1]",
"arrayContainer.shorts[1]",
"arrayContainer.bytes[1]",
"arrayContainer.ints[1]",
"arrayContainer.longs[1]",
"arrayContainer.floats[1]",
"arrayContainer.doubles[1]"
})
void setArrayElementToPrimitiveFromStringFailsWithTypeConversionError(String expression) {
setValueAndExpectError(expression, "enigma", TYPE_CONVERSION_ERROR);
}
@ParameterizedTest @ParameterizedTest
@ValueSource(strings = { @ValueSource(strings = {
"arrayContainer.booleans[1]", "arrayContainer.booleans[1]",