cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject [2/2] cayenne git commit: CAY-1971 Variants of Property.like(..) : contains(..), startsWith(..), endsWith(..)
Date Wed, 10 Dec 2014 08:33:17 GMT
CAY-1971 Variants of Property.like(..) : contains(..), startsWith(..), endsWith(..)

* fixing escaping bug
* escaping unit tests


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/2894d5ff
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/2894d5ff
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/2894d5ff

Branch: refs/heads/master
Commit: 2894d5ffa04df9b01e5bb3acd097be23a2c9d438
Parents: 0ffb210
Author: aadamchik <aadamchik@apache.org>
Authored: Sat Nov 22 14:46:31 2014 +0300
Committer: aadamchik <aadamchik@apache.org>
Committed: Wed Dec 10 11:30:22 2014 +0300

----------------------------------------------------------------------
 .../cayenne/exp/LikeExpressionHelper.java       |  1 -
 .../cayenne/exp/LikeExpressionHelperTest.java   | 83 ++++++++++++++++++++
 .../org/apache/cayenne/query/SelectQueryIT.java | 14 ++++
 3 files changed, 97 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/2894d5ff/cayenne-server/src/main/java/org/apache/cayenne/exp/LikeExpressionHelper.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/exp/LikeExpressionHelper.java
b/cayenne-server/src/main/java/org/apache/cayenne/exp/LikeExpressionHelper.java
index 31ec1f2..ad07386 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/exp/LikeExpressionHelper.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/exp/LikeExpressionHelper.java
@@ -119,7 +119,6 @@ class LikeExpressionHelper {
 			char c = pattern.charAt(i);
 			if (c == WILDCARD_SEQUENCE || c == WILDCARD_ONE) {
 				buffer.append(escapeChar);
-				break;
 			}
 
 			buffer.append(c);

http://git-wip-us.apache.org/repos/asf/cayenne/blob/2894d5ff/cayenne-server/src/test/java/org/apache/cayenne/exp/LikeExpressionHelperTest.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/exp/LikeExpressionHelperTest.java
b/cayenne-server/src/test/java/org/apache/cayenne/exp/LikeExpressionHelperTest.java
new file mode 100644
index 0000000..90051e3
--- /dev/null
+++ b/cayenne-server/src/test/java/org/apache/cayenne/exp/LikeExpressionHelperTest.java
@@ -0,0 +1,83 @@
+/*****************************************************************
+ *   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
+ *
+ *    http://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;
+
+import static org.junit.Assert.assertEquals;
+
+import org.apache.cayenne.exp.parser.ASTLike;
+import org.apache.cayenne.exp.parser.ASTObjPath;
+import org.apache.cayenne.exp.parser.PatternMatchNode;
+import org.junit.Test;
+
+public class LikeExpressionHelperTest {
+
+	@Test
+	public void testEscape_NoEscapeChars() {
+
+		PatternMatchNode node = new ASTLike(new ASTObjPath("x"), "abc");
+		LikeExpressionHelper.escape(node);
+		assertEquals("abc", node.getOperand(1));
+		assertEquals(0, node.getEscapeChar());
+	}
+	
+	@Test
+	public void testEscape_OneChar() {
+
+		PatternMatchNode node = new ASTLike(new ASTObjPath("x"), "ab_c");
+		LikeExpressionHelper.escape(node);
+		assertEquals("ab!_c", node.getOperand(1));
+		assertEquals('!', node.getEscapeChar());
+	}
+	
+	@Test
+	public void testEscape_TwoChars() {
+
+		PatternMatchNode node = new ASTLike(new ASTObjPath("x"), "ab_c_");
+		LikeExpressionHelper.escape(node);
+		assertEquals("ab!_c!_", node.getOperand(1));
+		assertEquals('!', node.getEscapeChar());
+	}
+	
+	@Test
+	public void testEscape_TwoChars_Mix() {
+
+		PatternMatchNode node = new ASTLike(new ASTObjPath("x"), "ab%c_");
+		LikeExpressionHelper.escape(node);
+		assertEquals("ab!%c!_", node.getOperand(1));
+		assertEquals('!', node.getEscapeChar());
+	}
+	
+	@Test
+	public void testEscape_AltEscapeChar1() {
+
+		PatternMatchNode node = new ASTLike(new ASTObjPath("x"), "a!%c");
+		LikeExpressionHelper.escape(node);
+		assertEquals("a!#%c", node.getOperand(1));
+		assertEquals('#', node.getEscapeChar());
+	}
+	
+	@Test
+	public void testEscape_AltEscapeChar2() {
+
+		PatternMatchNode node = new ASTLike(new ASTObjPath("x"), "a!%c#_");
+		LikeExpressionHelper.escape(node);
+		assertEquals("a!$%c#$_", node.getOperand(1));
+		assertEquals('$', node.getEscapeChar());
+	}
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/2894d5ff/cayenne-server/src/test/java/org/apache/cayenne/query/SelectQueryIT.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/query/SelectQueryIT.java b/cayenne-server/src/test/java/org/apache/cayenne/query/SelectQueryIT.java
index 74a738a..7911419 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/query/SelectQueryIT.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/query/SelectQueryIT.java
@@ -330,6 +330,20 @@ public class SelectQueryIT extends ServerCase {
 	}
 
 	@Test
+	public void testSelectLike_WildcardMatchAndEscapeMulti_AndOtherCriteria() throws Exception
{
+
+		tArtist.insert(1, "_X_", null);
+		tArtist.insert(2, "_X", null);
+
+		SelectQuery<Artist> query = new SelectQuery<Artist>(Artist.class);
+		query.andQualifier(ExpressionFactory.likeExp("artistName", "#_%#_", '#'));
+		query.andQualifier(Artist.ARTIST_NAME.eq("_X_"));
+
+		List<?> objects = context.performQuery(query);
+		assertEquals(1, objects.size());
+	}
+
+	@Test
 	public void testSelectLikeMultiple_WildcardMatch() throws Exception {
 		createArtistsDataSet();
 		SelectQuery<Artist> query = new SelectQuery<Artist>(Artist.class);


Mime
View raw message