How do I create a Pager object?

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/

1 answer

1 accepted

1 vote
Accepted answer

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<Group> getGroups() throws EntityException {

        List<Group> result;
        try {
            result = getGroupsFromHibernate();
        }
        catch (DataAccessException e)  {
            throw new RepositoryException(e);
        }

        if (result == null) {
            return DefaultPager.emptyPager();
        }

        return new DefaultPager<Group>(result);
}

DefaultPager is in the sourcetree too:

public class DefaultPager<T> implements Pager<T> {

    private final List<T> page = new ArrayList<T>();
    private Iterator<T> iter;
    private int index; 

    public static <T> DefaultPager<T> emptyPager() {
        return new DefaultPager<T>(Collections.<T>emptyList());
    } 

    /** @deprecated since 2.0.3. Use the generic-friendly emptyPager() method */
    public DefaultPager(){
        this(Collections.<T>emptyList());
    }

    public DefaultPager(Collection<T> col){
        if (col != null) {
            page.addAll(col);
        }
        iter = page.iterator();
    }

    public boolean isEmpty() {
        if (page == null) return true;
        return page.isEmpty();
    }

    public Iterator<T> iterator() {
        return iter;
    }

    /**
     * @return a single, preloaded page.
     */
    public List<T> getCurrentPage() {
        return new ArrayList<T>(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 < 0)
            throw new PagerException("Cannot skipTo a negative amount [" + index + "]"); 

        int originalIndex = this.index;
        int distance;

        if (index > page.size()) {
            distance = page.size();
            this.index = page.size();
        }
        else {
            distance = index - this.index;
            this.index = index;
        }

        for (int i = originalIndex; i < 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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted Monday in Confluence

Organizing your space just got easier - Page Tree Drag & Drop is here

Hi Community! I’m Elaine, Confluence Product Manager. You may have read my earlier post about page tree in space navigation sidebar. I'm excited to share another improvement that helps you organize ...

120 views 3 4
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you