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.

LabelConvertersTest.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package com.vaadin.tests.server.component.label;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertNull;
  4. import static org.junit.Assert.fail;
  5. import org.junit.Before;
  6. import org.junit.Test;
  7. import com.vaadin.server.VaadinSession;
  8. import com.vaadin.tests.data.bean.Person;
  9. import com.vaadin.tests.util.AlwaysLockedVaadinSession;
  10. import com.vaadin.util.CurrentInstance;
  11. import com.vaadin.v7.data.Property;
  12. import com.vaadin.v7.data.util.MethodProperty;
  13. import com.vaadin.v7.ui.Label;
  14. public class LabelConvertersTest {
  15. @Before
  16. public void clearExistingThreadLocals() {
  17. // Ensure no previous test left some thread locals hanging
  18. CurrentInstance.clearAll();
  19. }
  20. @Test
  21. public void testLabelSetDataSourceLaterOn() {
  22. Person p = Person.createTestPerson1();
  23. Label l = new Label("My label");
  24. assertEquals("My label", l.getValue());
  25. assertNull(l.getConverter());
  26. l.setPropertyDataSource(new MethodProperty<String>(p, "firstName"));
  27. assertEquals(p.getFirstName(), l.getValue());
  28. p.setFirstName("123");
  29. assertEquals("123", l.getValue());
  30. }
  31. @Test
  32. public void testIntegerDataSource() {
  33. VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null));
  34. Label l = new Label("Foo");
  35. Property ds = new MethodProperty<Integer>(Person.createTestPerson1(),
  36. "age");
  37. l.setPropertyDataSource(ds);
  38. assertEquals(String.valueOf(Person.createTestPerson1().getAge()),
  39. l.getValue());
  40. }
  41. @Test
  42. public void testSetValueWithDataSource() {
  43. try {
  44. MethodProperty<String> property = new MethodProperty<String>(
  45. Person.createTestPerson1(), "firstName");
  46. Label l = new Label(property);
  47. l.setValue("Foo");
  48. fail("setValue should throw an exception when a data source is set");
  49. } catch (Exception e) {
  50. }
  51. }
  52. @Test
  53. public void testLabelWithoutDataSource() {
  54. Label l = new Label("My label");
  55. assertEquals("My label", l.getValue());
  56. assertNull(l.getConverter());
  57. assertNull(l.getPropertyDataSource());
  58. l.setValue("New value");
  59. assertEquals("New value", l.getValue());
  60. assertNull(l.getConverter());
  61. assertNull(l.getPropertyDataSource());
  62. }
  63. }