diff options
author | Artur Signell <artur@vaadin.com> | 2016-08-25 19:37:57 +0300 |
---|---|---|
committer | Artur Signell <artur@vaadin.com> | 2016-08-26 19:49:06 +0300 |
commit | d13ca5506a2f7eb3dce75b98238bdb280ec7fa20 (patch) | |
tree | 02575981ecfeecd07561337e8f5e04ce8eca8236 /compatibility-server/src/test | |
parent | 818f7d1cb8a92473f685144fed55facdc960697d (diff) | |
download | vaadin-framework-d13ca5506a2f7eb3dce75b98238bdb280ec7fa20.tar.gz vaadin-framework-d13ca5506a2f7eb3dce75b98238bdb280ec7fa20.zip |
Implement new Label
Label does intentionally not implement HasValue as it
cannot provide a value to a model
Change-Id: I36b8e1794d64caf566fa802177051ae2eb637bf9
Diffstat (limited to 'compatibility-server/src/test')
3 files changed, 308 insertions, 0 deletions
diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/label/LabelConvertersTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/label/LabelConvertersTest.java new file mode 100644 index 0000000000..22f45c6bc6 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/label/LabelConvertersTest.java @@ -0,0 +1,87 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.server.component.label; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; + +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.server.VaadinSession; +import com.vaadin.tests.data.bean.Person; +import com.vaadin.tests.util.AlwaysLockedVaadinSession; +import com.vaadin.util.CurrentInstance; +import com.vaadin.v7.data.Property; +import com.vaadin.v7.data.util.MethodProperty; +import com.vaadin.v7.ui.Label; + +public class LabelConvertersTest { + @Before + public void clearExistingThreadLocals() { + // Ensure no previous test left some thread locals hanging + CurrentInstance.clearAll(); + } + + @Test + public void testLabelSetDataSourceLaterOn() { + Person p = Person.createTestPerson1(); + Label l = new Label("My label"); + assertEquals("My label", l.getValue()); + assertNull(l.getConverter()); + l.setPropertyDataSource(new MethodProperty<String>(p, "firstName")); + assertEquals(p.getFirstName(), l.getValue()); + p.setFirstName("123"); + assertEquals("123", l.getValue()); + } + + @Test + public void testIntegerDataSource() { + VaadinSession.setCurrent(new AlwaysLockedVaadinSession(null)); + Label l = new Label("Foo"); + Property ds = new MethodProperty<Integer>(Person.createTestPerson1(), + "age"); + l.setPropertyDataSource(ds); + assertEquals(String.valueOf(Person.createTestPerson1().getAge()), + l.getValue()); + } + + @Test + public void testSetValueWithDataSource() { + try { + MethodProperty<String> property = new MethodProperty<String>( + Person.createTestPerson1(), "firstName"); + Label l = new Label(property); + l.setValue("Foo"); + fail("setValue should throw an exception when a data source is set"); + } catch (Exception e) { + } + + } + + @Test + public void testLabelWithoutDataSource() { + Label l = new Label("My label"); + assertEquals("My label", l.getValue()); + assertNull(l.getConverter()); + assertNull(l.getPropertyDataSource()); + l.setValue("New value"); + assertEquals("New value", l.getValue()); + assertNull(l.getConverter()); + assertNull(l.getPropertyDataSource()); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/label/LabelListenersTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/label/LabelListenersTest.java new file mode 100644 index 0000000000..7c19c9c968 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/label/LabelListenersTest.java @@ -0,0 +1,103 @@ +package com.vaadin.tests.server.component.label; + +import static org.easymock.EasyMock.anyObject; +import static org.easymock.EasyMock.createStrictMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; + +import org.easymock.EasyMock; +import org.junit.Test; + +import com.vaadin.tests.server.component.AbstractListenerMethodsTestBase; +import com.vaadin.v7.data.Property; +import com.vaadin.v7.data.Property.ValueChangeListener; +import com.vaadin.v7.ui.Label; +import com.vaadin.v7.ui.Label.ValueChangeEvent; + +public class LabelListenersTest extends AbstractListenerMethodsTestBase { + + @Test + public void testValueChangeListenerAddGetRemove() throws Exception { + testListenerAddGetRemove(Label.class, ValueChangeEvent.class, + ValueChangeListener.class); + } + + @Test + public void testValueChangeFiredWhenSettingValue() { + Label underTest = new Label(); + + // setup the mock listener + ValueChangeListener mockListener = createStrictMock( + ValueChangeListener.class); + // record + mockListener.valueChange( + anyObject(com.vaadin.v7.data.Property.ValueChangeEvent.class)); + + // test + underTest.addValueChangeListener(mockListener); + + replay(mockListener); + underTest.setValue("A new value"); + + verify(mockListener); + + } + + @Test + public void testValueChangeFiredWhenSettingPropertyDataSource() { + // setup + Label underTest = new Label(); + + Property mockProperty = EasyMock.createMock(Property.class); + + ValueChangeListener mockListener = createStrictMock( + ValueChangeListener.class); + // record + mockListener.valueChange( + anyObject(com.vaadin.v7.data.Property.ValueChangeEvent.class)); + + expect(mockProperty.getType()).andReturn(String.class).atLeastOnce(); + expect(mockProperty.getValue()).andReturn("Any").atLeastOnce(); + + // test + + replay(mockListener, mockProperty); + underTest.addValueChangeListener(mockListener); + underTest.setPropertyDataSource(mockProperty); + + verify(mockListener); + + } + + @Test + public void testValueChangeNotFiredWhenNotSettingValue() { + Label underTest = new Label(); + // setup the mock listener + ValueChangeListener mockListener = createStrictMock( + ValueChangeListener.class); + // record: nothing to record + + // test + underTest.addValueChangeListener(mockListener); + replay(mockListener); + verify(mockListener); + } + + @Test + public void testNoValueChangeFiredWhenSettingPropertyDataSourceToNull() { + Label underTest = new Label(); + // setup the mock Listener + ValueChangeListener mockListener = createStrictMock( + ValueChangeListener.class); + // record: nothing to record + + // test + underTest.addValueChangeListener(mockListener); + underTest.setPropertyDataSource(null); + + replay(mockListener); + verify(mockListener); + } + +} diff --git a/compatibility-server/src/test/java/com/vaadin/v7/ui/LabelDataSourceTest.java b/compatibility-server/src/test/java/com/vaadin/v7/ui/LabelDataSourceTest.java new file mode 100644 index 0000000000..42d0d72867 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/v7/ui/LabelDataSourceTest.java @@ -0,0 +1,118 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.v7.ui; + +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.server.VaadinSession; +import com.vaadin.tests.util.AlwaysLockedVaadinSession; +import com.vaadin.tests.util.MockUI; +import com.vaadin.ui.UI; +import com.vaadin.v7.data.util.ObjectProperty; + +public class LabelDataSourceTest { + + Label label; + private static final String STRING_DS_VALUE = "String DatA source"; + private static final int INTEGER_DS_VALUE = 1587; + private static final String INTEGER_STRING_VALUE_FI = "1 587"; + private static final String INTEGER_STRING_VALUE_EN_US = "1,587"; + private static final Object INTEGER_STRING_VALUE_DE = "1.587"; + ObjectProperty<String> stringDataSource; + private ObjectProperty<Integer> integerDataSource; + VaadinSession vaadinSession; + + @Before + public void setup() { + vaadinSession = new AlwaysLockedVaadinSession(null); + VaadinSession.setCurrent(vaadinSession); + + label = new Label(); + stringDataSource = new ObjectProperty<String>(STRING_DS_VALUE); + integerDataSource = new ObjectProperty<Integer>(INTEGER_DS_VALUE); + } + + @Test + public void stringDataSource() { + label.setPropertyDataSource(stringDataSource); + Assert.assertEquals(STRING_DS_VALUE, label.getState().text); + Assert.assertEquals(STRING_DS_VALUE, label.getValue()); + Assert.assertEquals(stringDataSource, label.getPropertyDataSource()); + label.setPropertyDataSource(null); + Assert.assertEquals(STRING_DS_VALUE, label.getState().text); + Assert.assertEquals(STRING_DS_VALUE, label.getValue()); + Assert.assertEquals(null, label.getPropertyDataSource()); + label.setValue("foo"); + Assert.assertEquals("foo", label.getState().text); + Assert.assertEquals("foo", label.getValue()); + Assert.assertNull(label.getPropertyDataSource()); + + } + + @Test + public void integerDataSourceFi() { + label.setLocale(new Locale("fi", "FI")); + label.setPropertyDataSource(integerDataSource); + Assert.assertEquals(INTEGER_STRING_VALUE_FI, label.getState().text); + Assert.assertEquals(INTEGER_STRING_VALUE_FI, label.getValue()); + Assert.assertEquals(integerDataSource, label.getPropertyDataSource()); + } + + @Test + public void integerDataSourceEn() { + label.setLocale(new Locale("en", "US")); + label.setPropertyDataSource(integerDataSource); + Assert.assertEquals(INTEGER_STRING_VALUE_EN_US, label.getState().text); + Assert.assertEquals(INTEGER_STRING_VALUE_EN_US, label.getValue()); + Assert.assertEquals(integerDataSource, label.getPropertyDataSource()); + } + + @Test + public void changeLocaleAfterDataSource() { + label.setLocale(new Locale("en", "US")); + label.setPropertyDataSource(integerDataSource); + label.setLocale(new Locale("fi", "FI")); + Assert.assertEquals(INTEGER_STRING_VALUE_FI, label.getState().text); + Assert.assertEquals(INTEGER_STRING_VALUE_FI, label.getValue()); + Assert.assertEquals(integerDataSource, label.getPropertyDataSource()); + } + + @Test + public void setRemoveDataSource() { + label.setValue("before"); + label.setPropertyDataSource(stringDataSource); + Assert.assertEquals(STRING_DS_VALUE, label.getValue()); + label.setPropertyDataSource(null); + Assert.assertEquals(STRING_DS_VALUE, label.getValue()); + label.setValue("after"); + Assert.assertEquals("after", label.getValue()); + } + + @Test + public void attachToSessionWithDifferentLocale() { + label.setValue("before"); + // label.setLocale(Locale.GERMANY); + label.setPropertyDataSource(integerDataSource); + UI ui = new MockUI(); + ui.setLocale(Locale.GERMANY); + ui.setContent(label); + Assert.assertEquals(INTEGER_STRING_VALUE_DE, label.getState().text); + } +} |