1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.components;
23
24 import java.io.StringWriter;
25 import java.util.Map;
26
27 import junit.framework.TestCase;
28 import ognl.Ognl;
29
30 import org.apache.struts2.StrutsTestCase;
31 import org.apache.struts2.util.StrutsTypeConverter;
32
33 import com.opensymphony.xwork2.ActionContext;
34 import com.opensymphony.xwork2.ObjectFactory;
35 import com.opensymphony.xwork2.conversion.impl.XWorkConverter;
36 import com.opensymphony.xwork2.conversion.impl.XWorkBasicConverter;
37 import com.opensymphony.xwork2.util.ValueStack;
38 import com.opensymphony.xwork2.util.ValueStackFactory;
39
40 /***
41 *
42 */
43 public class PropertyTest extends StrutsTestCase {
44 private XWorkConverter converter;
45
46 public void setUp() throws Exception {
47 super.setUp();
48 converter = container.getInstance(XWorkConverter.class);
49 }
50
51 public void tearDown() throws Exception {
52 super.tearDown();
53 converter = null;
54 }
55 public void testNormalBehaviour() {
56 final ValueStack stack = ActionContext.getContext().getValueStack();
57 stack.push(new FooBar("foo-value", "bar-value"));
58 final Property property = new Property(stack);
59 property.setDefault("default");
60 property.setValue("foo");
61 assertPropertyOutput("foo-value", property);
62 }
63
64 public void testDefaultShouldBeOutputIfBeanNotAvailable() {
65 final ValueStack stack = ActionContext.getContext().getValueStack();
66 final Property property = new Property(stack);
67 property.setDefault("default");
68 property.setValue("foo");
69 assertPropertyOutput("default", property);
70 }
71
72 public void testDefaultShouldBeOutputIfPropertyIsNull() {
73 final ValueStack stack = ActionContext.getContext().getValueStack();
74 stack.push(new FooBar(null, "bar-value"));
75 final Property property = new Property(stack);
76 property.setDefault("default");
77 property.setValue("foo");
78 assertPropertyOutput("default", property);
79 }
80
81 public void testTopValueShouldReturnTopOfValueStack() {
82 final ValueStack stack = ActionContext.getContext().getValueStack();
83 stack.push(new FooBar("foo-value", "bar-value"));
84 final Property property = new Property(stack);
85 property.setDefault("default");
86 property.setValue("top");
87 assertPropertyOutput("foo-value/bar-value", property);
88 }
89
90 public void testTypeConverterShouldBeUsed() {
91 final ValueStack stack = ActionContext.getContext().getValueStack();
92 converter.registerConverter("org.apache.struts2.components.PropertyTest$FooBar", new FooBarConverter());
93 stack.push(new FooBar("foo-value", "bar-value"));
94 final Property property = new Property(stack);
95 property.setDefault("default");
96 property.setValue("top");
97 assertPropertyOutput("*foo-value + bar-value*", property);
98 }
99
100 public void testTypeConverterReturningNullShouldLeadToDisplayOfDefaultValue() {
101 final ValueStack stack = ActionContext.getContext().getValueStack();
102 converter.registerConverter("org.apache.struts2.components.PropertyTest$FooBar", new FooBarConverter());
103 stack.push(new FooBar("foo-value", null));
104 final Property property = new Property(stack);
105 property.setDefault("default");
106 property.setValue("top");
107 assertPropertyOutput("default", property);
108 }
109
110 private static void assertPropertyOutput(String expectedOutput, Property property) {
111 final StringWriter out = new StringWriter();
112 assertTrue(property.start(out));
113 assertEquals(expectedOutput, out.getBuffer().toString());
114 }
115
116 private final class FooBar {
117 private String foo;
118 private String bar;
119
120 public FooBar(String foo, String bar) {
121 this.foo = foo;
122 this.bar = bar;
123 }
124
125 public String getFoo() {
126 return foo;
127 }
128
129 public String getBar() {
130 return bar;
131 }
132
133 public String toString() {
134 return foo + "/" + bar;
135 }
136 }
137
138 private final class FooBarConverter extends StrutsTypeConverter {
139 public Object convertFromString(Map context, String[] values, Class toClass) {
140 return null;
141 }
142
143 public String convertToString(Map context, Object o) {
144 FooBar fooBar = (FooBar) o;
145 if (fooBar.getBar() == null) {
146 return null;
147 } else {
148 return "*" + fooBar.getFoo() + " + " + fooBar.getBar() + "*";
149 }
150 }
151 }
152 }