|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 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 | + * https://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 | + */ |
| 16 | + |
| 17 | +package org.springframework.expression.spel.ast; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import org.springframework.expression.ExpressionParser; |
| 26 | +import org.springframework.expression.spel.ExpressionState; |
| 27 | +import org.springframework.expression.spel.standard.SpelCompiler; |
| 28 | +import org.springframework.expression.spel.standard.SpelExpression; |
| 29 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 30 | +import org.springframework.expression.spel.support.StandardEvaluationContext; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Semyon Danilov |
| 36 | + */ |
| 37 | +public class InlineCollectionTests { |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testListCached() { |
| 41 | + InlineList list = parseList("{1, -2, 3, 4}"); |
| 42 | + assertThat(list.isConstant()).isTrue(); |
| 43 | + assertThat(list.getConstantValue()).isEqualTo(Arrays.asList(1, -2, 3, 4)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testDynamicListNotCached() { |
| 48 | + InlineList list = parseList("{1, 5-2, 3, 4}"); |
| 49 | + assertThat(list.isConstant()).isFalse(); |
| 50 | + assertThat(list.getValue(null)).isEqualTo(Arrays.asList(1, 3, 3, 4)); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testListWithVariableNotCached() { |
| 55 | + InlineList list = parseList("{1, -a, 3, 4}"); |
| 56 | + assertThat(list.isConstant()).isFalse(); |
| 57 | + final StandardEvaluationContext standardEvaluationContext = new StandardEvaluationContext(new AHolder()); |
| 58 | + standardEvaluationContext.setVariable("a", 2); |
| 59 | + assertThat(list.getValue(new ExpressionState(standardEvaluationContext))).isEqualTo(Arrays.asList(1, -2, 3, 4)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testListCanBeCompiled() { |
| 64 | + SpelExpression listExpression = parseExpression("{1, -2, 3, 4}"); |
| 65 | + assertThat(((SpelNodeImpl) listExpression.getAST()).isCompilable()).isTrue(); |
| 66 | + assertThat(SpelCompiler.compile(listExpression)).isTrue(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testDynamicListCantBeCompiled() { |
| 71 | + SpelExpression listExpression = parseExpression("{1, 5-2, 3, 4}"); |
| 72 | + assertThat(((SpelNodeImpl) listExpression.getAST()).isCompilable()).isFalse(); |
| 73 | + assertThat(SpelCompiler.compile(listExpression)).isFalse(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testMapCached() { |
| 78 | + InlineMap map = parseMap("{1 : 2, 3 : 4}"); |
| 79 | + assertThat(map.isConstant()).isTrue(); |
| 80 | + final Map<Integer, Integer> expected = new HashMap<>(); |
| 81 | + expected.put(1, 2); |
| 82 | + expected.put(3, 4); |
| 83 | + assertThat(map.getValue(null)).isEqualTo(expected); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testMapWithNegativeKeyCached() { |
| 88 | + InlineMap map = parseMap("{-1 : 2, -3 : 4}"); |
| 89 | + assertThat(map.isConstant()).isTrue(); |
| 90 | + final Map<Integer, Integer> expected = new HashMap<>(); |
| 91 | + expected.put(-1, 2); |
| 92 | + expected.put(-3, 4); |
| 93 | + assertThat(map.getValue(null)).isEqualTo(expected); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testMapWithNegativeValueCached() { |
| 98 | + InlineMap map = parseMap("{1 : -2, 3 : -4}"); |
| 99 | + assertThat(map.isConstant()).isTrue(); |
| 100 | + final Map<Integer, Integer> expected = new HashMap<>(); |
| 101 | + expected.put(1, -2); |
| 102 | + expected.put(3, -4); |
| 103 | + assertThat(map.getValue(null)).isEqualTo(expected); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testMapWithNegativeLongTypesCached() { |
| 108 | + InlineMap map = parseMap("{1L : -2L, 3L : -4L}"); |
| 109 | + assertThat(map.isConstant()).isTrue(); |
| 110 | + final Map<Long, Long> expected = new HashMap<>(); |
| 111 | + expected.put(1L, -2L); |
| 112 | + expected.put(3L, -4L); |
| 113 | + assertThat(map.getValue(null)).isEqualTo(expected); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testMapWithNegativeFloatTypesCached() { |
| 118 | + InlineMap map = parseMap("{-1.0f : -2.0f, -3.0f : -4.0f}"); |
| 119 | + assertThat(map.isConstant()).isTrue(); |
| 120 | + final Map<Float, Float> expected = new HashMap<>(); |
| 121 | + expected.put(-1.0f, -2.0f); |
| 122 | + expected.put(-3.0f, -4.0f); |
| 123 | + assertThat(map.getValue(null)).isEqualTo(expected); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testMapWithNegativeRealTypesCached() { |
| 128 | + InlineMap map = parseMap("{-1.0 : -2.0, -3.0 : -4.0}"); |
| 129 | + assertThat(map.isConstant()).isTrue(); |
| 130 | + final Map<Double, Double> expected = new HashMap<>(); |
| 131 | + expected.put(-1.0, -2.0); |
| 132 | + expected.put(-3.0, -4.0); |
| 133 | + assertThat(map.getValue(null)).isEqualTo(expected); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testMapWithNegativeKeyAndValueCached() { |
| 138 | + InlineMap map = parseMap("{-1 : -2, -3 : -4}"); |
| 139 | + assertThat(map.isConstant()).isTrue(); |
| 140 | + final Map<Integer, Integer> expected = new HashMap<>(); |
| 141 | + expected.put(-1, -2); |
| 142 | + expected.put(-3, -4); |
| 143 | + assertThat(map.getValue(null)).isEqualTo(expected); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void testMapWithDynamicNotCached() { |
| 148 | + InlineMap map = parseMap("{-1 : 2, -3+1 : -4}"); |
| 149 | + assertThat(map.isConstant()).isFalse(); |
| 150 | + final Map<Integer, Integer> expected = new HashMap<>(); |
| 151 | + expected.put(-1, 2); |
| 152 | + expected.put(-2, -4); |
| 153 | + assertThat(map.getValue(null)).isEqualTo(expected); |
| 154 | + } |
| 155 | + |
| 156 | + private InlineMap parseMap(String s) { |
| 157 | + SpelExpression expression = parseExpression(s); |
| 158 | + return (InlineMap) expression.getAST(); |
| 159 | + } |
| 160 | + |
| 161 | + private InlineList parseList(String s) { |
| 162 | + SpelExpression expression = parseExpression(s); |
| 163 | + return (InlineList) expression.getAST(); |
| 164 | + } |
| 165 | + |
| 166 | + private SpelExpression parseExpression(final String s) { |
| 167 | + ExpressionParser parser = new SpelExpressionParser(); |
| 168 | + return (SpelExpression) parser.parseExpression(s); |
| 169 | + } |
| 170 | + |
| 171 | + private static class AHolder { |
| 172 | + public int a = 2; |
| 173 | + } |
| 174 | + |
| 175 | +} |
0 commit comments