aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/main/java/com/vaadin/tests/components/DeclarativeTestUI.java
blob: b6fe78a77641433e1cb43fdd4bd03966f22a8907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.vaadin.tests.components;

import java.io.File;
import java.io.FileInputStream;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Component;
import com.vaadin.ui.declarative.Design;

/**
 * Declarative test UI. Provides simple instantiation of HTML designs located
 * under {@code uitest/src}. Also provides {@link OnLoad} annotation that lets
 * you easily hook up methods to run after the UI has been created. Note: you
 * <i>must</i> add the {@link DeclarativeUI} annotation to your subclass; not
 * doing this will result in program failure.
 */
@SuppressWarnings("serial")
public class DeclarativeTestUI extends AbstractTestUI {

    private Logger logger;
    private Component component;

    /**
     * Class marker indicating the design .html file to load
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public static @interface DeclarativeUI {
        String value();

        /**
         * Set this property to true if you provide an absolute path to your
         * design; otherwise, the DeclarativeTestUI logic will look for the HTML
         * design file under {@code vaadin_project/uitest/src/<package path>/}.
         */
        boolean absolutePath() default false;
    }

    /**
     * Method marker interface indicating that a method should be run after the
     * declarative UI has been created
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public static @interface OnLoad {

    }

    /**
     * Figure out the proper path for the HTML design file
     */
    private String getDesignPath() {
        Class<?> clazz = getClass();
        String designFilePath = null;
        if (clazz.getAnnotation(DeclarativeUI.class).absolutePath()) {
            designFilePath = "";
        } else {
            // This is rather nasty.. but it works well enough for now.
            String userDir = System.getProperty("user.dir");
            designFilePath = userDir + "/uitest/src/"
                    + clazz.getPackage().getName().replace('.', '/') + "/";
        }

        String designFileName = clazz.getAnnotation(DeclarativeUI.class)
                .value();

        return designFilePath + designFileName;
    }

    private Component readDesign() throws Exception {
        String path = getDesignPath();
        getLogger().log(Level.INFO, "Reading design from " + path);

        File file = new File(path);
        return Design.read(new FileInputStream(file));
    }

    @Override
    protected void setup(VaadinRequest request) {
        Class<?> clazz = getClass();

        if (clazz.isAnnotationPresent(DeclarativeUI.class)) {

            // Create component
            try {
                component = readDesign();
            } catch (Exception e1) {
                getLogger().log(Level.SEVERE, "Error reading design", e1);
                return;
            }

            addComponent(component);

            // Call on-load methods (if applicable)
            Method[] methods = clazz.getMethods();
            for (Method m : methods) {
                if (m.isAnnotationPresent(OnLoad.class)) {
                    try {
                        m.invoke(this, (Object[]) null);
                    } catch (IllegalAccessException e) {
                        getLogger().log(Level.SEVERE,
                                "Error invoking @OnLoad method", e);
                        return;
                    } catch (IllegalArgumentException e) {
                        getLogger().log(Level.SEVERE,
                                "Error invoking @OnLoad method", e);
                        return;
                    } catch (InvocationTargetException e) {
                        getLogger().log(Level.SEVERE,
                                "Error invoking @OnLoad method", e);
                        return;
                    }
                }
            }

        } else {
            throw new IllegalStateException(
                    "Cannot find declarative UI annotation");
        }
    }

    /**
     * Get access to the declaratively created component. This method typecasts
     * the component to the receiving type; if there's a mismatch between what
     * you expect and what's written in the design, this will fail with a
     * ClassCastException.
     *
     * @return a Vaadin component
     */
    @SuppressWarnings("unchecked")
    public <T extends Component> T getComponent() {
        try {
            return (T) component;
        } catch (ClassCastException ex) {
            getLogger().log(Level.SEVERE, "Component code/design type mismatch",
                    ex);
        }
        return null;
    }

    /**
     * Get access to the logger of this class
     *
     * @return a Logger instance
     */
    protected Logger getLogger() {
        if (logger == null) {
            logger = Logger.getLogger(getClass().getName());
        }
        return logger;
    }
}