DATACMNS-128 - Introduced ParameterAccessor.hasBindableNullValue().

This commit is contained in:
Oliver Gierke
2012-02-02 14:50:01 +01:00
parent 15c3b74c3f
commit b1f2eac2b9
3 changed files with 62 additions and 9 deletions

View File

@@ -52,6 +52,13 @@ public interface ParameterAccessor extends Iterable<Object> {
*/
Object getBindableValue(int index);
/**
* Returns whether one of the bindable parameter values is {@literal null}.
*
* @return
*/
boolean hasBindableNullValue();
/**
* Returns an iterator over all <em>bindable</em> parameters. This means parameters implementing {@link Pageable} or
* {@link Sort} will not be included in this {@link Iterator}.

View File

@@ -48,6 +48,15 @@ public class ParametersParameterAccessor implements ParameterAccessor {
this.values = values.clone();
}
/**
* Returns the {@link Parameters} instance backing the accessor.
*
* @return the parameters will never be {@literal null}.
*/
public Parameters getParameters() {
return parameters;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.ParameterAccessor#getPageable()
@@ -98,6 +107,21 @@ public class ParametersParameterAccessor implements ParameterAccessor {
return values[parameters.getBindableParameter(index).getIndex()];
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.ParameterAccessor#hasBindableNullValue()
*/
public boolean hasBindableNullValue() {
for (Parameter parameter : parameters.getBindableParameters()) {
if (values[parameter.getIndex()] == null) {
return true;
}
}
return false;
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.query.ParameterAccessor#iterator()

View File

@@ -18,24 +18,32 @@ package org.springframework.data.repository.query;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import java.util.Iterator;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.domain.Pageable;
/**
* Unit tests for {@link ParametersParameterAccessor}.
*
*
* @author Oliver Gierke
*/
public class ParametersParameterAccessorUnitTests {
Parameters parameters;
@Before
public void setUp() throws Exception {
parameters = new Parameters(Sample.class.getMethod("method", String.class, int.class));
}
@Test
public void accessorIteratorHasNext() throws SecurityException, NoSuchMethodException {
Parameters parameters = new Parameters(Sample.class.getMethod("method", String.class, int.class));
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, new Object[] { "Foo", 2});
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, new Object[] { "Foo", 2 });
Iterator<Object> iterator = accessor.iterator();
assertThat(iterator.hasNext(), is(true));
assertThat(iterator.next(), is((Object) "Foo"));
@@ -43,10 +51,24 @@ public class ParametersParameterAccessorUnitTests {
assertThat(iterator.next(), is((Object) 2));
assertThat(iterator.hasNext(), is(false));
}
@Test
public void detectsNullValue() throws Exception {
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, new Object[] { null, 5 });
assertThat(accessor.hasBindableNullValue(), is(true));
Method method = Sample.class.getMethod("method", Pageable.class, String.class);
Parameters parameters = new Parameters(method);
accessor = new ParametersParameterAccessor(parameters, new Object[] { null, "Foo" });
assertThat(accessor.hasBindableNullValue(), is(false));
}
interface Sample {
void method(String string, int integer);
void method(Pageable pageable, String string);
}
}