forked from connamara/quickfixn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageCrackerTests.cs
108 lines (84 loc) · 3.25 KB
/
MessageCrackerTests.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using QuickFix;
using QuickFix.Fields;
using System.Reflection;
namespace UnitTests
{
[TestFixture]
public class MessageCrackerTests
{
private readonly SessionID _DummySessionID = new SessionID("a","b","c");
[SetUp]
public void Setup()
{
}
[TearDown]
public void Teardown()
{
}
class TheseMightBeHandlerMethods
{
// This is a handler.
public void OnMessage(QuickFix.FIX42.News m, SessionID s) { }
// These are NOT handlers because...
// it's private
private void OnMessage(QuickFix.FIX42.ListCancelRequest m, SessionID s) { }
// name doesn't match convention
public void NameIsWrong(QuickFix.FIX42.ListExecute m, SessionID s) { }
// params count != 2
public void OnMessage(QuickFix.FIX42.ListStatus m) { }
public void OnMessage(QuickFix.FIX42.ListStatusRequest m, SessionID s, object o) { }
// param types aren't correct
public void OnMessage(object o, SessionID s) { }
public void OnMessage(QuickFix.FIX42.ListStrikePrice m, object o) { }
// return value isn't void
public int OnMessage(QuickFix.FIX42.MassQuote m, SessionID s) { return 0; }
}
[Test]
public void IsHandlerMethod()
{
MethodInfo[] methods = typeof(TheseMightBeHandlerMethods).GetMethods();
List<MethodInfo> handlers = new List<MethodInfo>();
foreach (MethodInfo m in methods)
{
if (MessageCracker.IsHandlerMethod(m))
{
handlers.Add(m);
}
}
Assert.AreEqual(1, handlers.Count);
Assert.AreEqual(handlers[0].GetParameters()[0].ParameterType, typeof(QuickFix.FIX42.News));
}
public class TestCracker : MessageCracker
{
public bool CrackedNews42 { get; set; }
public bool CrackedNews44 { get; set; }
public void OnMessage(QuickFix.FIX42.News msg, SessionID s) { CrackedNews42 = true; }
public void OnMessage(QuickFix.FIX44.News msg, SessionID s) { CrackedNews44 = true; }
}
[Test]
public void GoldenPath()
{
MessageCracker mc = new TestCracker();
TestCracker tc = mc as TestCracker;
mc.Crack(new QuickFix.FIX42.News(), _DummySessionID);
Assert.IsTrue(tc.CrackedNews42);
Assert.IsFalse(tc.CrackedNews44);
// reset and do the opposite
tc.CrackedNews42 = false;
mc.Crack(new QuickFix.FIX44.News(), _DummySessionID);
Assert.IsFalse(tc.CrackedNews42);
Assert.IsTrue(tc.CrackedNews44);
}
[Test]
public void UnsupportedMessage()
{
MessageCracker mc = new TestCracker();
Assert.Throws<UnsupportedMessageType>(delegate { mc.Crack(new QuickFix.FIX42.Email(), _DummySessionID); });
Assert.Throws<UnsupportedMessageType>(delegate { mc.Crack(new QuickFix.FIX43.News(), _DummySessionID); });
}
}
}