Skip to content

Commit 49a00cb

Browse files
committed
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.
1 parent 6f4b26b commit 49a00cb

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/main/java/org/springframework/data/domain/PageImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
import java.util.List;
1919

20+
import org.springframework.util.Assert;
21+
2022
/**
2123
* Basic {@code Page} implementation.
2224
*
@@ -39,6 +41,9 @@ public class PageImpl<T> extends Chunk<T> implements Page<T> {
3941
public PageImpl(List<T> content, Pageable pageable, long total) {
4042

4143
super(content, pageable);
44+
45+
Assert.isTrue(total >= content.size(), "Total must not be less than the number of elements given!");
46+
4247
this.total = total;
4348
}
4449

src/test/java/org/springframework/data/domain/PageImplUnitTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,12 @@ public void returnsCorrectTotalPages() {
126126
assertThat(page.hasNext(), is(false));
127127
assertThat(page.hasPrevious(), is(false));
128128
}
129+
130+
/**
131+
* @see DATACMNS-615
132+
*/
133+
@Test(expected = IllegalArgumentException.class)
134+
public void rejectsTotalLessThanContentLength() {
135+
new PageImpl<String>(Arrays.asList("foo", "bar"), new PageRequest(0, 10), 1);
136+
}
129137
}

0 commit comments

Comments
 (0)