From b7e37931a793d032bd53a3b57f814851f815d5bb Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Wed, 18 Jul 2012 11:28:52 +0200 Subject: [PATCH] DATAJPA-226 - Working around ambiguities in the JPA spec. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now expect a much broader range of exceptions possibly popping up from EntityManager.createQuery(…). It turns out the spec is not very strict about what must be returned from the call in case the provided query String is invalid. E.g. Hibernate seems to throw an IllegalStateException in case the query seems generally acceptable but has a typo in some keyword. We now catch RuntimeException invoking the call and simply rethrow the original exception if it is an IllegalArgumentException indeed but wrap any other into an IAE. See http://java.net/projects/jpa-spec/lists/jsr338-experts/archive/2012-07/message/17 --- .../jpa/repository/query/SimpleJpaQuery.java | 8 +- .../JpaQueryLookupStrategyUnitTests.java | 81 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java diff --git a/src/main/java/org/springframework/data/jpa/repository/query/SimpleJpaQuery.java b/src/main/java/org/springframework/data/jpa/repository/query/SimpleJpaQuery.java index ef2b7b816..0ba422464 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/SimpleJpaQuery.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/SimpleJpaQuery.java @@ -65,7 +65,13 @@ final class SimpleJpaQuery extends AbstractJpaQuery { // Try to create a Query object already to fail fast if (!method.isNativeQuery()) { - em.createQuery(queryString); + try { + em.createQuery(queryString); + } catch (RuntimeException e) { + // Needed as there's ambiguities in how an invalid query string shall be expressed by the persistence provider + // http://java.net/projects/jpa-spec/lists/jsr338-experts/archive/2012-07/message/17 + throw e instanceof IllegalArgumentException ? e : new IllegalArgumentException(e); + } } } diff --git a/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java new file mode 100644 index 000000000..b7f11a137 --- /dev/null +++ b/src/test/java/org/springframework/data/jpa/repository/query/JpaQueryLookupStrategyUnitTests.java @@ -0,0 +1,81 @@ +/* + * Copyright 2012 the original author or authors. + * + * Licensed 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.springframework.data.jpa.repository.query; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import java.lang.reflect.Method; + +import javax.persistence.EntityManager; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.jpa.domain.sample.User; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.Repository; +import org.springframework.data.repository.core.NamedQueries; +import org.springframework.data.repository.core.RepositoryMetadata; +import org.springframework.data.repository.core.support.DefaultRepositoryMetadata; +import org.springframework.data.repository.query.QueryLookupStrategy; +import org.springframework.data.repository.query.QueryLookupStrategy.Key; + +/** + * Unit tests for {@link JpaQueryLookupStrategy}. + * + * @author Oliver Gierke + */ +@RunWith(MockitoJUnitRunner.class) +public class JpaQueryLookupStrategyUnitTests { + + @Mock + EntityManager em; + @Mock + QueryExtractor extractor; + @Mock + NamedQueries namedQueries; + + /** + * @see DATAJPA-226 + */ + @Test + public void invalidAnnotatedQueryCausesException() throws Exception { + + QueryLookupStrategy strategy = JpaQueryLookupStrategy.create(em, Key.CREATE_IF_NOT_FOUND, extractor); + Method method = UserRepository.class.getMethod("findByFoo", String.class); + RepositoryMetadata metadata = new DefaultRepositoryMetadata(UserRepository.class); + + Throwable reference = new RuntimeException(); + when(em.createQuery(anyString())).thenThrow(reference); + + try { + strategy.resolveQuery(method, metadata, namedQueries); + } catch (Exception e) { + assertThat(e, is(instanceOf(IllegalArgumentException.class))); + assertThat(e.getCause(), is(reference)); + } + } + + interface UserRepository extends Repository { + + @Query("something absurd") + User findByFoo(String foo); + } +}