From 229a57e8f5cd249e7953767cfb9c8b0dd3e2f4e4 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Thu, 18 Mar 2010 21:47:54 +0000 Subject: [PATCH] HHH-2997 LikeExpression case sensitive not working properly git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19025 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../org/hibernate/test/criteria/LikeTest.java | 116 ++++++++++++++++++ .../test/criteria/StringExpression.java | 18 +++ .../test/criteria/TestObject.hbm.xml | 15 +++ .../hibernate/test/criteria/TestObject.java | 22 ++++ 4 files changed, 171 insertions(+) create mode 100644 testsuite/src/test/java/org/hibernate/test/criteria/LikeTest.java create mode 100644 testsuite/src/test/java/org/hibernate/test/criteria/StringExpression.java create mode 100644 testsuite/src/test/java/org/hibernate/test/criteria/TestObject.hbm.xml create mode 100644 testsuite/src/test/java/org/hibernate/test/criteria/TestObject.java diff --git a/testsuite/src/test/java/org/hibernate/test/criteria/LikeTest.java b/testsuite/src/test/java/org/hibernate/test/criteria/LikeTest.java new file mode 100644 index 0000000000..3649286e3d --- /dev/null +++ b/testsuite/src/test/java/org/hibernate/test/criteria/LikeTest.java @@ -0,0 +1,116 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Middleware LLC. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + * + */ +package org.hibernate.test.criteria; + +import java.util.List; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.criterion.Restrictions; +import org.hibernate.junit.functional.FunctionalTestCase; + + +public class LikeTest extends FunctionalTestCase { + + public LikeTest( String string ) { + super( string ); + } + + public String[] getMappings() { + return new String[]{"criteria/TestObject.hbm.xml"}; + } + public void testLike(){ + Session session=openSession(); + String uniq = "uniq" + System.currentTimeMillis(); + + // insert object + try { + Transaction tx = session.beginTransaction(); + + + TestObject obj = new TestObject(); + obj.setText("XyZ " + uniq + " blablabla"); + + session.saveOrUpdate(obj); + session.flush(); + tx.commit(); + } finally { + session.close(); + + } + String pattern = "XyZ " + uniq + "%"; + + // retrieve object - case sensitive - works ok + session = openSession(); + try { + List objects = session.createCriteria(TestObject.class) + .add(Restrictions.like("text", pattern)) + .list(); + + assertEquals(1, objects.size()); + } finally { + session.close(); + } + + // retrieve object - case insensitive - works ok + session = openSession(); + try { + List objects = session.createCriteria(TestObject.class) + .add(Restrictions.like("text", pattern).ignoreCase()) + .list(); + + assertEquals(1, objects.size()); + } finally { + session.close(); + } + + + // retrieve object - case insensitive via custom expression - works ok + session = openSession(); + try { + List objects = session.createCriteria(TestObject.class) + .add(StringExpression.stringExpression("text", pattern, true)) + .list(); + + assertEquals(1, objects.size()); + } finally { + session.close(); + } + + + // retrieve object - case sensitive via custom expression - not working + session = openSession(); + try { + List objects = session.createCriteria(TestObject.class) + .add(StringExpression.stringExpression("text", pattern, false)) + .list(); + + assertEquals(1, objects.size()); + } finally { + session.close(); + } + + } +} diff --git a/testsuite/src/test/java/org/hibernate/test/criteria/StringExpression.java b/testsuite/src/test/java/org/hibernate/test/criteria/StringExpression.java new file mode 100644 index 0000000000..81b0386c90 --- /dev/null +++ b/testsuite/src/test/java/org/hibernate/test/criteria/StringExpression.java @@ -0,0 +1,18 @@ +package org.hibernate.test.criteria; + +import org.hibernate.criterion.Criterion; +import org.hibernate.criterion.LikeExpression; + +public class StringExpression extends LikeExpression { + private final static Character ESCAPE_CODE = new Character( '\\' ); + + protected StringExpression( String property, String value, + boolean ignoreCase ) { + super( property, value, ESCAPE_CODE, ignoreCase ); + } + + public static Criterion stringExpression( String propertyName, + String value, boolean ignoreCase ) { + return new StringExpression( propertyName, value, ignoreCase ); + } +} diff --git a/testsuite/src/test/java/org/hibernate/test/criteria/TestObject.hbm.xml b/testsuite/src/test/java/org/hibernate/test/criteria/TestObject.hbm.xml new file mode 100644 index 0000000000..83d4bdd924 --- /dev/null +++ b/testsuite/src/test/java/org/hibernate/test/criteria/TestObject.hbm.xml @@ -0,0 +1,15 @@ + + + + + + + + + test_seq + + + + + + \ No newline at end of file diff --git a/testsuite/src/test/java/org/hibernate/test/criteria/TestObject.java b/testsuite/src/test/java/org/hibernate/test/criteria/TestObject.java new file mode 100644 index 0000000000..9659443c90 --- /dev/null +++ b/testsuite/src/test/java/org/hibernate/test/criteria/TestObject.java @@ -0,0 +1,22 @@ +package org.hibernate.test.criteria; + +public class TestObject { + private Integer id; + private String text; + + public Integer getId() { + return id; + } + + public void setId( Integer id ) { + this.id = id; + } + + public String getText() { + return text; + } + + public void setText( String text ) { + this.text = text; + } +}