Change-Id: I0bb3e7975f2b48cdc589de740cb07ac893b13461tags/7.5.0.alpha1
@@ -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); | |||
@@ -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 { | |||
} | |||
} |