Project

General

Profile

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

    
3
import javax.servlet.ServletContextEvent;
4
import javax.servlet.ServletContextListener;
5
import javax.servlet.ServletException;
6
import javax.sql.DataSource;
7

    
8
import org.easymock.EasyMock;
9
import org.junit.Assert;
10
import org.junit.BeforeClass;
11
import org.junit.Test;
12
import org.springframework.mock.web.MockServletConfig;
13
import org.springframework.mock.web.MockServletContext;
14
import org.springframework.web.context.ContextLoaderListener;
15
import org.springframework.web.context.support.XmlWebApplicationContext;
16
import org.unitils.UnitilsJUnit4;
17
import org.unitils.database.annotations.TestDataSource;
18
import org.unitils.database.annotations.Transactional;
19
import org.unitils.database.util.TransactionMode;
20

    
21
import com.vaadin.server.DeploymentConfiguration;
22
import com.vaadin.server.ServiceException;
23
import com.vaadin.server.VaadinService;
24
import com.vaadin.server.VaadinServlet;
25
import com.vaadin.server.VaadinServletService;
26
import com.vaadin.server.VaadinSession;
27

    
28
@Transactional(TransactionMode.DISABLED)
29
public class CdmVaadinBaseTest extends UnitilsJUnit4 {
30

    
31
    private static MockServletContext servletContext;
32
    private static VaadinServlet vaadinServlet;
33
    private static VaadinServletService vaadinService;
34
    private static VaadinSession vaadinSession;
35
    private static boolean isVaadinServletEnvCreated = false;
36
    
37
    @TestDataSource
38
    protected DataSource dataSource;
39
    
40
    @BeforeClass
41
    public static void setup() {
42
    	if(!isVaadinServletEnvCreated) {
43
    		createNewServletEnvironment();
44
    	}
45
    }
46
    
47
    public static void createNewServletEnvironment() {
48
		servletContext = new MockServletContext("/webapp");		  
49
		
50
        ServletContextListener listener = new ContextLoaderListener();
51
        ServletContextEvent event = new ServletContextEvent(servletContext);
52
		listener.contextInitialized(event);        
53
		
54
		MockServletConfig servletConfig = new MockServletConfig(servletContext);
55
		vaadinServlet = new VaadinServlet();
56
		
57
		
58
		try {
59
			vaadinServlet.init(servletConfig);
60
		} catch (ServletException e) {
61
			throw new RuntimeException(e);
62
		}						
63
				
64
		try {
65
			vaadinService = new VaadinServletService(vaadinServlet,
66
			        EasyMock.createMock(DeploymentConfiguration.class));
67
		} catch (ServiceException e) {
68
			throw new RuntimeException(e);
69
		}
70
		VaadinService.setCurrent(vaadinService);
71
		
72
		vaadinSession = new VaadinSession(vaadinService);
73
		VaadinSession.setCurrent(vaadinSession);	
74
		
75
		isVaadinServletEnvCreated = true;
76
    }
77
    
78
    public DataSource getDataSource() {
79
    	return dataSource;
80
    }
81
    @Test
82
    public void checkVaadinVariables() {
83
		Assert.assertEquals(vaadinServlet, VaadinServlet.getCurrent());
84
		Assert.assertEquals(servletContext, VaadinServlet.getCurrent().getServletContext());
85
		Assert.assertEquals(vaadinSession, VaadinSession.getCurrent());
86
		Assert.assertEquals(vaadinService, VaadinService.getCurrent());
87
    }
88
}
    (1-1/1)