|
| 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.jpa.repository.query; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.springframework.data.jpa.repository.query.JpaQueryParsingToken.*; |
| 20 | + |
| 21 | +import org.antlr.v4.runtime.CharStreams; |
| 22 | +import org.antlr.v4.runtime.CommonTokenStream; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Christoph Strobl |
| 27 | + */ |
| 28 | +public class JpqlComplianceTests { |
| 29 | + |
| 30 | + private static String parseWithoutChanges(String query) { |
| 31 | + |
| 32 | + JpqlLexer lexer = new JpqlLexer(CharStreams.fromString(query)); |
| 33 | + JpqlParser parser = new JpqlParser(new CommonTokenStream(lexer)); |
| 34 | + |
| 35 | + parser.addErrorListener(new BadJpqlGrammarErrorListener(query)); |
| 36 | + |
| 37 | + JpqlParser.StartContext parsedQuery = parser.start(); |
| 38 | + |
| 39 | + return render(new JpqlQueryRenderer().visit(parsedQuery)); |
| 40 | + } |
| 41 | + |
| 42 | + private void assertQuery(String query) { |
| 43 | + |
| 44 | + String slimmedDownQuery = reduceWhitespace(query); |
| 45 | + assertThat(parseWithoutChanges(slimmedDownQuery)).isEqualTo(slimmedDownQuery); |
| 46 | + } |
| 47 | + |
| 48 | + private String reduceWhitespace(String original) { |
| 49 | + |
| 50 | + return original // |
| 51 | + .replaceAll("[ \\t\\n]{1,}", " ") // |
| 52 | + .trim(); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void numericLiterals() { |
| 57 | + |
| 58 | + assertQuery("SELECT e FROM Employee e WHERE e.id = 1234"); |
| 59 | + assertQuery("SELECT e FROM Employee e WHERE e.id = 1234L"); |
| 60 | + assertQuery("SELECT s FROM Stat s WHERE s.ratio > 3.14"); |
| 61 | + assertQuery("SELECT s FROM Stat s WHERE s.ratio > 3.14F"); |
| 62 | + assertQuery("SELECT s FROM Stat s WHERE s.ratio > 3.14e32D"); |
| 63 | + } |
| 64 | + |
| 65 | +} |
0 commit comments