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;
}
// 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);
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;
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 {
+ }
}