Project

General

Profile

Download (4.12 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.vaadin;
2

    
3
import java.sql.SQLException;
4
import java.util.ArrayList;
5
import java.util.List;
6

    
7
import javax.servlet.ServletContextEvent;
8
import javax.servlet.ServletContextListener;
9
import javax.servlet.ServletException;
10

    
11
import org.easymock.EasyMock;
12
import org.h2.tools.Server;
13
import org.junit.Assert;
14
import org.junit.Before;
15
import org.junit.BeforeClass;
16
import org.junit.Ignore;
17
import org.springframework.mock.web.MockServletConfig;
18
import org.springframework.mock.web.MockServletContext;
19
import org.springframework.web.context.ContextLoaderListener;
20
import org.unitils.UnitilsJUnit4;
21
import org.unitils.database.DatabaseUnitils;
22
import org.unitils.database.annotations.Transactional;
23
import org.unitils.database.util.TransactionMode;
24

    
25
import com.vaadin.data.util.sqlcontainer.query.generator.filter.QueryBuilder;
26
import com.vaadin.server.DeploymentConfiguration;
27
import com.vaadin.server.ServiceException;
28
import com.vaadin.server.VaadinService;
29
import com.vaadin.server.VaadinServlet;
30
import com.vaadin.server.VaadinServletService;
31
import com.vaadin.server.VaadinSession;
32

    
33
import eu.etaxonomy.cdm.vaadin.util.CdmSQLStringDecorator;
34

    
35
@Ignore
36
@Transactional(TransactionMode.DISABLED)
37
public class CdmVaadinBaseTest extends UnitilsJUnit4 {
38

    
39
    private static MockServletContext servletContext;
40
    private static VaadinServlet vaadinServlet;
41
    private static VaadinServletService vaadinService;
42
    private static VaadinSession vaadinSession;
43
    private static boolean isVaadinServletEnvCreated = false;
44

    
45
    private static final String PROPERTY_H2_SERVER = "h2Server";
46

    
47
    @BeforeClass
48
    public static void setup() {
49
        DatabaseUnitils.disableConstraints();
50

    
51
    	if(!isVaadinServletEnvCreated) {
52
    		createNewServletEnvironment();
53
    	}
54
    	QueryBuilder.setStringDecorator(new CdmSQLStringDecorator());
55

    
56
    	Assert.assertNotNull(vaadinServlet);
57
    	Assert.assertEquals(vaadinServlet, VaadinServlet.getCurrent());
58

    
59
    	Assert.assertNotNull(servletContext);
60
        Assert.assertEquals(servletContext, VaadinServlet.getCurrent().getServletContext());
61

    
62
        Assert.assertNotNull(vaadinSession);
63
        Assert.assertEquals(vaadinSession, VaadinSession.getCurrent());
64

    
65
        Assert.assertNotNull(vaadinService);
66
        Assert.assertEquals(vaadinService, VaadinService.getCurrent());
67
    }
68

    
69
    @Before
70
    public void startH2Server() throws Exception {
71

    
72
        if(System.getProperty(PROPERTY_H2_SERVER) != null){
73
            try {
74
                List<String> args = new ArrayList<String>();
75
                try {
76
                    Integer port = Integer.parseInt(System.getProperty(PROPERTY_H2_SERVER));
77
                    args.add("-webPort");
78
                    args.add(port.toString());
79
                } catch (Exception e) {
80
                    // will start at port 8082 by default
81
                }
82
                Server.createWebServer(args.toArray(new String[]{})).start();
83
            } catch (SQLException e) {
84
                e.printStackTrace();
85
            }
86
        }
87
    }
88

    
89

    
90
    public static void createNewServletEnvironment() {
91
		servletContext = new MockServletContext("/webapp");
92

    
93
        ServletContextListener listener = new ContextLoaderListener();
94
        ServletContextEvent event = new ServletContextEvent(servletContext);
95
		listener.contextInitialized(event);
96

    
97
		MockServletConfig servletConfig = new MockServletConfig(servletContext);
98
		vaadinServlet = new VaadinServlet();
99

    
100

    
101
		try {
102
			vaadinServlet.init(servletConfig);
103
		} catch (ServletException e) {
104
			throw new RuntimeException(e);
105
		}
106

    
107
		try {
108
			vaadinService = new VaadinServletService(vaadinServlet,
109
			        EasyMock.createMock(DeploymentConfiguration.class));
110
		} catch (ServiceException e) {
111
			throw new RuntimeException(e);
112
		}
113
		VaadinService.setCurrent(vaadinService);
114

    
115
		vaadinSession = new MockVaadinSession(vaadinService);
116
		VaadinSession.setCurrent(vaadinSession);
117

    
118
		isVaadinServletEnvCreated = true;
119
    }
120

    
121
    public static  class MockVaadinSession extends VaadinSession {
122
        MockVaadinSession(VaadinService service) {
123
            super(service);
124
        }
125

    
126
        @Override
127
        public boolean hasLock() {
128
            return true;
129
        }
130
    }
131
}
    (1-1/1)