| 
 | 1 | +/*  | 
 | 2 | + * Copyright 2023 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 | +package org.springframework.data.repository.query;  | 
 | 17 | + | 
 | 18 | +import static org.assertj.core.api.Assertions.*;  | 
 | 19 | + | 
 | 20 | +import java.lang.reflect.Method;  | 
 | 21 | + | 
 | 22 | +import org.junit.jupiter.api.Test;  | 
 | 23 | +import org.springframework.data.repository.query.SpelQueryContext.SpelExtractor;  | 
 | 24 | + | 
 | 25 | +/**  | 
 | 26 | + * Unit tests for {@link SpelEvaluator}.  | 
 | 27 | + *  | 
 | 28 | + * @author Mark Paluch  | 
 | 29 | + */  | 
 | 30 | +class SpelEvaluatorUnitTests {  | 
 | 31 | + | 
 | 32 | +	final SpelQueryContext context = SpelQueryContext.of((counter, s) -> String.format("__$synthetic$__%d", counter + 1),  | 
 | 33 | +			String::concat);  | 
 | 34 | + | 
 | 35 | +	@Test // GH-2904  | 
 | 36 | +	void shouldEvaluateExpression() throws Exception {  | 
 | 37 | + | 
 | 38 | +		SpelExtractor extractor = context.parse("SELECT :#{#value}");  | 
 | 39 | +		Method method = MyRepository.class.getDeclaredMethod("simpleExpression", String.class);  | 
 | 40 | +		SpelEvaluator evaluator = new SpelEvaluator(QueryMethodEvaluationContextProvider.DEFAULT,  | 
 | 41 | +				new DefaultParameters(method), extractor);  | 
 | 42 | + | 
 | 43 | +		assertThat(evaluator.getQueryString()).isEqualTo("SELECT :__$synthetic$__1");  | 
 | 44 | +		assertThat(evaluator.evaluate(new Object[] { "hello" })).containsEntry("__$synthetic$__1", "hello");  | 
 | 45 | +	}  | 
 | 46 | + | 
 | 47 | +	@Test // GH-2904  | 
 | 48 | +	void shouldAllowNullValues() throws Exception {  | 
 | 49 | + | 
 | 50 | +		SpelExtractor extractor = context.parse("SELECT :#{#value}");  | 
 | 51 | +		Method method = MyRepository.class.getDeclaredMethod("simpleExpression", String.class);  | 
 | 52 | +		SpelEvaluator evaluator = new SpelEvaluator(QueryMethodEvaluationContextProvider.DEFAULT,  | 
 | 53 | +				new DefaultParameters(method), extractor);  | 
 | 54 | + | 
 | 55 | +		assertThat(evaluator.getQueryString()).isEqualTo("SELECT :__$synthetic$__1");  | 
 | 56 | +		assertThat(evaluator.evaluate(new Object[] { null })).containsEntry("__$synthetic$__1", null);  | 
 | 57 | +	}  | 
 | 58 | + | 
 | 59 | +	interface MyRepository {  | 
 | 60 | + | 
 | 61 | +		void simpleExpression(String value);  | 
 | 62 | + | 
 | 63 | +	}  | 
 | 64 | +}  | 
0 commit comments