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.

StringToCollectionConverterTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.util.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.Collection;
  20. import java.util.EnumSet;
  21. import java.util.HashSet;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Locale;
  25. import java.util.Set;
  26. import java.util.Vector;
  27. import org.junit.Assert;
  28. import org.junit.Test;
  29. import com.vaadin.v7.data.util.converter.StringToCollectionConverter;
  30. import com.vaadin.v7.data.util.converter.StringToCollectionConverter.CollectionFactory;
  31. import com.vaadin.v7.data.util.converter.StringToEnumConverter;
  32. import com.vaadin.v7.data.util.converter.StringToIntegerConverter;
  33. /**
  34. * Tests for {@link StringToCollectionConverter}.
  35. *
  36. * @author Vaadin Ltd
  37. */
  38. public class StringToCollectionConverterTest {
  39. @Test
  40. public void convertToModel_defaultCtor() {
  41. StringToCollectionConverter converter = new StringToCollectionConverter();
  42. Collection<?> model = converter.convertToModel("a, b, c", List.class,
  43. null);
  44. Assert.assertTrue("Unexpected model class", model instanceof ArrayList);
  45. Iterator<?> iterator = model.iterator();
  46. Assert.assertEquals("Incorrect fist token", "a", iterator.next());
  47. Assert.assertEquals("Incorrect second token", "b", iterator.next());
  48. Assert.assertEquals("Incorrect third token", "c", iterator.next());
  49. }
  50. @Test
  51. public void convertToModel_customDelimiter() {
  52. StringToCollectionConverter converter = new StringToCollectionConverter(
  53. "x");
  54. Collection<?> model = converter.convertToModel("axbxc", List.class,
  55. null);
  56. Assert.assertTrue("Unexpected model class", model instanceof ArrayList);
  57. Iterator<?> iterator = model.iterator();
  58. Assert.assertEquals("Incorrect fist token", "a", iterator.next());
  59. Assert.assertEquals("Incorrect second token", "b", iterator.next());
  60. Assert.assertEquals("Incorrect third token", "c", iterator.next());
  61. }
  62. @Test
  63. public void convertToModel_customConverter() {
  64. StringToCollectionConverter converter = new StringToCollectionConverter(
  65. ",", new StringToIntegerConverter(), Integer.class);
  66. Collection<?> model = converter.convertToModel("6,2,5", List.class,
  67. null);
  68. Assert.assertTrue("Unexpected model class", model instanceof ArrayList);
  69. Iterator<?> iterator = model.iterator();
  70. Assert.assertEquals("Incorrect fist token", 6, iterator.next());
  71. Assert.assertEquals("Incorrect second token", 2, iterator.next());
  72. Assert.assertEquals("Incorrect third token", 5, iterator.next());
  73. }
  74. @Test
  75. public void convertToModel_setAsCollection() {
  76. StringToCollectionConverter converter = new StringToCollectionConverter(
  77. " ", new StringToEnumConverter(), TestEnum.class);
  78. Collection<?> model = converter.convertToModel("Z X Y", Set.class,
  79. null);
  80. Assert.assertTrue("Unexpected model class", model instanceof HashSet);
  81. EnumSet<TestEnum> set = EnumSet.allOf(TestEnum.class);
  82. set.removeAll(model);
  83. Assert.assertTrue("Some values are not in resutling collection",
  84. set.isEmpty());
  85. }
  86. @Test
  87. public void convertToModel_customFactory() {
  88. CollectionFactory factory = new CollectionFactory() {
  89. @Override
  90. public Collection<?> createCollection(
  91. Class<? extends Collection> type) {
  92. return new Vector();
  93. }
  94. };
  95. StringToCollectionConverter converter = new StringToCollectionConverter(
  96. ", ", null, String.class, factory);
  97. Collection<?> model = converter.convertToModel("a, b, c",
  98. Collection.class, null);
  99. Assert.assertTrue("Unexpected model class", model instanceof Vector);
  100. Iterator<?> iterator = model.iterator();
  101. Assert.assertEquals("Incorrect fist token", "a", iterator.next());
  102. Assert.assertEquals("Incorrect second token", "b", iterator.next());
  103. Assert.assertEquals("Incorrect third token", "c", iterator.next());
  104. }
  105. @Test
  106. public void convertToPresentation_default() {
  107. StringToCollectionConverter converter = new StringToCollectionConverter();
  108. String presentation = converter.convertToPresentation(
  109. Arrays.asList("a", "b", "c"), String.class, null);
  110. Assert.assertEquals("a, b, c", presentation);
  111. }
  112. @Test
  113. public void convertToPresentation_customDelimiter() {
  114. StringToCollectionConverter converter = new StringToCollectionConverter(
  115. "x");
  116. String presentation = converter.convertToPresentation(
  117. Arrays.asList("a", "b", "c"), String.class, null);
  118. Assert.assertEquals("axbxc", presentation);
  119. }
  120. @Test
  121. public void convertToPresentation_customConverter() {
  122. StringToCollectionConverter converter = new StringToCollectionConverter(
  123. ",", new StringToEnumConverter(), TestEnum.class);
  124. String presentation = converter.convertToPresentation(
  125. Arrays.asList(TestEnum.Z, TestEnum.Y), String.class, null);
  126. Assert.assertEquals("Z,Y", presentation);
  127. }
  128. @Test
  129. public void convertToModel_singleItem() {
  130. StringToCollectionConverter converter = new StringToCollectionConverter();
  131. Collection<?> model = converter.convertToModel("a", List.class, null);
  132. Iterator<?> iterator = model.iterator();
  133. Assert.assertEquals("Incorrect fist token", "a", iterator.next());
  134. Assert.assertFalse("More than one item detected after conversation",
  135. iterator.hasNext());
  136. }
  137. @Test
  138. public void convertToModel_null() {
  139. StringToCollectionConverter converter = new StringToCollectionConverter();
  140. Assert.assertNull(converter.convertToModel(null, ArrayList.class,
  141. Locale.ENGLISH));
  142. }
  143. @Test
  144. public void convertToPresentation_null() {
  145. StringToCollectionConverter converter = new StringToCollectionConverter();
  146. Assert.assertNull(converter.convertToPresentation(null, String.class,
  147. Locale.ENGLISH));
  148. }
  149. public enum TestEnum {
  150. X, Y, Z;
  151. }
  152. }