a fork of EvalEx by ezylang with a handful of breaking changes

Apply spotless.

+71 -47
+71 -47
src/test/java/com/ezylang/evalex/ExpressionEvaluatorDataAccessorTest.java
··· 1 + /* 2 + Copyright 2012-2024 Udo Klimaschewski 3 + 4 + Licensed under the Apache License, Version 2.0 (the "License"); 5 + you may not use this file except in compliance with the License. 6 + You may obtain a copy of the License at 7 + 8 + http://www.apache.org/licenses/LICENSE-2.0 9 + 10 + Unless required by applicable law or agreed to in writing, software 11 + distributed under the License is distributed on an "AS IS" BASIS, 12 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 + See the License for the specific language governing permissions and 14 + limitations under the License. 15 + */ 1 16 package com.ezylang.evalex; 2 17 3 18 import com.ezylang.evalex.data.DataAccessorIfc; 4 19 import com.ezylang.evalex.data.EvaluationValue; 5 20 import com.ezylang.evalex.data.types.DataAccessorValue; 6 21 import com.ezylang.evalex.parser.ParseException; 7 - import org.assertj.core.api.Assertions; 8 - import org.junit.jupiter.api.Test; 9 - 10 22 import java.lang.reflect.Field; 11 23 import java.lang.reflect.Modifier; 12 24 import java.math.BigDecimal; ··· 15 27 import java.util.Map; 16 28 import java.util.Set; 17 29 import java.util.function.Function; 30 + import org.assertj.core.api.Assertions; 31 + import org.junit.jupiter.api.Test; 18 32 19 33 public class ExpressionEvaluatorDataAccessorTest extends BaseExpressionEvaluatorTest { 20 34 21 - @Test 22 - void testBasicAccessorAccess() throws ParseException, EvaluationException { 23 - DataAccessorValue value = DataAccessorValue.of(reflectiveAccessor(new TestClass())); 35 + @Test 36 + void testBasicAccessorAccess() throws ParseException, EvaluationException { 37 + DataAccessorValue value = DataAccessorValue.of(reflectiveAccessor(new TestClass())); 24 38 25 - Assertions.assertThat(createExpression("test_obj.testField") 39 + Assertions.assertThat( 40 + createExpression("test_obj.testField") 26 41 .evaluate(builder -> builder.parameter("test_obj", value))) 27 - .extracting(EvaluationValue::getBooleanValue).isEqualTo(true); 42 + .extracting(EvaluationValue::getBooleanValue) 43 + .isEqualTo(true); 28 44 29 - Assertions.assertThat(createExpression("test_obj.testIntField") 30 - .evaluate(builder -> builder.parameter("test_obj", value))) 31 - .extracting(EvaluationValue::getNumberValue).isEqualTo(BigDecimal.valueOf(45)); 45 + Assertions.assertThat( 46 + createExpression("test_obj.testIntField") 47 + .evaluate(builder -> builder.parameter("test_obj", value))) 48 + .extracting(EvaluationValue::getNumberValue) 49 + .isEqualTo(BigDecimal.valueOf(45)); 32 50 33 - Assertions.assertThat(createExpression("test_obj.coolString") 34 - .evaluate(builder -> builder.parameter("test_obj", value))) 35 - .extracting(EvaluationValue::getStringValue).isEqualTo("Hello World!"); 36 - 37 - Assertions.assertThatThrownBy(() -> createExpression("test_obj.noString") 51 + Assertions.assertThat( 52 + createExpression("test_obj.coolString") 38 53 .evaluate(builder -> builder.parameter("test_obj", value))) 39 - .isInstanceOf(EvaluationException.class) 40 - .hasMessage("Field 'noString' not found in structure"); 41 - } 54 + .extracting(EvaluationValue::getStringValue) 55 + .isEqualTo("Hello World!"); 42 56 43 - public static class TestClass { 44 - public boolean testField = true; 45 - public int testIntField = 45; 46 - public final String coolString = "Hello World!"; 47 - } 57 + Assertions.assertThatThrownBy( 58 + () -> 59 + createExpression("test_obj.noString") 60 + .evaluate(builder -> builder.parameter("test_obj", value))) 61 + .isInstanceOf(EvaluationException.class) 62 + .hasMessage("Field 'noString' not found in structure"); 63 + } 48 64 49 - public static DataAccessorIfc reflectiveAccessor(Object object) { 50 - Map<String, Function<Object, Object>> fields = new HashMap<>(); 51 - Set<String> invalid = new HashSet<>(); 52 - return (variable, token, context) -> { 53 - if (fields.containsKey(variable)) return context.expression().convertValue(fields.get(variable).apply(object)); 54 - if (invalid.contains(variable)) return null; 65 + public static class TestClass { 66 + public boolean testField = true; 67 + public int testIntField = 45; 68 + public final String coolString = "Hello World!"; 69 + } 55 70 56 - for (Field field : object.getClass().getFields()) { 57 - if (!field.getName().equals(variable)) continue; 58 - if (Modifier.isStatic(field.getModifiers())) continue; 59 - Function<Object, Object> function = object1 -> { 60 - try { 61 - return field.get(object1); 62 - } catch (IllegalAccessException e) { 63 - throw new RuntimeException(e); 64 - } 65 - }; 66 - fields.put(variable, function); 67 - return context.expression().convertValue(function.apply(object)); 68 - } 69 - invalid.add(variable); 70 - return null; 71 - }; 72 - } 71 + public static DataAccessorIfc reflectiveAccessor(Object object) { 72 + Map<String, Function<Object, Object>> fields = new HashMap<>(); 73 + Set<String> invalid = new HashSet<>(); 74 + return (variable, token, context) -> { 75 + if (fields.containsKey(variable)) 76 + return context.expression().convertValue(fields.get(variable).apply(object)); 77 + if (invalid.contains(variable)) return null; 78 + 79 + for (Field field : object.getClass().getFields()) { 80 + if (!field.getName().equals(variable)) continue; 81 + if (Modifier.isStatic(field.getModifiers())) continue; 82 + Function<Object, Object> function = 83 + object1 -> { 84 + try { 85 + return field.get(object1); 86 + } catch (IllegalAccessException e) { 87 + throw new RuntimeException(e); 88 + } 89 + }; 90 + fields.put(variable, function); 91 + return context.expression().convertValue(function.apply(object)); 92 + } 93 + invalid.add(variable); 94 + return null; 95 + }; 96 + } 73 97 }