001: package com.opensymphony.webwork.components;
002:
003: import com.opensymphony.webwork.components.Property;
004: import com.opensymphony.webwork.util.WebWorkTypeConverter;
005: import com.opensymphony.xwork.util.OgnlValueStack;
006: import com.opensymphony.xwork.util.XWorkConverter;
007: import junit.framework.TestCase;
008: import ognl.Ognl;
009:
010: import java.io.StringWriter;
011: import java.util.Map;
012:
013: /**
014: *
015: * @author gjoseph
016: * @author $Author: tmjee $ (last edit)
017: * @version $Revision: 1903 $
018: */
019: public class PropertyTest extends TestCase {
020: public void testNormalBehaviour() {
021: final OgnlValueStack stack = new OgnlValueStack();
022: stack.push(new FooBar("foo-value", "bar-value"));
023: final Property property = new Property(stack);
024: property.setDefault("default");
025: property.setValue("foo");
026: assertPropertyOutput("foo-value", property);
027: }
028:
029: public void testDefaultShouldBeOutputIfBeanNotAvailable() {
030: final OgnlValueStack stack = new OgnlValueStack();
031: final Property property = new Property(stack);
032: property.setDefault("default");
033: property.setValue("foo");
034: assertPropertyOutput("default", property);
035: }
036:
037: public void testDefaultShouldBeOutputIfPropertyIsNull() {
038: final OgnlValueStack stack = new OgnlValueStack();
039: stack.push(new FooBar(null, "bar-value"));
040: final Property property = new Property(stack);
041: property.setDefault("default");
042: property.setValue("foo");
043: assertPropertyOutput("default", property);
044: }
045:
046: public void testTopValueShouldReturnTopOfValueStack() {
047: final OgnlValueStack stack = new OgnlValueStack();
048: stack.push(new FooBar("foo-value", "bar-value"));
049: final Property property = new Property(stack);
050: property.setDefault("default");
051: property.setValue("top");
052: assertPropertyOutput("foo-value/bar-value", property);
053: }
054:
055: public void testTypeConverterShouldBeUsed() {
056: final OgnlValueStack stack = new OgnlValueStack();
057: Ognl.setTypeConverter(stack.getContext(),
058: new TestDefaultConverter());
059:
060: stack.push(new FooBar("foo-value", "bar-value"));
061: final Property property = new Property(stack);
062: property.setDefault("default");
063: property.setValue("top");
064: assertPropertyOutput("*foo-value + bar-value*", property);
065: }
066:
067: public void testTypeConverterReturningNullShouldLeadToDisplayOfDefaultValue() {
068: final OgnlValueStack stack = new OgnlValueStack();
069: Ognl.setTypeConverter(stack.getContext(),
070: new TestDefaultConverter());
071:
072: stack.push(new FooBar("foo-value", null));
073: final Property property = new Property(stack);
074: property.setDefault("default");
075: property.setValue("top");
076: assertPropertyOutput("default", property);
077: }
078:
079: private static void assertPropertyOutput(String expectedOutput,
080: Property property) {
081: final StringWriter out = new StringWriter();
082: assertTrue(property.start(out));
083: assertEquals(expectedOutput, out.getBuffer().toString());
084: }
085:
086: private final class FooBar {
087: private String foo;
088: private String bar;
089:
090: public FooBar(String foo, String bar) {
091: this .foo = foo;
092: this .bar = bar;
093: }
094:
095: public String getFoo() {
096: return foo;
097: }
098:
099: public String getBar() {
100: return bar;
101: }
102:
103: public String toString() {
104: return foo + "/" + bar;
105: }
106: }
107:
108: private final class FooBarConverter extends WebWorkTypeConverter {
109: public Object convertFromString(Map context, String[] values,
110: Class toClass) {
111: return null;
112: }
113:
114: public String convertToString(Map context, Object o) {
115: FooBar fooBar = (FooBar) o;
116: if (fooBar.getBar() == null) {
117: return null;
118: } else {
119: return "*" + fooBar.getFoo() + " + " + fooBar.getBar()
120: + "*";
121: }
122: }
123: }
124:
125: /** a simple hack to simply register a custom converter in our test */
126: private final class TestDefaultConverter extends XWorkConverter {
127: protected TestDefaultConverter() {
128: super ();
129: registerConverter(
130: "com.opensymphony.webwork.components.PropertyTest$FooBar",
131: new FooBarConverter());
132: }
133: }
134: }
|