diff --git a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvoker.java b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvoker.java index b6b1a2d01..8c8f27377 100644 --- a/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvoker.java +++ b/spring-data-rest-core/src/main/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvoker.java @@ -274,6 +274,8 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { */ @SuppressWarnings("unchecked") private T invoke(Method method, Object... arguments) { + + ReflectionUtils.makeAccessible(method); return (T) ReflectionUtils.invokeMethod(method, repository, arguments); } diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/Author.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/Author.java new file mode 100644 index 000000000..510dfe571 --- /dev/null +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/Author.java @@ -0,0 +1,31 @@ +/* + * Copyright 2014 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.rest.core.domain.jpa; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +/** + * @author Oliver Gierke + */ +@Entity +public class Author { + + @Id @GeneratedValue Long id; + String firstname, lastname; + +} diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/AuthorRepository.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/AuthorRepository.java new file mode 100644 index 000000000..ac7e8fc2b --- /dev/null +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/domain/jpa/AuthorRepository.java @@ -0,0 +1,31 @@ +/* + * Copyright 2014 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.rest.core.domain.jpa; + +import java.util.List; + +import org.springframework.data.repository.Repository; +import org.springframework.data.repository.query.Param; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + +/** + * @author Oliver Gierke + */ +@RepositoryRestResource +interface AuthorRepository extends Repository { + + List findByFirstnameContaining(@Param("firstname") String firstname); +} diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvokerIntegrationTests.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvokerIntegrationTests.java index a49a43803..01fc959d3 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvokerIntegrationTests.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/invoke/ReflectionRepositoryInvokerIntegrationTests.java @@ -19,9 +19,11 @@ import static org.hamcrest.CoreMatchers.*; import static org.junit.Assert.*; import java.lang.reflect.Method; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.Map; import org.junit.Before; import org.junit.Test; @@ -33,6 +35,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.repository.core.RepositoryInformation; import org.springframework.data.repository.support.Repositories; import org.springframework.data.rest.core.AbstractIntegrationTests; +import org.springframework.data.rest.core.domain.jpa.Author; import org.springframework.data.rest.core.domain.jpa.Order; import org.springframework.data.rest.core.domain.jpa.OrderRepository; import org.springframework.data.rest.core.domain.jpa.Person; @@ -119,4 +122,21 @@ public class ReflectionRepositoryInvokerIntegrationTests extends AbstractIntegra Page page = (Page) result; assertThat(page.getNumberOfElements(), is(1)); } + + /** + * @see DATAREST-325 + */ + @Test + public void invokesMethodOnPackageProtectedRepository() throws Exception { + + Object authorRepository = repositories.getRepositoryFor(Author.class); + RepositoryInformation repositoryInformation = repositories.getRepositoryInformationFor(Author.class); + Method method = repositoryInformation.getRepositoryInterface().getMethod("findByFirstnameContaining", String.class); + + Map parameters = Collections.singletonMap("firstname", new String[] { "Oliver" }); + + ReflectionRepositoryInvoker invoker = new ReflectionRepositoryInvoker(authorRepository, information, + conversionService); + invoker.invokeQueryMethod(method, parameters, null, null); + } } diff --git a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/ResourceMappingsIntegrationTests.java b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/ResourceMappingsIntegrationTests.java index f668646a5..6cecb7e12 100644 --- a/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/ResourceMappingsIntegrationTests.java +++ b/spring-data-rest-core/src/test/java/org/springframework/data/rest/core/mapping/ResourceMappingsIntegrationTests.java @@ -32,6 +32,7 @@ import org.springframework.data.mapping.PersistentProperty; import org.springframework.data.repository.support.Repositories; import org.springframework.data.rest.core.Path; import org.springframework.data.rest.core.config.RepositoryRestConfiguration; +import org.springframework.data.rest.core.domain.jpa.Author; import org.springframework.data.rest.core.domain.jpa.CreditCard; import org.springframework.data.rest.core.domain.jpa.JpaRepositoryConfig; import org.springframework.data.rest.core.domain.jpa.Person; @@ -63,7 +64,7 @@ public class ResourceMappingsIntegrationTests { @Test public void detectsAllMappings() { - assertThat(mappings, is(Matchers. iterableWithSize(6))); + assertThat(mappings, is(Matchers. iterableWithSize(8))); } @Test @@ -139,4 +140,25 @@ public class ResourceMappingsIntegrationTests { assertThat(methodNames, hasSize(3)); assertThat(methodNames, hasItems("findByFirstName", "findByCreatedGreaterThan", "findByCreatedUsingISO8601Date")); } + + /** + * @see DATAREST-325 + */ + @Test + public void exposesMethodResourceMappingInPackageProtectedButExportedRepo() { + + ResourceMetadata metadata = mappings.getMappingFor(Author.class); + assertThat(metadata.isExported(), is(true)); + + SearchResourceMappings searchMappings = metadata.getSearchResourceMappings(); + + assertThat(searchMappings.isExported(), is(true)); + assertThat(searchMappings.getMappedMethod("findByFirstnameContaining"), is(notNullValue())); + + for (MethodResourceMapping methodMapping : searchMappings) { + + System.out.println(methodMapping.getMethod().getName()); + assertThat(methodMapping.isExported(), is(true)); + } + } }