View Javadoc

    1   /*
    2    * $Id: StrutsTypeConverterTest.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.util;
    22
    23  import java.util.Date;
    24  import java.util.HashMap;
    25  import java.util.Map;
    26
    27  import junit.framework.TestCase;
    28
    29
    30  /***
    31   * Test case for Struts Type Converter.
    32   *
    33   */
    34  public class StrutsTypeConverterTest extends TestCase {
    35
    36      /***
    37       * Typically form Struts -> html
    38       *
    39       * @throws Exception
    40       */
    41      public void testConvertToString() throws Exception {
    42          InternalStrutsTypeConverter strutsTypeConverter = new InternalStrutsTypeConverter();
    43          strutsTypeConverter.convertValue(new HashMap(), "", String.class);
    44          assertTrue(strutsTypeConverter.isConvertToString);
    45          assertEquals(strutsTypeConverter.objToBeConverted, "");
    46      }
    47
    48      /***
    49       * Typically form html -> Struts
    50       *
    51       * @throws Exception
    52       */
    53      public void testConvertFromString() throws Exception {
    54          InternalStrutsTypeConverter strutsTypeConverter = new InternalStrutsTypeConverter();
    55          strutsTypeConverter.convertValue(new HashMap(), "12/12/1997", Date.class);
    56          assertTrue(strutsTypeConverter.isConvertFromString);
    57          assertTrue(strutsTypeConverter.objToBeConverted instanceof String[]);
    58          assertEquals(((String[])strutsTypeConverter.objToBeConverted).length, 1);
    59      }
    60
    61      /***
    62       * Typically from html -> Struts (in array due to the nature of html, param
    63       * being able to have many values).
    64       *
    65       * @throws Exception
    66       */
    67      public void testConvertFromStringInArrayForm() throws Exception {
    68          InternalStrutsTypeConverter strutsTypeConverter = new InternalStrutsTypeConverter();
    69          strutsTypeConverter.convertValue(new HashMap(), new String[] { "12/12/1997", "1/1/1977" }, Date.class);
    70          assertTrue(strutsTypeConverter.isConvertFromString);
    71          assertTrue(strutsTypeConverter.objToBeConverted instanceof String[]);
    72          assertEquals(((String[])strutsTypeConverter.objToBeConverted).length, 2);
    73      }
    74
    75
    76      public void testFallbackConversion() throws Exception {
    77          InternalStrutsTypeConverter strutsTypeConverter = new InternalStrutsTypeConverter();
    78          strutsTypeConverter.convertValue(new HashMap(), new Object(), Date.class);
    79          assertTrue(strutsTypeConverter.fallbackConversion);
    80      }
    81
    82      // === internal class for testing
    83      class InternalStrutsTypeConverter extends StrutsTypeConverter {
    84
    85          boolean isConvertFromString = false;
    86          boolean isConvertToString = false;
    87          boolean fallbackConversion = false;
    88
    89          Object objToBeConverted;
    90
    91          public Object convertFromString(Map context, String[] values, Class toClass) {
    92              isConvertFromString = true;
    93              objToBeConverted = values;
    94              return null;
    95          }
    96
    97          public String convertToString(Map context, Object o) {
    98              isConvertToString = true;
    99              objToBeConverted = o;
    100             return null;
    101         }
    102
    103         protected Object performFallbackConversion(Map context, Object o, Class toClass) {
    104             fallbackConversion = true;
    105             return null;
    106         }
    107
    108     }
    109
    110 }