⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dataconversiontest.java

📁 一个javabean的转换与copy非常的好用希望大家好好研究一下
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2005-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sf.dozer.util.mapping;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import junit.framework.Assert;
import net.sf.dozer.util.mapping.converters.ConversionException;
import net.sf.dozer.util.mapping.util.DateFormatContainer;
import net.sf.dozer.util.mapping.util.MappingUtils;
import net.sf.dozer.util.mapping.converters.PrimitiveOrWrapperConverter;

/**
 * @author tierney.matt
 */
public class DataConversionTest extends DozerTestBase {

  private MappingUtils mappingUtils = new MappingUtils();
  private PrimitiveOrWrapperConverter converter = new PrimitiveOrWrapperConverter(); 
  
  public void testConvertInteger() throws Exception {
    Object[] input = { String.valueOf(Integer.MIN_VALUE), "-17", "-1", "0", "1", "17",
        String.valueOf(Integer.MAX_VALUE), new Byte((byte) 7), new Short((short) 8), new Integer(9), new Long(10),
        new Float(11.1), new Double(12.2) };
    Integer[] expected = { new Integer(Integer.MIN_VALUE), new Integer(-17), new Integer(-1), new Integer(0),
        new Integer(1), new Integer(17), new Integer(Integer.MAX_VALUE), new Integer(7), new Integer(8),
        new Integer(9), new Integer(10), new Integer(11), new Integer(12) };

    for (int i = 0; i < expected.length; i++) {
      assertEquals(input[i].getClass().getName() + " to Integer", expected[i], converter.convert(
          input[i], Integer.class, null));
      assertEquals(input[i].getClass().getName() + " to int", expected[i], converter.convert(
          input[i], Integer.TYPE, null));
    }
  }

  public void testConvertIntegerWithFailure() throws Exception {
    Object input = "three";
    try {
      converter.convert(input, Integer.class, null);
      fail("Should have thrown conversion exception");
    } catch (ConversionException e) {
      assertTrue(true);
    }
  }

  public void testConvertDouble() throws Exception {
    Object[] input = { String.valueOf(Double.MIN_VALUE), "-17.2", "-1.1", "0.0", "1.1", "17.2",
        String.valueOf(Double.MAX_VALUE), new Byte((byte) 7), new Short((short) 8), new Integer(9), new Long(10),
        new Float(11.1), new Double(12.2) };
    Double[] expected = { new Double(Double.MIN_VALUE), new Double(-17.2), new Double(-1.1), new Double(0.0),
        new Double(1.1), new Double(17.2), new Double(Double.MAX_VALUE), new Double(7), new Double(8), new Double(9),
        new Double(10), new Double(11.1), new Double(12.2) };

    for (int i = 0; i < expected.length; i++) {
      assertEquals(input[i].getClass().getName() + " to Double", expected[i].doubleValue(), ((Double) (converter
          .convert(input[i], Double.class, null))).doubleValue(), 0.00001D);
      assertEquals(input[i].getClass().getName() + " to double", expected[i].doubleValue(), ((Double) (converter
          .convert(input[i], Double.TYPE, null))).doubleValue(), 0.00001D);
    }
  }

  public void testConvertFloat() throws Exception {
    Object[] input = { String.valueOf(Float.MIN_VALUE), "-17.2", "-1.1", "0.0", "1.1", "17.2",
        String.valueOf(Float.MAX_VALUE), new Byte((byte) 7), new Short((short) 8), new Integer(9), new Long(10),
        new Float(11.1), new Double(12.2) };
    Float[] expected = { new Float(Float.MIN_VALUE), new Float(-17.2), new Float(-1.1), new Float(0.0), new Float(1.1),
        new Float(17.2), new Float(Float.MAX_VALUE), new Float(7), new Float(8), new Float(9), new Float(10),
        new Float(11.1), new Float(12.2) };

    for (int i = 0; i < expected.length; i++) {
      assertEquals(input[i].getClass().getName() + " to Float", expected[i].floatValue(), ((Float) (converter
          .convert(input[i], Float.class, null))).floatValue(), 0.00001);
      assertEquals(input[i].getClass().getName() + " to float", expected[i].floatValue(), ((Float) (converter
          .convert(input[i], Float.TYPE, null))).floatValue(), 0.00001);
    }
  }

  public void testConvertLong() throws Exception {
    Object[] input = { String.valueOf(Long.MIN_VALUE), "-17", "-1", "0", "1", "17", String.valueOf(Long.MAX_VALUE),
        new Byte((byte) 7), new Short((short) 8), new Integer(9), new Long(10), new Float(11.1), new Double(12.2) };
    Long[] expected = { new Long(Long.MIN_VALUE), new Long(-17), new Long(-1), new Long(0), new Long(1), new Long(17),
        new Long(Long.MAX_VALUE), new Long(7), new Long(8), new Long(9), new Long(10), new Long(11), new Long(12) };

    for (int i = 0; i < expected.length; i++) {
      assertEquals(input[i].getClass().getName() + " to Long", expected[i], converter.convert(
          input[i], Long.class, null));
      assertEquals(input[i].getClass().getName() + " to long", expected[i], converter.convert(
          input[i], Long.TYPE, null));
    }
  }

  public void testConvertBigDecimal() throws Exception {
    Object[] input = { String.valueOf(Integer.MIN_VALUE), "-17", "-1", "0", "1", "17",
        String.valueOf(Integer.MAX_VALUE), new Byte((byte) 7), new Short((short) 8), new Integer(9), new Long(10), };
    BigDecimal[] expected = { new BigDecimal(Integer.MIN_VALUE), new BigDecimal(-17), new BigDecimal(-1),
        new BigDecimal(0), new BigDecimal(1), new BigDecimal(17), new BigDecimal(Integer.MAX_VALUE), new BigDecimal(7),
        new BigDecimal(8), new BigDecimal(9), new BigDecimal(10) };

    for (int i = 0; i < expected.length; i++) {
      assertEquals(input[i].getClass().getName() + " to BigDecimal", expected[i], converter
          .convert(input[i], BigDecimal.class, null));
    }
  }

  public void testConvertBigInteger() throws Exception {
    Object[] input = { String.valueOf(Integer.MIN_VALUE), "-17", "-1", "0", "1", "17",
        String.valueOf(Integer.MAX_VALUE), new Byte((byte) 7), new Short((short) 8), new Integer(9), new Long(10), };
    BigInteger[] expected = { new BigInteger(String.valueOf(Integer.MIN_VALUE)), new BigInteger("-17"), new BigInteger("-1"),
        new BigInteger("0"), new BigInteger("1"), new BigInteger("17"), new BigInteger(String.valueOf(Integer.MAX_VALUE)), new BigInteger("7"),
        new BigInteger("8"), new BigInteger("9"), new BigInteger("10")};

    for (int i = 0; i < expected.length; i++) {
      assertEquals(input[i].getClass().getName() + " to BigDecimal", expected[i], converter
          .convert(input[i], BigInteger.class, null));
    }
  }

  public void testConvertShort() throws Exception {
    Object[] input = { String.valueOf(Short.MIN_VALUE), "-17", "-1", "0", "1", "17", String.valueOf(Short.MAX_VALUE),
        new Byte((byte) 7), new Short((short) 8), new Integer(9), new Long(10), new Float(11.1), new Double(12.2) };
    Short[] expected = { new Short(Short.MIN_VALUE), new Short((short) -17), new Short((short) -1),
        new Short((short) 0), new Short((short) 1), new Short((short) 17), new Short(Short.MAX_VALUE),
        new Short((short) 7), new Short((short) 8), new Short((short) 9), new Short((short) 10), new Short((short) 11),
        new Short((short) 12) };

    for (int i = 0; i < expected.length; i++) {
      assertEquals(input[i].getClass().getName() + " to Short", expected[i], converter.convert(
          input[i], Short.class, null));
      assertEquals(input[i].getClass().getName() + " to short", expected[i], converter.convert(
          input[i], Short.TYPE, null));
    }
  }

  public void testConvertDate() throws Exception {
    long time = Calendar.getInstance().getTimeInMillis();

    java.util.Date date = new java.util.Date(time);

    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(time);

    GregorianCalendar gregCal = new GregorianCalendar();
    gregCal.setTimeInMillis(time);

    DateFormat[] dateFormats = new DateFormat[] {DateFormat.getDateInstance(DateFormat.FULL),
        DateFormat.getDateInstance(DateFormat.LONG), DateFormat.getDateInstance(DateFormat.MEDIUM),
        new SimpleDateFormat("MM/dd/yyyy"), new SimpleDateFormat("MM/dd/yyyy HH:mm:ss:SS")};

    //java.util.Date
    Object[] input = { new java.sql.Time(time), new java.sql.Timestamp(time), new java.sql.Date(time), cal, gregCal,
        String.valueOf(time) };
    Object expected = new java.util.Date(time);
    Object result = null;

    for (int i = 0; i < input.length; i++) {
      DateFormatContainer dfc = new DateFormatContainer(null, null);
      dfc.setDateFormat(null);
      result = converter.convert(input[i], java.util.Date.class, dfc);
      assertTrue("result should be instance of java.util.Date", result instanceof java.util.Date);
      assertEquals(input[i].getClass().getName() + " to java.util.Date", expected, result);
    }

    for (int i = 0; i < dateFormats.length; i++) {
      String dateStr = dateFormats[i].format(date);
      DateFormatContainer dfc = new DateFormatContainer(null, null);
      dfc.setDateFormat(dateFormats[i]);
      result = converter.convert(dateStr, java.util.Date.class, dfc);
      assertEquals("String to java.util.Date for input: " + dateStr,dateFormats[i].parse(dateStr), result);
      assertEquals("String to java.util.Date for input: " + dateStr, dateStr,dateFormats[i].format(result));
    }

    //java.sql.Date
    input = new Object[] { new java.util.Date(time), new java.sql.Time(time), new java.sql.Timestamp(time), cal,
        gregCal, String.valueOf(time) };
    expected = new java.sql.Date(time);

    for (int i = 0; i < input.length; i++) {
      DateFormatContainer dfc = new DateFormatContainer(null, null);
      dfc.setDateFormat(null);
      result = converter.convert(input[i], java.sql.Date.class, dfc);
      assertTrue("result should be instance of java.sql.Date", result instanceof java.sql.Date);
      assertEquals(input[i].getClass().getName() + " to java.sql.Date", expected, result);
    }

    for (int i = 0; i < dateFormats.length; i++) {
      String dateStr = dateFormats[i].format(date);
      DateFormatContainer dfc = new DateFormatContainer(null, null);
      dfc.setDateFormat(dateFormats[i]);
      result = converter.convert(dateStr, java.sql.Date.class, dfc);
      assertEquals("String to java.sql.Date for input: " + dateStr, dateFormats[i].parse(dateStr), result);
      assertEquals("String to java.sql.Date for input: " + dateStr, dateStr,dateFormats[i].format(result));
    }

    //java.sql.Time
    input = new Object[] { new java.util.Date(time), new java.sql.Date(time), new java.sql.Timestamp(time), cal,
        gregCal, String.valueOf(time) };
    expected = new java.sql.Time(time);

    for (int i = 0; i < input.length; i++) {
      DateFormatContainer dfc = new DateFormatContainer(null, null);
      dfc.setDateFormat(null);
      result = converter.convert(input[i], java.sql.Time.class, dfc);
      assertTrue("result should be instance of java.sql.Time", result instanceof java.sql.Time);
      assertEquals(input[i].getClass().getName() + " to java.sql.Time", expected, result);
    }

    for (int i = 0; i < dateFormats.length; i++) {
      String dateStr = dateFormats[i].format(date);
      DateFormatContainer dfc = new DateFormatContainer(null, null);
      dfc.setDateFormat(dateFormats[i]);
      result = converter.convert(dateStr, java.sql.Time.class, dfc);
      assertEquals("String to java.sql.Time for input: " + dateStr, dateFormats[i].parse(dateStr), result);
      assertEquals("String to java.sql.Time for input: " + dateStr, dateStr,dateFormats[i].format(result));
    }

    //java.sql.Timestamp
    input = new Object[] { new java.util.Date(time), new java.sql.Date(time), new java.sql.Time(time), cal, gregCal,
        String.valueOf(time) };

    for (int i = 0; i < input.length; i++) {
      DateFormatContainer dfc = new DateFormatContainer(null, null);
      dfc.setDateFormat(null);
      result = converter.convert(input[i], java.sql.Timestamp.class, dfc);
      assertTrue("result should be instance of java.sql.Timestamp", result instanceof java.sql.Timestamp);
      assertEquals(input[i].getClass().getName() + " to java.sql.Timestamp", time, ((java.sql.Timestamp) result)
          .getTime());
    }

    for (int i = 0; i < dateFormats.length; i++) {
      String dateStr = dateFormats[i].format(date);
      DateFormatContainer dfc = new DateFormatContainer(null, null);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -