aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/application/ThreadLocalInstances.java
blob: b6774ae56a42dd5b7921a51a9ce670c1b4ded4f0 (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
package com.vaadin.tests.application;

import com.vaadin.server.DownloadStream;
import com.vaadin.server.PaintException;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinSession;
import com.vaadin.tests.components.AbstractTestCase;
import com.vaadin.tests.integration.FlagSeResource;
import com.vaadin.tests.util.Log;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.LegacyWindow;
import com.vaadin.ui.UI;

public class ThreadLocalInstances extends AbstractTestCase {
    private static final VaadinSession staticInitApplication = VaadinSession
            .getCurrent();
    private static final UI staticInitRoot = UI.getCurrent();

    private final LegacyWindow mainWindow = new LegacyWindow() {
        boolean paintReported = false;

        @Override
        protected void init(VaadinRequest request) {
            reportCurrentStatus("root init");
        }

        @Override
        public void paintContent(com.vaadin.server.PaintTarget target)
                throws PaintException {
            if (!paintReported) {
                reportCurrentStatus("root paint");
                Thread thread = new Thread() {
                    @Override
                    public void run() {
                        getSession().lock();
                        try {
                            reportCurrentStatus("background thread");
                        } finally {
                            getSession().unlock();
                        }
                    }
                };
                thread.start();
                paintReported = true;
            }
            super.paintContent(target);
        }
    };

    private final FlagSeResource resource = new FlagSeResource() {
        @Override
        public DownloadStream getStream() {
            reportCurrentStatus("resource handler");
            return super.getStream();
        }
    };

    private final Log log = new Log(16);

    public ThreadLocalInstances() {
        mainWindow.addComponent(log);
        mainWindow.addComponent(new Embedded("Icon", resource));
        mainWindow.addComponent(new Button("Sync", new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                reportCurrentStatus("button listener");
            }
        }));

        reportStatus("class init", staticInitApplication, staticInitRoot);
        reportCurrentStatus("app constructor");
    }

    @Override
    protected void init() {
        reportCurrentStatus("app init");
        setMainWindow(mainWindow);
    }

    @Override
    protected String getDescription() {
        return "Tests the precence of Application.getCurrentApplication() and UI.getCurrentRoot() from different contexts";
    }

    @Override
    protected Integer getTicketNumber() {
        return Integer.valueOf(7895);
    }

    private void reportCurrentStatus(String phase) {
        reportStatus(phase, VaadinSession.getCurrent(), UI.getCurrent());
    }

    private void reportStatus(String phase, VaadinSession application, UI uI) {
        log.log(getState(application, this) + " app in " + phase);
        log.log(getState(uI, mainWindow) + " root in " + phase);
    }

    private static String getState(Object value, Object reference) {
        if (value == null) {
            return "null";
        } else if (value == reference) {
            return "this";
        } else {
            return "some";
        }
    }

}