View Javadoc

1   /*
2    * $Id: StrutsTypeConverterTest.java 418521 2006-07-01 23:36:50Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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  	// === internal class for testing 
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 }