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
Answer accepted

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
Published Mar 12, 2019 in Confluence

Confluence Admin Certification now $150 for Community Members

More and more people are building their careers with Atlassian, and we want you to be at the front of this wave! Important Dates Start the Certification Prep Course by 2 April 2019 Take your e...

1,507 views 4 13
Read article

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