How do I create a Pager object?
Asked by Chloe Sowers:

I'm trying to write a unit test using Mockito and Junit and creating mock objects, and the mock GroupManager has to return a list of users. It returns a Pager object, but Pager is an interface! I even tried to look at GroupManager souce code, but that is also an interface! How do I create a Pager object to return?

The JavaDocs doesn't even have the Pager class!

<http://docs.atlassian.com/atlassian-confluence/3.4.8/>


Answers:
author: Thomas Kalsberger, MSc
body: hi!

you found the groupmanager interface. why didn't you continue to look for an implementation class? you can find them close to the interface definitions.

in the impl.\* subpackage you will find e.g.the HibernateGroupManager which implements the GroupManager interface.

getGroups() is implemented this way:

```
public Pager&lt;Group&gt; getGroups() throws EntityException {        List&lt;Group&gt; result;        try {            result = getGroupsFromHibernate();        }        catch (DataAccessException e)  {            throw new RepositoryException(e);        }        if (result == null) {            return DefaultPager.emptyPager();        }        return new DefaultPager&lt;Group&gt;(result);}
```

DefaultPager is in the sourcetree too:

```
public class DefaultPager&lt;T&gt; implements Pager&lt;T&gt; {    private final List&lt;T&gt; page = new ArrayList&lt;T&gt;();    private Iterator&lt;T&gt; iter;    private int index;     public static &lt;T&gt; DefaultPager&lt;T&gt; emptyPager() {        return new DefaultPager&lt;T&gt;(Collections.&lt;T&gt;emptyList());    }     /** @deprecated since 2.0.3. Use the generic-friendly emptyPager() method */    public DefaultPager(){        this(Collections.&lt;T&gt;emptyList());    }    public DefaultPager(Collection&lt;T&gt; col){        if (col != null) {            page.addAll(col);        }        iter = page.iterator();    }    public boolean isEmpty() {        if (page == null) return true;        return page.isEmpty();    }    public Iterator&lt;T&gt; iterator() {        return iter;    }    /**     * @return a single, preloaded page.     */    public List&lt;T&gt; getCurrentPage() {        return new ArrayList&lt;T&gt;(page);    }    public void nextPage() {        //do nothing, all contents are already within the currentPage    }    public boolean onLastPage() {        return true;    }    public void skipTo(int index) throws PagerException  {        if (index &lt; 0)            throw new PagerException("Cannot skipTo a negative amount [" + index + "]");         int originalIndex = this.index;        int distance;        if (index &gt; page.size()) {            distance = page.size();            this.index = page.size();        }        else {            distance = index - this.index;            this.index = index;        }        for (int i = originalIndex; i &lt; distance; i++)          iter.next();    }    /**     * @return the current index position of the pager     */    public int getIndex()  {        return index;    }    /**     *  This pager always has all its items in a single page     * @return zero, because this pager has all its items in a single page     */    public int getIndexOfFirstItemInCurrentPage()  {        return 0;    }    public void remove()  {        throw new UnsupportedOperationException("This iterator does not support removal");    }    public boolean hasNext()   {        return iter.hasNext();    }    public T next()    {        T o = iter.next();        index++;        return o;    }}
```

this should help (-:  
cheers,  
thomas


Tags: junit