/*
* Copyright 2011 Objectos, Fábrica de Software LTDA.
*
* 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.
*/
package br.com.objectos.way.relational;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import org.testng.annotations.Test;
import br.com.objectos.way.relational.Page;
import br.com.objectos.way.relational.SimplePage;
/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
@Test
public class SimplePageTest {
public void pageShouldStartAtZero() {
Page page = new SimplePage();
assertThat(page.getFirstIndex(), equalTo(0));
assertThat(page.getNumber(), equalTo(0));
}
public void pageShouldAllowDefinitionOfLength() {
int length = 20;
Page page = SimplePage.build().withLengthOf(length).get();
assertThat(page.getFirstIndex(), equalTo(0));
assertThat(page.getNumber(), equalTo(0));
assertThat(page.getLength(), equalTo(20));
}
public void nextShouldIncrementPage() {
int length = 5;
Page page = SimplePage.build().withLengthOf(length).get();
assertThat(page.getFirstIndex(), equalTo(0));
assertThat(page.getNumber(), equalTo(0));
page.next();
assertThat(page.getFirstIndex(), equalTo(1 * length));
assertThat(page.getNumber(), equalTo(1));
}
public void previousShouldDecrementIndex() {
int length = 5;
Page page = SimplePage.build().withLengthOf(length).get();
assertThat(page.getFirstIndex(), equalTo(0));
page.next(); // 1
page.next(); // 2
page.next(); // 3
page.previous(); // 2
assertThat(page.getFirstIndex(), equalTo(2 * length));
assertThat(page.getNumber(), equalTo(2));
}
public void weShouldNotHaveNegativeIndexes() {
int length = 5;
Page page = SimplePage.build().withLengthOf(length).get();
assertThat(page.getFirstIndex(), equalTo(0));
page.next(); // 5
page.next(); // 10
page.previous(); // 5
page.previous(); // 0
page.previous(); // 0 (not -5)
assertThat(page.getFirstIndex(), equalTo(0));
assertThat(page.getNumber(), equalTo(0));
}
public void itShouldBePossibleToStartFromIndexOtherThanZero() {
int startIndex = 2;
int length = 5;
Page page = SimplePage.build().startAt(startIndex).withLengthOf(length).get();
assertThat(page.getFirstIndex(), equalTo(2));
assertThat(page.getNumber(), equalTo(0));
}
public void withOffsetPreviousPageShouldContinueToWorkAsExpected() {
int startIndex = 11;
int length = 5;
Page page = SimplePage.build().startAt(startIndex).withLengthOf(length).get();
assertThat(page.getNumber(), equalTo(2));
page.previous();
assertThat(page.getFirstIndex(), equalTo(6));
assertThat(page.getNumber(), equalTo(1));
page.previous();
assertThat(page.getFirstIndex(), equalTo(1));
assertThat(page.getNumber(), equalTo(0));
page.previous();
assertThat(page.getFirstIndex(), equalTo(0));
assertThat(page.getNumber(), equalTo(0));
}
}