diff --git a/QuickFIXn/CharEncoding.cs b/QuickFIXn/CharEncoding.cs index 6bfa032dc..71b003f6e 100644 --- a/QuickFIXn/CharEncoding.cs +++ b/QuickFIXn/CharEncoding.cs @@ -6,18 +6,21 @@ /// so we don't have to alter a bunch of other classes to pass /// a CharEncoding reference around. /// - public static class CharEncoding - { + public static class CharEncoding { + public const string DefaultCharEncoding = "iso-8859-1"; + public static System.Text.Encoding SelectedEncoding { get; private set; } - static CharEncoding() - { + static CharEncoding() { System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); - SelectedEncoding = System.Text.Encoding.GetEncoding("iso-8859-1"); + SelectedEncoding = System.Text.Encoding.GetEncoding(DefaultCharEncoding); + } + + public static void ResetToDefaultEncoding() { + SetEncoding(DefaultCharEncoding); } - public static void SetEncoding(string encoding) - { + public static void SetEncoding(string encoding) { SelectedEncoding = System.Text.Encoding.GetEncoding(encoding); } diff --git a/UnitTests/SessionDynamicTest.cs b/UnitTests/SessionDynamicTest.cs index 4706dc2e9..c83ec6a5b 100644 --- a/UnitTests/SessionDynamicTest.cs +++ b/UnitTests/SessionDynamicTest.cs @@ -85,7 +85,6 @@ private static SettingsDictionary CreateSessionConfig(bool isInitiator) settings.SetString(SessionSettings.START_TIME, "12:00:00"); settings.SetString(SessionSettings.END_TIME, "12:00:00"); settings.SetString(SessionSettings.HEARTBTINT, "300"); - settings.SetString(SessionSettings.ENCODING, "windows-1251"); return settings; } diff --git a/UnitTests/SessionFactoryTest.cs b/UnitTests/SessionFactoryTest.cs index e369ea979..01af60695 100755 --- a/UnitTests/SessionFactoryTest.cs +++ b/UnitTests/SessionFactoryTest.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using Microsoft.VisualStudio.TestPlatform.ObjectModel.DataCollection; -using NUnit.Framework; +using NUnit.Framework; using QuickFix; using QuickFix.Fields.Converters; using QuickFix.Store; @@ -10,6 +8,21 @@ namespace UnitTests [TestFixture] public class SessionFactoryTest { + [SetUp,TearDown] + public void TearDown() { + CharEncoding.ResetToDefaultEncoding(); + } + + private SettingsDictionary CreateMinimalSettings() { + SettingsDictionary rv = new(); + rv.SetString(SessionSettings.USE_DATA_DICTIONARY, "N"); + rv.SetString(SessionSettings.CONNECTION_TYPE, "initiator"); + rv.SetString(SessionSettings.HEARTBTINT, "30"); + rv.SetString(SessionSettings.START_TIME, "12:00:00"); + rv.SetString(SessionSettings.END_TIME, "12:00:00"); + return rv; + } + [Test] public void TestPersistMessages() { @@ -17,23 +30,19 @@ public void TestPersistMessages() IMessageStoreFactory storeFactory = new MemoryStoreFactory(); SessionFactory factory = new SessionFactory(app, storeFactory); - SessionID sessionID = new SessionID("FIX.4.2", "SENDER", "TARGET"); - SettingsDictionary settings = new SettingsDictionary(); - settings.SetString(SessionSettings.USE_DATA_DICTIONARY, "N"); - settings.SetString(SessionSettings.CONNECTION_TYPE, "initiator"); - settings.SetString(SessionSettings.HEARTBTINT, "30"); - settings.SetString(SessionSettings.START_TIME, "12:00:00"); - settings.SetString(SessionSettings.END_TIME, "12:00:00"); - Session session = factory.Create(sessionID, settings); + SessionID sessionId = new SessionID("FIX.4.2", "SENDER", "TARGET"); + SettingsDictionary settings = CreateMinimalSettings(); + Session session = factory.Create(sessionId, settings); //true by default Assert.That(session.PersistMessages); settings.SetBool(SessionSettings.PERSIST_MESSAGES, false); - session = factory.Create(sessionID, settings); + session = factory.Create(sessionId, settings); Assert.That(!session.PersistMessages); } + [Test] public void ValidConfiguration() { @@ -41,15 +50,10 @@ public void ValidConfiguration() IMessageStoreFactory storeFactory = new MemoryStoreFactory(); SessionFactory factory = new SessionFactory(app, storeFactory); - SessionID sessionID = new SessionID("FIX.4.2", "SENDER", "TARGET"); - SettingsDictionary settings = new SettingsDictionary(); - settings.SetString(SessionSettings.CONNECTION_TYPE, "initiator"); - settings.SetString(SessionSettings.USE_DATA_DICTIONARY, "N"); - settings.SetString(SessionSettings.START_TIME, "12:00:00"); - settings.SetString(SessionSettings.END_TIME, "12:00:00"); - settings.SetString(SessionSettings.HEARTBTINT, "30"); + SessionID sessionId = new SessionID("FIX.4.2", "SENDER", "TARGET"); + SettingsDictionary settings = CreateMinimalSettings(); - Assert.DoesNotThrow(delegate { factory.Create(sessionID, settings); }); + Assert.DoesNotThrow(delegate { factory.Create(sessionId, settings); }); } [Test] @@ -59,17 +63,12 @@ public void StartDayAndEndDayAreDifferent() IMessageStoreFactory storeFactory = new MemoryStoreFactory(); SessionFactory factory = new SessionFactory(app, storeFactory); - SessionID sessionID = new SessionID("FIX.4.2", "SENDER", "TARGET"); - SettingsDictionary settings = new SettingsDictionary(); - settings.SetString(SessionSettings.CONNECTION_TYPE, "initiator"); - settings.SetString(SessionSettings.USE_DATA_DICTIONARY, "N"); - settings.SetString(SessionSettings.START_TIME, "12:00:00"); - settings.SetString(SessionSettings.END_TIME, "12:00:00"); + SessionID sessionId = new SessionID("FIX.4.2", "SENDER", "TARGET"); + SettingsDictionary settings = CreateMinimalSettings(); settings.SetString(SessionSettings.START_DAY, "Sun"); settings.SetString(SessionSettings.END_DAY, "Mon"); - settings.SetString(SessionSettings.HEARTBTINT, "30"); - Assert.DoesNotThrow(delegate { factory.Create(sessionID, settings); }); + Assert.DoesNotThrow(delegate { factory.Create(sessionId, settings); }); } [Test] @@ -79,22 +78,17 @@ public void TestExtendedSettings() IMessageStoreFactory storeFactory = new MemoryStoreFactory(); SessionFactory factory = new SessionFactory(app, storeFactory); - SessionID sessionID = new SessionID("FIX.4.2", "SENDER", "TARGET"); - SettingsDictionary settings = new SettingsDictionary(); - settings.SetString(SessionSettings.USE_DATA_DICTIONARY, "N"); + SessionID sessionId = new SessionID("FIX.4.2", "SENDER", "TARGET"); + SettingsDictionary settings = CreateMinimalSettings(); settings.SetString(SessionSettings.SEND_REDUNDANT_RESENDREQUESTS, "Y"); settings.SetString(SessionSettings.RESEND_SESSION_LEVEL_REJECTS, "Y"); settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "millis"); - settings.SetString(SessionSettings.CONNECTION_TYPE, "initiator"); - settings.SetString(SessionSettings.HEARTBTINT, "30"); - settings.SetString(SessionSettings.START_TIME, "12:00:00"); - settings.SetString(SessionSettings.END_TIME, "12:00:00"); settings.SetString(SessionSettings.ENABLE_LAST_MSG_SEQ_NUM_PROCESSED, "Y"); settings.SetString(SessionSettings.MAX_MESSAGES_IN_RESEND_REQUEST, "2500"); settings.SetString(SessionSettings.SEND_LOGOUT_BEFORE_TIMEOUT_DISCONNECT, "Y"); settings.SetString(SessionSettings.IGNORE_POSSDUP_RESEND_REQUESTS, "Y"); - Session session = factory.Create(sessionID, settings); + Session session = factory.Create(sessionId, settings); Assert.That(session.SendRedundantResendRequests); Assert.That(session.ResendSessionLevelRejects); @@ -103,7 +97,7 @@ public void TestExtendedSettings() settings.SetString(SessionSettings.SEND_REDUNDANT_RESENDREQUESTS, "N"); settings.SetString(SessionSettings.RESEND_SESSION_LEVEL_REJECTS, "N"); settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "nanos"); - session = factory.Create(sessionID, settings); + session = factory.Create(sessionId, settings); Assert.That(!session.SendRedundantResendRequests); Assert.That(!session.ResendSessionLevelRejects); @@ -121,52 +115,63 @@ public void TestTimeStampPrecisionSettings() new NullApplication(), new MemoryStoreFactory()); - SessionID sessionID = new SessionID("FIX.4.2", "SENDER", "TARGET"); - SettingsDictionary settings = new SettingsDictionary(); - settings.SetString(SessionSettings.CONNECTION_TYPE, "initiator"); - settings.SetString(SessionSettings.USE_DATA_DICTIONARY, "N"); - settings.SetString(SessionSettings.HEARTBTINT, "30"); - settings.SetString(SessionSettings.START_TIME, "12:00:00"); - settings.SetString(SessionSettings.END_TIME, "12:00:00"); + SessionID sessionId = new SessionID("FIX.4.2", "SENDER", "TARGET"); + SettingsDictionary settings = CreateMinimalSettings(); // check default precision - Session session = factory.Create(sessionID, settings); - Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Millisecond ); + Session session = factory.Create(sessionId, settings); + Assert.That(session.TimeStampPrecision == TimeStampPrecision.Millisecond ); settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "MiCrOsEcOnD"); - session = factory.Create(sessionID, settings); - Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Microsecond ); + session = factory.Create(sessionId, settings); + Assert.That(session.TimeStampPrecision == TimeStampPrecision.Microsecond ); settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "Micro"); - session = factory.Create(sessionID, settings); - Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Microsecond); + session = factory.Create(sessionId, settings); + Assert.That(session.TimeStampPrecision == TimeStampPrecision.Microsecond); settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "Millisecond"); - session = factory.Create(sessionID, settings); - Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Millisecond); + session = factory.Create(sessionId, settings); + Assert.That(session.TimeStampPrecision == TimeStampPrecision.Millisecond); settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "Milli"); - session = factory.Create(sessionID, settings); - Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Millisecond); + session = factory.Create(sessionId, settings); + Assert.That(session.TimeStampPrecision == TimeStampPrecision.Millisecond); settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "Nanosecond"); - session = factory.Create(sessionID, settings); - Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Nanosecond); + session = factory.Create(sessionId, settings); + Assert.That(session.TimeStampPrecision == TimeStampPrecision.Nanosecond); settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "Nano"); - session = factory.Create(sessionID, settings); - Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Nanosecond); - + session = factory.Create(sessionId, settings); + Assert.That(session.TimeStampPrecision == TimeStampPrecision.Nanosecond); + settings.SetString(SessionSettings.TIMESTAMP_PRECISION, "Second"); - session = factory.Create(sessionID, settings); - Assert.That(session.TimeStampPrecision == QuickFix.Fields.Converters.TimeStampPrecision.Second); + session = factory.Create(sessionId, settings); + Assert.That(session.TimeStampPrecision == TimeStampPrecision.Second); } [Test] - public void TestEncodingSettings(){ + public void TestCharEncoding() { + IApplication app = new NullApplication(); + IMessageStoreFactory storeFactory = new MemoryStoreFactory(); + SessionFactory factory = new SessionFactory(app, storeFactory); + + SessionID sessionId = new SessionID("FIX.4.2", "SENDER", "TARGET"); + SettingsDictionary settings = CreateMinimalSettings(); + settings.SetString(SessionSettings.ENCODING, "windows-1251"); + + // Yes, CharEncoding is a global static at this time. Sorry. - Assert.That(CharEncoding.SelectedEncoding, Is.EqualTo(System.Text.Encoding.GetEncoding("windows-1251"))); + // CharEncoding is set to default before this session is created + Assert.That(CharEncoding.DefaultCharEncoding, Is.EqualTo("iso-8859-1")); + Assert.That(CharEncoding.SelectedEncoding, Is.EqualTo( + System.Text.Encoding.GetEncoding(CharEncoding.DefaultCharEncoding))); + // When a session changes the Encoding, it's a global change: + factory.Create(sessionId, settings); + Assert.That(CharEncoding.SelectedEncoding, Is.EqualTo( + System.Text.Encoding.GetEncoding("windows-1251"))); } } }