From 27ad91fbbc4de6ece7c0b3f85454bcc9e01e123f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 25 Aug 2011 19:42:58 +0100 Subject: [PATCH] DATAJPA-92 - JpaQueryCreator now inspects enum to decide how to handle ignore case scenarios. --- .../jpa/repository/query/JpaQueryCreator.java | 28 ++++++++++--- .../PartTreeJpaQueryIntegrationTests.java | 39 +++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java index c887ab5ed..7b23d5f85 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java @@ -37,6 +37,7 @@ import org.springframework.data.repository.query.Parameter; import org.springframework.data.repository.query.Parameters; import org.springframework.data.repository.query.parser.AbstractQueryCreator; import org.springframework.data.repository.query.parser.Part; +import org.springframework.data.repository.query.parser.Part.IgnoreCaseType; import org.springframework.data.repository.query.parser.Part.Type; import org.springframework.data.repository.query.parser.PartTree; import org.springframework.data.repository.query.parser.Property; @@ -406,12 +407,27 @@ public class JpaQueryCreator extends */ private Expression upperIfIgnoreCase(Expression expression) { - if (part.shouldIgnoreCase() - && String.class.equals(expression.getJavaType())) { - return (Expression) builder - .upper((Expression) expression); - } - return expression; + switch (part.shouldIgnoreCase()) { + case ALWAYS: + Assert.state(canUpperCase(expression), + "Unable to ignore case of " + + expression.getJavaType().getName() + + " types, the property '" + + part.getProperty().getName() + + "' must reference a String"); + return (Expression) builder + .upper((Expression) expression); + case WHEN_POSSIBLE: + if (canUpperCase(expression)) { + return (Expression) builder + .upper((Expression) expression); + } + } + return expression; + } + + private boolean canUpperCase(Expression expression) { + return String.class.equals(expression.getJavaType()); } } } diff --git a/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java b/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java index 3524d48f0..da2f661f2 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/PartTreeJpaQueryIntegrationTests.java @@ -15,12 +15,16 @@ */ package org.springframework.data.jpa.repository.query; +import static org.junit.Assert.*; + import java.lang.reflect.Method; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; @@ -42,6 +46,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration("classpath:infrastructure.xml") public class PartTreeJpaQueryIntegrationTests { + @Rule + public ExpectedException thrown = ExpectedException.none(); + @PersistenceContext EntityManager entityManager; @@ -67,8 +74,40 @@ public class PartTreeJpaQueryIntegrationTests { jpaQuery.createQuery(new Object[] { "Matthews", new PageRequest(0, 1) }); } + @Test + public void cannotIgnoreCaseIfNotString() throws Exception { + + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Unable to ignore case of java.lang.Integer types, the property 'id' must reference a String"); + testIgnoreCase("findByIdIgnoringCase", 3); + } + + @Test + public void cannotIgnoreCaseIfNotStringUnlessIgnoringAll() throws Exception { + + testIgnoreCase("findByIdAllIgnoringCase", 3); + } + + private void testIgnoreCase(String methodName, Object...values) throws Exception { + + Class[] parameterTypes = new Class[values.length]; + for (int i = 0; i < values.length; i++) { + parameterTypes[i] = values[i].getClass(); + } + Method method = UserRepository.class.getMethod(methodName, parameterTypes); + JpaQueryMethod queryMethod = + new JpaQueryMethod(method, new DefaultRepositoryMetadata( + UserRepository.class), + PersistenceProvider.fromEntityManager(entityManager)); + PartTreeJpaQuery jpaQuery = + new PartTreeJpaQuery(queryMethod, entityManager); + jpaQuery.createQuery(values); + } + interface UserRepository extends Repository { Page findByFirstname(String firstname, Pageable pageable); + User findByIdIgnoringCase(Integer id); + User findByIdAllIgnoringCase(Integer id); } }