1- using System . IO ;
1+ using System ;
2+ using System . IO ;
23using NUnit . Framework ;
34using QuickFix . Logger ;
45
@@ -9,6 +10,13 @@ public class FileLogTests
910{
1011 private FileLog ? _log ;
1112
13+ private readonly QuickFix . SessionID _defaultSessionId = new ( "FIX.4.2" , "SENDERCOMP" , "TARGETCOMP" ) ;
14+ private readonly string _logDirectory = Path . Combine ( TestContext . CurrentContext . TestDirectory , "log" ) ;
15+ private readonly string _eventLogFilePath = Path . Combine ( TestContext . CurrentContext . TestDirectory , "log" ,
16+ "FIX.4.2-SENDERCOMP-TARGETCOMP.event.current.log" ) ;
17+ private readonly string _messagesLogFilePath = Path . Combine ( TestContext . CurrentContext . TestDirectory , "log" ,
18+ "FIX.4.2-SENDERCOMP-TARGETCOMP.messages.current.log" ) ;
19+
1220 [ SetUp ]
1321 public void Setup ( )
1422 { }
@@ -40,60 +48,83 @@ public void TestPrefixForSubsAndLocation()
4048 Assert . That ( FileLog . Prefix ( sessionIdWithSubsNoLocation ) , Is . EqualTo ( "FIX.4.2-SENDERCOMP_SENDERSUB-TARGETCOMP_TARGETSUB" ) ) ;
4149 }
4250
43- [ Test ]
44- public void TestGeneratedFileName ( )
51+ private FileLogFactory CreateTestFactory ( )
4552 {
46- var logDirectory = Path . Combine ( TestContext . CurrentContext . TestDirectory , "log" ) ;
53+ if ( Directory . Exists ( _logDirectory ) )
54+ Directory . Delete ( _logDirectory , true ) ;
4755
48- if ( Directory . Exists ( logDirectory ) )
49- Directory . Delete ( logDirectory , true ) ;
50-
51- QuickFix . SessionID sessionId = new QuickFix . SessionID ( "FIX.4.2" , "SENDERCOMP" , "TARGETCOMP" ) ;
5256 QuickFix . SessionSettings settings = new QuickFix . SessionSettings ( ) ;
53-
5457 QuickFix . SettingsDictionary config = new QuickFix . SettingsDictionary ( ) ;
5558 config . SetString ( QuickFix . SessionSettings . CONNECTION_TYPE , "initiator" ) ;
56- config . SetString ( QuickFix . SessionSettings . FILE_LOG_PATH , logDirectory ) ;
57-
58- settings . Set ( sessionId , config ) ;
59+ config . SetString ( QuickFix . SessionSettings . FILE_LOG_PATH , _logDirectory ) ;
5960
60- string expectedEventLogFilePath = Path . Combine ( logDirectory , "FIX.4.2-SENDERCOMP-TARGETCOMP.event.current.log" ) ;
61- string expectedMessagesLogFilePath = Path . Combine ( logDirectory , "FIX.4.2-SENDERCOMP-TARGETCOMP.messages.current.log" ) ;
61+ settings . Set ( _defaultSessionId , config ) ;
62+ return new FileLogFactory ( settings ) ;
63+ }
6264
63- FileLogFactory factory = new FileLogFactory ( settings ) ;
64- _log = ( FileLog ) factory . Create ( sessionId ) ;
65+ [ Test ]
66+ public void TestGeneratedFileName ( )
67+ {
68+ FileLogFactory factory = CreateTestFactory ( ) ;
69+ _log = ( FileLog ) factory . Create ( _defaultSessionId ) ;
6570
66- Assert . That ( ! File . Exists ( expectedEventLogFilePath ) ) ;
67- Assert . That ( ! File . Exists ( expectedMessagesLogFilePath ) ) ;
71+ // Log files aren't created before first log statement
72+ Assert . That ( ! File . Exists ( _eventLogFilePath ) ) ;
73+ Assert . That ( ! File . Exists ( _messagesLogFilePath ) ) ;
6874
6975 _log . OnEvent ( "some event" ) ;
7076
71- Assert . That ( File . Exists ( expectedEventLogFilePath ) ) ;
72- Assert . That ( ! File . Exists ( expectedMessagesLogFilePath ) ) ;
77+ // The event file exists now
78+ Assert . That ( File . Exists ( _eventLogFilePath ) ) ;
79+ Assert . That ( ! File . Exists ( _messagesLogFilePath ) ) ;
7380
7481 _log . OnIncoming ( "some incoming" ) ;
7582 _log . OnOutgoing ( "some outgoing" ) ;
7683
77- Assert . That ( File . Exists ( expectedEventLogFilePath ) ) ;
78- Assert . That ( File . Exists ( expectedMessagesLogFilePath ) ) ;
84+ // The message file exists now
85+ Assert . That ( File . Exists ( _eventLogFilePath ) ) ;
86+ Assert . That ( File . Exists ( _messagesLogFilePath ) ) ;
7987
8088 // cleanup (don't delete log unless success)
8189 _log . Dispose ( ) ;
8290 _log = null ;
83- Directory . Delete ( logDirectory , true ) ;
91+ Directory . Delete ( _logDirectory , true ) ;
8492 }
8593
8694 [ Test ]
8795 public void TestThrowsIfNoConfig ( )
8896 {
89- QuickFix . SessionID sessionId = new QuickFix . SessionID ( "FIX.4.2" , "SENDERCOMP" , "TARGETCOMP" ) ;
9097 QuickFix . SettingsDictionary config = new QuickFix . SettingsDictionary ( ) ;
9198 config . SetString ( QuickFix . SessionSettings . CONNECTION_TYPE , "initiator" ) ;
9299 QuickFix . SessionSettings settings = new QuickFix . SessionSettings ( ) ;
93- settings . Set ( sessionId , config ) ;
100+ settings . Set ( _defaultSessionId , config ) ;
94101
95102 FileLogFactory factory = new FileLogFactory ( settings ) ;
103+ var ex = Assert . Throws < QuickFix . ConfigError > ( delegate { factory . Create ( _defaultSessionId ) ; } ) ;
104+ Assert . That ( ex ! . Message , Does . Contain ( "Configuration failed: No value for key: FileLogPath" ) ) ;
105+ }
96106
97- Assert . Throws < QuickFix . ConfigError > ( delegate { factory . Create ( sessionId ) ; } ) ;
107+ [ Test ]
108+ public void TestClear ( )
109+ {
110+ FileLogFactory factory = CreateTestFactory ( ) ;
111+ _log = ( FileLog ) factory . Create ( _defaultSessionId ) ;
112+
113+ _log . OnEvent ( "some event" ) ;
114+ _log . OnIncoming ( "some incoming" ) ;
115+
116+ // Clear() should delete the contents of the log files...
117+ _log . Clear ( ) ;
118+ Assert . That ( new FileInfo ( _eventLogFilePath ) . Length , Is . EqualTo ( 0 ) ) ;
119+ Assert . That ( new FileInfo ( _messagesLogFilePath ) . Length , Is . EqualTo ( 0 ) ) ;
120+
121+ // ... and leave them ready for more logging
122+ Assert . DoesNotThrow ( delegate { _log . OnEvent ( "another event after clear" ) ; } ) ;
123+ Assert . DoesNotThrow ( delegate { _log . OnIncoming ( "another incoming after clear" ) ; } ) ;
124+
125+ // cleanup (don't delete log unless success)
126+ _log . Dispose ( ) ;
127+ _log = null ;
128+ Directory . Delete ( _logDirectory , true ) ;
98129 }
99130}
0 commit comments