-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathDefaultSessionFactoryTest.java
More file actions
308 lines (269 loc) · 13.1 KB
/
DefaultSessionFactoryTest.java
File metadata and controls
308 lines (269 loc) · 13.1 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*******************************************************************************
* Copyright (c) quickfixengine.org All rights reserved.
*
* This file is part of the QuickFIX FIX Engine
*
* This file may be distributed under the terms of the quickfixengine.org
* license as defined by quickfixengine.org and appearing in the file
* LICENSE included in the packaging of this file.
*
* This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
* THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* See http://www.quickfixengine.org/LICENSE for licensing information.
*
* Contact ask@quickfixengine.org if any conditions of this licensing
* are not clear to you.
******************************************************************************/
package quickfix;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import quickfix.field.ApplVerID;
import quickfix.test.acceptance.ATApplication;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.fail;
public class DefaultSessionFactoryTest {
private SessionID sessionID;
private SessionSettings settings;
private SessionFactory factory;
@Before
public void setUp() throws Exception {
sessionID = new SessionID(FixVersions.BEGINSTRING_FIX42, "SENDER", "TARGET");
setUpDefaultSettings(sessionID);
factory = new DefaultSessionFactory(new ATApplication(), new MemoryStoreFactory(),
new SLF4JLogFactory(new SessionSettings()));
}
@After
public void tearDown() {
Session.unregisterSession(sessionID, true);
}
@Test
public void testMinimalSettings() throws Exception {
factory.create(sessionID, settings);
}
@Test
public void testFixTMinimalSettings() throws Exception {
sessionID = new SessionID(FixVersions.BEGINSTRING_FIXT11, "SENDER", "TARGET");
setUpDefaultSettings(sessionID);
factory = new DefaultSessionFactory(new ATApplication(), new MemoryStoreFactory(),
new SLF4JLogFactory(new SessionSettings()));
Exception e = null;
try {
factory.create(sessionID, settings);
} catch (Exception ex) {
e = ex;
}
assertNotNull(e);
settings.setString(sessionID, Session.SETTING_DEFAULT_APPL_VER_ID, "5");
e = null;
try {
Session sess = factory.create(sessionID, settings);
assertNotNull(sess);
assertEquals(new ApplVerID("5"), sess.getSenderDefaultApplicationVersionID());
} catch (Exception ex) {
e = ex;
}
assertNull(e);
}
@Test
public void testFixtDataDictionaryConfiguration() throws Exception {
SessionID sessionID = new SessionID(FixVersions.BEGINSTRING_FIXT11, "SENDER", "TARGET");
setUpDefaultSettings(sessionID);
settings.setBool(sessionID, Session.SETTING_USE_DATA_DICTIONARY, true);
settings.setString(sessionID, Session.SETTING_TRANSPORT_DATA_DICTIONARY, "FIXT11.xml");
settings.setString(sessionID, Session.SETTING_DEFAULT_APPL_VER_ID, "FIX.4.2");
settings.setString(sessionID, Session.SETTING_APP_DATA_DICTIONARY, "FIX42.xml");
settings.setString(sessionID, Session.SETTING_APP_DATA_DICTIONARY + "." + FixVersions.BEGINSTRING_FIX40, "FIX40.xml");
settings.setString(sessionID, Session.SETTING_ALLOW_UNKNOWN_MSG_FIELDS, "Y");
settings.setString(sessionID, Session.SETTING_VALIDATE_UNORDERED_GROUP_FIELDS, "N");
settings.setString(sessionID, Session.SETTING_FIELD_VALIDATION_LOGGING, "Y");
try (Session session = factory.create(sessionID, settings)) {
DataDictionaryProvider provider = session.getDataDictionaryProvider();
assertThat(provider.getSessionDataDictionary(sessionID.getBeginString()),
is(notNullValue()));
assertThat(provider.getApplicationDataDictionary(new ApplVerID(ApplVerID.FIX42)),
is(notNullValue()));
assertThat(provider.getApplicationDataDictionary(new ApplVerID(ApplVerID.FIX40)),
is(notNullValue()));
assertTrue(session.getValidationSettings().isAllowUnknownMessageFields());
assertFalse(session.getValidationSettings().isCheckUnorderedGroupFields());
assertTrue(session.getValidationSettings().isFieldValidationLogging());
}
}
/**
* Tests that validation settings are applied when no explicit AppDataDictionary is defined.
* QFJ-981
*/
@Test
public void testFixtDataDictionaryConfigurationWithDefaultAppDataDictionary() throws Exception {
SessionID sessionID = new SessionID(FixVersions.BEGINSTRING_FIXT11, "SENDER", "TARGET");
setUpDefaultSettings(sessionID);
settings.setBool(sessionID, Session.SETTING_USE_DATA_DICTIONARY, true);
settings.setString(sessionID, Session.SETTING_TRANSPORT_DATA_DICTIONARY, "FIXT11.xml");
settings.setString(sessionID, Session.SETTING_DEFAULT_APPL_VER_ID, "FIX.4.2");
settings.setString(sessionID, Session.SETTING_ALLOW_UNKNOWN_MSG_FIELDS, "Y");
settings.setString(sessionID, Session.SETTING_VALIDATE_UNORDERED_GROUP_FIELDS, "N");
try (Session session = factory.create(sessionID, settings)) {
DataDictionaryProvider provider = session.getDataDictionaryProvider();
assertThat(provider.getSessionDataDictionary(sessionID.getBeginString()),
is(notNullValue()));
assertThat(provider.getApplicationDataDictionary(new ApplVerID(ApplVerID.FIX42)),
is(notNullValue()));
assertThat(provider.getApplicationDataDictionary(new ApplVerID(ApplVerID.FIX40)),
is(notNullValue()));
assertTrue(session.getValidationSettings().isAllowUnknownMessageFields());
assertFalse(session.getValidationSettings().isCheckUnorderedGroupFields());
assertFalse(session.getValidationSettings().isFieldValidationLogging());
}
}
@Test
public void testPreFixtDataDictionaryConfiguration() throws Exception {
settings.setBool(sessionID, Session.SETTING_USE_DATA_DICTIONARY, true);
try (Session session = factory.create(sessionID, settings)) {
DataDictionaryProvider provider = session.getDataDictionaryProvider();
assertThat(provider.getSessionDataDictionary(sessionID.getBeginString()),
is(notNullValue()));
assertThat(provider.getApplicationDataDictionary(new ApplVerID(ApplVerID.FIX42)),
is(notNullValue()));
}
}
@Test
public void testNoConnectionType() throws Exception {
settings.removeSetting(sessionID, SessionFactory.SETTING_CONNECTION_TYPE);
createSessionAndAssertConfigError("no connection type exception", "Missing ConnectionType");
}
@Test
public void testWrongConnectionType() throws Exception {
settings.setString(sessionID, SessionFactory.SETTING_CONNECTION_TYPE, "fargle");
createSessionAndAssertConfigError("no connection type exception", "Invalid ConnectionType");
}
@Test
public void testUseDataDictionaryByDefault() throws Exception {
settings.removeSetting(sessionID, Session.SETTING_USE_DATA_DICTIONARY);
settings.setString(sessionID, Session.SETTING_DATA_DICTIONARY, "BOGUS");
createSessionAndAssertDictionaryNotFound();
}
private void createSessionAndAssertDictionaryNotFound() {
try {
factory.create(sessionID, settings);
fail("no data dictionary exception");
} catch (ConfigError e) {
assertTrue("exception message not matched, expected: " + "... Could not find data ..."
+ ", got: " + e.getMessage(),
e.getMessage().contains("Could not find data"));
}
}
@Test
public void testBadPathForDataDictionary() throws Exception {
settings.setBool(sessionID, Session.SETTING_USE_DATA_DICTIONARY, true);
settings.setString(sessionID, Session.SETTING_DATA_DICTIONARY, "xyz");
createSessionAndAssertDictionaryNotFound();
}
@Test
public void testInitiatorWithoutHeartbeat() throws Exception {
settings.removeSetting(sessionID, Session.SETTING_HEARTBTINT);
settings.setString(sessionID, SessionFactory.SETTING_CONNECTION_TYPE,
SessionFactory.INITIATOR_CONNECTION_TYPE);
createSessionAndAssertConfigError("no exception", "HeartBtInt not defined");
}
@Test
public void testIncorrectTimeValues() throws Exception {
settings.setString(sessionID, Session.SETTING_START_TIME, "00:00:00");
factory.create(sessionID, settings);
// no exception
setUpDefaultSettings(sessionID);
settings.setString(sessionID, Session.SETTING_END_TIME, "16:00:00");
factory.create(sessionID, settings);
// no exception
setUpDefaultSettings(sessionID);
settings.setString(sessionID, Session.SETTING_START_TIME, "xx");
createSessionAndAssertConfigError("no exception",
"Session FIX.4.2:SENDER->TARGET: could not parse time 'xx'.");
setUpDefaultSettings(sessionID);
settings.setString(sessionID, Session.SETTING_END_TIME, "yy");
createSessionAndAssertConfigError("no exception",
"Session FIX.4.2:SENDER->TARGET: could not parse time 'yy'.");
}
@Test
public void testTestRequestDelayMultiplier() throws Exception {
settings.setString(sessionID, Session.SETTING_TEST_REQUEST_DELAY_MULTIPLIER, "0.37");
try (Session session = factory.create(sessionID, settings)) {
assertEquals(0.37, session.getTestRequestDelayMultiplier(), 0);
}
}
private void createSessionAndAssertConfigError(String message, String pattern) {
Session session = null;
try {
session = factory.create(sessionID, settings);
fail(message);
} catch (ConfigError e) {
if (pattern != null) {
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(e.getMessage());
assertTrue("exception message not matched, expected: " + pattern + ", got: "
+ e.getMessage(), m.matches());
}
} finally {
if (session != null) {
try {
session.close();
} catch (IOException ex) {
// ignore
}
}
}
}
private void setUpDefaultSettings(SessionID sessionID) {
settings = new SessionSettings();
settings.setString(sessionID, SessionFactory.SETTING_CONNECTION_TYPE,
SessionFactory.INITIATOR_CONNECTION_TYPE);
settings.setBool(sessionID, Session.SETTING_USE_DATA_DICTIONARY, false);
settings.setString(sessionID, Session.SETTING_START_TIME, "09:00:00");
settings.setString(sessionID, Session.SETTING_END_TIME, "16:00:00");
settings.setString(sessionID, Session.SETTING_HEARTBTINT, "10");
settings.setString(sessionID, "BeginString", "FIX.4.2");
}
// QFJ-629
@Test
public void testReconnectIntervalInDefaultSession() throws Exception {
settings.setString(sessionID, "ReconnectInterval", "2x5;3x15");
Session session = factory.create(sessionID, settings);
session.close();
}
@Test
// QFJ-873
public void testTimestampPrecision() throws Exception {
settings.setString(Session.SETTING_TIMESTAMP_PRECISION, "FOO");
createSessionAndAssertConfigError("no exception", ".*No enum constant quickfix.UtcTimestampPrecision.FOO.*");
settings.setString(Session.SETTING_TIMESTAMP_PRECISION, "SECONDS");
factory.create(sessionID, settings);
settings.setString(Session.SETTING_TIMESTAMP_PRECISION, "MILLIS");
factory.create(sessionID, settings);
settings.setString(Session.SETTING_TIMESTAMP_PRECISION, "NANOS");
factory.create(sessionID, settings);
settings.setString(Session.SETTING_TIMESTAMP_PRECISION, "MICROS");
factory.create(sessionID, settings);
settings.setString(Session.SETTING_TIMESTAMP_PRECISION, "PICOS");
createSessionAndAssertConfigError("no exception", ".*No enum constant quickfix.UtcTimestampPrecision.PICOS.*");
}
// QFJ-973
@Test
public void testRejectGarbledMessageAndNotValidateChecksumError() {
settings.setString(Session.SETTING_REJECT_GARBLED_MESSAGE, "Y");
settings.setString(Session.SETTING_VALIDATE_CHECKSUM, "N");
createSessionAndAssertConfigError("no exception", ".*Not possible to reject " +
"garbled message and process messages with invalid checksum at the same time.*");
}
}