Introduce null-safe indexing test for custom IndexAccessor
This commit is contained in:
parent
a01f7cefae
commit
9eab7bb11d
|
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package example;
|
||||
|
||||
public enum Color {
|
||||
RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE
|
||||
}
|
||||
|
|
@ -33,6 +33,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.NullNode;
|
||||
import com.fasterxml.jackson.databind.node.TextNode;
|
||||
import example.Color;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
|
@ -455,6 +456,58 @@ class IndexingTests {
|
|||
assertThat(expression.getValue(context)).isEqualTo("Jane");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullSafeIndexWithCustomIndexAccessor() {
|
||||
context.addIndexAccessor(new BirdsIndexAccessor());
|
||||
context.setVariable("color", Color.RED);
|
||||
|
||||
expression = parser.parseExpression("birds?.[#color]");
|
||||
assertThat(expression.getValue(context)).isNull();
|
||||
rootContext.birds = new Birds();
|
||||
assertThat(expression.getValue(context)).isEqualTo("cardinal");
|
||||
}
|
||||
|
||||
static class Birds {
|
||||
|
||||
public String get(Color color) {
|
||||
return switch (color) {
|
||||
case RED -> "cardinal";
|
||||
case BLUE -> "blue jay";
|
||||
default -> throw new RuntimeException("unknown bird color: " + color);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
static class BirdsIndexAccessor implements IndexAccessor {
|
||||
|
||||
@Override
|
||||
public Class<?>[] getSpecificTargetClasses() {
|
||||
return new Class[] { Birds.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canRead(EvaluationContext context, Object target, Object index) {
|
||||
return (target instanceof Birds && index instanceof Color);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypedValue read(EvaluationContext context, Object target, Object index) {
|
||||
Birds birds = (Birds) target;
|
||||
Color color = (Color) index;
|
||||
return new TypedValue(birds.get(color));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canWrite(EvaluationContext context, Object target, Object index) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(EvaluationContext context, Object target, Object index, @Nullable Object newValue) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
static class RootContextWithIndexedProperties {
|
||||
public int[] array;
|
||||
public List<Integer> list;
|
||||
|
|
@ -462,6 +515,7 @@ class IndexingTests {
|
|||
public String string;
|
||||
public Map<String, Integer> map;
|
||||
public Person person;
|
||||
public Birds birds;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue