DATACMNS-350 - Improved extensibility of Parameters.

Parameters is now a class explicitly designed for extension using generics. We provide a new DefaultParameters which is essentially a drop in replacement for former Parameters as it exposes individual Parameter instances.

Modules that previously extended Parameters will now have to implement the abstract factory methods exposed to create concrete - and potentially customized - Parameter instances.
This commit is contained in:
Oliver Gierke
2013-07-30 15:02:51 +02:00
parent cc576a7398
commit b8e457db9b
8 changed files with 148 additions and 91 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2011 the original author or authors.
* Copyright 2011-2013 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.
@@ -32,11 +32,11 @@ import org.springframework.data.domain.Pageable;
*/
public class ParametersParameterAccessorUnitTests {
Parameters parameters;
Parameters<?, ?> parameters;
@Before
public void setUp() throws Exception {
parameters = new Parameters(Sample.class.getMethod("method", String.class, int.class));
parameters = new DefaultParameters(Sample.class.getMethod("method", String.class, int.class));
}
@Test
@@ -59,7 +59,7 @@ public class ParametersParameterAccessorUnitTests {
assertThat(accessor.hasBindableNullValue(), is(true));
Method method = Sample.class.getMethod("method", Pageable.class, String.class);
Parameters parameters = new Parameters(method);
DefaultParameters parameters = new DefaultParameters(method);
accessor = new ParametersParameterAccessor(parameters, new Object[] { null, "Foo" });
assertThat(accessor.hasBindableNullValue(), is(false));

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2010 the original author or authors.
* Copyright 2008-2013 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
@@ -46,28 +46,28 @@ public class ParametersUnitTests {
Method validWithPageable = SampleDao.class.getMethod("validWithPageable", String.class, Pageable.class);
Method validWithSort = SampleDao.class.getMethod("validWithSort", String.class, Sort.class);
new Parameters(valid);
new Parameters(validWithPageable);
new Parameters(validWithSort);
new DefaultParameters(valid);
new DefaultParameters(validWithPageable);
new DefaultParameters(validWithSort);
}
@Test(expected = IllegalArgumentException.class)
public void rejectsInvalidMethodWithParamMissing() throws Exception {
Method method = SampleDao.class.getMethod("invalidParamMissing", String.class, String.class);
new Parameters(method);
new DefaultParameters(method);
}
@Test(expected = IllegalArgumentException.class)
public void rejectsNullMethod() throws Exception {
new Parameters(null);
new DefaultParameters(null);
}
@Test
public void detectsNamedParameterCorrectly() throws Exception {
Parameters parameters = getParametersFor("validWithSort", String.class, Sort.class);
Parameters<?, ?> parameters = getParametersFor("validWithSort", String.class, Sort.class);
Parameter parameter = parameters.getParameter(0);
@@ -85,12 +85,12 @@ public class ParametersUnitTests {
Method method = SampleDao.class.getMethod("validWithSortFirst", Sort.class, String.class);
Parameters parameters = new Parameters(method);
Parameters<?, ?> parameters = new DefaultParameters(method);
assertThat(parameters.getBindableParameter(0).getIndex(), is(1));
method = SampleDao.class.getMethod("validWithSortInBetween", String.class, Sort.class, String.class);
parameters = new Parameters(method);
parameters = new DefaultParameters(method);
assertThat(parameters.getBindableParameter(0).getIndex(), is(0));
assertThat(parameters.getBindableParameter(1).getIndex(), is(2));
@@ -99,28 +99,28 @@ public class ParametersUnitTests {
@Test
public void detectsEmptyParameterListCorrectly() throws Exception {
Parameters parameters = getParametersFor("emptyParameters");
Parameters<?, ?> parameters = getParametersFor("emptyParameters");
assertThat(parameters.hasParameterAt(0), is(false));
}
@Test
public void detectsPageableParameter() throws Exception {
Parameters parameters = getParametersFor("validWithPageable", String.class, Pageable.class);
Parameters<?, ?> parameters = getParametersFor("validWithPageable", String.class, Pageable.class);
assertThat(parameters.getPageableIndex(), is(1));
}
@Test
public void detectsSortParameter() throws Exception {
Parameters parameters = getParametersFor("validWithSort", String.class, Sort.class);
Parameters<?, ?> parameters = getParametersFor("validWithSort", String.class, Sort.class);
assertThat(parameters.getSortIndex(), is(1));
}
private Parameters getParametersFor(String methodName, Class<?>... parameterTypes) throws SecurityException,
private Parameters<?, ?> getParametersFor(String methodName, Class<?>... parameterTypes) throws SecurityException,
NoSuchMethodException {
Method method = SampleDao.class.getMethod(methodName, parameterTypes);
return new Parameters(method);
return new DefaultParameters(method);
}
static class User {

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2008-2010 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
*
* Copyright 2008-2013 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.
* 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.repository.query;
@@ -31,14 +31,14 @@ import org.springframework.data.domain.Sort;
*/
public class SimpleParameterAccessorUnitTests {
Parameters parameters, sortParameters, pageableParameters;
Parameters<?, ?> parameters, sortParameters, pageableParameters;
@Before
public void setUp() throws SecurityException, NoSuchMethodException {
parameters = new Parameters(Sample.class.getMethod("sample", String.class));
sortParameters = new Parameters(Sample.class.getMethod("sample1", String.class, Sort.class));
pageableParameters = new Parameters(Sample.class.getMethod("sample2", String.class, Pageable.class));
parameters = new DefaultParameters(Sample.class.getMethod("sample", String.class));
sortParameters = new DefaultParameters(Sample.class.getMethod("sample1", String.class, Sort.class));
pageableParameters = new DefaultParameters(Sample.class.getMethod("sample2", String.class, Pageable.class));
}
@Test