This is an automated email from the ASF dual-hosted git repository.
ntimofeev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git
The following commit(s) were added to refs/heads/master by this push:
new 01c3b45a8 Minor code cleanup and additional tests
01c3b45a8 is described below
commit 01c3b45a8d822793f99c0d14373e76a1538c97f9
Author: Nikita Timofeev <[email protected]>
AuthorDate: Tue May 17 20:03:50 2022 +0300
Minor code cleanup and additional tests
---
.../apache/cayenne/exp/parser/ExpressionUtils.java | 37 ++++++-----
.../cayenne/exp/parser/ExpressionUtilsTest.java | 73 ++++++++++++++++++++++
2 files changed, 93 insertions(+), 17 deletions(-)
diff --git
a/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ExpressionUtils.java
b/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ExpressionUtils.java
index 3839f9e0c..e5700c025 100644
---
a/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ExpressionUtils.java
+++
b/cayenne-server/src/main/java/org/apache/cayenne/exp/parser/ExpressionUtils.java
@@ -26,27 +26,30 @@ import java.util.Map;
* @since 4.2
*/
class ExpressionUtils {
+ static void parsePath(ASTPath pathExp, String path) throws ParseException {
+ if(path == null || !path.contains("#")) {
+ pathExp.setPath(path);
+ return;
+ }
- static void parsePath(ASTPath pathExp, Object path) throws ParseException {
- if (path != null && path.toString().contains("#")) {
- String[] pathSegments = path.toString().split("\\.");
- Map<String, String> aliasMap = new HashMap<>();
- for (int i = 0; i < pathSegments.length; i++) {
- if (pathSegments[i].contains("#")) {
- String[] splitedSegment = pathSegments[i].split("#");
- splitedSegment[0] += splitedSegment[1].endsWith("+") ? "+"
: "";
- splitedSegment[1] = splitedSegment[1].endsWith("+") ?
splitedSegment[1].substring(0, splitedSegment[1].length() - 1) :
splitedSegment[1];
- if (aliasMap.putIfAbsent(splitedSegment[1],
splitedSegment[0]) != null &&
!aliasMap.get(splitedSegment[1]).equals(splitedSegment[0])) {
- throw new ParseException("Can't add the same alias to
different path segments.");
- }
- pathSegments[i] = splitedSegment[1];
+ String[] pathSegments = path.split("\\.");
+ Map<String, String> aliasMap = new HashMap<>();
+ for (int i = 0; i < pathSegments.length; i++) {
+ if (pathSegments[i].contains("#")) {
+ String[] splitSegment = pathSegments[i].split("#");
+ if(splitSegment[1].endsWith("+")) {
+ splitSegment[0] += '+';
+ splitSegment[1] = splitSegment[1].substring(0,
splitSegment[1].length() - 1);
}
+ String previousAlias = aliasMap.putIfAbsent(splitSegment[1],
splitSegment[0]);
+ if (previousAlias != null &&
!previousAlias.equals(splitSegment[0])) {
+ throw new ParseException("Can't add the same alias to
different path segments.");
+ }
+ pathSegments[i] = splitSegment[1];
}
- pathExp.setPath(String.join(".", pathSegments));
- pathExp.setPathAliases(aliasMap);
- } else {
- pathExp.setPath(path);
}
+ pathExp.setPath(String.join(".", pathSegments));
+ pathExp.setPathAliases(aliasMap);
}
}
diff --git
a/cayenne-server/src/test/java/org/apache/cayenne/exp/parser/ExpressionUtilsTest.java
b/cayenne-server/src/test/java/org/apache/cayenne/exp/parser/ExpressionUtilsTest.java
new file mode 100644
index 000000000..4c3c57798
--- /dev/null
+++
b/cayenne-server/src/test/java/org/apache/cayenne/exp/parser/ExpressionUtilsTest.java
@@ -0,0 +1,73 @@
+/*****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.cayenne.exp.parser;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class ExpressionUtilsTest {
+
+ @Test
+ public void testParsePath() throws ParseException {
+ ASTPath path = new ASTObjPath();
+ ExpressionUtils.parsePath(path, "a.b.c.d");
+
+ assertEquals("a.b.c.d", path.getPath());
+ assertEquals(0, path.getPathAliases().size());
+ }
+
+ @Test
+ public void testParsePathOuterJoin() throws ParseException {
+ ASTPath path = new ASTObjPath();
+ ExpressionUtils.parsePath(path, "a.b+.c+.d");
+
+ assertEquals("a.b+.c+.d", path.getPath());
+ assertEquals(0, path.getPathAliases().size());
+ }
+
+ @Test
+ public void testParsePathWithAlias() throws ParseException {
+ ASTPath path = new ASTObjPath();
+ ExpressionUtils.parsePath(path, "a.b.c#p1.d#p2");
+
+ assertEquals("a.b.p1.p2", path.getPath());
+ assertEquals(2, path.getPathAliases().size());
+ assertEquals("c", path.getPathAliases().get("p1"));
+ assertEquals("d", path.getPathAliases().get("p2"));
+ }
+
+ @Test
+ public void testParsePathWithAliasAndOuterJoin() throws ParseException {
+ ASTPath path = new ASTObjPath();
+ ExpressionUtils.parsePath(path, "a.b+.c#p1+.d#p2");
+
+ assertEquals("a.b+.p1.p2", path.getPath());
+ assertEquals(2, path.getPathAliases().size());
+ assertEquals("c+", path.getPathAliases().get("p1"));
+ assertEquals("d", path.getPathAliases().get("p2"));
+ }
+
+ @Test(expected = ParseException.class)
+ public void testParseInvalidPath() throws ParseException {
+ ASTPath path = new ASTObjPath();
+ ExpressionUtils.parsePath(path, "a.b.c#p1.d#p1");
+ }
+}
\ No newline at end of file