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.

TablePropertyValueConverterTest.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Copyright 2000-2013 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.server.component.table;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertTrue;
  19. import static org.junit.Assert.fail;
  20. import java.lang.reflect.Field;
  21. import java.util.Collection;
  22. import java.util.HashSet;
  23. import java.util.Locale;
  24. import java.util.Map;
  25. import java.util.Map.Entry;
  26. import java.util.Set;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import com.vaadin.v7.data.Container;
  30. import com.vaadin.v7.data.Item;
  31. import com.vaadin.v7.data.Property;
  32. import com.vaadin.v7.data.util.IndexedContainer;
  33. import com.vaadin.v7.data.util.converter.Converter;
  34. import com.vaadin.v7.ui.Table;
  35. public class TablePropertyValueConverterTest {
  36. protected TestableTable table;
  37. protected Collection<?> initialProperties;
  38. @Test
  39. public void testRemovePropertyId() {
  40. Collection<Object> converters = table.getCurrentConverters();
  41. assertFalse("Set of converters was empty at the start.",
  42. converters.isEmpty());
  43. Object firstId = converters.iterator().next();
  44. table.removeContainerProperty(firstId);
  45. Collection<Object> converters2 = table.getCurrentConverters();
  46. assertTrue("FirstId was not removed", !converters2.contains(firstId));
  47. assertTrue("The number of removed converters was not one.",
  48. converters.size() - converters2.size() == 1);
  49. for (Object originalId : converters) {
  50. if (!originalId.equals(firstId)) {
  51. assertTrue("The wrong converter was removed.",
  52. converters2.contains(originalId));
  53. }
  54. }
  55. }
  56. @Test
  57. public void testSetContainer() {
  58. table.setContainerDataSource(createContainer(
  59. new String[] { "col1", "col3", "col4", "col5" }));
  60. Collection<Object> converters = table.getCurrentConverters();
  61. assertTrue("There should only have been one converter left.",
  62. converters.size() == 1);
  63. Object onlyKey = converters.iterator().next();
  64. assertTrue("The incorrect key was left.", onlyKey.equals("col1"));
  65. }
  66. @Test
  67. public void testSetContainerWithInexactButCompatibleTypes() {
  68. TestableTable customTable = new TestableTable("Test table",
  69. createContainer(new String[] { "col1", "col2", "col3" },
  70. new Class[] { String.class, BaseClass.class,
  71. DerivedClass.class }));
  72. customTable.setConverter("col1", new Converter<String, String>() {
  73. private static final long serialVersionUID = 1L;
  74. @Override
  75. public String convertToModel(String value,
  76. Class<? extends String> targetType, Locale locale)
  77. throws ConversionException {
  78. return "model";
  79. }
  80. @Override
  81. public String convertToPresentation(String value,
  82. Class<? extends String> targetType, Locale locale)
  83. throws ConversionException {
  84. return "presentation";
  85. }
  86. @Override
  87. public Class<String> getModelType() {
  88. return String.class;
  89. }
  90. @Override
  91. public Class<String> getPresentationType() {
  92. return String.class;
  93. }
  94. });
  95. customTable.setConverter("col2", new Converter<String, BaseClass>() {
  96. private static final long serialVersionUID = 1L;
  97. @Override
  98. public BaseClass convertToModel(String value,
  99. Class<? extends BaseClass> targetType, Locale locale)
  100. throws ConversionException {
  101. return new BaseClass("model");
  102. }
  103. @Override
  104. public Class<BaseClass> getModelType() {
  105. return BaseClass.class;
  106. }
  107. @Override
  108. public Class<String> getPresentationType() {
  109. return String.class;
  110. }
  111. @Override
  112. public String convertToPresentation(BaseClass value,
  113. Class<? extends String> targetType, Locale locale)
  114. throws ConversionException {
  115. return null;
  116. }
  117. });
  118. customTable.setConverter("col3", new Converter<String, DerivedClass>() {
  119. private static final long serialVersionUID = 1L;
  120. @Override
  121. public DerivedClass convertToModel(String value,
  122. Class<? extends DerivedClass> targetType, Locale locale)
  123. throws ConversionException {
  124. return new DerivedClass("derived" + 1001);
  125. }
  126. @Override
  127. public Class<DerivedClass> getModelType() {
  128. return DerivedClass.class;
  129. }
  130. @Override
  131. public Class<String> getPresentationType() {
  132. return String.class;
  133. }
  134. @Override
  135. public String convertToPresentation(DerivedClass value,
  136. Class<? extends String> targetType, Locale locale)
  137. throws ConversionException {
  138. return null;
  139. }
  140. });
  141. customTable.setContainerDataSource(
  142. createContainer(new String[] { "col1", "col2", "col3" },
  143. new Class[] { DerivedClass.class, DerivedClass.class,
  144. BaseClass.class }));
  145. Set<Object> converters = customTable.getCurrentConverters();
  146. // TODO Test temporarily disabled as this feature
  147. // is not yet implemented in Table
  148. /*
  149. * assertTrue("Incompatible types were not removed.", converters.size()
  150. * <= 1); assertTrue("Even compatible types were removed",
  151. * converters.size() == 1); assertTrue("Compatible type was missing.",
  152. * converters.contains("col2"));
  153. */
  154. }
  155. @Test
  156. public void testPrimitiveTypeConverters() {
  157. TestableTable customTable = new TestableTable("Test table",
  158. createContainer(new String[] { "col1", "col2", "col3" },
  159. new Class[] { int.class, BaseClass.class,
  160. DerivedClass.class }));
  161. customTable.setConverter("col1", new Converter<String, Integer>() {
  162. private static final long serialVersionUID = 1L;
  163. @Override
  164. public Integer convertToModel(String value,
  165. Class<? extends Integer> targetType, Locale locale)
  166. throws ConversionException {
  167. return 11;
  168. }
  169. @Override
  170. public String convertToPresentation(Integer value,
  171. Class<? extends String> targetType, Locale locale)
  172. throws ConversionException {
  173. return "presentation";
  174. }
  175. @Override
  176. public Class<Integer> getModelType() {
  177. return Integer.class;
  178. }
  179. @Override
  180. public Class<String> getPresentationType() {
  181. return String.class;
  182. }
  183. });
  184. Set<Object> converters = customTable.getCurrentConverters();
  185. assertFalse("Converter was not set.", converters.isEmpty());
  186. }
  187. @Test
  188. public void testInheritance() {
  189. assertTrue("BaseClass isn't assignable from DerivedClass",
  190. BaseClass.class.isAssignableFrom(DerivedClass.class));
  191. assertFalse("DerivedClass is assignable from BaseClass",
  192. DerivedClass.class.isAssignableFrom(BaseClass.class));
  193. }
  194. @Before
  195. public void setUp() {
  196. table = new TestableTable("Test table",
  197. createContainer(new String[] { "col1", "col2", "col3" }));
  198. table.setConverter("col1", new Converter<String, String>() {
  199. private static final long serialVersionUID = 1L;
  200. @Override
  201. public String convertToModel(String value,
  202. Class<? extends String> targetType, Locale locale)
  203. throws ConversionException {
  204. return "model";
  205. }
  206. @Override
  207. public String convertToPresentation(String value,
  208. Class<? extends String> targetType, Locale locale)
  209. throws ConversionException {
  210. return "presentation";
  211. }
  212. @Override
  213. public Class<String> getModelType() {
  214. return String.class;
  215. }
  216. @Override
  217. public Class<String> getPresentationType() {
  218. return String.class;
  219. }
  220. });
  221. table.setConverter("col2", new Converter<String, String>() {
  222. private static final long serialVersionUID = 1L;
  223. @Override
  224. public String convertToModel(String value,
  225. Class<? extends String> targetType, Locale locale)
  226. throws ConversionException {
  227. return "model2";
  228. }
  229. @Override
  230. public String convertToPresentation(String value,
  231. Class<? extends String> targetType, Locale locale)
  232. throws ConversionException {
  233. return "presentation2";
  234. }
  235. @Override
  236. public Class<String> getModelType() {
  237. return String.class;
  238. }
  239. @Override
  240. public Class<String> getPresentationType() {
  241. return String.class;
  242. }
  243. });
  244. initialProperties = table.getContainerPropertyIds();
  245. }
  246. private static Container createContainer(Object[] ids) {
  247. Class[] types = new Class[ids.length];
  248. for (int i = 0; i < types.length; ++i) {
  249. types[i] = String.class;
  250. }
  251. return createContainer(ids, types);
  252. }
  253. private static Container createContainer(Object[] ids, Class[] types) {
  254. IndexedContainer container = new IndexedContainer();
  255. if (ids.length > types.length) {
  256. throw new IllegalArgumentException("Too few defined types");
  257. }
  258. for (int i = 0; i < ids.length; ++i) {
  259. container.addContainerProperty(ids[i], types[i], "");
  260. }
  261. for (int i = 0; i < 100; i++) {
  262. Item item = container.addItem("item " + i);
  263. for (int j = 0; j < ids.length; ++j) {
  264. Property itemProperty = item.getItemProperty(ids[j]);
  265. if (types[j] == String.class) {
  266. itemProperty.setValue(ids[j].toString() + i);
  267. } else if (types[j] == BaseClass.class) {
  268. itemProperty.setValue(new BaseClass("base" + i));
  269. } else if (types[j] == DerivedClass.class) {
  270. itemProperty.setValue(new DerivedClass("derived" + i));
  271. } else if (types[j] == int.class) {
  272. // FIXME can't set values because the int is autoboxed into
  273. // an Integer and not unboxed prior to set
  274. // itemProperty.setValue(i);
  275. } else {
  276. throw new IllegalArgumentException(
  277. "Unhandled type in createContainer: " + types[j]);
  278. }
  279. }
  280. }
  281. return container;
  282. }
  283. private class TestableTable extends Table {
  284. /**
  285. * @param string
  286. * @param createContainer
  287. */
  288. public TestableTable(String string, Container container) {
  289. super(string, container);
  290. }
  291. Set<Object> getCurrentConverters() {
  292. try {
  293. Field f = Table.class
  294. .getDeclaredField("propertyValueConverters");
  295. f.setAccessible(true);
  296. Map<Object, Converter<String, Object>> pvc = (Map<Object, Converter<String, Object>>) f
  297. .get(this);
  298. Set<Object> currentConverters = new HashSet<Object>();
  299. for (Entry<Object, Converter<String, Object>> entry : pvc
  300. .entrySet()) {
  301. currentConverters.add(entry.getKey());
  302. }
  303. return currentConverters;
  304. } catch (Exception e) {
  305. fail("Unable to retrieve propertyValueConverters");
  306. return null;
  307. }
  308. }
  309. }
  310. private static class BaseClass {
  311. private String title;
  312. public BaseClass(String title) {
  313. this.title = title;
  314. }
  315. }
  316. private static class DerivedClass extends BaseClass {
  317. public DerivedClass(String title) {
  318. super(title);
  319. }
  320. }
  321. }