-
Notifications
You must be signed in to change notification settings - Fork 637
/
Copy pathFieldMapTest.java
124 lines (109 loc) · 5.44 KB
/
FieldMapTest.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
package quickfix;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import quickfix.field.EffectiveTime;
import quickfix.field.MDEntryTime;
import quickfix.field.converter.UtcTimeOnlyConverter;
import java.util.Iterator;
import java.util.Optional;
/**
* Tests the {@link FieldMap} class.
* Specifically, verifies that the setters for {@link UtcTimeStampField} work correctly.
*
* @author toli
* @version $Id$
*/
public class FieldMapTest extends TestCase {
public FieldMapTest(String inName) {
super(inName);
}
public static Test suite() {
return new TestSuite(FieldMapTest.class);
}
public void testDefaultFieldOrderIsInsertionOrdering() {
FieldMap map = new Message();
map.setString(2, "A");
map.setString(1, "B");
assertEquals("9=82=A1=B10=017",map.toString());
}
public void testSetUtcTimeStampField() throws Exception {
FieldMap map = new Message();
LocalDateTime aDate = LocalDateTime.now();
map.setField(new UtcTimeStampField(EffectiveTime.FIELD, aDate, false));
assertEquals("milliseconds should not be preserved", epochMilliOfLocalDate(aDate) - (epochMilliOfLocalDate(aDate) % 1000),
epochMilliOfLocalDate(map.getField(new EffectiveTime()).getValue()));
// now set it with preserving millis
map.setField(new UtcTimeStampField(EffectiveTime.FIELD, aDate, true));
assertEquals("milliseconds should be preserved", epochMilliOfLocalDate(aDate),
epochMilliOfLocalDate(map.getField(new EffectiveTime()).getValue()));
}
public void testSetUtcTimeOnlyField() throws Exception {
FieldMap map = new Message();
LocalTime aDate = LocalTime.now();
map.setField(new UtcTimeOnlyField(MDEntryTime.FIELD, aDate, false));
assertEquals("milliseconds should not be preserved", UtcTimeOnlyConverter.convert(aDate, UtcTimestampPrecision.SECONDS),
UtcTimeOnlyConverter.convert(map.getField(new MDEntryTime()).getValue(), UtcTimestampPrecision.SECONDS));
// now set it with preserving millis
map.setField(new UtcTimeOnlyField(MDEntryTime.FIELD, aDate, true));
assertEquals("milliseconds should be preserved", UtcTimeOnlyConverter.convert(aDate, UtcTimestampPrecision.MILLIS),
UtcTimeOnlyConverter.convert(map.getField(new MDEntryTime()).getValue(), UtcTimestampPrecision.MILLIS));
}
/**
* Try a subclass of {@link UtcTimeOnlyField} and {@link UtcTimeStampField} directly
*/
public void testSpecificFields() throws Exception {
FieldMap map = new Message();
LocalDateTime aDate = LocalDateTime.now();
map.setField(new EffectiveTime(aDate));
assertEquals("milliseconds should be preserved", epochMilliOfLocalDate(aDate),
epochMilliOfLocalDate(map.getField(new EffectiveTime()).getValue()));
LocalTime aTime = LocalTime.now();
map.setField(new MDEntryTime(aTime));
assertEquals("milliseconds should be preserved", UtcTimeOnlyConverter.convert(aTime, UtcTimestampPrecision.MILLIS),
UtcTimeOnlyConverter.convert(map.getField(new MDEntryTime()).getValue(), UtcTimestampPrecision.MILLIS));
}
private void testOrdering(int[] vals, int[] order, int[] expected) {
FieldMap map = new Message(order);
for (int v : vals)
map.setInt(v, v);
Iterator<Field<?>> it = map.iterator();
for (int e : expected)
assertEquals(String.valueOf(e), it.next().getObject());
}
public void testOrdering() {
testOrdering(new int[] { 1, 2, 3 }, null, new int[] { 1, 2, 3 });
testOrdering(new int[] { 3, 2, 1 }, null, new int[] { 3, 2, 1 });
testOrdering(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 });
testOrdering(new int[] { 3, 2, 1 }, new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 });
testOrdering(new int[] { 1, 2, 3 }, new int[] { 1, 3, 2 }, new int[] { 1, 3, 2 });
testOrdering(new int[] { 3, 2, 1 }, new int[] { 1, 3, 2 }, new int[] { 1, 3, 2 });
testOrdering(new int[] { 1, 2, 3 }, new int[] { 1, 3 }, new int[] { 1, 3, 2 });
testOrdering(new int[] { 3, 2, 1 }, new int[] { 1, 3 }, new int[] { 1, 3, 2 });
testOrdering(new int[] { 1, 2, 3 }, new int[] { 3, 1 }, new int[] { 3, 1, 2 });
testOrdering(new int[] { 3, 2, 1 }, new int[] { 3, 1 }, new int[] { 3, 1, 2 });
}
public void testOptionalString() {
FieldMap map = new Message();
map.setField(new StringField(128, "bigbank"));
Optional<String> optValue = map.getOptionalString(128);
assertTrue(optValue.isPresent());
assertEquals("bigbank", optValue.get());
assertFalse(map.getOptionalString(129).isPresent());
}
public void testOptionalDecimal() {
FieldMap map = new Message();
map.setField(new DecimalField(44, new BigDecimal("1565.10")));
Optional<BigDecimal> optValue = map.getOptionalDecimal(44);
assertTrue(optValue.isPresent());
assertEquals(0, optValue.get().compareTo(new BigDecimal("1565.10")));
assertFalse(map.getOptionalDecimal(6).isPresent());
}
private long epochMilliOfLocalDate(LocalDateTime localDateTime) {
return localDateTime.toInstant(ZoneOffset.UTC).toEpochMilli();
}
}