View Javadoc

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