001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.commons.betwixt.strategy;
019:
020: import junit.framework.Test;
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.apache.commons.beanutils.ConvertUtils;
025: import org.apache.commons.beanutils.Converter;
026: import org.apache.commons.betwixt.expression.Context;
027:
028: /**
029: * Test harness for ObjectStringConverter implementations
030: *
031: * @author <a href="mailto:rdonkin at apache.org">Robert Burrell Donkin</a>
032: * @version $Id: TestObjectStringConverters.java 438373 2006-08-30 05:17:21Z bayard $
033: */
034: public class TestObjectStringConverters extends TestCase {
035:
036: protected Context dummyContext = new Context();
037:
038: public static Test suite() {
039: return new TestSuite(TestObjectStringConverters.class);
040: }
041:
042: public TestObjectStringConverters(String testName) {
043: super (testName);
044: }
045:
046: public void testBaseConverter() {
047: Object test = new Object() {
048: public String toString() {
049: return "funciporcini";
050: }
051: };
052:
053: ObjectStringConverter converter = new ObjectStringConverter();
054: String stringFromObject = converter.objectToString(null,
055: Object.class, "raspberry", dummyContext);
056: assertEquals("Null should return empty string", "",
057: stringFromObject);
058: stringFromObject = converter.objectToString(test, Object.class,
059: "raspberry", dummyContext);
060: assertEquals("Object should return toString", "funciporcini",
061: stringFromObject);
062:
063: Object objectFromString = converter
064: .stringToObject("Mungo Jerry", Object.class,
065: "strawberry", dummyContext);
066: assertEquals("String should return itself", "Mungo Jerry",
067: objectFromString);
068: }
069:
070: public void testConvertUtilsConverter() throws Exception {
071: ObjectStringConverter converter = new ConvertUtilsObjectStringConverter();
072: commonTestForConvertUtilsConverters(converter);
073: }
074:
075: private void commonTestForConvertUtilsConverters(
076: ObjectStringConverter objectStringConverter) {
077: Converter converter = new Converter() {
078: public Object convert(Class type, Object value) {
079: if (type == SecurityManager.class) {
080: return "Life, The Universe And Everything";
081: }
082: return "The answer is " + value.toString();
083: }
084: };
085:
086: Long test = new Long(42);
087:
088: ConvertUtils.register(converter, Object.class);
089: ConvertUtils.register(converter, String.class);
090: ConvertUtils.register(converter, SecurityManager.class);
091:
092: String stringFromObject = objectStringConverter.objectToString(
093: null, Object.class, "gooseberry", dummyContext);
094: assertEquals("Null should return empty string", "",
095: stringFromObject);
096: stringFromObject = objectStringConverter.objectToString(test,
097: Object.class, "logonberry", dummyContext);
098: assertEquals("Normal object conversion (1)",
099: "The answer is 42", stringFromObject);
100:
101: Object objectFromString = objectStringConverter.stringToObject(
102: "Forty Two", Object.class, "damsen", dummyContext);
103: assertEquals("Normal object conversion (2)",
104: "The answer is Forty Two", objectFromString);
105: objectFromString = objectStringConverter.stringToObject(
106: "Trillian", SecurityManager.class, "cranberry",
107: dummyContext);
108: assertEquals("Special object conversion",
109: "Life, The Universe And Everything", objectFromString);
110:
111: ConvertUtils.deregister();
112: }
113:
114: public void testDefaultOSConverter() {
115: ObjectStringConverter converter = new DefaultObjectStringConverter();
116: commonTestForConvertUtilsConverters(converter);
117: }
118:
119: public void testDefaultOSConverterDates() {
120:
121: Converter converter = new Converter() {
122: public Object convert(Class type, Object value) {
123: return "Arthur Dent";
124: }
125: };
126:
127: ConvertUtils.register(converter, java.sql.Date.class);
128:
129: converter = new Converter() {
130: public Object convert(Class type, Object value) {
131: return "Ford Prefect";
132: }
133: };
134:
135: ConvertUtils.register(converter, String.class);
136:
137: converter = new Converter() {
138: public Object convert(Class type, Object value) {
139: return "Marvin";
140: }
141: };
142:
143: ConvertUtils.register(converter, java.util.Date.class);
144:
145: java.util.Date utilNow = new java.util.Date();
146: String nowAsString = utilNow.toString();
147: java.sql.Date sqlNow = new java.sql.Date(System
148: .currentTimeMillis());
149: ObjectStringConverter objectStringConverter = new DefaultObjectStringConverter();
150:
151: String stringFromObject = objectStringConverter.objectToString(
152: utilNow, java.util.Date.class, "blackcurrent",
153: dummyContext);
154: assertEquals(
155: "String output same as java.util.Date.toString() (1)",
156: utilNow.toString(), stringFromObject);
157:
158: stringFromObject = objectStringConverter.objectToString(sqlNow,
159: java.util.Date.class, "redcurrent", dummyContext);
160: assertEquals(
161: "String output same as java.util.Date.toString() (2)",
162: utilNow.toString(), stringFromObject);
163:
164: stringFromObject = objectStringConverter.objectToString(
165: utilNow, java.sql.Date.class, "whitecurrent",
166: dummyContext);
167: assertEquals("Should use converter (2)", "Ford Prefect",
168: stringFromObject);
169:
170: Object objectFromString = objectStringConverter.stringToObject(
171: nowAsString, java.sql.Date.class, "blackberry",
172: dummyContext);
173: assertEquals("Should use converter (3)", "Ford Prefect",
174: stringFromObject);
175: objectFromString = objectStringConverter.stringToObject(
176: nowAsString, java.util.Date.class, "tayberry",
177: dummyContext);
178: assertTrue("Date should be returned",
179: objectFromString instanceof java.util.Date);
180: assertEquals("Date returned should be the same", nowAsString,
181: objectFromString.toString());
182:
183: ConvertUtils.deregister();
184: }
185: }
|