Skip to content

Commit

Permalink
WW-5360 Introduces additional countStr & indexStr to allow to ignore …
Browse files Browse the repository at this point in the history
…conversion
  • Loading branch information
lukaszlenart committed Jan 21, 2024
1 parent a763071 commit 455e7e9
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,29 @@
* <li>count: iterations so far, starts on 1. count is always index + 1</li>
* <li>first: true if index == 0</li>
* <li>even: true if (index + 1) % 2 == 0</li>
* <li>last: true if current iteration is the last iteration</li>
* <li>last: true if current iteration is the last iteration</li>
* <li>odd: true if (index + 1) % 2 == 1</li>
* </ul>
* <p>Example</p>
* <pre>
* &lt;s:iterator status="status" value='{0, 1}'&gt;
* Index: &lt;s:property value="%{#status.index}" /&gt; &lt;br /&gt;
* Count: &lt;s:property value="%{#status.count}" /&gt; &lt;br /&gt;
* Index Str: &lt;s:property value="%{#status.indexStr}" /&gt; &lt;br /&gt;
* Count: &lt;s:property value="%{#status.count}" /&gt; &lt;br /&gt;
* Count Str: &lt;s:property value="%{#status.countStr}" /&gt; &lt;br /&gt;
* &lt;/s:iterator&gt;
* </pre>
*
*
* <p>will print</p>
* <pre>
* Index: 0
* Index Str: 0
* Count: 1
* Count Str: 1
* Index: 1
* Index Str: 1
* Count: 2
* Count Str: 2
* </pre>
*/
public class IteratorStatus {
Expand All @@ -56,6 +62,10 @@ public int getCount() {
return state.index + 1;
}

public String getCountStr() {
return String.valueOf(state.index + 1);
}

public boolean isEven() {
return ((state.index + 1) % 2) == 0;
}
Expand All @@ -68,6 +78,10 @@ public int getIndex() {
return state.index;
}

public String getIndexStr() {
return String.valueOf(state.index);
}

public boolean isLast() {
return state.last;
}
Expand Down
6 changes: 6 additions & 0 deletions core/src/test/java/com/opensymphony/xwork2/test/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public class User implements UserMarker {
private String email2;
private String name;

public User() {
}

public User(String name) {
this.name = name;
}

public void setCollection(Collection collection) {
this.collection = collection;
Expand Down
9 changes: 9 additions & 0 deletions core/src/test/java/org/apache/struts2/TestAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class TestAction extends ActionSupport {
private String result;
private User user;
private String[] array;
private Object[] objectArray;
private String[][] list;
private List list2;
private List list3;
Expand Down Expand Up @@ -135,6 +136,14 @@ public void setArray(String[] array) {
this.array = array;
}

public Object[] getObjectArray() {
return objectArray;
}

public void setObjectArray(Object[] arrayObject) {
this.objectArray = arrayObject;
}

public String[][] getList() {
return list;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@
package org.apache.struts2.components;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.test.User;
import com.opensymphony.xwork2.util.ValueStack;
import org.apache.struts2.StrutsInternalTestCase;
import org.apache.struts2.TestAction;

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;

public class IteratorComponentTest extends StrutsInternalTestCase {

public void testIterator() throws Exception {
public void testIterator() {
// given
final ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new FooAction());
Expand Down Expand Up @@ -62,7 +66,50 @@ public void testIterator() throws Exception {
assertEquals("item1 item2 item3 item4 ", out.getBuffer().toString());
}

public void testIteratorWithBegin() throws Exception {
public void testSimpleIterator() {
// given
final ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new FooAction());

StringWriter out = new StringWriter();

IteratorComponent ic = new IteratorComponent(stack);
ic.setBegin("1");
ic.setEnd("8");
ic.setStep("2");
ic.setStatus("status");

Property prop = new Property(stack);
Property status = new Property(stack);
status.setValue("#status.index");

ic.getComponentStack().push(prop);
ic.getComponentStack().push(status);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(status);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(status);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(status);

String body = " ";

// when
assertTrue(ic.start(out));

for (int i = 0; i < 4; i++) {
status.start(out);
status.end(out, body);
prop.start(out);
prop.end(out, body);
ic.end(out, null);
}

// then
assertEquals("0 1 1 3 2 5 3 7 ", out.getBuffer().toString());
}

public void testIteratorWithBegin() {
// given
final ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new FooAction());
Expand Down Expand Up @@ -94,13 +141,13 @@ public void testIteratorWithBegin() throws Exception {
assertEquals("item2 item3 item4 ", out.getBuffer().toString());
}

public void testIteratorWithNulls() throws Exception {
public void testIteratorWithNulls() {
// given
final ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new FooAction() {
private List items = Arrays.asList("1", "2", null, "4");
private final List<String> items = Arrays.asList("1", "2", null, "4");

public List getItems() {
public List<String> getItems() {
return items;
}
});
Expand Down Expand Up @@ -132,15 +179,153 @@ public List getItems() {
assertEquals("1, 2, , 4, ", out.getBuffer().toString());
}

public void testIteratorWithDifferentLocale() {
// given
ActionContext.getContext().withLocale(new Locale("fa_IR"));
final ValueStack stack = ActionContext.getContext().getValueStack();
stack.push(new FooAction());

StringWriter out = new StringWriter();

IteratorComponent ic = new IteratorComponent(stack);
ic.setBegin("1");
ic.setEnd("3");
ic.setStatus("status");

Property prop = new Property(stack);
Property status = new Property(stack);
status.setValue("#status.count");

ic.getComponentStack().push(prop);
ic.getComponentStack().push(status);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(status);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(status);

String body = ",";

// when
assertTrue(ic.start(out));

for (int i = 0; i < 3; i++) {
status.start(out);
status.end(out, body);

prop.start(out);
prop.end(out, body);
ic.end(out, null);
}

// then
assertEquals("1,1,2,2,3,3,", out.getBuffer().toString());
}

public void testListOfBeansIterator() {
// given
final ValueStack stack = ActionContext.getContext().getValueStack();
TestAction action = new TestAction();
action.setList2(new ArrayList<User>() {{
add(new User("Anton"));
add(new User("Tym"));
add(new User("Luk"));
}});
stack.push(action);

StringWriter out = new StringWriter();

IteratorComponent ic = new IteratorComponent(stack);
ic.setValue("list2");
ic.setStatus("status");

Property prop = new Property(stack);
prop.setValue("name");
Property status = new Property(stack);
status.setValue("#status.indexStr");

ic.getComponentStack().push(status);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(status);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(status);
ic.getComponentStack().push(prop);

String body = ",";

// when
assertTrue(ic.start(out));

for (int i = 0; i < 3; i++) {
status.start(out);
status.end(out, body);

prop.start(out);
prop.end(out, body);

ic.end(out, null);
}

// then
assertEquals("0,Anton,1,Tym,2,Luk,", out.getBuffer().toString());
}

public void testArrayOfBeansIterator() {
// given
final ValueStack stack = ActionContext.getContext().getValueStack();
TestAction action = new TestAction();
action.setObjectArray(new ArrayList<User>() {{
add(new User("Anton"));
add(new User("Tym"));
add(new User("Luk"));
}}.toArray());
stack.push(action);

StringWriter out = new StringWriter();

IteratorComponent ic = new IteratorComponent(stack);
ic.setValue("objectArray");
ic.setStatus("status");

Property prop = new Property(stack);
prop.setValue("name");
Property status = new Property(stack);
status.setValue("#status.countStr");

ic.getComponentStack().push(status);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(status);
ic.getComponentStack().push(prop);
ic.getComponentStack().push(status);
ic.getComponentStack().push(prop);

String body = " ";

// when
assertTrue(ic.start(out));

for (int i = 0; i < 3; i++) {
status.start(out);
status.end(out, body);

prop.start(out);
prop.end(out, body);

ic.end(out, null);
}

// then
assertEquals("1 Anton 2 Tym 3 Luk ", out.getBuffer().toString());
}

static class FooAction {

private List items;
private final List<String> items;

public FooAction() {
items = Arrays.asList("item1", "item2", "item3", "item4");
}

public List getItems() {
public List<String> getItems() {
return items;
}
}
Expand Down
Loading

0 comments on commit 455e7e9

Please sign in to comment.