From: Johannes Dahlström Date: Tue, 16 Oct 2012 08:13:35 +0000 (+0300) Subject: Delegate UI.replaceComponent() to content instead of throwing UOE (#9967) X-Git-Tag: 7.0.0.beta6~38 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=refs%2Fchanges%2F25%2F125%2F3;p=vaadin-framework.git Delegate UI.replaceComponent() to content instead of throwing UOE (#9967) * Added a test, also renamed tests/server/component/root to tests/server/component/ui Change-Id: I80d30dd5acb3643cfb3cfc0b972f52ffa4512691 --- diff --git a/server/src/com/vaadin/ui/UI.java b/server/src/com/vaadin/ui/UI.java index 31a7446e96..2878d10fa1 100644 --- a/server/src/com/vaadin/ui/UI.java +++ b/server/src/com/vaadin/ui/UI.java @@ -556,9 +556,13 @@ public abstract class UI extends AbstractComponentContainer implements return this; } + /** + * This implementation replaces a component in the content container ( + * {@link #getContent()}) instead of in the actual UI. + */ @Override public void replaceComponent(Component oldComponent, Component newComponent) { - throw new UnsupportedOperationException(); + getContent().replaceComponent(oldComponent, newComponent); } /** diff --git a/server/tests/src/com/vaadin/tests/server/component/root/CustomUIClassLoader.java b/server/tests/src/com/vaadin/tests/server/component/root/CustomUIClassLoader.java deleted file mode 100644 index 405d3d1931..0000000000 --- a/server/tests/src/com/vaadin/tests/server/component/root/CustomUIClassLoader.java +++ /dev/null @@ -1,122 +0,0 @@ -package com.vaadin.tests.server.component.root; - -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; - -import junit.framework.TestCase; - -import org.easymock.EasyMock; - -import com.vaadin.DefaultDeploymentConfiguration; -import com.vaadin.server.DefaultUIProvider; -import com.vaadin.server.DeploymentConfiguration; -import com.vaadin.server.UIClassSelectionEvent; -import com.vaadin.server.VaadinRequest; -import com.vaadin.server.VaadinService; -import com.vaadin.server.VaadinServiceSession; -import com.vaadin.ui.UI; - -public class CustomUIClassLoader extends TestCase { - - /** - * Stub root - */ - public static class MyUI extends UI { - @Override - protected void init(VaadinRequest request) { - // Nothing to see here - } - } - - /** - * Dummy ClassLoader that just saves the name of the requested class before - * delegating to the default implementation. - */ - public class LoggingClassLoader extends ClassLoader { - - private List requestedClasses = new ArrayList(); - - @Override - protected synchronized Class loadClass(String name, boolean resolve) - throws ClassNotFoundException { - requestedClasses.add(name); - return super.loadClass(name, resolve); - } - } - - /** - * Tests that a UI class can be loaded even if no classloader has been - * provided. - * - * @throws Exception - * if thrown - */ - public void testWithNullClassLoader() throws Exception { - VaadinServiceSession application = createStubApplication(); - application.setConfiguration(createConfigurationMock()); - - DefaultUIProvider uiProvider = new DefaultUIProvider(); - Class uiClass = uiProvider - .getUIClass(new UIClassSelectionEvent(createRequestMock(null))); - - assertEquals(MyUI.class, uiClass); - } - - private static DeploymentConfiguration createConfigurationMock() { - Properties properties = new Properties(); - properties.put(VaadinServiceSession.UI_PARAMETER, MyUI.class.getName()); - return new DefaultDeploymentConfiguration(CustomUIClassLoader.class, - properties); - } - - private static VaadinRequest createRequestMock(ClassLoader classloader) { - // Mock a VaadinService to give the passed classloader - VaadinService configurationMock = EasyMock - .createMock(VaadinService.class); - EasyMock.expect(configurationMock.getDeploymentConfiguration()) - .andReturn(createConfigurationMock()); - EasyMock.expect(configurationMock.getClassLoader()).andReturn( - classloader); - - // Mock a VaadinRequest to give the mocked vaadin service - VaadinRequest requestMock = EasyMock.createMock(VaadinRequest.class); - EasyMock.expect(requestMock.getService()).andReturn(configurationMock); - EasyMock.expect(requestMock.getService()).andReturn(configurationMock); - EasyMock.expect(requestMock.getService()).andReturn(configurationMock); - - EasyMock.replay(configurationMock, requestMock); - return requestMock; - } - - /** - * Tests that the ClassLoader passed in the ApplicationStartEvent is used to - * load UI classes. - * - * @throws Exception - * if thrown - */ - public void testWithClassLoader() throws Exception { - LoggingClassLoader loggingClassLoader = new LoggingClassLoader(); - - DefaultUIProvider uiProvider = new DefaultUIProvider(); - Class uiClass = uiProvider - .getUIClass(new UIClassSelectionEvent( - createRequestMock(loggingClassLoader))); - - assertEquals(MyUI.class, uiClass); - assertEquals(1, loggingClassLoader.requestedClasses.size()); - assertEquals(MyUI.class.getName(), - loggingClassLoader.requestedClasses.get(0)); - - } - - private VaadinServiceSession createStubApplication() { - return new VaadinServiceSession(null) { - @Override - public DeploymentConfiguration getConfiguration() { - return createConfigurationMock(); - } - }; - } -} diff --git a/server/tests/src/com/vaadin/tests/server/component/ui/CustomUIClassLoader.java b/server/tests/src/com/vaadin/tests/server/component/ui/CustomUIClassLoader.java new file mode 100644 index 0000000000..82b9944371 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/ui/CustomUIClassLoader.java @@ -0,0 +1,122 @@ +package com.vaadin.tests.server.component.ui; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import junit.framework.TestCase; + +import org.easymock.EasyMock; + +import com.vaadin.DefaultDeploymentConfiguration; +import com.vaadin.server.DefaultUIProvider; +import com.vaadin.server.DeploymentConfiguration; +import com.vaadin.server.UIClassSelectionEvent; +import com.vaadin.server.VaadinRequest; +import com.vaadin.server.VaadinService; +import com.vaadin.server.VaadinServiceSession; +import com.vaadin.ui.UI; + +public class CustomUIClassLoader extends TestCase { + + /** + * Stub root + */ + public static class MyUI extends UI { + @Override + protected void init(VaadinRequest request) { + // Nothing to see here + } + } + + /** + * Dummy ClassLoader that just saves the name of the requested class before + * delegating to the default implementation. + */ + public class LoggingClassLoader extends ClassLoader { + + private List requestedClasses = new ArrayList(); + + @Override + protected synchronized Class loadClass(String name, boolean resolve) + throws ClassNotFoundException { + requestedClasses.add(name); + return super.loadClass(name, resolve); + } + } + + /** + * Tests that a UI class can be loaded even if no classloader has been + * provided. + * + * @throws Exception + * if thrown + */ + public void testWithNullClassLoader() throws Exception { + VaadinServiceSession application = createStubApplication(); + application.setConfiguration(createConfigurationMock()); + + DefaultUIProvider uiProvider = new DefaultUIProvider(); + Class uiClass = uiProvider + .getUIClass(new UIClassSelectionEvent(createRequestMock(null))); + + assertEquals(MyUI.class, uiClass); + } + + private static DeploymentConfiguration createConfigurationMock() { + Properties properties = new Properties(); + properties.put(VaadinServiceSession.UI_PARAMETER, MyUI.class.getName()); + return new DefaultDeploymentConfiguration(CustomUIClassLoader.class, + properties); + } + + private static VaadinRequest createRequestMock(ClassLoader classloader) { + // Mock a VaadinService to give the passed classloader + VaadinService configurationMock = EasyMock + .createMock(VaadinService.class); + EasyMock.expect(configurationMock.getDeploymentConfiguration()) + .andReturn(createConfigurationMock()); + EasyMock.expect(configurationMock.getClassLoader()).andReturn( + classloader); + + // Mock a VaadinRequest to give the mocked vaadin service + VaadinRequest requestMock = EasyMock.createMock(VaadinRequest.class); + EasyMock.expect(requestMock.getService()).andReturn(configurationMock); + EasyMock.expect(requestMock.getService()).andReturn(configurationMock); + EasyMock.expect(requestMock.getService()).andReturn(configurationMock); + + EasyMock.replay(configurationMock, requestMock); + return requestMock; + } + + /** + * Tests that the ClassLoader passed in the ApplicationStartEvent is used to + * load UI classes. + * + * @throws Exception + * if thrown + */ + public void testWithClassLoader() throws Exception { + LoggingClassLoader loggingClassLoader = new LoggingClassLoader(); + + DefaultUIProvider uiProvider = new DefaultUIProvider(); + Class uiClass = uiProvider + .getUIClass(new UIClassSelectionEvent( + createRequestMock(loggingClassLoader))); + + assertEquals(MyUI.class, uiClass); + assertEquals(1, loggingClassLoader.requestedClasses.size()); + assertEquals(MyUI.class.getName(), + loggingClassLoader.requestedClasses.get(0)); + + } + + private VaadinServiceSession createStubApplication() { + return new VaadinServiceSession(null) { + @Override + public DeploymentConfiguration getConfiguration() { + return createConfigurationMock(); + } + }; + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/ui/UIAddRemoveComponents.java b/server/tests/src/com/vaadin/tests/server/component/ui/UIAddRemoveComponents.java new file mode 100644 index 0000000000..1b07321605 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/ui/UIAddRemoveComponents.java @@ -0,0 +1,65 @@ +package com.vaadin.tests.server.component.ui; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; + +import org.junit.Test; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.ui.Component; +import com.vaadin.ui.Label; +import com.vaadin.ui.UI; + +public class UIAddRemoveComponents { + + private static class TestUI extends UI { + @Override + protected void init(VaadinRequest request) { + } + } + + @Test + public void addComponent() { + UI ui = new TestUI(); + Component c = new Label("abc"); + + ui.addComponent(c); + + assertSame(c, ui.iterator().next()); + assertSame(c, ui.getContent().iterator().next()); + assertEquals(1, ui.getComponentCount()); + assertEquals(1, ui.getContent().getComponentCount()); + } + + @Test + public void removeComponent() { + UI ui = new TestUI(); + Component c = new Label("abc"); + + ui.addComponent(c); + + ui.removeComponent(c); + + assertFalse(ui.iterator().hasNext()); + assertFalse(ui.getContent().iterator().hasNext()); + assertEquals(0, ui.getComponentCount()); + assertEquals(0, ui.getContent().getComponentCount()); + } + + @Test + public void replaceComponent() { + UI ui = new TestUI(); + Component c = new Label("abc"); + Component d = new Label("def"); + + ui.addComponent(c); + + ui.replaceComponent(c, d); + + assertSame(d, ui.iterator().next()); + assertSame(d, ui.getContent().iterator().next()); + assertEquals(1, ui.getComponentCount()); + assertEquals(1, ui.getContent().getComponentCount()); + } +}