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: package org.apache.commons.beanutils.converters;
018:
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021:
022: import org.apache.commons.beanutils.ConversionException;
023: import org.apache.commons.beanutils.Converter;
024:
025: /**
026: * Test Case for the ClassConverter class.
027: *
028: * @version $Revision: 539508 $ $Date: 2007-05-18 16:57:59 +0100 (Fri, 18 May 2007) $
029: */
030: public class ClassConverterTestCase extends TestCase {
031:
032: /**
033: * Construct a new Class Converter test case.
034: * @param name Test Name
035: */
036: public ClassConverterTestCase(String name) {
037: super (name);
038: }
039:
040: // ------------------------------------------------------------------------
041:
042: /**
043: * Create Test Suite
044: * @return test suite
045: */
046: public static TestSuite suite() {
047: return new TestSuite(ClassConverterTestCase.class);
048: }
049:
050: /** Set Up */
051: public void setUp() throws Exception {
052: }
053:
054: /** Tear Down */
055: public void tearDown() throws Exception {
056: }
057:
058: // ------------------------------------------------------------------------
059:
060: /**
061: * Test Conversion to String
062: */
063: public void testConvertToString() {
064: Converter converter = new ClassConverter();
065:
066: assertEquals("Class Test", "java.lang.Integer", converter
067: .convert(String.class, Integer.class));
068: assertEquals("Value Test", "foo", converter.convert(
069: String.class, "foo"));
070: assertEquals("Value Test", "bar", converter.convert(
071: String.class, new StringBuffer("bar")));
072: assertEquals("Null Test", null, converter.convert(String.class,
073: null));
074: }
075:
076: /**
077: * Test Conversion to Class
078: */
079: public void testConvertToClass() {
080: Converter converter = new ClassConverter();
081:
082: assertEquals("Class Test", Integer.class, converter.convert(
083: Class.class, Integer.class));
084: assertEquals("String Test", Integer.class, converter.convert(
085: Class.class, "java.lang.Integer"));
086: assertEquals("StringBuffer Test", Integer.class, converter
087: .convert(Class.class, new StringBuffer(
088: "java.lang.Integer")));
089:
090: // Invalid Test
091: try {
092: converter.convert(Class.class, new Integer(6));
093: fail("Expected invalid value to fail");
094: } catch (ConversionException e) {
095: // expected result
096: }
097:
098: // Test Null
099: try {
100: converter.convert(Class.class, null);
101: fail("Expected null value to fail");
102: } catch (ConversionException e) {
103: // expected result
104: }
105: }
106:
107: /**
108: * Test Invalid Conversion with default
109: */
110: public void testConvertToClassDefault() {
111:
112: Converter converter = new ClassConverter(Object.class);
113:
114: assertEquals("Invalid Test", Object.class, converter.convert(
115: Class.class, new Integer(6)));
116: assertEquals("Null Test", Object.class, converter.convert(
117: Class.class, null));
118: }
119:
120: /**
121: * Test Invalid Conversion with default "null"
122: */
123: public void testConvertToClassDefaultNull() {
124:
125: Converter converter = new ClassConverter(null);
126:
127: assertEquals("Invalid Test", null, converter.convert(
128: Class.class, new Integer(6)));
129: assertEquals("Null Test", null, converter.convert(Class.class,
130: null));
131: }
132:
133: /**
134: * Test Array Conversion
135: */
136: public void testArray() {
137: Converter converter = new ClassConverter();
138:
139: // Test Array Class to String
140: assertEquals("Array to String", "[Ljava.lang.Boolean;",
141: converter.convert(String.class, Boolean[].class));
142:
143: // *** N.B. for some reason the following works on m1, but not m2
144: // Test String to Array Class
145: // assertEquals("String to Array", Boolean[].class, converter.convert(Class.class, "[Ljava.lang.Boolean;"));
146: }
147:
148: /**
149: * Test Invalid
150: */
151: public void testInvalid() {
152: Converter converter = new ClassConverter();
153:
154: // Test invalid class name
155: try {
156: converter.convert(Class.class, "foo.bar");
157: fail("Invalid class name, expected ConversionException");
158: } catch (ConversionException e) {
159: // expected result
160: }
161: }
162:
163: }
|