diff options
author | Leif Åstrand <leif@vaadin.com> | 2012-03-27 12:53:03 +0300 |
---|---|---|
committer | Leif Åstrand <leif@vaadin.com> | 2012-03-27 12:53:03 +0300 |
commit | df7b86614735afb02fe1a142f096ae0191a34ec8 (patch) | |
tree | 66608a0417f38bb3eb734223e5f91dbae0a44e45 /tests/server-side | |
parent | 0030463b2784ca2e919adab013cf5efc3f07b7e9 (diff) | |
download | vaadin-framework-df7b86614735afb02fe1a142f096ae0191a34ec8.tar.gz vaadin-framework-df7b86614735afb02fe1a142f096ae0191a34ec8.zip |
Server side unit test for #8542
Diffstat (limited to 'tests/server-side')
-rw-r--r-- | tests/server-side/com/vaadin/tests/server/component/root/CustomRootClassLoader.java | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/tests/server-side/com/vaadin/tests/server/component/root/CustomRootClassLoader.java b/tests/server-side/com/vaadin/tests/server/component/root/CustomRootClassLoader.java new file mode 100644 index 0000000000..99b77b0d29 --- /dev/null +++ b/tests/server-side/com/vaadin/tests/server/component/root/CustomRootClassLoader.java @@ -0,0 +1,98 @@ +package com.vaadin.tests.server.component.root; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import junit.framework.TestCase; + +import com.vaadin.Application; +import com.vaadin.Application.ApplicationStartEvent; +import com.vaadin.RootRequiresMoreInformationException; +import com.vaadin.terminal.WrappedRequest; +import com.vaadin.ui.Root; + +public class CustomRootClassLoader extends TestCase { + + /** + * Stub root + */ + public static class MyRoot extends Root { + @Override + protected void init(WrappedRequest 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 Root class can be loaded even if no classloader has been + * provided. + * + * @throws Exception + * if thrown + */ + public void testWithNullClassLoader() throws Exception { + Application application = createStubApplication(); + application.start(new ApplicationStartEvent(null, new Properties(), + null, false, null)); + + Root root = application.getRootForRequest(null); + assertTrue(root instanceof MyRoot); + } + + /** + * Tests that the ClassLoader passed in the ApplicationStartEvent is used to + * load Root classes. + * + * @throws Exception + * if thrown + */ + public void testWithClassLoader() throws Exception { + LoggingClassLoader loggingClassLoader = new LoggingClassLoader(); + + Application application = createStubApplication(); + application.start(new ApplicationStartEvent(null, new Properties(), + null, false, loggingClassLoader)); + + Root root = application.getRootForRequest(null); + assertTrue(root instanceof MyRoot); + assertEquals(1, loggingClassLoader.requestedClasses.size()); + assertEquals(MyRoot.class.getName(), + loggingClassLoader.requestedClasses.get(0)); + + } + + private Application createStubApplication() { + return new Application() { + @Override + protected String getRootClassName(WrappedRequest request) { + // Always use the same root class + return MyRoot.class.getName(); + } + + @Override + public Root getRootForRequest(WrappedRequest request) + throws RootRequiresMoreInformationException { + // Always create a new root for testing (can't directly use + // getRoot as it's protected) + return getRoot(request); + } + }; + } +} |