Skip to content

Commit 8bae003

Browse files
committed
650 - filestore message length fix
1 parent 8cf71f9 commit 8bae003

File tree

7 files changed

+248
-29
lines changed

7 files changed

+248
-29
lines changed

quickfixj-base/src/main/java/org/quickfixj/CharsetSupport.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public static void setCharset(String charset) throws UnsupportedEncodingExceptio
6464
CharsetSupport.isStringEquivalent = isStringEquivalent(charsetInstance);
6565
}
6666

67+
public static void setDefaultCharset() throws UnsupportedEncodingException {
68+
setCharset(getDefaultCharset());
69+
}
70+
6771
public static String getCharset() {
6872
return charset;
6973
}

quickfixj-core/src/main/java/quickfix/FileStore.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ public class FileStore implements MessageStore, Closeable {
6363
private final String sessionFileName;
6464
private final boolean syncWrites;
6565
private final int maxCachedMsgs;
66-
private final String charsetEncoding = CharsetSupport.getCharset();
6766
private RandomAccessFile messageFileReader;
6867
private RandomAccessFile messageFileWriter;
6968
private DataOutputStream headerDataOutputStream;
@@ -350,7 +349,7 @@ private String getMessage(long offset, int size, int i) throws IOException {
350349
final byte[] data = new byte[size];
351350
messageFileReader.seek(offset);
352351
messageFileReader.readFully(data);
353-
return new String(data, charsetEncoding);
352+
return new String(data, CharsetSupport.getCharset());
354353
} catch (EOFException eofe) { // can't read fully
355354
throw new IOException("Truncated input while reading message: messageIndex=" + i
356355
+ ", offset=" + offset + ", expected size=" + size, eofe);
@@ -363,7 +362,8 @@ private String getMessage(long offset, int size, int i) throws IOException {
363362
@Override
364363
public boolean set(int sequence, String message) throws IOException {
365364
final long offset = messageFileWriter.getFilePointer();
366-
final int size = message.length();
365+
byte[] messageBytes = message.getBytes(CharsetSupport.getCharset());
366+
final int size = messageBytes.length;
367367
if (messageIndex != null) {
368368
updateMessageIndex(sequence, offset, size);
369369
}
@@ -374,7 +374,7 @@ public boolean set(int sequence, String message) throws IOException {
374374
if (syncWrites) {
375375
headerFileOutputStream.getFD().sync();
376376
}
377-
messageFileWriter.write(message.getBytes(CharsetSupport.getCharset()));
377+
messageFileWriter.write(messageBytes);
378378
return true;
379379
}
380380

quickfixj-core/src/test/java/quickfix/FieldTest.java

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,185 @@ public void testMessageSetGetString() {
5252
assertEquals(side1, side2);
5353
}
5454

55+
private void testFieldCalcuations(String value, int checksum, int length) {
56+
Field<String> field = new Field<>(12, value);
57+
field.setObject(value);
58+
assertEquals("12=" + value, field.toString());
59+
assertEquals(checksum, field.getChecksum());
60+
assertEquals(length, field.getLength());
61+
62+
value = value.substring(0, value.length() - 1) + (char)(value.charAt(value.length() - 1) + 1);
63+
checksum = (checksum + 1) & 0xFF;
64+
field.setObject(value);
65+
assertEquals("12=" + value, field.toString());
66+
assertEquals(checksum, field.getChecksum());
67+
assertEquals(length, field.getLength());
68+
69+
field.setTag(13);
70+
checksum = (checksum + 1) & 0xFF;
71+
assertEquals("13=" + value, field.toString());
72+
assertEquals(checksum, field.getChecksum());
73+
assertEquals(length, field.getLength());
74+
}
75+
76+
@Test
77+
public void testFieldCalculationsWithDefaultCharset() {
78+
testFieldCalcuations("VALUE", 30, 9);
79+
}
80+
81+
@Test
82+
public void testFieldCalculationsWithUTF8Charset() throws UnsupportedEncodingException {
83+
CharsetSupport.setCharset("UTF-8");
84+
try {
85+
testFieldCalcuations("\u6D4B\u9A8C\u6570\u636E", 50, 16);
86+
} finally {
87+
CharsetSupport.setDefaultCharset();
88+
}
89+
}
90+
91+
@Test
92+
public void testDateField() {
93+
DateField field = new DateField(11);
94+
Date date = new Date();
95+
field.setValue(date);
96+
assertEquals(11, field.getTag());
97+
assertEquals(date, field.getValue());
98+
field = new DateField(11, date);
99+
assertEquals(11, field.getTag());
100+
assertEquals(date, field.getValue());
101+
}
102+
103+
@Test
104+
public void testUtcDateOnlyField() {
105+
UtcDateOnlyField field = new UtcDateOnlyField(11);
106+
LocalDate date = LocalDate.now();
107+
field.setValue(date);
108+
assertEquals(11, field.getTag());
109+
assertEquals(date, field.getValue());
110+
field = new UtcDateOnlyField(11, date);
111+
assertEquals(11, field.getTag());
112+
assertEquals(date, field.getValue());
113+
}
114+
115+
@Test
116+
public void testUtcTimeOnlyField() {
117+
UtcTimeOnlyField field = new UtcTimeOnlyField(11);
118+
LocalTime date = LocalTime.now();
119+
field.setValue(date);
120+
assertEquals(11, field.getTag());
121+
assertEquals(date, field.getValue());
122+
field = new UtcTimeOnlyField(11, date);
123+
assertEquals(11, field.getTag());
124+
assertEquals(date, field.getValue());
125+
}
126+
127+
@Test
128+
public void testUtcTimeStampField() {
129+
UtcTimeStampField field = new UtcTimeStampField(11);
130+
LocalDateTime date = LocalDateTime.now();
131+
field.setValue(date);
132+
assertEquals(11, field.getTag());
133+
assertEquals(date, field.getValue());
134+
field = new UtcTimeStampField(11, date);
135+
assertEquals(11, field.getTag());
136+
assertEquals(date, field.getValue());
137+
}
138+
139+
@Test
140+
public void testBooleanField() {
141+
BooleanField field = new BooleanField(11);
142+
field.setValue(true);
143+
assertEquals(11, field.getTag());
144+
assertTrue(field.getValue());
145+
field.setValue(Boolean.FALSE);
146+
assertEquals(11, field.getTag());
147+
assertFalse(field.getValue());
148+
field = new BooleanField(22, true);
149+
assertEquals(22, field.getTag());
150+
assertTrue(field.getValue());
151+
field = new BooleanField(33, Boolean.TRUE);
152+
assertEquals(33, field.getTag());
153+
assertTrue(field.getValue());
154+
}
155+
156+
@Test
157+
public void testDoubleField() {
158+
DoubleField field = new DoubleField(11);
159+
field.setValue(12.3);
160+
assertEquals(11, field.getTag());
161+
assertEquals(12.3, field.getValue(), 0);
162+
field.setValue(new Double(23.4));
163+
assertEquals(11, field.getTag());
164+
assertEquals(23.4, field.getValue(), 0);
165+
field = new DoubleField(22, 34.5);
166+
assertEquals(22, field.getTag());
167+
assertEquals(34.5, field.getValue(), 0);
168+
field = new DoubleField(33, new Double(45.6));
169+
assertEquals(33, field.getTag());
170+
assertEquals(45.6, field.getValue(), 0);
171+
}
172+
173+
@Test(expected = NumberFormatException.class)
174+
public void testDoubleFieldException() {
175+
DoubleField field = new DoubleField(11, Double.NaN);
176+
}
177+
178+
@Test
179+
public void testDecimalField() {
180+
DecimalField field = new DecimalField(11);
181+
field.setValue(12.3);
182+
assertEquals(11, field.getTag());
183+
assertEquals(BigDecimal.valueOf(12.3), field.getValue());
184+
field.setValue(23.4);
185+
assertEquals(11, field.getTag());
186+
assertEquals(BigDecimal.valueOf(23.4), field.getValue());
187+
field = new DecimalField(22, 34.5);
188+
assertEquals(22, field.getTag());
189+
assertEquals(BigDecimal.valueOf(34.5), field.getValue());
190+
field = new DecimalField(33, 45.6);
191+
assertEquals(33, field.getTag());
192+
assertEquals(BigDecimal.valueOf(45.6), field.getValue());
193+
}
194+
195+
@Test(expected = NumberFormatException.class)
196+
public void testDecimalFieldException() {
197+
DecimalField field = new DecimalField(11, Double.POSITIVE_INFINITY);
198+
}
199+
200+
@Test
201+
public void testCharField() {
202+
CharField field = new CharField(11);
203+
field.setValue('x');
204+
assertEquals(11, field.getTag());
205+
assertEquals('x', field.getValue());
206+
field.setValue(Character.valueOf('X'));
207+
assertEquals(11, field.getTag());
208+
assertEquals('X', field.getValue());
209+
field = new CharField(22, 'a');
210+
assertEquals(22, field.getTag());
211+
assertEquals('a', field.getValue());
212+
field = new CharField(33, Character.valueOf('A'));
213+
assertEquals(33, field.getTag());
214+
assertEquals('A', field.getValue());
215+
}
216+
217+
@Test
218+
public void testIntField() {
219+
IntField field = new IntField(11);
220+
field.setValue(12);
221+
assertEquals(11, field.getTag());
222+
assertEquals(12, field.getValue());
223+
field.setValue(Integer.valueOf(23));
224+
assertEquals(11, field.getTag());
225+
assertEquals(23, field.getValue());
226+
field = new IntField(22, 23);
227+
assertEquals(22, field.getTag());
228+
assertEquals(23, field.getValue());
229+
field = new IntField(33, Integer.valueOf(44));
230+
assertEquals(33, field.getTag());
231+
assertEquals(44, field.getValue());
232+
}
233+
55234
@Test
56235
public void testBytesField() {
57236
byte[] data = "rawdata".getBytes();

quickfixj-core/src/test/java/quickfix/FileStoreTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@
1919

2020
package quickfix;
2121

22+
import org.quickfixj.CharsetSupport;
23+
2224
import java.io.IOException;
2325
import java.util.ArrayList;
2426
import java.util.Date;
2527
import java.util.List;
2628

2729
public class FileStoreTest extends AbstractMessageStoreTest {
2830

29-
protected void tearDown() throws Exception {
31+
public void tearDown() throws Exception {
3032
super.tearDown();
33+
CharsetSupport.setDefaultCharset();
3134
FileStore fileStore = (FileStore) getStore();
3235
try {
3336
fileStore.closeAndDeleteFiles();
@@ -36,6 +39,7 @@ protected void tearDown() throws Exception {
3639
}
3740
}
3841

42+
@Override
3943
protected MessageStoreFactory getMessageStoreFactory() throws ConfigError, FieldConvertError {
4044
SessionSettings settings = new SessionSettings(getConfigurationFileName());
4145
// Initialize the session settings from the defaults
@@ -44,6 +48,7 @@ protected MessageStoreFactory getMessageStoreFactory() throws ConfigError, Field
4448
return new FileStoreFactory(settings);
4549
}
4650

51+
@Override
4752
protected Class<?> getMessageStoreClass() {
4853
return FileStore.class;
4954
}
@@ -118,4 +123,28 @@ public void testResetShouldNeverFail() throws Exception {
118123
thread.interrupt();
119124
thread.join();
120125
}
126+
127+
public void testSetAndGetMessageWithAsciiCharacters() throws IOException {
128+
MessageStore underTest = getStore();
129+
underTest.set(1, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789");
130+
131+
List<String> messages = new ArrayList<>();
132+
underTest.get(1, 1, messages);
133+
134+
assertEquals(1, messages.size());
135+
assertEquals("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789", messages.get(0));
136+
}
137+
138+
public void testSetAndGetMessageWithUnicodeCharacters() throws IOException {
139+
CharsetSupport.setCharset("UTF-8");
140+
141+
MessageStore underTest = getStore();
142+
underTest.set(1, "a \u00A9 \u2603 \uD834\uDF06");
143+
144+
List<String> messages = new ArrayList<>();
145+
underTest.get(1, 1, messages);
146+
147+
assertEquals(1, messages.size());
148+
assertEquals("a \u00A9 \u2603 \uD834\uDF06", messages.get(0));
149+
}
121150
}

quickfixj-core/src/test/java/quickfix/MessageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private void doTestMessageWithEncodedField(String charset, String text) throws E
189189
final Message msg = new Message(order.toString(), DataDictionaryTest.getDictionary());
190190
assertEquals(charset + " encoded field", text, msg.getString(EncodedText.FIELD));
191191
} finally {
192-
CharsetSupport.setCharset(CharsetSupport.getDefaultCharset());
192+
CharsetSupport.setDefaultCharset();
193193
}
194194
}
195195

quickfixj-core/src/test/java/quickfix/mina/message/FIXMessageDecoderTest.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,21 @@ public class FIXMessageDecoderTest {
5555

5656
@Before
5757
public void setUp() throws Exception {
58+
CharsetSupport.setDefaultCharset();
5859
decoder = new FIXMessageDecoder();
5960
buffer = IoBuffer.allocate(8192);
6061
decoderOutput = new ProtocolDecoderOutputForTest();
6162
}
6263

6364
@After
6465
public void tearDown() throws Exception {
66+
CharsetSupport.setDefaultCharset();
6567
buffer.clear();
6668
}
6769

68-
@Test
70+
@Test(expected = UnsupportedEncodingException.class)
6971
public void testInvalidStringCharset() throws Exception {
70-
try {
71-
decoder = new FIXMessageDecoder("BOGUS");
72-
fail("no exception thrown");
73-
} catch (UnsupportedEncodingException e) {
74-
// expected
75-
}
72+
decoder = new FIXMessageDecoder("BOGUS");
7673
}
7774

7875
@Test
@@ -104,8 +101,6 @@ public void testWesternEuropeanDecoding() throws Exception {
104101
doWesternEuropeanDecodingTest();
105102
} catch (InvalidMessage e) {
106103
// expected
107-
} finally {
108-
CharsetSupport.setCharset(CharsetSupport.getDefaultCharset());
109104
}
110105
}
111106

0 commit comments

Comments
 (0)