001: /*
002: * $Id: PropertyTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components;
022:
023: import java.io.StringWriter;
024: import java.util.Map;
025:
026: import junit.framework.TestCase;
027: import ognl.Ognl;
028:
029: import org.apache.struts2.util.StrutsTypeConverter;
030:
031: import com.opensymphony.xwork2.util.ValueStack;
032: import com.opensymphony.xwork2.util.ValueStackFactory;
033: import com.opensymphony.xwork2.util.XWorkConverter;
034:
035: /**
036: *
037: */
038: public class PropertyTest extends TestCase {
039: public void testNormalBehaviour() {
040: final ValueStack stack = ValueStackFactory.getFactory()
041: .createValueStack();
042: stack.push(new FooBar("foo-value", "bar-value"));
043: final Property property = new Property(stack);
044: property.setDefault("default");
045: property.setValue("foo");
046: assertPropertyOutput("foo-value", property);
047: }
048:
049: public void testDefaultShouldBeOutputIfBeanNotAvailable() {
050: final ValueStack stack = ValueStackFactory.getFactory()
051: .createValueStack();
052: final Property property = new Property(stack);
053: property.setDefault("default");
054: property.setValue("foo");
055: assertPropertyOutput("default", property);
056: }
057:
058: public void testDefaultShouldBeOutputIfPropertyIsNull() {
059: final ValueStack stack = ValueStackFactory.getFactory()
060: .createValueStack();
061: stack.push(new FooBar(null, "bar-value"));
062: final Property property = new Property(stack);
063: property.setDefault("default");
064: property.setValue("foo");
065: assertPropertyOutput("default", property);
066: }
067:
068: public void testTopValueShouldReturnTopOfValueStack() {
069: final ValueStack stack = ValueStackFactory.getFactory()
070: .createValueStack();
071: stack.push(new FooBar("foo-value", "bar-value"));
072: final Property property = new Property(stack);
073: property.setDefault("default");
074: property.setValue("top");
075: assertPropertyOutput("foo-value/bar-value", property);
076: }
077:
078: public void testTypeConverterShouldBeUsed() {
079: final ValueStack stack = ValueStackFactory.getFactory()
080: .createValueStack();
081: Ognl.setTypeConverter(stack.getContext(),
082: new TestDefaultConverter());
083:
084: stack.push(new FooBar("foo-value", "bar-value"));
085: final Property property = new Property(stack);
086: property.setDefault("default");
087: property.setValue("top");
088: assertPropertyOutput("*foo-value + bar-value*", property);
089: }
090:
091: public void testTypeConverterReturningNullShouldLeadToDisplayOfDefaultValue() {
092: final ValueStack stack = ValueStackFactory.getFactory()
093: .createValueStack();
094: Ognl.setTypeConverter(stack.getContext(),
095: new TestDefaultConverter());
096:
097: stack.push(new FooBar("foo-value", null));
098: final Property property = new Property(stack);
099: property.setDefault("default");
100: property.setValue("top");
101: assertPropertyOutput("default", property);
102: }
103:
104: private static void assertPropertyOutput(String expectedOutput,
105: Property property) {
106: final StringWriter out = new StringWriter();
107: assertTrue(property.start(out));
108: assertEquals(expectedOutput, out.getBuffer().toString());
109: }
110:
111: private final class FooBar {
112: private String foo;
113: private String bar;
114:
115: public FooBar(String foo, String bar) {
116: this .foo = foo;
117: this .bar = bar;
118: }
119:
120: public String getFoo() {
121: return foo;
122: }
123:
124: public String getBar() {
125: return bar;
126: }
127:
128: public String toString() {
129: return foo + "/" + bar;
130: }
131: }
132:
133: private final class FooBarConverter extends StrutsTypeConverter {
134: public Object convertFromString(Map context, String[] values,
135: Class toClass) {
136: return null;
137: }
138:
139: public String convertToString(Map context, Object o) {
140: FooBar fooBar = (FooBar) o;
141: if (fooBar.getBar() == null) {
142: return null;
143: } else {
144: return "*" + fooBar.getFoo() + " + " + fooBar.getBar()
145: + "*";
146: }
147: }
148: }
149:
150: /** a simple hack to simply register a custom converter in our test */
151: private final class TestDefaultConverter extends XWorkConverter {
152: protected TestDefaultConverter() {
153: super ();
154: registerConverter(
155: "org.apache.struts2.components.PropertyTest$FooBar",
156: new FooBarConverter());
157: }
158: }
159: }
|