001: /*
002: * Copyright 2005-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
005: * in compliance with the License. You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software distributed under the License
010: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
011: * or implied. See the License for the specific language governing permissions and limitations under
012: * the License.
013: */
014:
015: package org.strecks.converter;
016:
017: import java.net.URL;
018: import java.sql.Date;
019:
020: import org.strecks.exceptions.ConversionException;
021: import org.testng.annotations.BeforeMethod;
022: import org.testng.annotations.Test;
023:
024: /**
025: * @author Phil Zoio
026: */
027: public class TestBeanUtilsConverter {
028:
029: private StandardBeanUtilsConverter converter;
030:
031: @BeforeMethod
032: public void setUp() throws Exception {
033: converter = new StandardBeanUtilsConverter();
034: }
035:
036: @Test
037: public void testConvertInteger() throws Exception {
038:
039: converter.setTargetClass(Integer.class);
040:
041: Object object = converter.toTargetType("1");
042: assert new Integer(1).equals(object);
043:
044: assert converter.toSourceType(object).equals("1");
045:
046: // now check null case
047: assert null == converter.toTargetType(null);
048: assert null == converter.toSourceType(null);
049:
050: }
051:
052: @Test
053: public void testCharacter() throws Exception {
054: converter.setTargetClass(Character.class);
055: Object toTargetType = converter.toTargetType("duff");
056: assert new Character('d').equals(toTargetType);
057: }
058:
059: @Test(expectedExceptions=ConversionException.class)
060: public void testDuffInteger1() throws Exception {
061: converter.setTargetClass(Integer.class);
062: // notice the behaviour with blanks
063: converter.toTargetType("");
064: }
065:
066: @Test(expectedExceptions=ConversionException.class)
067: public void testDuffInteger2() throws Exception {
068: converter.setTargetClass(Integer.class);
069: converter.toTargetType("duff");
070: }
071:
072: @Test(expectedExceptions=ConversionException.class)
073: public void testDuffByte() throws Exception {
074: converter.setTargetClass(Byte.class);
075: converter.toTargetType("duff");
076: }
077:
078: @Test(expectedExceptions=ConversionException.class)
079: public void testDuffLong() throws Exception {
080: converter.setTargetClass(Long.class);
081: converter.toTargetType("duff");
082: }
083:
084: @Test(expectedExceptions=ConversionException.class)
085: public void testDuffFloat() throws Exception {
086: converter.setTargetClass(Float.class);
087: converter.toTargetType("duff");
088: }
089:
090: @Test(expectedExceptions=ConversionException.class)
091: public void testDuffDouble() throws Exception {
092: converter.setTargetClass(Double.class);
093: converter.toTargetType("duff");
094: }
095:
096: @Test(expectedExceptions=ConversionException.class)
097: public void testDuffDate() throws Exception {
098: converter.setTargetClass(Date.class);
099: converter.toTargetType("duff");
100: }
101:
102: @Test(expectedExceptions=ConversionException.class)
103: public void testDuffURL() throws Exception {
104: converter.setTargetClass(URL.class);
105: converter.toTargetType("duff");
106: }
107:
108: }
|