mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-03-11 06:40:18 +01:00
Implemented unit tests for Manager and added param checks
This commit is contained in:
@@ -41,6 +41,8 @@ import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
/**
|
||||
* Base interface for all manager classes.
|
||||
*
|
||||
@@ -139,6 +141,9 @@ public interface Manager<T extends ModelObject, E extends Exception>
|
||||
* empty page result is returned.
|
||||
*/
|
||||
default PageResult<T> getPage(Comparator<T> comparator, int pageNumber, int pageSize) {
|
||||
checkArgument(pageSize > 0, "pageSize must be at least 1");
|
||||
checkArgument(pageNumber >= 0, "pageNumber must be non-negative");
|
||||
|
||||
Collection<T> entities = getAll(comparator, pageNumber * pageSize, pageSize + 1);
|
||||
boolean hasMore = entities.size() > pageSize;
|
||||
return new PageResult<>(Util.createSubCollection(entities, 0, pageSize), hasMore);
|
||||
|
||||
112
scm-core/src/test/java/sonia/scm/ManagerTest.java
Normal file
112
scm-core/src/test/java/sonia/scm/ManagerTest.java
Normal file
@@ -0,0 +1,112 @@
|
||||
package sonia.scm;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ManagerTest {
|
||||
|
||||
private Manager manager = new Manager() {
|
||||
@Override
|
||||
public void refresh(ModelObject object) throws IOException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public ModelObject get(String id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection getAll() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection getAll(Comparator comparator) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection getAll(int start, int limit) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection getAll(Comparator comparator, int start, int limit) {
|
||||
if (start == 0 && (limit == 3) || (limit == 5)) {
|
||||
return Arrays.asList(1, 2, 3);
|
||||
} else if (start == 0 && limit == 6) {
|
||||
return Collections.emptyList();
|
||||
} else {
|
||||
return Arrays.asList(3);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void create(TypedObject object) throws Exception, IOException {}
|
||||
|
||||
@Override
|
||||
public void delete(TypedObject object) throws Exception, IOException {}
|
||||
|
||||
@Override
|
||||
public void modify(TypedObject object) throws Exception, IOException {}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {}
|
||||
|
||||
@Override
|
||||
public void init(SCMContextProvider context) {}
|
||||
|
||||
@Override
|
||||
public Long getLastModified() { return null; }
|
||||
};
|
||||
|
||||
@Mock
|
||||
private Comparator comparator;
|
||||
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void validatesPageNumber() {
|
||||
manager.getPage(comparator, -1, 5);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void validatesPageSize() {
|
||||
manager.getPage(comparator, 2, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getsNoPage() {
|
||||
PageResult singlePage = manager.getPage(comparator, 0, 5);
|
||||
assertFalse(singlePage.hasMore());
|
||||
assertEquals(0, singlePage.getEntities().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getsSinglePage() {
|
||||
|
||||
PageResult singlePage = manager.getPage(comparator, 0, 4);
|
||||
assertFalse(singlePage.hasMore());
|
||||
assertEquals(3, singlePage.getEntities().size() );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getsTwoPages() {
|
||||
|
||||
PageResult page1 = manager.getPage(comparator, 0, 2);
|
||||
assertTrue(page1.hasMore());
|
||||
assertEquals(2, page1.getEntities().size());
|
||||
|
||||
PageResult page2 = manager.getPage(comparator, 1, 2);
|
||||
assertFalse(page2.hasMore());
|
||||
assertEquals(1, page2.getEntities().size());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user