WW-3942 solves NPE in iterator tag

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1421210 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Lukasz Lenart
2012-12-13 10:42:09 +00:00
parent 0d93ceedf3
commit b86d3d13fe
2 changed files with 102 additions and 12 deletions
@@ -21,19 +21,18 @@
package org.apache.struts2.components;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import java.util.Arrays;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import org.apache.struts2.util.MakeIterator;
import org.apache.struts2.views.jsp.IteratorStatus;
import com.opensymphony.xwork2.util.ValueStack;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
import org.apache.struts2.util.MakeIterator;
import org.apache.struts2.views.annotations.StrutsTag;
import org.apache.struts2.views.annotations.StrutsTagAttribute;
import org.apache.struts2.views.jsp.IteratorStatus;
import java.io.Writer;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
/**
* <!-- START SNIPPET: javadoc -->
@@ -357,7 +356,7 @@ public class IteratorComponent extends ContextBean {
}
}
class CounterIterator implements Iterator<Object> {
static class CounterIterator implements Iterator<Object> {
private int step;
private int end;
private int currentIndex;
@@ -380,7 +379,7 @@ public class IteratorComponent extends ContextBean {
if (hasNext()) {
int nextIndex = peekNextIndex();
currentIndex += step;
return value != null ? values.get(nextIndex) : nextIndex;
return values != null ? values.get(nextIndex) : nextIndex;
} else {
throw new IndexOutOfBoundsException("Index " + ( currentIndex + step) + " must be less than or equal to " + end);
}
@@ -0,0 +1,91 @@
package org.apache.struts2.components;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.StrutsTestCase;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
public class IteratorComponentTest extends StrutsTestCase {
public void testIterator() throws Exception {
// given
final ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new FooAction());
StringWriter out = new StringWriter();
IteratorComponent ic = new IteratorComponent(stack);
ic.setValue("items");
ic.setVar("val");
Property prop = new Property(stack);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(prop);
String body = " ";
// when
assertTrue(ic.start(out));
for (int i = 0; i < 4; i++) {
prop.start(out);
prop.end(out, body);
ic.end(out, null);
}
// then
assertEquals("item1 item2 item3 item4 ", out.getBuffer().toString());
}
public void testIteratorWithBegin() throws Exception {
// given
final ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new FooAction());
StringWriter out = new StringWriter();
IteratorComponent ic = new IteratorComponent(stack);
ic.setValue("items");
ic.setVar("val");
ic.setBegin("1");
Property prop = new Property(stack);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(prop);
String body = " ";
// when
assertTrue(ic.start(out));
for (int i = 0; i < 3; i++) {
prop.start(out);
prop.end(out, body);
ic.end(out, null);
}
// then
assertEquals("item2 item3 item4 ", out.getBuffer().toString());
}
static class FooAction {
private List items;
public FooAction() {
items = Arrays.asList("item1", "item2", "item3", "item4");
}
public List getItems() {
return items;
}
}
}