1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }