]> source.dussan.org Git - vaadin-framework.git/commitdiff
Using ComponentFactory in DesignContext.getDefaultInstance() (#16990)
authorAlexey Fansky <alexey.fansky@effective-soft.com>
Wed, 4 Mar 2015 21:23:42 +0000 (13:23 -0800)
committerVaadin Code Review <review@vaadin.com>
Tue, 10 Mar 2015 09:11:45 +0000 (09:11 +0000)
Change-Id: I0bb3e7975f2b48cdc589de740cb07ac893b13461

server/src/com/vaadin/ui/declarative/DesignContext.java
server/tests/src/com/vaadin/tests/design/ComponentFactoryTest.java

index 09fefd0a6be6cc4c494836c4f1dabf246b0e1c50..218774c72d15904798a063ae22c2055c01aab50e 100644 (file)
@@ -278,16 +278,8 @@ public class DesignContext implements Serializable {
             Class<? extends Component> componentClass) {
         Component instance = instanceCache.get(componentClass);
         if (instance == null) {
-            try {
-                instance = componentClass.newInstance();
-                instanceCache.put(componentClass, instance);
-            } catch (InstantiationException e) {
-                throw new RuntimeException("Could not instantiate "
-                        + componentClass.getName());
-            } catch (IllegalAccessException e) {
-                throw new RuntimeException("Could not instantiate "
-                        + componentClass.getName());
-            }
+            instance = instantiateClass(componentClass.getName());
+            instanceCache.put(componentClass, instance);
         }
         return instance;
     }
@@ -484,6 +476,15 @@ public class DesignContext implements Serializable {
         // Extract the package and class names.
         String qualifiedClassName = tagNameToClassName(node);
 
+        return instantiateClass(qualifiedClassName);
+    }
+
+    /**
+     * Instantiates given class via ComponentFactory.
+     * @param qualifiedClassName class name to instantiate
+     * @return instance of a given class
+     */
+    private Component instantiateClass(String qualifiedClassName) {
         ComponentFactory factory = Design.getComponentFactory();
         Component component = factory.createComponent(qualifiedClassName, this);
 
index a5f1d288a2daaff26f9b4b4af62ef991c31301b7..4115872fb7bd2b6c8cde6cf3b57752148e062ab5 100644 (file)
@@ -19,6 +19,8 @@ import java.io.ByteArrayInputStream;
 import java.util.ArrayList;
 import java.util.List;
 
+import com.vaadin.ui.AbstractComponent;
+import com.vaadin.ui.TextField;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Test;
@@ -109,9 +111,32 @@ public class ComponentFactoryTest {
         Design.read(new ByteArrayInputStream("<v-label />".getBytes()));
     }
 
+    @Test
+    public void testGetDefaultInstanceUsesComponentFactory() {
+        final List<String> classes = new ArrayList<String>();
+        currentComponentFactory.set(new ComponentFactory() {
+            @Override
+            public Component createComponent(String fullyQualifiedClassName,
+                                             DesignContext context) {
+                classes.add(fullyQualifiedClassName);
+                return defaultFactory.createComponent(fullyQualifiedClassName,
+                        context);
+            }
+        });
+
+        DesignContext designContext = new DesignContext();
+        designContext.getDefaultInstance(new DefaultInstanceTestComponent());
+
+        Assert.assertEquals("There should be one class requests", 1, classes.size());
+        Assert.assertEquals("First class should be DefaultInstanceTestComponent",
+                DefaultInstanceTestComponent.class.getName(), classes.get(0));
+    }
+
     @After
     public void cleanup() {
         currentComponentFactory.remove();
     }
 
+    public static class DefaultInstanceTestComponent extends AbstractComponent {
+    }
 }