Skip to content

Commit af740f3

Browse files
author
BRUNER Patrick
committed
unit tests update
1 parent c458feb commit af740f3

4 files changed

Lines changed: 111 additions & 12 deletions

File tree

src/LogExpert.Core/Config/Preferences.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public class Preferences
2626
/// Will be removed in a future version once migration period is complete.
2727
/// </summary>
2828
[Obsolete("This property exists only for backward compatibility with old settings files. Use HighlightGroupList instead.")]
29-
[Newtonsoft.Json.JsonProperty("hilightGroupList")]
30-
[System.Text.Json.Serialization.JsonPropertyName("hilightGroupList")]
29+
[Newtonsoft.Json.JsonProperty("hilightGroupList", DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
30+
[System.Text.Json.Serialization.JsonIgnore]
3131
public List<HighlightGroup> HilightGroupList
3232
{
33-
get => HighlightGroupList;
33+
get => null; // Always return null so Newtonsoft.Json won't serialize this property
3434
set => HighlightGroupList = value ?? [];
3535
}
3636

src/LogExpert.Core/Config/Settings.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ public class Settings
4242
/// Will be removed in a future version once migration period is complete.
4343
/// </summary>
4444
[Obsolete("This property exists only for backward compatibility with old settings files. Data is stored in Preferences.HighlightGroupList.")]
45-
[Newtonsoft.Json.JsonProperty("hilightEntryList")]
46-
[System.Text.Json.Serialization.JsonPropertyName("hilightEntryList")]
47-
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
45+
[Newtonsoft.Json.JsonProperty("hilightEntryList", DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
46+
[System.Text.Json.Serialization.JsonIgnore]
4847
public List<HighlightEntry> HilightEntryList
4948
{
5049
get => null; // Never serialize this
@@ -61,9 +60,8 @@ public List<HighlightEntry> HilightEntryList
6160
/// Will be removed in a future version once migration period is complete.
6261
/// </summary>
6362
[Obsolete("This property exists only for backward compatibility with old settings files. Data is stored in Preferences.HighlightGroupList.")]
64-
[Newtonsoft.Json.JsonProperty("hilightGroupList")]
65-
[System.Text.Json.Serialization.JsonPropertyName("hilightGroupList")]
66-
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
63+
[Newtonsoft.Json.JsonProperty("hilightGroupList", DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
64+
[System.Text.Json.Serialization.JsonIgnore]
6765
public List<HighlightGroup> HilightGroupList
6866
{
6967
get => null; // Never serialize this

src/LogExpert.Core/Entities/HighlightGroup.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ public class HighlightGroup : ICloneable
2626
/// Will be removed in a future version once migration period is complete.
2727
/// </summary>
2828
[Obsolete("This property exists only for backward compatibility with old settings files. Use HighlightEntryList instead.")]
29-
[Newtonsoft.Json.JsonProperty("hilightEntryList")]
30-
[System.Text.Json.Serialization.JsonPropertyName("hilightEntryList")]
29+
[Newtonsoft.Json.JsonProperty("hilightEntryList", DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
30+
[System.Text.Json.Serialization.JsonIgnore]
3131
public List<HighlightEntry> HilightEntryList
3232
{
33-
get => HighlightEntryList;
33+
get => null; // Always return null so Newtonsoft.Json won't serialize this property
3434
set => HighlightEntryList = value ?? [];
3535
}
3636

src/LogExpert.Tests/ConfigManagerTest.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -975,4 +975,105 @@ public void BackupFile_AlwaysValid_WhenExists ()
975975
}
976976

977977
#endregion
978+
979+
#region Backward Compatibility Tests
980+
981+
[Test]
982+
[Category("BackwardCompatibility")]
983+
[Description("LoadOrCreateNew should successfully load old JSON with 'hilightGroupList' typo")]
984+
public void LoadOrCreateNew_LegacyHilightGroupList_LoadsSuccessfully ()
985+
{
986+
// Arrange - Create JSON with old "hilightGroupList" property name (with typo)
987+
string legacyJson = @"{
988+
""Preferences"": {
989+
""hilightGroupList"": [
990+
{
991+
""GroupName"": ""LegacyGroup"",
992+
""hilightEntryList"": []
993+
}
994+
],
995+
""FontName"": ""Courier New"",
996+
""FontSize"": 9
997+
},
998+
""FilterList"": [],
999+
""SearchHistoryList"": []
1000+
}";
1001+
File.WriteAllText(_testSettingsFile.FullName, legacyJson);
1002+
1003+
// Act
1004+
LoadResult loadResult = InvokePrivateInstanceMethod<LoadResult>("LoadOrCreateNew", _testSettingsFile);
1005+
1006+
// Assert
1007+
Assert.That(loadResult, Is.Not.Null);
1008+
Assert.That(loadResult.Settings, Is.Not.Null);
1009+
Assert.That(loadResult.Settings.Preferences.HighlightGroupList.Count, Is.EqualTo(1), "Should load legacy 'hilightGroupList' into HighlightGroupList");
1010+
Assert.That(loadResult.Settings.Preferences.HighlightGroupList[0].GroupName, Is.EqualTo("LegacyGroup"));
1011+
}
1012+
1013+
[Test]
1014+
[Category("BackwardCompatibility")]
1015+
[Description("SaveAsJSON should not write the obsolete 'hilightGroupList' property")]
1016+
public void SaveAsJSON_DoesNotWriteLegacyProperty ()
1017+
{
1018+
// Arrange
1019+
Settings settings = CreateTestSettings();
1020+
settings.Preferences.HighlightGroupList.Add(new HighlightGroup { GroupName = "TestGroup" });
1021+
1022+
// Act
1023+
InvokePrivateInstanceMethod("SaveAsJSON", _testSettingsFile, settings);
1024+
1025+
// Assert
1026+
string json = File.ReadAllText(_testSettingsFile.FullName);
1027+
1028+
// Should contain the new property name
1029+
Assert.That(json, Does.Contain("HighlightGroupList"),
1030+
"Should write 'HighlightGroupList' property");
1031+
1032+
// Should NOT contain the legacy property name
1033+
Assert.That(json, Does.Not.Contain("hilightGroupList"),
1034+
"Should NOT write obsolete 'hilightGroupList' property");
1035+
1036+
// Verify the content is correct
1037+
Assert.That(json, Does.Contain("TestGroup"));
1038+
}
1039+
1040+
[Test]
1041+
[Category("BackwardCompatibility")]
1042+
[Description("Old JSON with both properties should not create duplicates")]
1043+
public void LoadOrCreateNew_BothPropertiesInJSON_NoDuplicates ()
1044+
{
1045+
// Arrange - Create JSON with BOTH property names (simulating a corrupted file)
1046+
string jsonWithBoth = @"{
1047+
""Preferences"": {
1048+
""HighlightGroupList"": [
1049+
{
1050+
""GroupName"": ""Group1"",
1051+
""HighlightEntryList"": []
1052+
}
1053+
],
1054+
""hilightGroupList"": [
1055+
{
1056+
""GroupName"": ""Group1"",
1057+
""hilightEntryList"": []
1058+
}
1059+
],
1060+
""FontName"": ""Courier New"",
1061+
""FontSize"": 9
1062+
},
1063+
""FilterList"": [],
1064+
""SearchHistoryList"": []
1065+
}";
1066+
File.WriteAllText(_testSettingsFile.FullName, jsonWithBoth);
1067+
1068+
// Act
1069+
LoadResult loadResult = InvokePrivateInstanceMethod<LoadResult>("LoadOrCreateNew", _testSettingsFile);
1070+
1071+
// Assert
1072+
Assert.That(loadResult, Is.Not.Null);
1073+
Assert.That(loadResult.Settings, Is.Not.Null);
1074+
Assert.That(loadResult.Settings.Preferences.HighlightGroupList.Count, Is.EqualTo(1), "Should have exactly 1 group, not duplicates");
1075+
Assert.That(loadResult.Settings.Preferences.HighlightGroupList[0].GroupName, Is.EqualTo("Group1"));
1076+
}
1077+
1078+
#endregion
9781079
}

0 commit comments

Comments
 (0)