FileDocCategorySizeDatePackage
SortIteratorTagTest.javaAPI DocExample6974Mon Jul 23 13:26:16 BST 2007org.apache.struts2.views.jsp

SortIteratorTagTest

public class SortIteratorTagTest extends AbstractTagTest
Test case to test SortIteratorTag.

Fields Summary
Constructors Summary
Methods Summary
public com.opensymphony.xwork2.ActiongetAction()

        return new ActionSupport() {
            public Comparator getComparator() {
                return new Comparator() {
                    public int compare(Object o1, Object o2) {
                        Integer i1 = (Integer) o1;
                        Integer i2 = (Integer) o2;

                        return (i1.intValue() - i2.intValue());
                    }
                };
            }

            public List getSource() {
                List l = new ArrayList();
                l.add(new Integer(3));
                l.add(new Integer(1));
                l.add(new Integer(2));
                l.add(new Integer(5));
                l.add(new Integer(4));
                return l;
            }

            public Object getBadComparator() {
                return new Object();
            }

            public Object getBadSource() {
                return new Object();
            }
        };
    
public voidtestSortWithIdIteratorAvailableInPageContext()

        SortIteratorTag tag = new SortIteratorTag();

        tag.setId("myId");
        tag.setComparator("comparator");
        tag.setSource("source");

        tag.setPageContext(pageContext);
        tag.doStartTag();

        {
            Iterator sortedIterator = (Iterator) pageContext.getAttribute("myId");

            assertNotNull(sortedIterator);
            // 1
            assertTrue(sortedIterator.hasNext());
            assertEquals(sortedIterator.next(), new Integer(1));
            // 2
            assertTrue(sortedIterator.hasNext());
            assertEquals(sortedIterator.next(), new Integer(2));
            // 3
            assertTrue(sortedIterator.hasNext());
            assertEquals(sortedIterator.next(), new Integer(3));
            // 4
            assertTrue(sortedIterator.hasNext());
            assertEquals(sortedIterator.next(), new Integer(4));
            // 5
            assertTrue(sortedIterator.hasNext());
            assertEquals(sortedIterator.next(), new Integer(5));

            assertFalse(sortedIterator.hasNext());
        }

        tag.doEndTag();
    
public voidtestSortWithIdIteratorAvailableInStackTop()


        SortIteratorTag tag = new SortIteratorTag();

        tag.setId("myId");
        tag.setComparator("comparator");
        tag.setSource("source");

        tag.setPageContext(pageContext);
        tag.doStartTag();

        {
            Iterator sortedIterator = (Iterator) stack.findValue("top");

            assertNotNull(sortedIterator);
            // 1
            assertTrue(sortedIterator.hasNext());
            assertEquals(sortedIterator.next(), new Integer(1));
            // 2
            assertTrue(sortedIterator.hasNext());
            assertEquals(sortedIterator.next(), new Integer(2));
            // 3
            assertTrue(sortedIterator.hasNext());
            assertEquals(sortedIterator.next(), new Integer(3));
            // 4
            assertTrue(sortedIterator.hasNext());
            assertEquals(sortedIterator.next(), new Integer(4));
            // 5
            assertTrue(sortedIterator.hasNext());
            assertEquals(sortedIterator.next(), new Integer(5));

            assertFalse(sortedIterator.hasNext());
        }

        tag.doEndTag();
    
public voidtestSortWithIllegalComparator()

        SortIteratorTag tag = new SortIteratorTag();

        tag.setComparator("badComparator");
        tag.setSource("source");

        try {
            tag.setPageContext(pageContext);
            tag.doStartTag();
            tag.doEndTag();
            fail("JspException expected");
        }
        catch (JspException e) {
            // good
            assertTrue(true);
        }

    
public voidtestSortWithIllegalSource()

        SortIteratorTag tag = new SortIteratorTag();

        tag.setComparator("comparator");
        tag.setSource("badSource");

        try {
            tag.setPageContext(pageContext);
            tag.doStartTag();
            tag.doEndTag();
            fail("JspException expected");
        }
        catch (JspException e) {
            // ok
            assertTrue(true);
        }
    
public voidtestSortWithoutId()

        SortIteratorTag tag = new SortIteratorTag();

        tag.setComparator("comparator");
        tag.setSource("source");

        tag.setPageContext(pageContext);
        tag.doStartTag();

        // if not an Iterator, just let the ClassCastException be thrown as error instead of failure
        Iterator sortedIterator = (Iterator) stack.findValue("top");

        assertNotNull(sortedIterator);
        // 1
        assertTrue(sortedIterator.hasNext());
        assertEquals(sortedIterator.next(), new Integer(1));
        // 2
        assertTrue(sortedIterator.hasNext());
        assertEquals(sortedIterator.next(), new Integer(2));
        // 3.
        assertTrue(sortedIterator.hasNext());
        assertEquals(sortedIterator.next(), new Integer(3));
        // 4.
        assertTrue(sortedIterator.hasNext());
        assertEquals(sortedIterator.next(), new Integer(4));
        // 5
        assertTrue(sortedIterator.hasNext());
        assertEquals(sortedIterator.next(), new Integer(5));

        assertFalse(sortedIterator.hasNext());
        tag.doEndTag();