diff options
Diffstat (limited to 'server/tests/src')
38 files changed, 1576 insertions, 48 deletions
diff --git a/server/tests/src/ClassInDefaultPackage.java b/server/tests/src/ClassInDefaultPackage.java new file mode 100644 index 0000000000..52dc87e0e2 --- /dev/null +++ b/server/tests/src/ClassInDefaultPackage.java @@ -0,0 +1,29 @@ +import org.junit.Ignore; + +/* + * Copyright 2000-2014 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. + */ + +/** + * + * This class is test data. Don't delete it. + * + * @author Vaadin Ltd + * @since 7.2 + */ +@Ignore +public class ClassInDefaultPackage { + +} diff --git a/server/tests/src/com/vaadin/data/util/BeanContainerTest.java b/server/tests/src/com/vaadin/data/util/BeanContainerTest.java index 684ab5d6bc..a6c3bb0b8b 100644 --- a/server/tests/src/com/vaadin/data/util/BeanContainerTest.java +++ b/server/tests/src/com/vaadin/data/util/BeanContainerTest.java @@ -466,6 +466,8 @@ public class BeanContainerTest extends AbstractBeanContainerTest { assertTrue(container .addNestedContainerProperty("address.postalCodeObject")); assertTrue(container.addNestedContainerProperty("address.street")); + // the nested properties added with allowNullBean setting should return + // null assertNull(container.getContainerProperty("John", "address.street") .getValue()); } diff --git a/server/tests/src/com/vaadin/data/util/BeanItemContainerTest.java b/server/tests/src/com/vaadin/data/util/BeanItemContainerTest.java index 767a9e2e4d..c7d992dbeb 100644 --- a/server/tests/src/com/vaadin/data/util/BeanItemContainerTest.java +++ b/server/tests/src/com/vaadin/data/util/BeanItemContainerTest.java @@ -723,6 +723,7 @@ public class BeanItemContainerTest extends AbstractBeanContainerTest { assertTrue(container .addNestedContainerProperty("address.postalCodeObject")); assertTrue(container.addNestedContainerProperty("address.street")); + // the nested properties should return null assertNull(container.getContainerProperty(john, "address.street") .getValue()); } diff --git a/server/tests/src/com/vaadin/data/util/MethodPropertyMemoryConsumption.java b/server/tests/src/com/vaadin/data/util/MethodPropertyMemoryConsumption.java new file mode 100644 index 0000000000..caff33cf50 --- /dev/null +++ b/server/tests/src/com/vaadin/data/util/MethodPropertyMemoryConsumption.java @@ -0,0 +1,145 @@ +/* + * Copyright 2000-2013 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.data.util; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.lang.reflect.Field; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Test for MethodProperty: don't allocate unnecessary Object arrays. + * + * @since 7.2 + * @author Vaadin Ltd + */ +public class MethodPropertyMemoryConsumption { + + @Test + public void testSetArguments() throws NoSuchFieldException, + SecurityException, IllegalArgumentException, IllegalAccessException { + TestBean bean = new TestBean(); + TestMethodProperty<String> property = new TestMethodProperty<String>( + bean, "name"); + Object[] getArgs = property.getGetArgs(); + Object[] setArgs = property.getSetArgs(); + + Field getArgsField = TestMethodProperty.class + .getDeclaredField("getArgs"); + getArgsField.setAccessible(true); + + Field setArgsField = TestMethodProperty.class + .getDeclaredField("setArgs"); + setArgsField.setAccessible(true); + + Assert.assertSame("setArguments method sets non-default instance" + + " of empty Object array for getArgs", + getArgsField.get(property), getArgs); + + Assert.assertSame("setArguments method sets non-default instance" + + " of empty Object array for setArgs", + setArgsField.get(property), setArgs); + } + + @Test + public void testDefaultCtor() { + TestBean bean = new TestBean(); + TestMethodProperty<String> property = new TestMethodProperty<String>( + bean, "name"); + + Object[] getArgs = property.getGetArgs(); + Object[] setArgs = property.getSetArgs(); + + TestBean otherBean = new TestBean(); + TestMethodProperty<String> otherProperty = new TestMethodProperty<String>( + otherBean, "name"); + Assert.assertSame("setArguments method uses different instance" + + " of empty Object array for getArgs", getArgs, + otherProperty.getGetArgs()); + Assert.assertSame("setArguments method uses different instance" + + " of empty Object array for setArgs", setArgs, + otherProperty.getSetArgs()); + } + + @Test + public void testDefaultArgsSerialization() throws IOException, + ClassNotFoundException { + TestBean bean = new TestBean(); + TestMethodProperty<String> property = new TestMethodProperty<String>( + bean, "name"); + + ByteArrayOutputStream sourceOutStream = new ByteArrayOutputStream(); + ObjectOutputStream outStream = new ObjectOutputStream(sourceOutStream); + outStream.writeObject(property); + + ObjectInputStream inputStream = new ObjectInputStream( + new ByteArrayInputStream(sourceOutStream.toByteArray())); + Object red = inputStream.readObject(); + TestMethodProperty<?> deserialized = (TestMethodProperty<?>) red; + + Assert.assertNotNull("Deseriliation doesn't call setArguments method", + deserialized.getGetArgs()); + Assert.assertNotNull("Deseriliation doesn't call setArguments method", + deserialized.getSetArgs()); + + } + + public static class TestMethodProperty<T> extends MethodProperty<T> { + + public TestMethodProperty(Object instance, String beanPropertyName) { + super(instance, beanPropertyName); + } + + @Override + public void setArguments(Object[] getArgs, Object[] setArgs, + int setArgumentIndex) { + super.setArguments(getArgs, setArgs, setArgumentIndex); + this.getArgs = getArgs; + this.setArgs = setArgs; + } + + Object[] getGetArgs() { + return getArgs; + } + + Object[] getSetArgs() { + return setArgs; + } + + private transient Object[] getArgs; + private transient Object[] setArgs; + } + + public static class TestBean implements Serializable { + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + } +} diff --git a/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTest.java b/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTest.java index 844ef705b1..4c132eba30 100644 --- a/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTest.java +++ b/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTest.java @@ -1917,7 +1917,6 @@ public class SQLContainerTest { EasyMock.expect(delegate.getCountStatement()) .andAnswer(new IAnswer<StatementHelper>() { @Override - @SuppressWarnings("deprecation") public StatementHelper answer() throws Throwable { StatementHelper sh = new StatementHelper(); StringBuffer query = new StringBuffer( @@ -1990,7 +1989,6 @@ public class SQLContainerTest { EasyMock.expect(delegate.getCountStatement()) .andAnswer(new IAnswer<StatementHelper>() { @Override - @SuppressWarnings("deprecation") public StatementHelper answer() throws Throwable { StatementHelper sh = new StatementHelper(); StringBuffer query = new StringBuffer( @@ -2134,7 +2132,6 @@ public class SQLContainerTest { EasyMock.expect(delegate.getCountStatement()) .andAnswer(new IAnswer<StatementHelper>() { @Override - @SuppressWarnings("deprecation") public StatementHelper answer() throws Throwable { StatementHelper sh = new StatementHelper(); StringBuffer query = new StringBuffer( @@ -2213,7 +2210,6 @@ public class SQLContainerTest { EasyMock.expect(delegate.getCountStatement()) .andAnswer(new IAnswer<StatementHelper>() { @Override - @SuppressWarnings("deprecation") public StatementHelper answer() throws Throwable { StatementHelper sh = new StatementHelper(); StringBuffer query = new StringBuffer( @@ -2292,7 +2288,6 @@ public class SQLContainerTest { EasyMock.expect(delegate.getCountStatement()) .andAnswer(new IAnswer<StatementHelper>() { @Override - @SuppressWarnings("deprecation") public StatementHelper answer() throws Throwable { StatementHelper sh = new StatementHelper(); StringBuffer query = new StringBuffer( diff --git a/server/tests/src/com/vaadin/data/util/sqlcontainer/TicketTests.java b/server/tests/src/com/vaadin/data/util/sqlcontainer/TicketTests.java index 8e8c83d234..110225e206 100644 --- a/server/tests/src/com/vaadin/data/util/sqlcontainer/TicketTests.java +++ b/server/tests/src/com/vaadin/data/util/sqlcontainer/TicketTests.java @@ -88,7 +88,6 @@ public class TicketTests { EasyMock.expect(delegate.getCountStatement()) .andAnswer(new IAnswer<StatementHelper>() { @Override - @SuppressWarnings("deprecation") public StatementHelper answer() throws Throwable { StatementHelper sh = new StatementHelper(); StringBuffer query = new StringBuffer( diff --git a/server/tests/src/com/vaadin/server/ConnectorResourceHandlerTest.java b/server/tests/src/com/vaadin/server/ConnectorResourceHandlerTest.java new file mode 100644 index 0000000000..828c35ced3 --- /dev/null +++ b/server/tests/src/com/vaadin/server/ConnectorResourceHandlerTest.java @@ -0,0 +1,97 @@ +/* + * Copyright 2000-2013 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.server; + +import java.io.IOException; + +import org.easymock.EasyMock; +import org.easymock.IMocksControl; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.ui.Button; +import com.vaadin.ui.UI; + +public class ConnectorResourceHandlerTest { + + VaadinRequest request; + VaadinResponse response; + VaadinSession session; + UI ui; + + @Before + public void setUp() { + IMocksControl control = EasyMock.createNiceControl(); + + request = control.createMock(VaadinRequest.class); + response = control.createMock(VaadinResponse.class); + VaadinService service = control.createMock(VaadinService.class); + + EasyMock.expect(request.getPathInfo()) + .andReturn("/APP/connector/0/1/2"); + + control.replay(); + + session = new MockVaadinSession(service); + + ui = new UI() { + @Override + protected void init(VaadinRequest request) { + } + }; + ui.doInit(request, 0, ""); + + session.lock(); + try { + session.setCommunicationManager(new LegacyCommunicationManager( + session)); + ui.setSession(session); + session.addUI(ui); + } finally { + session.unlock(); + } + } + + @Test + public void testErrorHandling() throws IOException { + + ErrorHandler errorHandler = EasyMock.createMock(ErrorHandler.class); + errorHandler.error(EasyMock.anyObject(ErrorEvent.class)); + EasyMock.replay(errorHandler); + + Button button = new Button() { + @Override + public boolean handleConnectorRequest(VaadinRequest request, + VaadinResponse response, String path) { + throw new RuntimeException(); + } + }; + button.setErrorHandler(errorHandler); + + session.lock(); + try { + ui.setContent(button); + } finally { + session.unlock(); + } + + ConnectorResourceHandler handler = new ConnectorResourceHandler(); + Assert.assertTrue(handler.handleRequest(session, request, response)); + + EasyMock.verify(errorHandler); + } +} diff --git a/server/tests/src/com/vaadin/server/DefaultDeploymentConfigurationTest.java b/server/tests/src/com/vaadin/server/DefaultDeploymentConfigurationTest.java new file mode 100644 index 0000000000..45d9853537 --- /dev/null +++ b/server/tests/src/com/vaadin/server/DefaultDeploymentConfigurationTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2013 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.server; + +import java.util.Properties; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link DefaultDeploymentConfiguration} + * + * @author Vaadin Ltd + * @since 7.2 + */ +public class DefaultDeploymentConfigurationTest { + + @Test + public void testGetSystemPropertyForDefaultPackage() + throws ClassNotFoundException { + Class<?> clazz = Class.forName("ClassInDefaultPackage"); + String value = "value"; + String prop = "prop"; + System.setProperty(prop, value); + DefaultDeploymentConfiguration config = new DefaultDeploymentConfiguration( + clazz, new Properties()); + Assert.assertEquals(value, config.getSystemProperty(prop)); + } + + @Test + public void testGetSystemProperty() throws ClassNotFoundException { + String value = "value"; + String prop = "prop"; + System.setProperty(DefaultDeploymentConfigurationTest.class + .getPackage().getName() + '.' + prop, value); + DefaultDeploymentConfiguration config = new DefaultDeploymentConfiguration( + DefaultDeploymentConfigurationTest.class, new Properties()); + Assert.assertEquals(value, config.getSystemProperty(prop)); + } +} diff --git a/server/tests/src/com/vaadin/server/MockVaadinSession.java b/server/tests/src/com/vaadin/server/MockVaadinSession.java new file mode 100644 index 0000000000..660767e0d4 --- /dev/null +++ b/server/tests/src/com/vaadin/server/MockVaadinSession.java @@ -0,0 +1,49 @@ +/* + * Copyright 2000-2014 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.server; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +/** + * + * @author Vaadin Ltd + */ +public class MockVaadinSession extends VaadinSession { + + public MockVaadinSession(VaadinService service) { + super(service); + } + + @Override + public void close() { + super.close(); + closeCount++; + } + + public int getCloseCount() { + return closeCount; + } + + @Override + public Lock getLockInstance() { + return lock; + } + + private int closeCount; + + private ReentrantLock lock = new ReentrantLock(); +} diff --git a/server/tests/src/com/vaadin/server/VaadinGateInRequestTests.java b/server/tests/src/com/vaadin/server/VaadinGateInRequestTests.java new file mode 100644 index 0000000000..017613661e --- /dev/null +++ b/server/tests/src/com/vaadin/server/VaadinGateInRequestTests.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2013 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.server; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +import com.vaadin.server.VaadinPortlet.VaadinGateInRequest; + +public class VaadinGateInRequestTests extends + VaadinHttpAndPortletRequestTests<VaadinGateInRequest> { + + @Override + protected VaadinGateInRequest createSut() { + + VaadinGateInRequest request = new VaadinGateInRequest(portletRequest, + vaadinPortletService); + + // Although partial mocking can be considered a code smell, + // here it's actually quite useful to mock reflection calls. + VaadinGateInRequest spy = spy(request); + doReturn(servletRequest).when(spy).getServletRequest(portletRequest); + + return spy; + } +} diff --git a/server/tests/src/com/vaadin/server/VaadinHttpAndPortletRequestTests.java b/server/tests/src/com/vaadin/server/VaadinHttpAndPortletRequestTests.java new file mode 100644 index 0000000000..01d501c4fb --- /dev/null +++ b/server/tests/src/com/vaadin/server/VaadinHttpAndPortletRequestTests.java @@ -0,0 +1,140 @@ +/* + * Copyright 2000-2013 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.server; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.Enumeration; +import java.util.Map; + +import javax.portlet.PortletRequest; +import javax.servlet.http.HttpServletRequest; + +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; + +import com.vaadin.server.VaadinPortlet.VaadinHttpAndPortletRequest; +import com.vaadin.server.VaadinPortletService; + +//Have to ignore this class - otherwise JUnit tries to instantiate it... +@Ignore +public abstract class VaadinHttpAndPortletRequestTests<T extends VaadinHttpAndPortletRequest> { + + protected VaadinHttpAndPortletRequest sut; + protected HttpServletRequest servletRequest; + protected PortletRequest portletRequest; + protected VaadinPortletService vaadinPortletService; + + protected abstract T createSut(); + + @Before + public void setup() { + portletRequest = mock(PortletRequest.class); + vaadinPortletService = mock(VaadinPortletService.class); + servletRequest = mock(HttpServletRequest.class); + + sut = createSut(); + } + + @Test + public void parameterIsFetchedFromServletRequest() { + when(servletRequest.getParameter("foo")).thenReturn("bar"); + + String parameter = sut.getParameter("foo"); + + assertThat(parameter, is("bar")); + } + + @Test + public void originalParameterIsOverridden() { + when(servletRequest.getParameter("foo")).thenReturn("braa"); + when(portletRequest.getParameter("foo")).thenReturn("bar"); + + String parameter = sut.getParameter("foo"); + + assertThat(parameter, is("bar")); + } + + @Test + public void remoteAddressIsFetchedFromServletRequest() { + when(servletRequest.getRemoteAddr()).thenReturn("foo"); + + String remoteAddr = sut.getRemoteAddr(); + + assertThat(remoteAddr, is("foo")); + } + + @Test + public void remoteHostIsFetchedFromServletRequest() { + when(servletRequest.getRemoteHost()).thenReturn("foo"); + + String remoteHost = sut.getRemoteHost(); + + assertThat(remoteHost, is("foo")); + } + + @Test + public void remotePortIsFetchedFromServletRequest() { + when(servletRequest.getRemotePort()).thenReturn(12345); + + int remotePort = sut.getRemotePort(); + + assertThat(remotePort, is(12345)); + } + + @Test + public void headerIsFetchedFromServletRequest() { + when(servletRequest.getHeader("foo")).thenReturn("bar"); + + String header = sut.getHeader("foo"); + + assertThat(header, is("bar")); + } + + @Test + public void headerNamesAreFetchedFromServletRequest() { + Enumeration expectedHeaderNames = mock(Enumeration.class); + when(servletRequest.getHeaderNames()).thenReturn(expectedHeaderNames); + + Enumeration<String> actualHeaderNames = sut.getHeaderNames(); + + assertThat(actualHeaderNames, is(expectedHeaderNames)); + } + + @Test + public void headersAreFetchedFromServletRequest() { + Enumeration expectedHeaders = mock(Enumeration.class); + when(servletRequest.getHeaders("foo")).thenReturn(expectedHeaders); + + Enumeration<String> actualHeaders = sut.getHeaders("foo"); + + assertThat(actualHeaders, is(expectedHeaders)); + } + + @Test + public void parameterMapIsFetchedFromServletRequest() { + Map expectedParameterMap = mock(Map.class); + when(servletRequest.getParameterMap()).thenReturn(expectedParameterMap); + + Map<String, String[]> actualParameterMap = sut.getParameterMap(); + + assertThat(actualParameterMap, is(expectedParameterMap)); + } +} diff --git a/server/tests/src/com/vaadin/server/VaadinLiferayRequestTests.java b/server/tests/src/com/vaadin/server/VaadinLiferayRequestTests.java new file mode 100644 index 0000000000..074941a556 --- /dev/null +++ b/server/tests/src/com/vaadin/server/VaadinLiferayRequestTests.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2013 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.server; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +import com.vaadin.server.VaadinPortlet.VaadinLiferayRequest; + +public class VaadinLiferayRequestTests extends + VaadinHttpAndPortletRequestTests<VaadinLiferayRequest> { + + @Override + protected VaadinLiferayRequest createSut() { + + VaadinLiferayRequest request = new VaadinLiferayRequest(portletRequest, + vaadinPortletService); + + // Although partial mocking can be considered a code smell, + // here it's actually quite useful to mock reflection calls. + VaadinLiferayRequest spy = spy(request); + doReturn(servletRequest).when(spy).getServletRequest(portletRequest); + + return spy; + } +}
\ No newline at end of file diff --git a/server/tests/src/com/vaadin/server/VaadinPortletTests.java b/server/tests/src/com/vaadin/server/VaadinPortletTests.java new file mode 100644 index 0000000000..93f8fd0778 --- /dev/null +++ b/server/tests/src/com/vaadin/server/VaadinPortletTests.java @@ -0,0 +1,94 @@ +/* + * Copyright 2000-2013 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.server; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsInstanceOf.instanceOf; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import javax.portlet.PortalContext; +import javax.portlet.PortletRequest; + +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.server.VaadinPortlet.VaadinGateInRequest; +import com.vaadin.server.VaadinPortlet.VaadinLiferayRequest; +import com.vaadin.server.VaadinPortlet.VaadinWebSpherePortalRequest; + +public class VaadinPortletTests { + + private VaadinPortlet sut; + private PortletRequest portletRequest; + private PortalContext portalContext; + + @Before + public void setup() { + sut = new VaadinPortlet(); + + portletRequest = mock(PortletRequest.class); + portalContext = mock(PortalContext.class); + + when(portletRequest.getPortalContext()).thenReturn(portalContext); + } + + private void mockPortalInfo(String name) { + when(portalContext.getPortalInfo()).thenReturn(name); + } + + private VaadinPortletRequest createRequest() { + VaadinPortletRequest request = sut.createVaadinRequest(portletRequest); + return request; + } + + @Test + public void gateInRequestIsCreated() { + mockPortalInfo("gatein"); + + VaadinPortletRequest request = createRequest(); + + assertThat(request, instanceOf(VaadinGateInRequest.class)); + } + + @Test + public void liferayRequestIsCreated() { + mockPortalInfo("liferay"); + + VaadinPortletRequest request = createRequest(); + + assertThat(request, instanceOf(VaadinLiferayRequest.class)); + } + + @Test + public void webspherePortalRequestIsCreated() { + mockPortalInfo("websphere portal"); + + VaadinPortletRequest request = createRequest(); + + assertThat(request, instanceOf(VaadinWebSpherePortalRequest.class)); + } + + @Test + public void defaultPortletRequestIsCreated() { + mockPortalInfo("foobar"); + + VaadinPortletRequest request = createRequest(); + + assertThat(request, instanceOf(VaadinPortletRequest.class)); + } + +} diff --git a/server/tests/src/com/vaadin/server/VaadinServiceTest.java b/server/tests/src/com/vaadin/server/VaadinServiceTest.java new file mode 100644 index 0000000000..77eb155378 --- /dev/null +++ b/server/tests/src/com/vaadin/server/VaadinServiceTest.java @@ -0,0 +1,69 @@ +/* + * Copyright 2000-2013 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.server; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpSessionBindingEvent; + +import org.easymock.EasyMock; +import org.junit.Assert; +import org.junit.Test; + +/** + * + * @author Vaadin Ltd + */ +public class VaadinServiceTest { + + private class TestSessionDestroyListener implements SessionDestroyListener { + + int callCount = 0; + + @Override + public void sessionDestroy(SessionDestroyEvent event) { + callCount++; + } + } + + @Test + public void testFireSessionDestroy() throws ServletException { + ServletConfig servletConfig = new MockServletConfig(); + VaadinServlet servlet = new VaadinServlet(); + servlet.init(servletConfig); + VaadinService service = servlet.getService(); + + TestSessionDestroyListener listener = new TestSessionDestroyListener(); + + service.addSessionDestroyListener(listener); + + MockVaadinSession vaadinSession = new MockVaadinSession(service); + service.fireSessionDestroy(vaadinSession); + Assert.assertEquals( + "'fireSessionDestroy' method doesn't call 'close' for the session", + 1, vaadinSession.getCloseCount()); + + vaadinSession.valueUnbound(EasyMock + .createMock(HttpSessionBindingEvent.class)); + + Assert.assertEquals("'fireSessionDestroy' method may not call 'close' " + + "method for closing session", 1, + vaadinSession.getCloseCount()); + + Assert.assertEquals("SessionDestroyListeners not called exactly once", + 1, listener.callCount); + } +} diff --git a/server/tests/src/com/vaadin/server/VaadinServletTest.java b/server/tests/src/com/vaadin/server/VaadinServletTest.java new file mode 100644 index 0000000000..be21586be4 --- /dev/null +++ b/server/tests/src/com/vaadin/server/VaadinServletTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2013 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.server; + +import org.junit.Assert; +import org.junit.Test; + +public class VaadinServletTest { + + @Test + public void testGetLastPathParameter() { + Assert.assertEquals("", + VaadinServlet.getLastPathParameter("http://myhost.com")); + Assert.assertEquals(";a", + VaadinServlet.getLastPathParameter("http://myhost.com;a")); + Assert.assertEquals("", + VaadinServlet.getLastPathParameter("http://myhost.com/hello")); + Assert.assertEquals(";b=c", VaadinServlet + .getLastPathParameter("http://myhost.com/hello;b=c")); + Assert.assertEquals("", + VaadinServlet.getLastPathParameter("http://myhost.com/hello/")); + Assert.assertEquals("", VaadinServlet + .getLastPathParameter("http://myhost.com/hello;a/")); + Assert.assertEquals("", VaadinServlet + .getLastPathParameter("http://myhost.com/hello;a=1/")); + Assert.assertEquals(";b", VaadinServlet + .getLastPathParameter("http://myhost.com/hello/;b")); + Assert.assertEquals(";b=1", VaadinServlet + .getLastPathParameter("http://myhost.com/hello/;b=1")); + Assert.assertEquals(";b=1,c=2", VaadinServlet + .getLastPathParameter("http://myhost.com/hello/;b=1,c=2")); + Assert.assertEquals("", VaadinServlet + .getLastPathParameter("http://myhost.com/hello/;b=1,c=2/")); + Assert.assertEquals("", VaadinServlet + .getLastPathParameter("http://myhost.com/a;hello/;a/")); + Assert.assertEquals("", VaadinServlet + .getLastPathParameter("http://myhost.com/a;hello/;a=1/")); + Assert.assertEquals(";b", VaadinServlet + .getLastPathParameter("http://myhost.com/a;hello/;b")); + Assert.assertEquals(";b=1", VaadinServlet + .getLastPathParameter("http://myhost.com/a;hello/;b=1")); + Assert.assertEquals(";b=1,c=2", VaadinServlet + .getLastPathParameter("http://myhost.com/a;hello/;b=1,c=2")); + Assert.assertEquals("", VaadinServlet + .getLastPathParameter("http://myhost.com/a;hello/;b=1,c=2/")); + } +} diff --git a/server/tests/src/com/vaadin/server/VaadinSessionTest.java b/server/tests/src/com/vaadin/server/VaadinSessionTest.java index d4d07ed108..d39da211f4 100644 --- a/server/tests/src/com/vaadin/server/VaadinSessionTest.java +++ b/server/tests/src/com/vaadin/server/VaadinSessionTest.java @@ -100,7 +100,7 @@ public class VaadinSessionTest { } }; - ui.doInit(vaadinRequest, session.getNextUIid()); + ui.doInit(vaadinRequest, session.getNextUIid(), null); ui.setSession(session); session.addUI(ui); @@ -146,4 +146,23 @@ public class VaadinSessionTest { mockService.cleanupSession(session); Assert.assertTrue(detachCalled.get()); } + + @Test + public void testValueUnbound() { + MockVaadinSession vaadinSession = new MockVaadinSession(mockService); + + vaadinSession.valueUnbound(EasyMock + .createMock(HttpSessionBindingEvent.class)); + org.junit.Assert.assertEquals( + "'valueUnbound' method doesn't call 'close' for the session", + 1, vaadinSession.getCloseCount()); + + vaadinSession.valueUnbound(EasyMock + .createMock(HttpSessionBindingEvent.class)); + + org.junit.Assert.assertEquals( + "'valueUnbound' method may not call 'close' " + + "method for closing session", 1, + vaadinSession.getCloseCount()); + } } diff --git a/server/tests/src/com/vaadin/server/VaadinWebSpherePortalRequestTests.java b/server/tests/src/com/vaadin/server/VaadinWebSpherePortalRequestTests.java new file mode 100644 index 0000000000..0da85c7111 --- /dev/null +++ b/server/tests/src/com/vaadin/server/VaadinWebSpherePortalRequestTests.java @@ -0,0 +1,39 @@ +/* + * Copyright 2000-2013 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.server; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +import com.vaadin.server.VaadinPortlet.VaadinWebSpherePortalRequest; + +public class VaadinWebSpherePortalRequestTests extends + VaadinHttpAndPortletRequestTests<VaadinWebSpherePortalRequest> { + + @Override + protected VaadinWebSpherePortalRequest createSut() { + + VaadinWebSpherePortalRequest request = new VaadinWebSpherePortalRequest( + portletRequest, vaadinPortletService); + + // Although partial mocking can be considered a code smell, + // here it's actually quite useful to mock reflection calls. + VaadinWebSpherePortalRequest spy = spy(request); + doReturn(servletRequest).when(spy).getServletRequest(portletRequest); + + return spy; + } +}
\ No newline at end of file diff --git a/server/tests/src/com/vaadin/tests/data/bean/BeanToValidate.java b/server/tests/src/com/vaadin/tests/data/bean/BeanToValidate.java index 416563baba..034609764f 100644 --- a/server/tests/src/com/vaadin/tests/data/bean/BeanToValidate.java +++ b/server/tests/src/com/vaadin/tests/data/bean/BeanToValidate.java @@ -4,6 +4,7 @@ import javax.validation.constraints.Digits; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; +import javax.validation.constraints.Pattern; import javax.validation.constraints.Size; public class BeanToValidate { @@ -21,6 +22,10 @@ public class BeanToValidate { @Digits(integer = 3, fraction = 2) private String decimals; + @Pattern(regexp = "V*", message = "Must start with letter V") + @Size(min = 3, max = 6, message = "Must contain 3 - 6 letters") + private String nickname; + public String getFirstname() { return firstname; } @@ -53,4 +58,12 @@ public class BeanToValidate { this.decimals = decimals; } + public String getNickname() { + return nickname; + } + + public void setNickname(String nickname) { + this.nickname = nickname; + } + } diff --git a/server/tests/src/com/vaadin/tests/data/bean/PersonWithBeanValidationAnnotations.java b/server/tests/src/com/vaadin/tests/data/bean/PersonWithBeanValidationAnnotations.java index 93b2273263..575730d946 100644 --- a/server/tests/src/com/vaadin/tests/data/bean/PersonWithBeanValidationAnnotations.java +++ b/server/tests/src/com/vaadin/tests/data/bean/PersonWithBeanValidationAnnotations.java @@ -8,12 +8,15 @@ import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Past; +import javax.validation.constraints.Pattern; import javax.validation.constraints.Size; public class PersonWithBeanValidationAnnotations { @NotNull @Size(min = 5, max = 20) + @Pattern(regexp = "A.*") private String firstName; + @NotNull private String lastName; diff --git a/server/tests/src/com/vaadin/tests/data/converter/ConverterFactory.java b/server/tests/src/com/vaadin/tests/data/converter/ConverterFactory.java index c3b53940d3..32c9dc6604 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/ConverterFactory.java +++ b/server/tests/src/com/vaadin/tests/data/converter/ConverterFactory.java @@ -78,7 +78,7 @@ public class ConverterFactory extends TestCase { @Override public VaadinSession getSession() { return appWithCustomIntegerConverter; - }; + } }; tf.setConverter(Integer.class); // The application converter always returns 42. Current application is diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestDateToSqlDateConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestDateToSqlDateConverter.java index 685404ded6..d591d10e18 100644 --- a/server/tests/src/com/vaadin/tests/data/converter/TestDateToSqlDateConverter.java +++ b/server/tests/src/com/vaadin/tests/data/converter/TestDateToSqlDateConverter.java @@ -19,7 +19,7 @@ public class TestDateToSqlDateConverter extends TestCase { public void testValueConversion() { Date testDate = new Date(100, 0, 1); long time = testDate.getTime(); - assertEquals(testDate, converter.convertToModel(new java.sql.Date(time), - java.sql.Date.class, Locale.ENGLISH)); + assertEquals(testDate, converter.convertToModel( + new java.sql.Date(time), java.sql.Date.class, Locale.ENGLISH)); } } diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToBigDecimalConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToBigDecimalConverter.java new file mode 100644 index 0000000000..5db33691b6 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToBigDecimalConverter.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2013 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.data.converter; + +import java.math.BigDecimal; +import java.util.Locale; + +import junit.framework.TestCase; + +import com.vaadin.data.util.converter.StringToBigDecimalConverter; + +public class TestStringToBigDecimalConverter extends TestCase { + + StringToBigDecimalConverter converter = new StringToBigDecimalConverter(); + + public void testNullConversion() { + assertEquals(null, + converter.convertToModel(null, BigDecimal.class, null)); + } + + public void testEmptyStringConversion() { + assertEquals(null, converter.convertToModel("", BigDecimal.class, null)); + } + + public void testValueParsing() { + BigDecimal converted = converter.convertToModel("10", BigDecimal.class, + null); + BigDecimal expected = new BigDecimal(10); + assertEquals(expected, converted); + } + + public void testValueFormatting() { + BigDecimal bd = new BigDecimal(12.5); + String expected = "12,5"; + + String converted = converter.convertToPresentation(bd, String.class, + Locale.GERMAN); + assertEquals(expected, converted); + } +} diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToNumberConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToNumberConverter.java deleted file mode 100644 index 66fc4f6532..0000000000 --- a/server/tests/src/com/vaadin/tests/data/converter/TestStringToNumberConverter.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.vaadin.tests.data.converter; - -import junit.framework.TestCase; - -import com.vaadin.data.util.converter.StringToNumberConverter; - -public class TestStringToNumberConverter extends TestCase { - - StringToNumberConverter converter = new StringToNumberConverter(); - - public void testNullConversion() { - assertEquals(null, converter.convertToModel(null, Number.class, null)); - } - - public void testEmptyStringConversion() { - assertEquals(null, converter.convertToModel("", Number.class, null)); - } - - public void testValueConversion() { - assertEquals(Long.valueOf(10), - converter.convertToModel("10", Number.class, null)); - assertEquals(10.5, converter.convertToModel("10.5", Number.class, null)); - } -} diff --git a/server/tests/src/com/vaadin/tests/server/TestClassesSerializable.java b/server/tests/src/com/vaadin/tests/server/TestClassesSerializable.java index e5420b8921..e938a1cd37 100644 --- a/server/tests/src/com/vaadin/tests/server/TestClassesSerializable.java +++ b/server/tests/src/com/vaadin/tests/server/TestClassesSerializable.java @@ -42,6 +42,7 @@ public class TestClassesSerializable extends TestCase { "com\\.vaadin\\.event\\.FieldEvents", // "com\\.vaadin\\.event\\.LayoutEvents", // "com\\.vaadin\\.event\\.MouseEvents", // + "com\\.vaadin\\.event\\.UIEvents", // "com\\.vaadin\\.server\\.VaadinPortlet", // "com\\.vaadin\\.server\\.MockServletConfig", // "com\\.vaadin\\.server\\.MockServletContext", // @@ -49,6 +50,8 @@ public class TestClassesSerializable extends TestCase { "com\\.vaadin\\.server\\.communication\\.FileUploadHandler\\$SimpleMultiPartInputStream", // "com\\.vaadin\\.server\\.communication\\.PushRequestHandler.*", "com\\.vaadin\\.server\\.communication\\.PushHandler.*", // PushHandler + "com\\.vaadin\\.server\\.communication\\.DateSerializer", // + "com\\.vaadin\\.server\\.communication\\.JSONSerializer", // // and its inner classes do not need to be serializable "com\\.vaadin\\.util\\.SerializerHelper", // fully static // class level filtering, also affecting nested classes and @@ -60,6 +63,7 @@ public class TestClassesSerializable extends TestCase { "com\\.vaadin\\.sass.*", // "com\\.vaadin\\.testbench.*", // "com\\.vaadin\\.util\\.CurrentInstance\\$1", // + "com\\.vaadin\\.server\\.JsonCodec\\$1", // "com\\.vaadin\\.server\\.communication\\.PushConnection", // "com\\.vaadin\\.server\\.communication\\.AtmospherePushConnection", // "com\\.vaadin\\.util\\.ConnectorHelper", // diff --git a/server/tests/src/com/vaadin/tests/server/TestPropertyFormatter.java b/server/tests/src/com/vaadin/tests/server/TestPropertyFormatter.java index 48c60c83c4..b47a2953ce 100644 --- a/server/tests/src/com/vaadin/tests/server/TestPropertyFormatter.java +++ b/server/tests/src/com/vaadin/tests/server/TestPropertyFormatter.java @@ -26,7 +26,7 @@ public class TestPropertyFormatter extends TestCase { public Object parse(String formattedValue) throws Exception { return getExpectedClass().newInstance(); } - }; + } @SuppressWarnings("rawtypes") private Class expectedClass; diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValueConversions.java b/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValueConversions.java index a5e825bddb..85116dd152 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValueConversions.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstractfield/AbsFieldValueConversions.java @@ -205,14 +205,15 @@ public class AbsFieldValueConversions extends TestCase { } + // Now specific to Integer because StringToNumberConverter has been removed public static class NumberBean { - private Number number; + private Integer number; - public Number getNumber() { + public Integer getNumber() { return number; } - public void setNumber(Number number) { + public void setNumber(Integer number) { this.number = number; } @@ -239,7 +240,7 @@ public class AbsFieldValueConversions extends TestCase { tf.setPropertyDataSource(new MethodProperty<Number>(nb, "number")); Converter c2 = tf.getConverter(); assertTrue( - "StringToNumber converter is ok for integer types and should stay even though property is changed", + "StringToInteger converter is ok for integer types and should stay even though property is changed", c1 == c2); assertEquals(490, tf.getPropertyDataSource().getValue()); assertEquals("490", tf.getValue()); diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractfield/RemoveListenersOnDetach.java b/server/tests/src/com/vaadin/tests/server/component/abstractfield/RemoveListenersOnDetach.java index fcf17cc499..f391d15fb3 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstractfield/RemoveListenersOnDetach.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstractfield/RemoveListenersOnDetach.java @@ -56,12 +56,12 @@ public class RemoveListenersOnDetach { @Override public com.vaadin.ui.UI getUI() { return uI; - }; + } @Override public VaadinSession getSession() { return application; - }; + } }; Property<String> property = new AbstractProperty<String>() { diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/LayoutSettingsOnReplace.java b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/LayoutSettingsOnReplace.java new file mode 100644 index 0000000000..0af21d8cb8 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractorderedlayout/LayoutSettingsOnReplace.java @@ -0,0 +1,85 @@ +/* + * Copyright 2000-2013 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.abstractorderedlayout; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.AbstractOrderedLayout; +import com.vaadin.ui.Alignment; + +/** + * Tests for abstract layout settings which should be preserved on replace + * component + * + * @since 7.2 + * @author Vaadin Ltd + */ +public class LayoutSettingsOnReplace { + + @Test + public void testExpandRatio() { + AbstractOrderedLayout layout = new AbstractOrderedLayout() { + }; + + AbstractComponent first = new AbstractComponent() { + }; + AbstractComponent second = new AbstractComponent() { + }; + + layout.addComponent(first); + layout.addComponent(second); + + int ratio = 2; + layout.setExpandRatio(first, ratio); + layout.setExpandRatio(second, 1); + + AbstractComponent replace = new AbstractComponent() { + }; + layout.replaceComponent(first, replace); + + Assert.assertEquals("Expand ratio for replaced component is not " + + "the same as for previous one", ratio, + layout.getExpandRatio(replace), 0.0001); + } + + @Test + public void testAlignment() { + AbstractOrderedLayout layout = new AbstractOrderedLayout() { + }; + + AbstractComponent first = new AbstractComponent() { + }; + AbstractComponent second = new AbstractComponent() { + }; + + layout.addComponent(first); + layout.addComponent(second); + + Alignment alignment = Alignment.BOTTOM_RIGHT; + layout.setComponentAlignment(first, alignment); + layout.setComponentAlignment(second, Alignment.MIDDLE_CENTER); + + AbstractComponent replace = new AbstractComponent() { + }; + layout.replaceComponent(first, replace); + + Assert.assertEquals("Alignment for replaced component is not " + + "the same as for previous one", alignment, + layout.getComponentAlignment(replace)); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractselect/OptionGroupTests.java b/server/tests/src/com/vaadin/tests/server/component/abstractselect/OptionGroupTests.java new file mode 100644 index 0000000000..c4a3d73652 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractselect/OptionGroupTests.java @@ -0,0 +1,32 @@ +package com.vaadin.tests.server.component.abstractselect; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Collection; + +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.ui.OptionGroup; + +public class OptionGroupTests { + + private OptionGroup optionGroup; + + @Before + public void setup() { + optionGroup = new OptionGroup(); + } + + @Test + public void itemsAreAdded() { + optionGroup.addItems("foo", "bar"); + + Collection<?> itemIds = optionGroup.getItemIds(); + + assertEquals(2, itemIds.size()); + assertTrue(itemIds.contains("foo")); + assertTrue(itemIds.contains("bar")); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractsinglecomponentcontainer/TestRemoveFromParentLocking.java b/server/tests/src/com/vaadin/tests/server/component/abstractsinglecomponentcontainer/TestRemoveFromParentLocking.java new file mode 100644 index 0000000000..26443ead2b --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/abstractsinglecomponentcontainer/TestRemoveFromParentLocking.java @@ -0,0 +1,125 @@ +/* + * Copyright 2000-2013 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.abstractsinglecomponentcontainer; + +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.server.VaadinSession; +import com.vaadin.ui.UI; +import com.vaadin.ui.VerticalLayout; + +public class TestRemoveFromParentLocking { + + private static VerticalLayout createTestComponent() { + VaadinSession session = new VaadinSession(null) { + private final ReentrantLock lock = new ReentrantLock(); + + @Override + public Lock getLockInstance() { + return lock; + } + }; + + session.getLockInstance().lock(); + + UI ui = new UI() { + @Override + protected void init(VaadinRequest request) { + } + }; + ui.setSession(session); + + VerticalLayout layout = new VerticalLayout(); + ui.setContent(layout); + + session.getLockInstance().unlock(); + return layout; + } + + @Test + public void attachNoSessionLocked() { + VerticalLayout testComponent = createTestComponent(); + + VerticalLayout target = new VerticalLayout(); + + try { + target.addComponent(testComponent); + throw new AssertionError( + "Moving component when not holding its sessions's lock should throw"); + } catch (IllegalStateException e) { + Assert.assertEquals( + "Cannot remove from parent when the session is not locked.", + e.getMessage()); + } + } + + @Test + public void attachSessionLocked() { + VerticalLayout testComponent = createTestComponent(); + + VerticalLayout target = new VerticalLayout(); + + testComponent.getUI().getSession().getLockInstance().lock(); + + target.addComponent(testComponent); + // OK if we get here without any exception + } + + @Test + public void crossAttachOtherSessionLocked() { + VerticalLayout notLockedComponent = createTestComponent(); + + VerticalLayout lockedComponent = createTestComponent(); + + // Simulate the situation when attaching cross sessions + lockedComponent.getUI().getSession().getLockInstance().lock(); + VaadinSession.setCurrent(lockedComponent.getUI().getSession()); + + try { + lockedComponent.addComponent(notLockedComponent); + throw new AssertionError( + "Moving component when not holding its sessions's lock should throw"); + } catch (IllegalStateException e) { + Assert.assertEquals( + "Cannot remove from parent when the session is not locked." + + " Furthermore, there is another locked session, indicating that the component might be about to be moved from one session to another.", + e.getMessage()); + } + } + + @Test + public void crossAttachThisSessionLocked() { + VerticalLayout notLockedComponent = createTestComponent(); + + VerticalLayout lockedComponent = createTestComponent(); + + // Simulate the situation when attaching cross sessions + lockedComponent.getUI().getSession().getLockInstance().lock(); + VaadinSession.setCurrent(lockedComponent.getUI().getSession()); + + try { + notLockedComponent.addComponent(lockedComponent); + } catch (AssertionError e) { + // All is fine, don't care about the exact wording in this case + } + } + +} diff --git a/server/tests/src/com/vaadin/tests/server/component/fieldgroup/BeanFieldGroupTest.java b/server/tests/src/com/vaadin/tests/server/component/fieldgroup/BeanFieldGroupTest.java index 44b77e88e2..112d36d884 100644 --- a/server/tests/src/com/vaadin/tests/server/component/fieldgroup/BeanFieldGroupTest.java +++ b/server/tests/src/com/vaadin/tests/server/component/fieldgroup/BeanFieldGroupTest.java @@ -2,19 +2,60 @@ package com.vaadin.tests.server.component.fieldgroup; import static org.junit.Assert.assertEquals; +import org.junit.Assert; import org.junit.Test; import com.vaadin.data.fieldgroup.BeanFieldGroup; +import com.vaadin.data.fieldgroup.FieldGroup.CommitException; +import com.vaadin.data.fieldgroup.PropertyId; +import com.vaadin.ui.Field; +import com.vaadin.ui.TextField; public class BeanFieldGroupTest { + private static final String DEFAULT_FOR_BASIC_FIELD = "default"; + public static class MyBean { + private String basicField = DEFAULT_FOR_BASIC_FIELD; + + private String anotherField; + private MyNestedBean nestedBean = new MyNestedBean(); public MyNestedBean getNestedBean() { return nestedBean; } + + /** + * @return the basicField + */ + public String getBasicField() { + return basicField; + } + + /** + * @param basicField + * the basicField to set + */ + public void setBasicField(String basicField) { + this.basicField = basicField; + } + + /** + * @return the anotherField + */ + public String getAnotherField() { + return anotherField; + } + + /** + * @param anotherField + * the anotherField to set + */ + public void setAnotherField(String anotherField) { + this.anotherField = anotherField; + } } public static class MyNestedBean { @@ -26,6 +67,59 @@ public class BeanFieldGroupTest { } } + public static class ViewStub { + + TextField basicField = new TextField(); + + @PropertyId("anotherField") + TextField boundWithAnnotation = new TextField(); + } + + @SuppressWarnings("unchecked") + @Test + public void testStaticBindingHelper() { + MyBean myBean = new MyBean(); + + ViewStub viewStub = new ViewStub(); + BeanFieldGroup<MyBean> bindFields = BeanFieldGroup + .bindFieldsUnbuffered(myBean, viewStub); + + Field<String> field = (Field<String>) bindFields.getField("basicField"); + Assert.assertEquals(DEFAULT_FOR_BASIC_FIELD, myBean.basicField); + field.setValue("Foo"); + Assert.assertEquals("Foo", myBean.basicField); + + field = (Field<String>) bindFields.getField("anotherField"); + field.setValue("Foo"); + Assert.assertEquals("Foo", myBean.anotherField); + } + + @SuppressWarnings("unchecked") + @Test + public void testStaticBufferedBindingHelper() throws CommitException { + MyBean myBean = new MyBean(); + + ViewStub viewStub = new ViewStub(); + BeanFieldGroup<MyBean> bindFields = BeanFieldGroup.bindFieldsBuffered( + myBean, viewStub); + + Field<String> basicField = (Field<String>) bindFields + .getField("basicField"); + basicField.setValue("Foo"); + Assert.assertEquals(DEFAULT_FOR_BASIC_FIELD, myBean.basicField); + + Field<String> anotherField = (Field<String>) bindFields + .getField("anotherField"); + anotherField.setValue("Foo"); + Assert.assertNull(myBean.anotherField); + + bindFields.commit(); + + Assert.assertEquals("Foo", myBean.basicField); + Assert.assertEquals("Foo", myBean.anotherField); + + } + @Test public void buildAndBindNestedProperty() { diff --git a/server/tests/src/com/vaadin/tests/server/component/label/LabelListeners.java b/server/tests/src/com/vaadin/tests/server/component/label/LabelListeners.java index 3ed79f5010..9bb4c53ba2 100644 --- a/server/tests/src/com/vaadin/tests/server/component/label/LabelListeners.java +++ b/server/tests/src/com/vaadin/tests/server/component/label/LabelListeners.java @@ -1,5 +1,14 @@ 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 com.vaadin.data.Property; import com.vaadin.data.Property.ValueChangeListener; import com.vaadin.tests.server.component.AbstractListenerMethodsTest; import com.vaadin.ui.Label; @@ -10,4 +19,74 @@ public class LabelListeners extends AbstractListenerMethodsTest { testListenerAddGetRemove(Label.class, ValueChangeEvent.class, ValueChangeListener.class); } + + public void testValueChangeFiredWhenSettingValue() { + Label underTest = new Label(); + + // setup the mock listener + ValueChangeListener mockListener = createStrictMock(ValueChangeListener.class); + // record + mockListener + .valueChange(anyObject(com.vaadin.data.Property.ValueChangeEvent.class)); + + // test + underTest.addValueChangeListener(mockListener); + + replay(mockListener); + underTest.setValue("A new value"); + + verify(mockListener); + + } + + 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.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); + + } + + 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); + } + + 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/server/tests/src/com/vaadin/tests/server/component/window/AttachDetachWindow.java b/server/tests/src/com/vaadin/tests/server/component/window/AttachDetachWindow.java index 485b17830f..51d396801d 100644 --- a/server/tests/src/com/vaadin/tests/server/component/window/AttachDetachWindow.java +++ b/server/tests/src/com/vaadin/tests/server/component/window/AttachDetachWindow.java @@ -31,7 +31,7 @@ public class AttachDetachWindow { private class TestWindow extends Window implements TestContainer { boolean windowAttachCalled = false; boolean windowDetachCalled = false; - private TestContent testContent = new TestContent();; + private TestContent testContent = new TestContent(); TestWindow() { setContent(testContent); @@ -110,7 +110,7 @@ public class AttachDetachWindow { private class TestUI extends UI implements TestContainer { boolean rootAttachCalled = false; boolean rootDetachCalled = false; - private TestContent testContent = new TestContent();; + private TestContent testContent = new TestContent(); public TestUI() { setContent(testContent); diff --git a/server/tests/src/com/vaadin/tests/server/component/window/WindowTest.java b/server/tests/src/com/vaadin/tests/server/component/window/WindowTest.java new file mode 100644 index 0000000000..2cd19ee153 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/window/WindowTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2013 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.window; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.ui.Button; +import com.vaadin.ui.Label; +import com.vaadin.ui.Window; + +public class WindowTest { + + public Window window; + + @Before + public void setup() { + window = new Window(); + } + + @Test + public void testAssistiveDescription() { + Label l1 = new Label("label 1"); + Button b2 = new Button("button 2"); + window.setAssistiveDescription(l1, b2); + + Assert.assertEquals(2, window.getAssistiveDescription().length); + Assert.assertEquals(l1, window.getAssistiveDescription()[0]); + Assert.assertEquals(b2, window.getAssistiveDescription()[1]); + + // Modifying return value must not change actual value + window.getAssistiveDescription()[0] = null; + + Assert.assertEquals(2, window.getAssistiveDescription().length); + Assert.assertEquals(l1, window.getAssistiveDescription()[0]); + Assert.assertEquals(b2, window.getAssistiveDescription()[1]); + + } +} diff --git a/server/tests/src/com/vaadin/tests/server/validation/TestBeanValidation.java b/server/tests/src/com/vaadin/tests/server/validation/TestBeanValidation.java index e1d08a989b..832ae14950 100644 --- a/server/tests/src/com/vaadin/tests/server/validation/TestBeanValidation.java +++ b/server/tests/src/com/vaadin/tests/server/validation/TestBeanValidation.java @@ -1,7 +1,6 @@ package com.vaadin.tests.server.validation; -import junit.framework.Assert; - +import org.junit.Assert; import org.junit.Test; import com.vaadin.data.Validator.InvalidValueException; @@ -59,6 +58,33 @@ public class TestBeanValidation { } @Test + public void testBeanValidationException_OneValidationError() { + InvalidValueException[] causes = null; + BeanValidator validator = new BeanValidator(BeanToValidate.class, + "lastname"); + try { + validator.validate(null); + } catch (InvalidValueException e) { + causes = e.getCauses(); + } + + Assert.assertEquals(1, causes.length); + } + + @Test + public void testBeanValidationsException_TwoValidationErrors() { + InvalidValueException[] causes = null; + BeanValidator validator = new BeanValidator(BeanToValidate.class, + "nickname"); + try { + validator.validate("A"); + } catch (InvalidValueException e) { + causes = e.getCauses(); + } + + Assert.assertEquals(2, causes.length); + } + public void testBeanValidationNotAddedTwice() { // See ticket #11045 BeanFieldGroup<BeanToValidate> fieldGroup = new BeanFieldGroup<BeanToValidate>( diff --git a/server/tests/src/com/vaadin/ui/AbsFieldDataSourceLocaleChange.java b/server/tests/src/com/vaadin/ui/AbsFieldDataSourceLocaleChange.java index 55d5771f80..ff615e3d91 100644 --- a/server/tests/src/com/vaadin/ui/AbsFieldDataSourceLocaleChange.java +++ b/server/tests/src/com/vaadin/ui/AbsFieldDataSourceLocaleChange.java @@ -37,7 +37,6 @@ public class AbsFieldDataSourceLocaleChange { public void localeChangesOnAttach() { TextField tf = new TextField(); - ; tf.setConverter(new StringToIntegerConverter() { @Override protected NumberFormat getFormat(Locale locale) { diff --git a/server/tests/src/com/vaadin/ui/UIInitRefreshTest.java b/server/tests/src/com/vaadin/ui/UIInitRefreshTest.java new file mode 100644 index 0000000000..a807e4b656 --- /dev/null +++ b/server/tests/src/com/vaadin/ui/UIInitRefreshTest.java @@ -0,0 +1,116 @@ +/* + * Copyright 2000-2013 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.ui; + +import org.easymock.EasyMock; +import org.easymock.IMocksControl; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.server.Page.BrowserWindowResizeEvent; +import com.vaadin.server.Page.BrowserWindowResizeListener; +import com.vaadin.server.Page.UriFragmentChangedEvent; +import com.vaadin.server.Page.UriFragmentChangedListener; +import com.vaadin.server.VaadinRequest; + +public class UIInitRefreshTest { + + private boolean initCalled; + private boolean refreshCalled; + private boolean fragmentChangeCalled; + private boolean browserWindowResizeCalled; + + private class TestUI extends UI implements UriFragmentChangedListener, + BrowserWindowResizeListener { + @Override + protected void init(VaadinRequest request) { + getPage().addBrowserWindowResizeListener(this); + getPage().addUriFragmentChangedListener(this); + + initCalled = true; + + Assert.assertEquals("foo", getPage().getUriFragment()); + Assert.assertEquals(100, getPage().getBrowserWindowWidth()); + Assert.assertEquals(100, getPage().getBrowserWindowHeight()); + + Assert.assertFalse(fragmentChangeCalled); + Assert.assertFalse(browserWindowResizeCalled); + } + + @Override + protected void refresh(VaadinRequest request) { + refreshCalled = true; + + Assert.assertEquals("bar", getPage().getUriFragment()); + Assert.assertEquals(200, getPage().getBrowserWindowWidth()); + Assert.assertEquals(200, getPage().getBrowserWindowHeight()); + + Assert.assertFalse(fragmentChangeCalled); + Assert.assertFalse(browserWindowResizeCalled); + } + + @Override + public void browserWindowResized(BrowserWindowResizeEvent event) { + Assert.assertEquals(200, event.getWidth()); + Assert.assertEquals(200, event.getHeight()); + browserWindowResizeCalled = true; + } + + @Override + public void uriFragmentChanged(UriFragmentChangedEvent event) { + Assert.assertEquals("bar", event.getUriFragment()); + fragmentChangeCalled = true; + } + }; + + @Before + public void setUp() { + initCalled = refreshCalled = fragmentChangeCalled = browserWindowResizeCalled = false; + } + + @Test + public void testListenersCalled() { + IMocksControl control = EasyMock.createNiceControl(); + + VaadinRequest initRequest = control.createMock(VaadinRequest.class); + EasyMock.expect(initRequest.getParameter("v-loc")).andReturn( + "http://example.com/#foo"); + EasyMock.expect(initRequest.getParameter("v-cw")).andReturn("100"); + EasyMock.expect(initRequest.getParameter("v-ch")).andReturn("100"); + + VaadinRequest reinitRequest = control.createMock(VaadinRequest.class); + EasyMock.expect(reinitRequest.getParameter("v-loc")).andReturn( + "http://example.com/#bar"); + EasyMock.expect(reinitRequest.getParameter("v-cw")).andReturn("200"); + EasyMock.expect(reinitRequest.getParameter("v-ch")).andReturn("200"); + + control.replay(); + + UI ui = new TestUI(); + ui.doInit(initRequest, 0, ""); + + Assert.assertTrue(initCalled); + Assert.assertFalse(fragmentChangeCalled); + Assert.assertFalse(browserWindowResizeCalled); + + ui.doRefresh(reinitRequest); + + Assert.assertTrue(refreshCalled); + Assert.assertTrue(fragmentChangeCalled); + Assert.assertTrue(browserWindowResizeCalled); + } +} diff --git a/server/tests/src/com/vaadin/util/TestCurrentInstance.java b/server/tests/src/com/vaadin/util/TestCurrentInstance.java index a78784d88e..666a0ef019 100644 --- a/server/tests/src/com/vaadin/util/TestCurrentInstance.java +++ b/server/tests/src/com/vaadin/util/TestCurrentInstance.java @@ -70,7 +70,7 @@ public class TestCurrentInstance { Assert.assertEquals(TestCurrentInstance.this, CurrentInstance.get(TestCurrentInstance.class)); threadFailed.set(false); - }; + } }; t.start(); CurrentInstance.set(TestCurrentInstance.class, null); @@ -103,7 +103,7 @@ public class TestCurrentInstance { } catch (Exception e) { e.printStackTrace(); } - }; + } }; t.start(); |