Skip to content

Commit 66c566d

Browse files
committed
650 - filestore message length fix
1 parent dce91e6 commit 66c566d

File tree

7 files changed

+70
-30
lines changed

7 files changed

+70
-30
lines changed

quickfixj-core/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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void testFieldCalculationsWithUTF8Charset() throws UnsupportedEncodingExc
8989
try {
9090
testFieldCalcuations("\u6D4B\u9A8C\u6570\u636E", 50, 16);
9191
} finally {
92-
CharsetSupport.setCharset(CharsetSupport.getDefaultCharset());
92+
CharsetSupport.setDefaultCharset();
9393
}
9494
}
9595

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
@@ -318,7 +318,7 @@ private void doTestMessageWithEncodedField(String charset, String text) throws E
318318
final Message msg = new Message(order.toString(), DataDictionaryTest.getDictionary());
319319
assertEquals(charset + " encoded field", text, msg.getString(EncodedText.FIELD));
320320
} finally {
321-
CharsetSupport.setCharset(CharsetSupport.getDefaultCharset());
321+
CharsetSupport.setDefaultCharset();
322322
}
323323
}
324324

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

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919

2020
package quickfix.mina.message;
2121

22-
import java.io.UnsupportedEncodingException;
22+
import org.apache.mina.filter.codec.ProtocolCodecException;
2323

24-
import junit.framework.ComparisonFailure;
25-
import junit.framework.TestCase;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.ComparisonFailure;
27+
import org.junit.Test;
2628

27-
import org.apache.mina.filter.codec.ProtocolCodecException;
2829
import org.quickfixj.CharsetSupport;
2930

3031
import quickfix.Message;
@@ -34,13 +35,25 @@
3435
import quickfix.fix40.Logon;
3536
import quickfix.fix44.News;
3637

37-
public class FIXMessageEncoderTest extends TestCase {
38+
import java.io.UnsupportedEncodingException;
39+
40+
import static org.junit.Assert.assertEquals;
41+
import static org.junit.Assert.assertTrue;
42+
import static org.junit.Assert.fail;
43+
44+
public class FIXMessageEncoderTest {
3845

46+
@Before
47+
public void setUp() throws UnsupportedEncodingException {
48+
CharsetSupport.setDefaultCharset();
49+
}
50+
51+
@After
3952
public void tearDown() throws UnsupportedEncodingException {
40-
// reset charset after every test
41-
CharsetSupport.setCharset(CharsetSupport.getDefaultCharset());
53+
CharsetSupport.setDefaultCharset();
4254
}
4355

56+
@Test
4457
public void testEncoding() throws Exception {
4558
FIXMessageEncoder encoder = new FIXMessageEncoder();
4659
Message message = new Logon();
@@ -51,6 +64,7 @@ public void testEncoding() throws Exception {
5164
assertTrue(protocolEncoderOutputForTest.buffer.limit() > 0);
5265
}
5366

67+
@Test
5468
public void testWesternEuropeanEncoding() throws Exception {
5569
String headline = "\u00E4bcf\u00F6d\u00E7\u00E9";
5670

@@ -67,6 +81,7 @@ public void testWesternEuropeanEncoding() throws Exception {
6781
}
6882
}
6983

84+
@Test
7085
public void testChineseEncoding() throws Exception {
7186
String headline = "\u6D4B\u9A8C\u6570\u636E";
7287

@@ -96,29 +111,26 @@ private void doEncodingTest(String headline) throws ProtocolCodecException, Unsu
96111
assertEquals("wrong encoding", new String(bytes, CharsetSupport.getCharset()), news.toString());
97112
}
98113

114+
@Test(expected = ProtocolCodecException.class)
99115
public void testEncodingBadType() throws Exception {
100116
FIXMessageEncoder encoder = new FIXMessageEncoder();
101-
try {
102-
encoder.encode(null, new Object(), new ProtocolEncoderOutputForTest());
103-
fail("expected exception");
104-
} catch (ProtocolCodecException e) {
105-
// expected
106-
}
117+
encoder.encode(null, new Object(), new ProtocolEncoderOutputForTest());
107118
}
108119

120+
@Test
109121
public void testEncodingStringEnglish() throws Exception {
110122
FIXMessageEncoder encoder = new FIXMessageEncoder();
111123
ProtocolEncoderOutputForTest protocolEncoderOutputForTest = new ProtocolEncoderOutputForTest();
112124
encoder.encode(null, "abcd", protocolEncoderOutputForTest);
113125
assertEquals(4, protocolEncoderOutputForTest.buffer.limit());
114126
}
115127

128+
@Test
116129
public void testEncodingStringChinese() throws Exception {
117130
CharsetSupport.setCharset("UTF-8");
118131
FIXMessageEncoder encoder = new FIXMessageEncoder();
119132
ProtocolEncoderOutputForTest protocolEncoderOutputForTest = new ProtocolEncoderOutputForTest();
120133
encoder.encode(null, "\u6D4B\u9A8C\u6570\u636E", protocolEncoderOutputForTest);
121134
assertEquals(12, protocolEncoderOutputForTest.buffer.limit());
122135
}
123-
124136
}

0 commit comments

Comments
 (0)