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