Browse Source

DATACMNS-804 - ParametersParameterAccessor now correctly iterates over only the bindable values.

Corrected the way the BindableParametersIterator works by limiting the upper bound of the iteration to the number of actually bindable parameters.
pull/153/head
Oliver Gierke 10 years ago
parent
commit
85e569b4f6
  1. 24
      src/main/java/org/springframework/data/repository/query/ParametersParameterAccessor.java
  2. 20
      src/test/java/org/springframework/data/repository/query/ParametersParameterAccessorUnitTests.java

24
src/main/java/org/springframework/data/repository/query/ParametersParameterAccessor.java

@ -145,7 +145,7 @@ public class ParametersParameterAccessor implements ParameterAccessor { @@ -145,7 +145,7 @@ public class ParametersParameterAccessor implements ParameterAccessor {
* @see org.springframework.data.repository.query.ParameterAccessor#iterator()
*/
public BindableParameterIterator iterator() {
return new BindableParameterIterator();
return new BindableParameterIterator(this);
}
/**
@ -153,17 +153,33 @@ public class ParametersParameterAccessor implements ParameterAccessor { @@ -153,17 +153,33 @@ public class ParametersParameterAccessor implements ParameterAccessor {
*
* @author Oliver Gierke
*/
private class BindableParameterIterator implements Iterator<Object> {
private static class BindableParameterIterator implements Iterator<Object> {
private final int bindableParameterCount;
private final ParameterAccessor accessor;
private int currentIndex = 0;
/**
* Creates a new {@link BindableParameterIterator}.
*
* @param accessor must not be {@literal null}.
*/
public BindableParameterIterator(ParametersParameterAccessor accessor) {
Assert.notNull(accessor, "ParametersParameterAccessor must not be null!");
this.accessor = accessor;
this.bindableParameterCount = accessor.getParameters().getBindableParameters().getNumberOfParameters();
}
/**
* Returns the next bindable parameter.
*
* @return
*/
public Object next() {
return getBindableValue(currentIndex++);
return accessor.getBindableValue(currentIndex++);
}
/*
@ -171,7 +187,7 @@ public class ParametersParameterAccessor implements ParameterAccessor { @@ -171,7 +187,7 @@ public class ParametersParameterAccessor implements ParameterAccessor {
* @see java.util.Iterator#hasNext()
*/
public boolean hasNext() {
return values.size() > currentIndex;
return bindableParameterCount > currentIndex;
}
/*

20
src/test/java/org/springframework/data/repository/query/ParametersParameterAccessorUnitTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2011-2013 the original author or authors.
* Copyright 2011-2016 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.
@ -15,7 +15,7 @@ @@ -15,7 +15,7 @@
*/
package org.springframework.data.repository.query;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
@ -23,6 +23,7 @@ import java.util.Iterator; @@ -23,6 +23,7 @@ import java.util.Iterator;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
/**
@ -65,6 +66,21 @@ public class ParametersParameterAccessorUnitTests { @@ -65,6 +66,21 @@ public class ParametersParameterAccessorUnitTests {
assertThat(accessor.hasBindableNullValue(), is(false));
}
/**
* @see DATACMNS-804
*/
@Test
public void iteratesonlyOverBindableValues() throws Exception {
Method method = Sample.class.getMethod("method", Pageable.class, String.class);
DefaultParameters parameters = new DefaultParameters(method);
ParametersParameterAccessor accessor = new ParametersParameterAccessor(parameters,
new Object[] { new PageRequest(0, 10), "Foo" });
assertThat(accessor, is(iterableWithSize(1)));
}
interface Sample {
void method(String string, int integer);

Loading…
Cancel
Save