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.runner.RunWith;
|
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.UnitilsJUnit4TestClassRunner;
|
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
|
|
36
|
@RunWith(UnitilsJUnit4TestClassRunner.class)
|
37
|
@Transactional(TransactionMode.DISABLED)
|
38
|
public abstract class CdmVaadinBaseTest {
|
39
|
|
40
|
private static MockServletContext servletContext;
|
41
|
private static VaadinServlet vaadinServlet;
|
42
|
private static VaadinServletService vaadinService;
|
43
|
private static VaadinSession vaadinSession;
|
44
|
private static boolean isVaadinServletEnvCreated = false;
|
45
|
|
46
|
private static final String PROPERTY_H2_SERVER = "h2Server";
|
47
|
|
48
|
@BeforeClass
|
49
|
public static void setup() {
|
50
|
DatabaseUnitils.disableConstraints();
|
51
|
|
52
|
if(!isVaadinServletEnvCreated) {
|
53
|
createNewServletEnvironment();
|
54
|
}
|
55
|
QueryBuilder.setStringDecorator(new CdmSQLStringDecorator());
|
56
|
|
57
|
Assert.assertNotNull(vaadinServlet);
|
58
|
Assert.assertEquals(vaadinServlet, VaadinServlet.getCurrent());
|
59
|
|
60
|
Assert.assertNotNull(servletContext);
|
61
|
Assert.assertEquals(servletContext, VaadinServlet.getCurrent().getServletContext());
|
62
|
|
63
|
Assert.assertNotNull(vaadinSession);
|
64
|
Assert.assertEquals(vaadinSession, VaadinSession.getCurrent());
|
65
|
|
66
|
Assert.assertNotNull(vaadinService);
|
67
|
Assert.assertEquals(vaadinService, VaadinService.getCurrent());
|
68
|
}
|
69
|
|
70
|
@Before
|
71
|
public void startH2Server() throws Exception {
|
72
|
|
73
|
if(System.getProperty(PROPERTY_H2_SERVER) != null){
|
74
|
try {
|
75
|
List<String> args = new ArrayList<String>();
|
76
|
try {
|
77
|
Integer port = Integer.parseInt(System.getProperty(PROPERTY_H2_SERVER));
|
78
|
args.add("-webPort");
|
79
|
args.add(port.toString());
|
80
|
} catch (Exception e) {
|
81
|
// will start at port 8082 by default
|
82
|
}
|
83
|
Server.createWebServer(args.toArray(new String[]{})).start();
|
84
|
} catch (SQLException e) {
|
85
|
e.printStackTrace();
|
86
|
}
|
87
|
}
|
88
|
}
|
89
|
|
90
|
|
91
|
public static void createNewServletEnvironment() {
|
92
|
servletContext = new MockServletContext("/webapp");
|
93
|
|
94
|
ServletContextListener listener = new ContextLoaderListener();
|
95
|
ServletContextEvent event = new ServletContextEvent(servletContext);
|
96
|
listener.contextInitialized(event);
|
97
|
|
98
|
MockServletConfig servletConfig = new MockServletConfig(servletContext);
|
99
|
vaadinServlet = new VaadinServlet();
|
100
|
|
101
|
|
102
|
try {
|
103
|
vaadinServlet.init(servletConfig);
|
104
|
} catch (ServletException e) {
|
105
|
throw new RuntimeException(e);
|
106
|
}
|
107
|
|
108
|
try {
|
109
|
vaadinService = new VaadinServletService(vaadinServlet,
|
110
|
EasyMock.createMock(DeploymentConfiguration.class));
|
111
|
} catch (ServiceException e) {
|
112
|
throw new RuntimeException(e);
|
113
|
}
|
114
|
VaadinService.setCurrent(vaadinService);
|
115
|
|
116
|
vaadinSession = new MockVaadinSession(vaadinService);
|
117
|
VaadinSession.setCurrent(vaadinSession);
|
118
|
|
119
|
isVaadinServletEnvCreated = true;
|
120
|
}
|
121
|
|
122
|
public static class MockVaadinSession extends VaadinSession {
|
123
|
MockVaadinSession(VaadinService service) {
|
124
|
super(service);
|
125
|
}
|
126
|
|
127
|
@Override
|
128
|
public boolean hasLock() {
|
129
|
return true;
|
130
|
}
|
131
|
}
|
132
|
}
|