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