forked from quickfix-j/quickfixj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJdbcStoreTest.java
More file actions
202 lines (165 loc) · 7.92 KB
/
Copy pathJdbcStoreTest.java
File metadata and controls
202 lines (165 loc) · 7.92 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
/*******************************************************************************
* 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 javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static quickfix.JdbcSetting.SETTING_JDBC_DS_NAME;
import static quickfix.JdbcSetting.SETTING_JDBC_STORE_MESSAGES_TABLE_NAME;
import static quickfix.JdbcSetting.SETTING_JDBC_STORE_SESSIONS_TABLE_NAME;
import static quickfix.JdbcTestSupport.HSQL_CONNECTION_URL;
import static quickfix.JdbcTestSupport.HSQL_DRIVER;
import static quickfix.JdbcTestSupport.HSQL_USER;
import static quickfix.JdbcTestSupport.assertNoActiveConnections;
import static quickfix.JdbcTestSupport.dropTable;
import static quickfix.JdbcTestSupport.loadSQL;
import static quickfix.JdbcUtil.close;
public class JdbcStoreTest extends AbstractMessageStoreTest {
private String initialContextFactory;
protected void setUp() throws Exception {
initialContextFactory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
initialContextFactory = System.getProperty(Context.PROVIDER_URL);
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "tyrex.naming.MemoryContextFactory");
System.setProperty(Context.PROVIDER_URL, "TEST");
bindDataSource();
super.setUp();
}
protected void tearDown() throws Exception {
assertNoActiveConnections(getTestDataSource());
if (initialContextFactory != null) {
System.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
}
super.tearDown();
}
private void bindDataSource() throws NamingException {
new InitialContext().rebind("TestDataSource", getTestDataSource());
}
protected MessageStoreFactory getMessageStoreFactory() throws ConfigError, SQLException,
IOException {
return getMessageStoreFactory(null, null);
}
private JdbcStoreFactory getMessageStoreFactory(String sessionTableName, String messageTableName)
throws ConfigError, SQLException, IOException {
SessionSettings settings = new SessionSettings();
settings.setString(SETTING_JDBC_DS_NAME, "TestDataSource");
if (sessionTableName != null) {
settings.setString(SETTING_JDBC_STORE_SESSIONS_TABLE_NAME, sessionTableName);
}
if (messageTableName != null) {
settings.setString(SETTING_JDBC_STORE_MESSAGES_TABLE_NAME, messageTableName);
}
initializeTableDefinitions(null, null);
return new JdbcStoreFactory(settings);
}
public void testExplicitDataSource() throws Exception {
// No JNDI data source name is set up here
JdbcStoreFactory factory = new JdbcStoreFactory(new SessionSettings());
factory.setDataSource(getTestDataSource());
factory.create(new SessionID("FIX4.4", "SENDER", "TARGET"));
}
public void testSequenceNumbersWithCustomSessionsTableName() throws Exception {
initializeTableDefinitions("xsessions", "messages");
JdbcStore store = (JdbcStore) getMessageStoreFactory("xsessions", "messages").create(
getSessionID());
store.reset();
assertEquals("wrong value", 1, store.getNextSenderMsgSeqNum());
assertEquals("wrong value", 1, store.getNextTargetMsgSeqNum());
}
public void testIncrementSequences() throws ConfigError, SQLException, IOException {
initializeTableDefinitions("xsessions", "messages");
JdbcStore store = (JdbcStore) getMessageStoreFactory("xsessions", "messages").create(
getSessionID());
store.reset();
assertEquals("wrong value", 1, store.getNextSenderMsgSeqNum());
assertEquals("wrong value", 1, store.getNextTargetMsgSeqNum());
store.incrNextSenderMsgSeqNum();
assertEquals("wrong value", 2, store.getNextSenderMsgSeqNum());
assertEquals("wrong value", 1, store.getNextTargetMsgSeqNum());
store.incrNextTargetMsgSeqNum();
assertEquals("wrong value", 2, store.getNextSenderMsgSeqNum());
assertEquals("wrong value", 2, store.getNextTargetMsgSeqNum());
store.incrNextTargetMsgSeqNum();
assertEquals("wrong value", 2, store.getNextSenderMsgSeqNum());
assertEquals("wrong value", 3, store.getNextTargetMsgSeqNum());
store.incrNextSenderMsgSeqNum();
assertEquals("wrong value", 3, store.getNextSenderMsgSeqNum());
assertEquals("wrong value", 3, store.getNextTargetMsgSeqNum());
}
public void testMessageStorageMessagesWithCustomMessagesTableName() throws Exception {
initializeTableDefinitions("sessions", "xmessages");
JdbcStore store = (JdbcStore) getMessageStoreFactory("sessions", "xmessages").create(
getSessionID());
assertTrue("set failed", store.set(111, "message2"));
assertTrue("set failed", store.set(113, "message1"));
assertTrue("set failed", store.set(120, "message3"));
ArrayList<String> messages = new ArrayList<>();
store.get(100, 115, messages);
assertEquals("wrong # of messages", 2, messages.size());
assertEquals("wrong message", "message2", messages.get(0));
assertEquals("wrong message", "message1", messages.get(1));
}
protected void initializeTableDefinitions(String sessionsTableName, String messagesTableName)
throws ConfigError, SQLException, IOException {
Connection connection = null;
try {
connection = getTestDataSource().getConnection();
if (messagesTableName != null) {
dropTable(connection, messagesTableName);
}
loadSQL(connection, "config/sql/hsqldb/messages_table.sql",
new JdbcTestSupport.HypersonicPreprocessor(messagesTableName));
if (sessionsTableName != null) {
dropTable(connection, sessionsTableName);
}
loadSQL(connection, "config/sql/hsqldb/sessions_table.sql",
new JdbcTestSupport.HypersonicPreprocessor(sessionsTableName));
} finally {
close(null, connection);
}
}
protected DataSource getTestDataSource() {
return JdbcTestSupport.getTestDataSource(HSQL_DRIVER, HSQL_CONNECTION_URL, HSQL_USER, "");
}
public void testCreationTime() throws Exception {
JdbcStore store = (JdbcStore) getStore();
Date creationTime = store.getCreationTime();
store = (JdbcStore) createStore();
Date creationTime2 = store.getCreationTime();
assertEquals("creation time not stored correctly", creationTime, creationTime2);
}
protected Class<JdbcStore> getMessageStoreClass() {
return JdbcStore.class;
}
public void testMessageUpdate() throws Exception {
JdbcStore store = (JdbcStore) getMessageStoreFactory().create(getSessionID());
store.reset();
assertTrue(store.set(1, "MESSAGE1"));
assertTrue(store.set(1, "MESSAGE2"));
List<String> messages = new ArrayList<>();
store.get(1, 1, messages);
assertEquals("MESSAGE2", messages.get(0));
}
}