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 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.v7.tests.data.converter;
  17. import java.math.BigDecimal;
  18. import java.math.BigInteger;
  19. import java.util.Date;
  20. import java.util.Locale;
  21. import org.junit.Assert;
  22. import org.junit.Test;
  23. import com.vaadin.v7.data.util.converter.DefaultConverterFactory;
  24. public class DefaultConverterFactoryTest {
  25. private DefaultConverterFactory factory = new DefaultConverterFactory();
  26. @Test
  27. public void stringToBigDecimal() {
  28. assertConverter("14", new BigDecimal("14"));
  29. }
  30. @Test
  31. public void stringToBigInteger() {
  32. assertConverter("14", new BigInteger("14"));
  33. }
  34. @Test
  35. public void stringToDouble() {
  36. assertConverter("14", new Double("14"));
  37. }
  38. @Test
  39. public void stringToFloat() {
  40. assertConverter("14", new Float("14"));
  41. }
  42. @Test
  43. public void stringToInteger() {
  44. assertConverter("14", new Integer("14"));
  45. }
  46. @Test
  47. public void stringToLong() {
  48. assertConverter("14", new Long("14"));
  49. }
  50. @SuppressWarnings("deprecation")
  51. @Test
  52. public void stringToDate() {
  53. assertConverter("Oct 12, 2014 12:00:00 AM",
  54. new Date(2014 - 1900, 10 - 1, 12));
  55. }
  56. @Test
  57. public void sqlDateToDate() {
  58. long l = 1413071210000L;
  59. assertConverter(new java.sql.Date(l), new java.util.Date(l));
  60. }
  61. @SuppressWarnings("deprecation")
  62. @Test
  63. public void longToDate() {
  64. Date d = new Date(2014 - 1900, 10 - 1, 12);
  65. assertConverter(
  66. 1413061200000L + (d.getTimezoneOffset() + 180) * 60 * 1000L, d);
  67. }
  68. public enum Foo {
  69. BAR, BAZ;
  70. }
  71. @Test
  72. public void stringToEnum() {
  73. assertConverter("Bar", Foo.BAR);
  74. }
  75. @Test
  76. public void stringToShort() {
  77. assertConverter("14", new Short("14"));
  78. }
  79. @Test
  80. public void stringToByte() {
  81. assertConverter("14", new Byte("14"));
  82. }
  83. private <T, U> void assertConverter(T t, U u) {
  84. Class<T> tClass = (Class<T>) t.getClass();
  85. Class<U> uClass = (Class<U>) u.getClass();
  86. U tConvertedToU = factory.createConverter(tClass, uClass)
  87. .convertToModel(t, uClass, Locale.ENGLISH);
  88. Assert.assertEquals("Incorrect type of value converted from "
  89. + tClass.getSimpleName() + " to " + uClass.getSimpleName(),
  90. uClass, tConvertedToU.getClass());
  91. Assert.assertEquals("Incorrect conversion of " + t + " to "
  92. + uClass.getSimpleName(), u, tConvertedToU);
  93. T uConvertedToT = factory.createConverter(uClass, tClass)
  94. .convertToModel(u, tClass, Locale.ENGLISH);
  95. Assert.assertEquals("Incorrect type of value converted from "
  96. + uClass.getSimpleName() + " to " + tClass.getSimpleName(),
  97. tClass, uConvertedToT.getClass());
  98. Assert.assertEquals("Incorrect conversion of " + u + " to "
  99. + tClass.getSimpleName(), t, uConvertedToT);
  100. }
  101. }