* Added a test, also renamed tests/server/component/root to tests/server/component/ui
Change-Id: I80d30dd5acb3643cfb3cfc0b972f52ffa4512691
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);
}
/**
+++ /dev/null
-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();
- }
- };
- }
-}
--- /dev/null
+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();
+ }
+ };
+ }
+}
--- /dev/null
+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());
+ }
+}