]> source.dussan.org Git - vaadin-framework.git/commitdiff
Delegate UI.replaceComponent() to content instead of throwing UOE (#9967) 25/125/3
authorJohannes Dahlström <johannesd@vaadin.com>
Tue, 16 Oct 2012 08:13:35 +0000 (11:13 +0300)
committerVaadin Code Review <review@vaadin.com>
Tue, 16 Oct 2012 09:42:18 +0000 (09:42 +0000)
* Added a test, also renamed tests/server/component/root to tests/server/component/ui

Change-Id: I80d30dd5acb3643cfb3cfc0b972f52ffa4512691

server/src/com/vaadin/ui/UI.java
server/tests/src/com/vaadin/tests/server/component/root/CustomUIClassLoader.java [deleted file]
server/tests/src/com/vaadin/tests/server/component/ui/CustomUIClassLoader.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/component/ui/UIAddRemoveComponents.java [new file with mode: 0644]

index 31a7446e967be1d97e16248336249e72857e5a74..2878d10fa1890161b02227fbfcd02fc0de3b7812 100644 (file)
@@ -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 (file)
index 405d3d1..0000000
+++ /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<String> requestedClasses = new ArrayList<String>();
-
-        @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<? extends UI> 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<? extends UI> 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 (file)
index 0000000..82b9944
--- /dev/null
@@ -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<String> requestedClasses = new ArrayList<String>();
+
+        @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<? extends UI> 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<? extends UI> 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 (file)
index 0000000..1b07321
--- /dev/null
@@ -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());
+    }
+}