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