View Javadoc

    1   /*
    2    * $Id: PropertyTest.java 471756 2006-11-06 15:01:43Z husted $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.components;
    22
    23  import java.io.StringWriter;
    24  import java.util.Map;
    25
    26  import junit.framework.TestCase;
    27  import ognl.Ognl;
    28
    29  import org.apache.struts2.util.StrutsTypeConverter;
    30
    31  import com.opensymphony.xwork2.util.ValueStack;
    32  import com.opensymphony.xwork2.util.ValueStackFactory;
    33  import com.opensymphony.xwork2.util.XWorkConverter;
    34
    35  /***
    36   *
    37   */
    38  public class PropertyTest extends TestCase {
    39      public void testNormalBehaviour() {
    40          final ValueStack stack = ValueStackFactory.getFactory().createValueStack();
    41          stack.push(new FooBar("foo-value", "bar-value"));
    42          final Property property = new Property(stack);
    43          property.setDefault("default");
    44          property.setValue("foo");
    45          assertPropertyOutput("foo-value", property);
    46      }
    47
    48      public void testDefaultShouldBeOutputIfBeanNotAvailable() {
    49          final ValueStack stack = ValueStackFactory.getFactory().createValueStack();
    50          final Property property = new Property(stack);
    51          property.setDefault("default");
    52          property.setValue("foo");
    53          assertPropertyOutput("default", property);
    54      }
    55
    56      public void testDefaultShouldBeOutputIfPropertyIsNull() {
    57          final ValueStack stack = ValueStackFactory.getFactory().createValueStack();
    58          stack.push(new FooBar(null, "bar-value"));
    59          final Property property = new Property(stack);
    60          property.setDefault("default");
    61          property.setValue("foo");
    62          assertPropertyOutput("default", property);
    63      }
    64
    65      public void testTopValueShouldReturnTopOfValueStack() {
    66          final ValueStack stack = ValueStackFactory.getFactory().createValueStack();
    67          stack.push(new FooBar("foo-value", "bar-value"));
    68          final Property property = new Property(stack);
    69          property.setDefault("default");
    70          property.setValue("top");
    71          assertPropertyOutput("foo-value/bar-value", property);
    72      }
    73
    74      public void testTypeConverterShouldBeUsed() {
    75          final ValueStack stack = ValueStackFactory.getFactory().createValueStack();
    76          Ognl.setTypeConverter(stack.getContext(), new TestDefaultConverter());
    77
    78          stack.push(new FooBar("foo-value", "bar-value"));
    79          final Property property = new Property(stack);
    80          property.setDefault("default");
    81          property.setValue("top");
    82          assertPropertyOutput("*foo-value + bar-value*", property);
    83      }
    84
    85      public void testTypeConverterReturningNullShouldLeadToDisplayOfDefaultValue() {
    86          final ValueStack stack = ValueStackFactory.getFactory().createValueStack();
    87          Ognl.setTypeConverter(stack.getContext(), new TestDefaultConverter());
    88
    89          stack.push(new FooBar("foo-value", null));
    90          final Property property = new Property(stack);
    91          property.setDefault("default");
    92          property.setValue("top");
    93          assertPropertyOutput("default", property);
    94      }
    95
    96      private static void assertPropertyOutput(String expectedOutput, Property property) {
    97          final StringWriter out = new StringWriter();
    98          assertTrue(property.start(out));
    99          assertEquals(expectedOutput, out.getBuffer().toString());
    100     }
    101
    102     private final class FooBar {
    103         private String foo;
    104         private String bar;
    105
    106         public FooBar(String foo, String bar) {
    107             this.foo = foo;
    108             this.bar = bar;
    109         }
    110
    111         public String getFoo() {
    112             return foo;
    113         }
    114
    115         public String getBar() {
    116             return bar;
    117         }
    118
    119         public String toString() {
    120             return foo + "/" + bar;
    121         }
    122     }
    123
    124     private final class FooBarConverter extends StrutsTypeConverter {
    125         public Object convertFromString(Map context, String[] values, Class toClass) {
    126             return null;
    127         }
    128
    129         public String convertToString(Map context, Object o) {
    130             FooBar fooBar = (FooBar) o;
    131             if (fooBar.getBar() == null) {
    132                 return null;
    133             } else {
    134                 return "*" + fooBar.getFoo() + " + " + fooBar.getBar() + "*";
    135             }
    136         }
    137     }
    138
    139     /*** a simple hack to simply register a custom converter in our test */
    140     private final class TestDefaultConverter extends XWorkConverter {
    141         protected TestDefaultConverter() {
    142             super();
    143             registerConverter("org.apache.struts2.components.PropertyTest$FooBar", new FooBarConverter());
    144         }
    145     }
    146 }