DATACMNS-615 - PageImpl now rejects a total less than the amount of items given.

PageImpl now makes sure that the total given to the constructor is never less than then number of items given to make sure we do not mask broken count calculation by creating an actually invalid instance.

Related ticket: DATAMONGO-1120.
This commit is contained in:
Oliver Gierke
2014-12-16 12:40:48 +01:00
parent 6f4b26bf56
commit 49a00cb5ec
2 changed files with 13 additions and 0 deletions

View File

@@ -17,6 +17,8 @@ package org.springframework.data.domain;
import java.util.List;
import org.springframework.util.Assert;
/**
* Basic {@code Page} implementation.
*
@@ -39,6 +41,9 @@ public class PageImpl<T> extends Chunk<T> implements Page<T> {
public PageImpl(List<T> content, Pageable pageable, long total) {
super(content, pageable);
Assert.isTrue(total >= content.size(), "Total must not be less than the number of elements given!");
this.total = total;
}

View File

@@ -126,4 +126,12 @@ public class PageImplUnitTests {
assertThat(page.hasNext(), is(false));
assertThat(page.hasPrevious(), is(false));
}
/**
* @see DATACMNS-615
*/
@Test(expected = IllegalArgumentException.class)
public void rejectsTotalLessThanContentLength() {
new PageImpl<String>(Arrays.asList("foo", "bar"), new PageRequest(0, 10), 1);
}
}