001: /*
002: * $Id: StrutsTypeConverterTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.util;
022:
023: import java.util.Date;
024: import java.util.HashMap;
025: import java.util.Map;
026:
027: import junit.framework.TestCase;
028:
029: /**
030: * Test case for Struts Type Converter.
031: *
032: */
033: public class StrutsTypeConverterTest extends TestCase {
034:
035: /**
036: * Typically form Struts -> html
037: *
038: * @throws Exception
039: */
040: public void testConvertToString() throws Exception {
041: InternalStrutsTypeConverter strutsTypeConverter = new InternalStrutsTypeConverter();
042: strutsTypeConverter.convertValue(new HashMap(), "",
043: String.class);
044: assertTrue(strutsTypeConverter.isConvertToString);
045: assertEquals(strutsTypeConverter.objToBeConverted, "");
046: }
047:
048: /**
049: * Typically form html -> Struts
050: *
051: * @throws Exception
052: */
053: public void testConvertFromString() throws Exception {
054: InternalStrutsTypeConverter strutsTypeConverter = new InternalStrutsTypeConverter();
055: strutsTypeConverter.convertValue(new HashMap(), "12/12/1997",
056: Date.class);
057: assertTrue(strutsTypeConverter.isConvertFromString);
058: assertTrue(strutsTypeConverter.objToBeConverted instanceof String[]);
059: assertEquals(
060: ((String[]) strutsTypeConverter.objToBeConverted).length,
061: 1);
062: }
063:
064: /**
065: * Typically from html -> Struts (in array due to the nature of html, param
066: * being able to have many values).
067: *
068: * @throws Exception
069: */
070: public void testConvertFromStringInArrayForm() throws Exception {
071: InternalStrutsTypeConverter strutsTypeConverter = new InternalStrutsTypeConverter();
072: strutsTypeConverter.convertValue(new HashMap(), new String[] {
073: "12/12/1997", "1/1/1977" }, Date.class);
074: assertTrue(strutsTypeConverter.isConvertFromString);
075: assertTrue(strutsTypeConverter.objToBeConverted instanceof String[]);
076: assertEquals(
077: ((String[]) strutsTypeConverter.objToBeConverted).length,
078: 2);
079: }
080:
081: public void testFallbackConversion() throws Exception {
082: InternalStrutsTypeConverter strutsTypeConverter = new InternalStrutsTypeConverter();
083: strutsTypeConverter.convertValue(new HashMap(), new Object(),
084: Date.class);
085: assertTrue(strutsTypeConverter.fallbackConversion);
086: }
087:
088: // === internal class for testing
089: class InternalStrutsTypeConverter extends StrutsTypeConverter {
090:
091: boolean isConvertFromString = false;
092: boolean isConvertToString = false;
093: boolean fallbackConversion = false;
094:
095: Object objToBeConverted;
096:
097: public Object convertFromString(Map context, String[] values,
098: Class toClass) {
099: isConvertFromString = true;
100: objToBeConverted = values;
101: return null;
102: }
103:
104: public String convertToString(Map context, Object o) {
105: isConvertToString = true;
106: objToBeConverted = o;
107: return null;
108: }
109:
110: protected Object performFallbackConversion(Map context,
111: Object o, Class toClass) {
112: fallbackConversion = true;
113: return null;
114: }
115:
116: }
117:
118: }
|