You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultConverterFactoryTest.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.vaadin.v7.tests.data.converter;
  2. import static org.junit.Assert.assertEquals;
  3. import java.math.BigDecimal;
  4. import java.math.BigInteger;
  5. import java.util.Date;
  6. import java.util.Locale;
  7. import org.junit.Test;
  8. import com.vaadin.v7.data.util.converter.DefaultConverterFactory;
  9. public class DefaultConverterFactoryTest {
  10. private DefaultConverterFactory factory = new DefaultConverterFactory();
  11. @Test
  12. public void stringToBigDecimal() {
  13. assertConverter("14", new BigDecimal("14"));
  14. }
  15. @Test
  16. public void stringToBigInteger() {
  17. assertConverter("14", new BigInteger("14"));
  18. }
  19. @Test
  20. public void stringToDouble() {
  21. assertConverter("14", new Double("14"));
  22. }
  23. @Test
  24. public void stringToFloat() {
  25. assertConverter("14", new Float("14"));
  26. }
  27. @Test
  28. public void stringToInteger() {
  29. assertConverter("14", new Integer("14"));
  30. }
  31. @Test
  32. public void stringToLong() {
  33. assertConverter("14", new Long("14"));
  34. }
  35. @SuppressWarnings("deprecation")
  36. @Test
  37. public void stringToDate() {
  38. assertConverter("Oct 12, 2014 12:00:00 AM",
  39. new Date(2014 - 1900, 10 - 1, 12));
  40. }
  41. @Test
  42. public void sqlDateToDate() {
  43. long l = 1413071210000L;
  44. assertConverter(new java.sql.Date(l), new java.util.Date(l));
  45. }
  46. @SuppressWarnings("deprecation")
  47. @Test
  48. public void longToDate() {
  49. Date d = new Date(2014 - 1900, 10 - 1, 12);
  50. assertConverter(
  51. 1413061200000L + (d.getTimezoneOffset() + 180) * 60 * 1000L, d);
  52. }
  53. public enum Foo {
  54. BAR, BAZ;
  55. }
  56. @Test
  57. public void stringToEnum() {
  58. assertConverter("Bar", Foo.BAR);
  59. }
  60. @Test
  61. public void stringToShort() {
  62. assertConverter("14", new Short("14"));
  63. }
  64. @Test
  65. public void stringToByte() {
  66. assertConverter("14", new Byte("14"));
  67. }
  68. private <T, U> void assertConverter(T t, U u) {
  69. Class<T> tClass = (Class<T>) t.getClass();
  70. Class<U> uClass = (Class<U>) u.getClass();
  71. U tConvertedToU = factory.createConverter(tClass, uClass)
  72. .convertToModel(t, uClass, Locale.ENGLISH);
  73. assertEquals("Incorrect type of value converted from "
  74. + tClass.getSimpleName() + " to " + uClass.getSimpleName(),
  75. uClass, tConvertedToU.getClass());
  76. assertEquals("Incorrect conversion of " + t + " to "
  77. + uClass.getSimpleName(), u, tConvertedToU);
  78. T uConvertedToT = factory.createConverter(uClass, tClass)
  79. .convertToModel(u, tClass, Locale.ENGLISH);
  80. assertEquals("Incorrect type of value converted from "
  81. + uClass.getSimpleName() + " to " + tClass.getSimpleName(),
  82. tClass, uConvertedToT.getClass());
  83. assertEquals("Incorrect conversion of " + u + " to "
  84. + tClass.getSimpleName(), t, uConvertedToT);
  85. }
  86. }