/
BaseTest.java
270 lines (227 loc) · 10.6 KB
/
BaseTest.java
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Copyright 2010 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jbpm.task;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.TestCase;
import org.drools.SystemEventListenerFactory;
import org.jbpm.task.service.MockEscalatedDeadlineHandler;
import org.jbpm.task.service.MockEscalatedDeadlineHandler.Item;
import org.jbpm.task.service.SendIcal;
import org.jbpm.task.service.TaskService;
import org.jbpm.task.service.TaskServiceSession;
import org.jbpm.task.service.UserGroupCallbackManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import bitronix.tm.resource.jdbc.PoolingDataSource;
public abstract class BaseTest extends TestCase {
protected static Logger logger = LoggerFactory.getLogger(BaseTest.class);
protected EntityManagerFactory emf;
protected Map<String, User> users;
protected Map<String, Group> groups;
protected TaskService taskService;
protected TaskServiceSession taskSession;
protected boolean useJTA = false;
protected static final String DATASOURCE_PROPERTIES = "/datasource.properties";
private PoolingDataSource pds;
protected EntityManagerFactory createEntityManagerFactory() {
return Persistence.createEntityManagerFactory("org.jbpm.task");
}
protected void setUp() throws Exception {
Properties conf = new Properties();
conf.setProperty("mail.smtp.host", "localhost");
conf.setProperty("mail.smtp.port", "2345");
conf.setProperty("from", "from@domain.com");
conf.setProperty("replyTo", "replyTo@domain.com");
conf.setProperty("defaultLanguage", "en-UK");
SendIcal.initInstance(conf);
Properties dsProps = loadDataSourceProperties();
String txType = dsProps.getProperty("txType", "RESOURCE_LOCAL");
if( "RESOURCE_LOCAL".equals(txType) ) {
useJTA = false;
} else if( "JTA".equals(txType) ) {
useJTA = true;
}
if( useJTA ) {
pds = new PoolingDataSource();
pds.setUniqueName( "jdbc/taskDS" );
pds.setClassName(dsProps.getProperty("className"));
pds.setMaxPoolSize(Integer.parseInt(dsProps.getProperty("maxPoolSize")));
pds.setAllowLocalTransactions(Boolean.parseBoolean(dsProps.getProperty("allowLocalTransactions")));
for (String propertyName : new String[] { "user", "password" }) {
pds.getDriverProperties().put(propertyName, dsProps.getProperty(propertyName));
}
setDatabaseSpecificDataSourceProperties(pds, dsProps);
pds.init();
}
// Use persistence.xml configuration
emf = createEntityManagerFactory();
taskService = new TaskService(emf, SystemEventListenerFactory.getSystemEventListener());
MockUserInfo userInfo = new MockUserInfo();
taskService.setUserinfo(userInfo);
users = fillUsersOrGroups("LoadUsers.mvel");
groups = fillUsersOrGroups("LoadGroups.mvel");
taskService.addUsersAndGroups(users, groups);
disableUserGroupCallback();
logger = LoggerFactory.getLogger(getClass());
taskSession = taskService.createSession();
}
private Properties loadDataSourceProperties() {
String propertiesNotFoundMessage = "Unable to load datasource properties [" + DATASOURCE_PROPERTIES + "]";
InputStream propsInputStream = getClass().getResourceAsStream(DATASOURCE_PROPERTIES);
assertNotNull(propertiesNotFoundMessage, propsInputStream);
Properties dsProps = new Properties();
if (propsInputStream != null) {
try {
dsProps.load(propsInputStream);
} catch (IOException ioe) {
logger.warn("Unable to find properties, using default H2 properties: " + ioe.getMessage());
ioe.printStackTrace();
}
}
return dsProps;
}
protected void tearDown() throws Exception {
if( taskSession != null ) {
taskSession.dispose();
}
emf.close();
if( useJTA ) {
pds.close();
}
}
public void disableUserGroupCallback() {
UserGroupCallbackManager.getInstance().setCallback(null);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Map fillUsersOrGroups(String mvelFileName) throws Exception {
Map<String, Object> vars = new HashMap<String, Object>();
Reader reader = null;
Map<String, Object> result = null;
try {
reader = new InputStreamReader(BaseTest.class.getResourceAsStream(mvelFileName));
result = (Map<String, Object>) eval(reader, vars);
} finally {
if (reader != null) reader.close();
}
return result;
}
public static void loadUsersAndGroups(TaskServiceSession taskSession, Map<String, User> users, Map<String, Group> groups) throws Exception {
for (User user : users.values()) {
taskSession.addUser(user);
}
for (Group group : groups.values()) {
taskSession.addGroup(group);
}
}
public static Object eval(Reader reader, Map vars) {
vars.put("now", new Date());
return TaskService.eval(reader, vars);
}
public Object eval(String str, Map vars) {
vars.put("now", new Date());
return TaskService.eval(str, vars);
}
protected Map<String, Object> fillVariables() {
return fillVariables(users, groups);
}
public static Map<String, Object> fillVariables(Map<String, User> users, Map<String, Group> groups ) {
Map <String, Object> vars = new HashMap<String, Object>();
vars.put( "users", users );
vars.put( "groups", groups );
vars.put( "now", new Date() );
return vars;
}
protected static void testDeadlines(long now, MockEscalatedDeadlineHandler handler) throws Exception {
int sleep = 8000;
handler.wait(3, sleep);
assertEquals(3, handler.getList().size());
boolean firstDeadlineMet = false;
boolean secondDeadlineMet = false;
boolean thirdDeadlineMet = false;
for( Item item : handler.getList() ) {
long deadlineTime = item.getDeadline().getDate().getTime();
if( deadlineTime == now + 2000 ) {
firstDeadlineMet = true;
}
else if( deadlineTime == now + 4000 ) {
secondDeadlineMet = true;
}
else if( deadlineTime == now + 6000 ) {
thirdDeadlineMet = true;
}
else {
fail( deadlineTime + " is not an expected deadline time. Now is [" + now + " (" + (deadlineTime-now) + ")]." );
}
}
assertTrue( "First deadline was not met." , firstDeadlineMet );
assertTrue( "Second deadline was not met." , secondDeadlineMet );
assertTrue( "Third deadline was not met." , thirdDeadlineMet );
// Wait for deadlines to finish
Thread.sleep(1000);
}
private void setDatabaseSpecificDataSourceProperties(PoolingDataSource pds, Properties dsProps) {
String driverClass = dsProps.getProperty("driverClassName");
if (driverClass.startsWith("org.h2")) {
for (String propertyName : new String[] { "url", "driverClassName" }) {
pds.getDriverProperties().put(propertyName, dsProps.getProperty(propertyName));
}
} else {
if (driverClass.startsWith("oracle")) {
pds.getDriverProperties().put("driverType", "thin");
pds.getDriverProperties().put("URL", dsProps.getProperty("url"));
} else if (driverClass.startsWith("com.ibm.db2")) {
for (String propertyName : new String[] { "databaseName", "portNumber", "serverName" }) {
pds.getDriverProperties().put(propertyName, dsProps.getProperty(propertyName));
}
pds.getDriverProperties().put("driverType", "4");
} else if (driverClass.startsWith("com.microsoft")) {
for (String propertyName : new String[] { "serverName", "portNumber", "databaseName" }) {
pds.getDriverProperties().put(propertyName, dsProps.getProperty(propertyName));
}
pds.getDriverProperties().put("URL", dsProps.getProperty("url"));
pds.getDriverProperties().put("selectMethod", "cursor");
pds.getDriverProperties().put("InstanceName", "MSSQL01");
} else if (driverClass.startsWith("com.mysql")) {
for (String propertyName : new String[] { "databaseName", "serverName", "portNumber", "url" }) {
pds.getDriverProperties().put(propertyName, dsProps.getProperty(propertyName));
}
} else if (driverClass.startsWith("com.sybase")) {
for (String propertyName : new String[] { "databaseName", "portNumber", "serverName" }) {
pds.getDriverProperties().put(propertyName, dsProps.getProperty(propertyName));
}
pds.getDriverProperties().put("REQUEST_HA_SESSION", "false");
pds.getDriverProperties().put("networkProtocol", "Tds");
} else if (driverClass.startsWith("org.postgresql")) {
for (String propertyName : new String[] { "databaseName", "portNumber", "serverName" }) {
pds.getDriverProperties().put(propertyName, dsProps.getProperty(propertyName));
}
} else {
throw new RuntimeException("Unknown driver class: " + driverClass);
}
}
}
}