You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ComponentFactoryTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.design;
  17. import java.io.ByteArrayInputStream;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import org.junit.After;
  21. import org.junit.Assert;
  22. import org.junit.Test;
  23. import com.vaadin.ui.AbstractComponent;
  24. import com.vaadin.ui.Label;
  25. import com.vaadin.ui.declarative.Design;
  26. import com.vaadin.ui.declarative.Design.ComponentFactory;
  27. import com.vaadin.ui.declarative.DesignContext;
  28. import com.vaadin.ui.declarative.DesignException;
  29. public class ComponentFactoryTest {
  30. private static final ComponentFactory defaultFactory = Design
  31. .getComponentFactory();
  32. private static final ThreadLocal<ComponentFactory> currentComponentFactory = new ThreadLocal<>();
  33. // Set static component factory that delegate to a thread local factory
  34. static {
  35. Design.setComponentFactory(
  36. (String fullyQualifiedClassName, DesignContext context) -> {
  37. ComponentFactory componentFactory = currentComponentFactory
  38. .get();
  39. if (componentFactory == null) {
  40. componentFactory = defaultFactory;
  41. }
  42. return componentFactory
  43. .createComponent(fullyQualifiedClassName, context);
  44. });
  45. }
  46. @Test(expected = IllegalArgumentException.class)
  47. public void testSetNullComponentFactory() {
  48. Design.setComponentFactory(null);
  49. }
  50. @Test
  51. public void testComponentFactoryLogging() {
  52. final List<String> messages = new ArrayList<>();
  53. currentComponentFactory
  54. .set((ComponentFactory) (String fullyQualifiedClassName,
  55. DesignContext context) -> {
  56. messages.add("Requested class " + fullyQualifiedClassName);
  57. return defaultFactory
  58. .createComponent(fullyQualifiedClassName, context);
  59. });
  60. Design.read(new ByteArrayInputStream("<vaadin-label />".getBytes()));
  61. Assert.assertEquals("There should be one message logged", 1,
  62. messages.size());
  63. Assert.assertEquals("Requested class " + Label.class.getCanonicalName(),
  64. messages.get(0));
  65. }
  66. @Test(expected = DesignException.class)
  67. public void testComponentFactoryReturningNull() {
  68. currentComponentFactory
  69. .set((ComponentFactory) (String fullyQualifiedClassName,
  70. DesignContext context) -> null);
  71. Design.read(new ByteArrayInputStream("<vaadin-label />".getBytes()));
  72. }
  73. @Test(expected = DesignException.class)
  74. public void testComponentFactoryThrowingStuff() {
  75. currentComponentFactory.set((ComponentFactory) (
  76. String fullyQualifiedClassName,
  77. // Will throw because class is not found
  78. DesignContext context) -> defaultFactory.createComponent(
  79. "foobar." + fullyQualifiedClassName, context));
  80. Design.read(new ByteArrayInputStream("<vaadin-label />".getBytes()));
  81. }
  82. @Test
  83. public void testGetDefaultInstanceUsesComponentFactory() {
  84. final List<String> classes = new ArrayList<>();
  85. currentComponentFactory
  86. .set((ComponentFactory) (String fullyQualifiedClassName,
  87. DesignContext context) -> {
  88. classes.add(fullyQualifiedClassName);
  89. return defaultFactory
  90. .createComponent(fullyQualifiedClassName, context);
  91. });
  92. DesignContext designContext = new DesignContext();
  93. designContext.getDefaultInstance(new DefaultInstanceTestComponent());
  94. Assert.assertEquals("There should be one class requests", 1,
  95. classes.size());
  96. Assert.assertEquals(
  97. "First class should be DefaultInstanceTestComponent",
  98. DefaultInstanceTestComponent.class.getName(), classes.get(0));
  99. }
  100. @After
  101. public void cleanup() {
  102. currentComponentFactory.remove();
  103. }
  104. public static class DefaultInstanceTestComponent extends AbstractComponent {
  105. }
  106. }