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.

DeclarativeTestUI.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.components;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.lang.annotation.ElementType;
  20. import java.lang.annotation.Retention;
  21. import java.lang.annotation.RetentionPolicy;
  22. import java.lang.annotation.Target;
  23. import java.lang.reflect.InvocationTargetException;
  24. import java.lang.reflect.Method;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import com.vaadin.server.VaadinRequest;
  28. import com.vaadin.ui.Component;
  29. import com.vaadin.ui.declarative.Design;
  30. /**
  31. * Declarative test UI. Provides simple instantiation of HTML designs located
  32. * under {@code uitest/src}. Also provides {@link OnLoad} annotation that lets
  33. * you easily hook up methods to run after the UI has been created. Note: you
  34. * <i>must</i> add the {@link DeclarativeUI} annotation to your subclass; not
  35. * doing this will result in program failure.
  36. */
  37. @SuppressWarnings("serial")
  38. public class DeclarativeTestUI extends AbstractTestUI {
  39. private Logger logger;
  40. private Component component;
  41. /**
  42. * Class marker indicating the design .html file to load
  43. */
  44. @Retention(RetentionPolicy.RUNTIME)
  45. @Target(ElementType.TYPE)
  46. public static @interface DeclarativeUI {
  47. String value();
  48. /**
  49. * Set this property to true if you provide an absolute path to your
  50. * design; otherwise, the DeclarativeTestUI logic will look for the HTML
  51. * design file under {@code vaadin_project/uitest/src/<package path>/}.
  52. */
  53. boolean absolutePath() default false;
  54. }
  55. /**
  56. * Method marker interface indicating that a method should be run after the
  57. * declarative UI has been created
  58. */
  59. @Retention(RetentionPolicy.RUNTIME)
  60. @Target(ElementType.METHOD)
  61. public static @interface OnLoad {
  62. }
  63. /**
  64. * Figure out the proper path for the HTML design file
  65. */
  66. private String getDesignPath() {
  67. Class<?> clazz = getClass();
  68. String designFilePath = null;
  69. if (clazz.getAnnotation(DeclarativeUI.class).absolutePath()) {
  70. designFilePath = "";
  71. } else {
  72. // This is rather nasty.. but it works well enough for now.
  73. String userDir = System.getProperty("user.dir");
  74. designFilePath = userDir + "/uitest/src/"
  75. + clazz.getPackage().getName().replace('.', '/') + "/";
  76. }
  77. String designFileName = clazz.getAnnotation(DeclarativeUI.class)
  78. .value();
  79. return designFilePath + designFileName;
  80. }
  81. private Component readDesign() throws Exception {
  82. String path = getDesignPath();
  83. getLogger().log(Level.INFO, "Reading design from " + path);
  84. File file = new File(path);
  85. return Design.read(new FileInputStream(file));
  86. }
  87. @Override
  88. protected void setup(VaadinRequest request) {
  89. Class<?> clazz = getClass();
  90. if (clazz.isAnnotationPresent(DeclarativeUI.class)) {
  91. // Create component
  92. try {
  93. component = readDesign();
  94. } catch (Exception e1) {
  95. getLogger().log(Level.SEVERE, "Error reading design", e1);
  96. return;
  97. }
  98. addComponent(component);
  99. // Call on-load methods (if applicable)
  100. Method[] methods = clazz.getMethods();
  101. for (Method m : methods) {
  102. if (m.isAnnotationPresent(OnLoad.class)) {
  103. try {
  104. m.invoke(this, (Object[]) null);
  105. } catch (IllegalAccessException e) {
  106. getLogger().log(Level.SEVERE,
  107. "Error invoking @OnLoad method", e);
  108. return;
  109. } catch (IllegalArgumentException e) {
  110. getLogger().log(Level.SEVERE,
  111. "Error invoking @OnLoad method", e);
  112. return;
  113. } catch (InvocationTargetException e) {
  114. getLogger().log(Level.SEVERE,
  115. "Error invoking @OnLoad method", e);
  116. return;
  117. }
  118. }
  119. }
  120. } else {
  121. throw new IllegalStateException(
  122. "Cannot find declarative UI annotation");
  123. }
  124. }
  125. /**
  126. * Get access to the declaratively created component. This method typecasts
  127. * the component to the receiving type; if there's a mismatch between what
  128. * you expect and what's written in the design, this will fail with a
  129. * ClassCastException.
  130. *
  131. * @return a Vaadin component
  132. */
  133. @SuppressWarnings("unchecked")
  134. public <T extends Component> T getComponent() {
  135. try {
  136. return (T) component;
  137. } catch (ClassCastException ex) {
  138. getLogger().log(Level.SEVERE, "Component code/design type mismatch",
  139. ex);
  140. }
  141. return null;
  142. }
  143. /**
  144. * Get access to the logger of this class
  145. *
  146. * @return a Logger instance
  147. */
  148. protected Logger getLogger() {
  149. if (logger == null) {
  150. logger = Logger.getLogger(getClass().getName());
  151. }
  152. return logger;
  153. }
  154. }