-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathProjectFileValidatorTests.cs
More file actions
980 lines (802 loc) · 34.9 KB
/
ProjectFileValidatorTests.cs
File metadata and controls
980 lines (802 loc) · 34.9 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
using System.Globalization;
using LogExpert.Core.Classes.Persister;
namespace LogExpert.Persister.Tests;
/// <summary>
/// Unit tests for the Project File Validator implementation (Issue #514).
/// Tests validation logic for missing files in project/session loading.
/// Includes tests for ProjectFileResolver, PersisterHelpers, and ProjectPersister updates.
/// </summary>
[TestFixture]
public class ProjectFileValidatorTests
{
private string _testDirectory;
private string _projectFile;
private List<string> _testLogFiles;
[SetUp]
public void Setup ()
{
// Create temporary test directory
_testDirectory = Path.Join(Path.GetTempPath(), "LogExpertTests", "ProjectValidator", Guid.NewGuid().ToString());
_ = Directory.CreateDirectory(_testDirectory);
// Initialize test log files list
_testLogFiles = [];
// Create a project file path (will be created in individual tests)
_projectFile = Path.Join(_testDirectory, "test_project.lxj");
// Initialize PluginRegistry for tests
_ = PluginRegistry.PluginRegistry.Create(_testDirectory, 1000);
}
[TearDown]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Unit Test")]
public void TearDown ()
{
// Clean up test directory
if (Directory.Exists(_testDirectory))
{
try
{
Directory.Delete(_testDirectory, true);
}
catch
{
// Ignore cleanup errors
}
}
}
#region Helper Methods
/// <summary>
/// Creates test log files with specified names.
/// </summary>
private void CreateTestLogFiles (params string[] fileNames)
{
foreach (var fileName in fileNames)
{
var filePath = Path.Join(_testDirectory, fileName);
File.WriteAllText(filePath, $"Test log content for {fileName}");
_testLogFiles.Add(filePath);
}
}
/// <summary>
/// Creates a test project file with specified log file references.
/// </summary>
private void CreateTestProjectFile (params string[] logFileNames)
{
var projectData = new ProjectData
{
FileNames = [.. logFileNames.Select(name => Path.Join(_testDirectory, name))],
TabLayoutXml = "<layout><dockpanel>test</dockpanel></layout>"
};
ProjectPersister.SaveProjectData(_projectFile, projectData);
}
/// <summary>
/// Creates a .lxp persistence file pointing to a log file.
/// </summary>
private void CreatePersistenceFile (string lxpFileName, string logFileName)
{
var lxpPath = Path.Join(_testDirectory, lxpFileName);
var logPath = Path.Join(_testDirectory, logFileName);
var persistenceData = new PersistenceData
{
FileName = logPath
};
// Use the correct namespace: LogExpert.Core.Classes.Persister.Persister
_ = Core.Classes.Persister.Persister.SavePersistenceDataWithFixedName(lxpPath, persistenceData);
}
/// <summary>
/// Deletes specified log files to simulate missing files.
/// </summary>
private void DeleteLogFiles (params string[] fileNames)
{
foreach (var filePath in fileNames.Select(fileName => Path.Join(_testDirectory, fileName)))
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
}
#endregion
#region PersisterHelpers Tests
[Test]
public void PersisterHelpers_FindFilenameForSettings_RegularLogFile_ReturnsUnchanged ()
{
// Arrange
CreateTestLogFiles("test.log");
var logPath = Path.Join(_testDirectory, "test.log");
// Act
var result = PersisterHelpers.FindFilenameForSettings(logPath, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Is.EqualTo(logPath), "Regular log file should be returned unchanged");
}
[Test]
public void PersisterHelpers_FindFilenameForSettings_LxpFile_ReturnsLogPath ()
{
// Arrange
CreateTestLogFiles("actual.log");
CreatePersistenceFile("settings.lxp", "actual.log");
var lxpPath = Path.Join(_testDirectory, "settings.lxp");
var expectedLogPath = Path.Join(_testDirectory, "actual.log");
// Act
var result = PersisterHelpers.FindFilenameForSettings(lxpPath, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Is.EqualTo(expectedLogPath), "Should resolve .lxp to actual log file");
}
[Test]
public void PersisterHelpers_FindFilenameForSettings_NullFileName_ThrowsArgumentNullException ()
{
// Act & Assert - ThrowIfNullOrWhiteSpace throws ArgumentNullException for null
_ = Assert.Throws<ArgumentNullException>(() =>
PersisterHelpers.FindFilenameForSettings((string)null, PluginRegistry.PluginRegistry.Instance));
}
[Test]
public void PersisterHelpers_FindFilenameForSettings_EmptyFileName_ThrowsArgumentException ()
{
// Act & Assert
_ = Assert.Throws<ArgumentException>(() =>
PersisterHelpers.FindFilenameForSettings(string.Empty, PluginRegistry.PluginRegistry.Instance));
}
[Test]
public void PersisterHelpers_FindFilenameForSettings_ListOfFiles_ResolvesAll ()
{
// Arrange
CreateTestLogFiles("log1.log", "log2.log", "log3.log");
var fileList = new List<string>
{
Path.Join(_testDirectory, "log1.log"),
Path.Join(_testDirectory, "log2.log"),
Path.Join(_testDirectory, "log3.log")
};
// Act - call the List overload explicitly
var result = PersisterHelpers.FindFilenameForSettings(fileList.AsReadOnly(), PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Has.Count.EqualTo(3), "Should resolve all files");
Assert.That(result[0], Does.EndWith("log1.log"));
Assert.That(result[1], Does.EndWith("log2.log"));
Assert.That(result[2], Does.EndWith("log3.log"));
}
[Test]
public void PersisterHelpers_FindFilenameForSettings_MixedLxpAndLog_ResolvesBoth ()
{
// Arrange
CreateTestLogFiles("direct.log", "referenced.log");
CreatePersistenceFile("indirect.lxp", "referenced.log");
var fileList = new List<string>
{
Path.Join(_testDirectory, "direct.log"),
Path.Join(_testDirectory, "indirect.lxp")
};
// Act - call the List overload explicitly
var result = PersisterHelpers.FindFilenameForSettings(fileList.AsReadOnly(), PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Has.Count.EqualTo(2));
Assert.That(result[0], Does.EndWith("direct.log"), "Direct log should be unchanged");
Assert.That(result[1], Does.EndWith("referenced.log"), ".lxp should resolve to referenced log");
}
[Test]
public void PersisterHelpers_FindFilenameForSettings_CorruptedLxp_ReturnsLxpPath ()
{
// Arrange
var lxpPath = Path.Join(_testDirectory, "corrupted.lxp");
File.WriteAllText(lxpPath, "This is not valid XML");
// Act
var result = PersisterHelpers.FindFilenameForSettings(lxpPath, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Is.EqualTo(lxpPath), "Corrupted .lxp should return original path");
}
#endregion
#region ProjectFileResolver Tests
[Test]
public void ProjectFileResolver_ResolveProjectFiles_AllLogFiles_ReturnsUnchanged ()
{
// Arrange
CreateTestLogFiles("file1.log", "file2.log");
var projectData = new ProjectData
{
FileNames =
[
Path.Join(_testDirectory, "file1.log"),
Path.Join(_testDirectory, "file2.log")
]
};
// Act
var result = ProjectFileResolver.ResolveProjectFiles(projectData, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Has.Count.EqualTo(2));
Assert.That(result[0].LogFile, Does.EndWith("file1.log"));
Assert.That(result[0].OriginalFile, Does.EndWith("file1.log"));
Assert.That(result[1].LogFile, Does.EndWith("file2.log"));
Assert.That(result[1].OriginalFile, Does.EndWith("file2.log"));
}
[Test]
public void ProjectFileResolver_ResolveProjectFiles_WithLxpFiles_ResolvesToLogs ()
{
// Arrange
CreateTestLogFiles("actual1.log", "actual2.log");
CreatePersistenceFile("settings1.lxp", "actual1.log");
CreatePersistenceFile("settings2.lxp", "actual2.log");
var projectData = new ProjectData
{
FileNames =
[
Path.Join(_testDirectory, "settings1.lxp"),
Path.Join(_testDirectory, "settings2.lxp")
]
};
// Act
var result = ProjectFileResolver.ResolveProjectFiles(projectData, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Has.Count.EqualTo(2));
Assert.That(result[0].LogFile, Does.EndWith("actual1.log"), "Should resolve to actual log");
Assert.That(result[0].OriginalFile, Does.EndWith("settings1.lxp"), "Should preserve original .lxp");
Assert.That(result[1].LogFile, Does.EndWith("actual2.log"));
Assert.That(result[1].OriginalFile, Does.EndWith("settings2.lxp"));
}
[Test]
public void ProjectFileResolver_ResolveProjectFiles_MixedFiles_ResolvesProperly ()
{
// Arrange
CreateTestLogFiles("direct.log", "referenced.log");
CreatePersistenceFile("indirect.lxp", "referenced.log");
var projectData = new ProjectData
{
FileNames =
[
Path.Join(_testDirectory, "direct.log"),
Path.Join(_testDirectory, "indirect.lxp")
]
};
// Act
var result = ProjectFileResolver.ResolveProjectFiles(projectData, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Has.Count.EqualTo(2));
Assert.That(result[0].LogFile, Does.EndWith("direct.log"));
Assert.That(result[0].OriginalFile, Does.EndWith("direct.log"));
Assert.That(result[1].LogFile, Does.EndWith("referenced.log"));
Assert.That(result[1].OriginalFile, Does.EndWith("indirect.lxp"));
}
[Test]
public void ProjectFileResolver_ResolveProjectFiles_NullProjectData_ThrowsArgumentNullException ()
{
// Act & Assert
_ = Assert.Throws<ArgumentNullException>(() =>
ProjectFileResolver.ResolveProjectFiles(null, PluginRegistry.PluginRegistry.Instance));
}
[Test]
public void ProjectFileResolver_ResolveProjectFiles_EmptyProject_ReturnsEmptyList ()
{
// Arrange
var projectData = new ProjectData
{
FileNames = []
};
// Act
var result = ProjectFileResolver.ResolveProjectFiles(projectData, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Is.Empty, "Empty project should return empty list");
}
[Test]
public void ProjectFileResolver_ResolveProjectFiles_ReturnsReadOnlyCollection ()
{
// Arrange
CreateTestLogFiles("test.log");
var projectData = new ProjectData
{
FileNames = [Path.Join(_testDirectory, "test.log")]
};
// Act
var result = ProjectFileResolver.ResolveProjectFiles(projectData, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Is.InstanceOf<System.Collections.ObjectModel.ReadOnlyCollection<(string, string)>>());
}
#endregion
#region ProjectLoadResult Tests
[Test]
public void ProjectLoadResult_HasValidFiles_AllFilesValid_ReturnsTrue ()
{
// Arrange
var projectData = new ProjectData();
var validationResult = new ProjectValidationResult();
validationResult.ValidFiles.Add("file1.log");
validationResult.ValidFiles.Add("file2.log");
var result = new ProjectLoadResult
{
ProjectData = projectData,
ValidationResult = validationResult
};
// Act
var hasValidFiles = result.HasValidFiles;
// Assert
Assert.That(hasValidFiles, Is.True, "Should have valid files");
}
[Test]
public void ProjectLoadResult_HasValidFiles_NoValidFiles_ReturnsFalse ()
{
// Arrange
var projectData = new ProjectData();
var validationResult = new ProjectValidationResult();
validationResult.MissingFiles.Add("file1.log");
validationResult.MissingFiles.Add("file2.log");
var result = new ProjectLoadResult
{
ProjectData = projectData,
ValidationResult = validationResult
};
// Act
var hasValidFiles = result.HasValidFiles;
// Assert
Assert.That(hasValidFiles, Is.False, "Should not have valid files");
}
[Test]
public void ProjectLoadResult_HasValidFiles_SomeValidFiles_ReturnsTrue ()
{
// Arrange
var projectData = new ProjectData();
var validationResult = new ProjectValidationResult();
validationResult.ValidFiles.Add("file1.log");
validationResult.MissingFiles.Add("file2.log");
validationResult.MissingFiles.Add("file3.log");
var result = new ProjectLoadResult
{
ProjectData = projectData,
ValidationResult = validationResult
};
// Act
var hasValidFiles = result.HasValidFiles;
// Assert
Assert.That(hasValidFiles, Is.True, "Should have at least one valid file");
}
[Test]
public void ProjectLoadResult_RequiresUserIntervention_AllFilesValid_ReturnsFalse ()
{
// Arrange
var projectData = new ProjectData();
var validationResult = new ProjectValidationResult();
validationResult.ValidFiles.Add("file1.log");
validationResult.ValidFiles.Add("file2.log");
var result = new ProjectLoadResult
{
ProjectData = projectData,
ValidationResult = validationResult
};
// Act
var requiresIntervention = result.RequiresUserIntervention;
// Assert
Assert.That(requiresIntervention, Is.False, "Should not require user intervention");
}
[Test]
public void ProjectLoadResult_RequiresUserIntervention_SomeMissingFiles_ReturnsTrue ()
{
// Arrange
var projectData = new ProjectData();
var validationResult = new ProjectValidationResult();
validationResult.ValidFiles.Add("file1.log");
validationResult.MissingFiles.Add("file2.log");
var result = new ProjectLoadResult
{
ProjectData = projectData,
ValidationResult = validationResult
};
// Act
var requiresIntervention = result.RequiresUserIntervention;
// Assert
Assert.That(requiresIntervention, Is.True, "Should require user intervention");
}
[Test]
public void ProjectLoadResult_LogToOriginalFileMapping_StoresMapping ()
{
// Arrange
var mapping = new Dictionary<string, string>
{
["C:\\logs\\actual.log"] = "C:\\settings\\config.lxp",
["C:\\logs\\direct.log"] = "C:\\logs\\direct.log"
};
var result = new ProjectLoadResult
{
LogToOriginalFileMapping = mapping
};
// Act & Assert
Assert.That(result.LogToOriginalFileMapping, Has.Count.EqualTo(2));
Assert.That(result.LogToOriginalFileMapping["C:\\logs\\actual.log"], Is.EqualTo("C:\\settings\\config.lxp"));
Assert.That(result.LogToOriginalFileMapping["C:\\logs\\direct.log"], Is.EqualTo("C:\\logs\\direct.log"));
}
#endregion
#region ProjectValidationResult Tests
[Test]
public void ProjectValidationResult_HasMissingFiles_WithMissingFiles_ReturnsTrue ()
{
// Arrange
var result = new ProjectValidationResult();
result.ValidFiles.Add("file1.log");
result.MissingFiles.Add("file2.log");
// Act
var hasMissing = result.HasMissingFiles;
// Assert
Assert.That(hasMissing, Is.True, "Should have missing files");
}
[Test]
public void ProjectValidationResult_HasMissingFiles_WithoutMissingFiles_ReturnsFalse ()
{
// Arrange
var result = new ProjectValidationResult();
result.ValidFiles.Add("file1.log");
result.ValidFiles.Add("file2.log");
// Act
var hasMissing = result.HasMissingFiles;
// Assert
Assert.That(hasMissing, Is.False, "Should not have missing files");
}
#endregion
#region ProjectPersister.LoadProjectData - All Files Valid
[Test]
public void LoadProjectData_AllFilesExist_ReturnsSuccessResult ()
{
// Arrange
CreateTestLogFiles("log1.log", "log2.log", "log3.log");
CreateTestProjectFile("log1.log", "log2.log", "log3.log");
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Is.Not.Null, "Result should not be null");
Assert.That(result.ProjectData, Is.Not.Null, "ProjectData should not be null");
Assert.That(result.ValidationResult, Is.Not.Null, "ValidationResult should not be null");
Assert.That(result.HasValidFiles, Is.True, "Should have valid files");
Assert.That(result.RequiresUserIntervention, Is.False, "Should not require intervention");
Assert.That(result.ValidationResult.ValidFiles.Count, Is.EqualTo(3), "Should have 3 valid files");
Assert.That(result.ValidationResult.MissingFiles.Count, Is.EqualTo(0), "Should have 0 missing files");
}
[Test]
public void LoadProjectData_AllFilesExist_ProjectDataContainsCorrectFiles ()
{
// Arrange
CreateTestLogFiles("alpha.log", "beta.log", "gamma.log");
CreateTestProjectFile("alpha.log", "beta.log", "gamma.log");
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
var fileNames = result.ProjectData.FileNames.Select(Path.GetFileName).ToList();
Assert.That(fileNames, Does.Contain("alpha.log"), "Should contain alpha.log");
Assert.That(fileNames, Does.Contain("beta.log"), "Should contain beta.log");
Assert.That(fileNames, Does.Contain("gamma.log"), "Should contain gamma.log");
}
[Test]
public void LoadProjectData_AllFilesExist_PreservesTabLayoutXml ()
{
// Arrange
CreateTestLogFiles("test.log");
CreateTestProjectFile("test.log");
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result.ProjectData.TabLayoutXml, Is.Not.Null.And.Not.Empty, "TabLayoutXml should be preserved");
Assert.That(result.ProjectData.TabLayoutXml, Does.Contain("<layout>"), "Should contain layout XML");
}
[Test]
public void LoadProjectData_WithLxpFiles_ResolvesToActualLogs ()
{
// Arrange
CreateTestLogFiles("actual1.log", "actual2.log");
CreatePersistenceFile("settings1.lxp", "actual1.log");
CreatePersistenceFile("settings2.lxp", "actual2.log");
// Create project referencing .lxp files
var projectData = new ProjectData
{
FileNames =
[
Path.Join(_testDirectory, "settings1.lxp"),
Path.Join(_testDirectory, "settings2.lxp")
]
};
ProjectPersister.SaveProjectData(_projectFile, projectData);
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Is.Not.Null);
Assert.That(result.ValidationResult.ValidFiles.Count, Is.EqualTo(2), "Should validate actual log files");
var fileNames = result.ProjectData.FileNames.Select(Path.GetFileName).ToList();
Assert.That(fileNames, Does.Contain("actual1.log"), "Should contain resolved log file");
Assert.That(fileNames, Does.Contain("actual2.log"), "Should contain resolved log file");
}
[Test]
public void LoadProjectData_WithLxpFiles_PreservesMapping ()
{
// Arrange
CreateTestLogFiles("actual.log");
CreatePersistenceFile("settings.lxp", "actual.log");
var projectData = new ProjectData
{
FileNames = [Path.Join(_testDirectory, "settings.lxp")]
};
ProjectPersister.SaveProjectData(_projectFile, projectData);
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result.LogToOriginalFileMapping, Is.Not.Null);
Assert.That(result.LogToOriginalFileMapping, Has.Count.EqualTo(1));
var actualLogPath = Path.Join(_testDirectory, "actual.log");
var lxpPath = Path.Join(_testDirectory, "settings.lxp");
Assert.That(result.LogToOriginalFileMapping[actualLogPath], Is.EqualTo(lxpPath));
}
#endregion
#region ProjectPersister.LoadProjectData - Some Files Missing
[Test]
public void LoadProjectData_SomeFilesMissing_ReturnsPartialSuccessResult ()
{
// Arrange
CreateTestLogFiles("exists1.log", "exists2.log", "missing.log");
DeleteLogFiles("missing.log"); // Delete to simulate missing
CreateTestProjectFile("exists1.log", "exists2.log", "missing.log");
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Is.Not.Null, "Result should not be null");
Assert.That(result.HasValidFiles, Is.True, "Should have some valid files");
Assert.That(result.RequiresUserIntervention, Is.True, "Should require user intervention");
Assert.That(result.ValidationResult.ValidFiles.Count, Is.EqualTo(2), "Should have 2 valid files");
Assert.That(result.ValidationResult.MissingFiles.Count, Is.EqualTo(1), "Should have 1 missing file");
}
[Test]
public void LoadProjectData_SomeFilesMissing_ValidFilesListIsCorrect ()
{
// Arrange
CreateTestLogFiles("valid1.log", "valid2.log", "invalid.log");
DeleteLogFiles("invalid.log");
CreateTestProjectFile("valid1.log", "valid2.log", "invalid.log");
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
var validFileNames = result.ValidationResult.ValidFiles.Select(Path.GetFileName).ToList();
Assert.That(validFileNames, Does.Contain("valid1.log"), "Should contain valid1.log");
Assert.That(validFileNames, Does.Contain("valid2.log"), "Should contain valid2.log");
Assert.That(validFileNames, Does.Not.Contain("invalid.log"), "Should not contain invalid.log");
}
[Test]
public void LoadProjectData_SomeFilesMissing_MissingFilesListIsCorrect ()
{
// Arrange
CreateTestLogFiles("present.log", "absent1.log", "absent2.log");
DeleteLogFiles("absent1.log", "absent2.log");
CreateTestProjectFile("present.log", "absent1.log", "absent2.log");
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
var missingFileNames = result.ValidationResult.MissingFiles.Select(Path.GetFileName).ToList();
Assert.That(missingFileNames, Does.Contain("absent1.log"), "Should contain absent1.log");
Assert.That(missingFileNames, Does.Contain("absent2.log"), "Should contain absent2.log");
Assert.That(missingFileNames, Does.Not.Contain("present.log"), "Should not contain present.log");
}
[Test]
public void LoadProjectData_MajorityFilesMissing_StillReturnsValidFiles ()
{
// Arrange
CreateTestLogFiles("only_valid.log", "missing1.log", "missing2.log", "missing3.log", "missing4.log");
DeleteLogFiles("missing1.log", "missing2.log", "missing3.log", "missing4.log");
CreateTestProjectFile("only_valid.log", "missing1.log", "missing2.log", "missing3.log", "missing4.log");
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result.HasValidFiles, Is.True, "Should have at least one valid file");
Assert.That(result.ValidationResult.ValidFiles.Count, Is.EqualTo(1), "Should have 1 valid file");
Assert.That(result.ValidationResult.MissingFiles.Count, Is.EqualTo(4), "Should have 4 missing files");
}
[Test]
public void LoadProjectData_LxpReferencingMissingLog_ReportsLogAsMissing ()
{
// Arrange
CreateTestLogFiles("missing.log");
CreatePersistenceFile("settings.lxp", "missing.log");
DeleteLogFiles("missing.log");
var projectData = new ProjectData
{
FileNames = [Path.Join(_testDirectory, "settings.lxp")]
};
ProjectPersister.SaveProjectData(_projectFile, projectData);
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result.ValidationResult.MissingFiles.Count, Is.EqualTo(1), "Should report missing log file");
Assert.That(result.ValidationResult.MissingFiles[0], Does.EndWith("missing.log"));
}
#endregion
#region ProjectPersister.LoadProjectData - All Files Missing
[Test]
public void LoadProjectData_AllFilesMissing_ReturnsFailureResult ()
{
// Arrange
CreateTestLogFiles("missing1.log", "missing2.log");
DeleteLogFiles("missing1.log", "missing2.log");
CreateTestProjectFile("missing1.log", "missing2.log");
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Is.Not.Null, "Result should not be null");
Assert.That(result.HasValidFiles, Is.False, "Should not have valid files");
Assert.That(result.ValidationResult.ValidFiles.Count, Is.EqualTo(0), "Should have 0 valid files");
Assert.That(result.ValidationResult.MissingFiles.Count, Is.EqualTo(2), "Should have 2 missing files");
}
[Test]
public void LoadProjectData_AllFilesMissing_MissingFilesListComplete ()
{
// Arrange
CreateTestLogFiles("gone1.log", "gone2.log", "gone3.log");
DeleteLogFiles("gone1.log", "gone2.log", "gone3.log");
CreateTestProjectFile("gone1.log", "gone2.log", "gone3.log");
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result.ValidationResult.MissingFiles.Count, Is.EqualTo(3), "Should have 3 missing files");
var missingFileNames = result.ValidationResult.MissingFiles.Select(Path.GetFileName).ToList();
Assert.That(missingFileNames, Does.Contain("gone1.log"));
Assert.That(missingFileNames, Does.Contain("gone2.log"));
Assert.That(missingFileNames, Does.Contain("gone3.log"));
}
#endregion
#region ProjectPersister.LoadProjectData - Empty/Invalid Projects
[Test]
public void LoadProjectData_EmptyProject_ReturnsEmptyResult ()
{
// Arrange
CreateTestProjectFile(); // Empty project with no files
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Is.Not.Null, "Result should not be null");
Assert.That(result.ProjectData.FileNames, Is.Empty, "FileNames should be empty");
Assert.That(result.ValidationResult.ValidFiles, Is.Empty, "ValidFiles should be empty");
Assert.That(result.ValidationResult.MissingFiles, Is.Empty, "MissingFiles should be empty");
}
[Test]
public void LoadProjectData_NonExistentProjectFile_ReturnsNull ()
{
// Arrange
var nonExistentProject = Path.Join(_testDirectory, "does_not_exist.lxj");
// Act
var result = ProjectPersister.LoadProjectData(nonExistentProject, PluginRegistry.PluginRegistry.Instance);
// Assert
// FIXED: Now returns empty result instead of null when file doesn't exist
Assert.That(result, Is.Not.Null, "Result should not be null even for non-existent file");
Assert.That(result.ProjectData, Is.Not.Null, "ProjectData should be initialized");
}
[Test]
public void LoadProjectData_CorruptedProjectFile_ThrowsJsonReaderException ()
{
// Arrange
var corruptedProject = Path.Join(_testDirectory, "corrupted.lxj");
File.WriteAllText(corruptedProject, "This is not valid XML or JSON");
// Act & Assert - JsonReaderException is not caught, so it propagates
_ = Assert.Throws<Newtonsoft.Json.JsonReaderException>(() =>
ProjectPersister.LoadProjectData(corruptedProject, PluginRegistry.PluginRegistry.Instance));
}
#endregion
#region Edge Cases and Special Scenarios
[Test]
public void LoadProjectData_DuplicateFileReferences_HandlesCorrectly ()
{
// Arrange
CreateTestLogFiles("duplicate.log");
var projectData = new ProjectData
{
FileNames =
[
Path.Join(_testDirectory, "duplicate.log"),
Path.Join(_testDirectory, "duplicate.log"),
Path.Join(_testDirectory, "duplicate.log")
]
};
ProjectPersister.SaveProjectData(_projectFile, projectData);
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Is.Not.Null, "Result should not be null");
Assert.That(result.HasValidFiles, Is.True, "Should have valid files");
// Validation should handle duplicates gracefully
}
[Test]
public void LoadProjectData_FilesWithSpecialCharacters_ValidatesCorrectly ()
{
// Arrange
CreateTestLogFiles("file with spaces.log", "file-with-dashes.log", "file_with_underscores.log");
CreateTestProjectFile("file with spaces.log", "file-with-dashes.log", "file_with_underscores.log");
// Act
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
// Assert
Assert.That(result, Is.Not.Null, "Result should not be null");
Assert.That(result.ValidationResult.ValidFiles.Count, Is.EqualTo(3), "Should validate all files with special characters");
}
[Test]
public void LoadProjectData_VeryLargeProject_ValidatesEfficiently ()
{
// Arrange
const int fileCount = 100;
var fileNames = new List<string>();
for (int i = 0; i < fileCount; i++)
{
var fileName = $"log_{i:D4}.log";
fileNames.Add(fileName);
}
CreateTestLogFiles([.. fileNames]);
CreateTestProjectFile([.. fileNames]);
// Act
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
stopwatch.Stop();
// Assert
Assert.That(result, Is.Not.Null, "Result should not be null");
Assert.That(result.ValidationResult.ValidFiles.Count, Is.EqualTo(fileCount), $"Should validate all {fileCount} files");
Assert.That(stopwatch.ElapsedMilliseconds, Is.LessThan(60000), "Should complete validation in reasonable time");
}
#endregion
#region Performance and Stress Tests
[Test]
public void LoadProjectData_ManyMissingFiles_PerformsEfficiently ()
{
// Arrange
const int totalFiles = 50;
var fileNames = new List<string>();
// Create only first 10 files, rest will be missing
for (int i = 0; i < 10; i++)
{
var fileName = $"exists_{i}.log";
fileNames.Add(fileName);
CreateTestLogFiles(fileName);
}
for (int i = 10; i < totalFiles; i++)
{
fileNames.Add($"missing_{i}.log");
}
CreateTestProjectFile([.. fileNames]);
// Act
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
var result = ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance);
stopwatch.Stop();
// Assert
Assert.That(result, Is.Not.Null, "Result should not be null");
Assert.That(result.ValidationResult.ValidFiles.Count, Is.EqualTo(10), "Should have 10 valid files");
Assert.That(result.ValidationResult.MissingFiles.Count, Is.EqualTo(40), "Should have 40 missing files");
Assert.That(stopwatch.ElapsedMilliseconds, Is.LessThan(5000), "Should handle many missing files efficiently");
}
#endregion
#region Null and Exception Handling
[Test]
public void LoadProjectData_NullProjectFile_ThrowsArgumentNullException ()
{
// Act & Assert - File.ReadAllText throws ArgumentNullException for null path
_ = Assert.Throws<ArgumentNullException>(() =>
ProjectPersister.LoadProjectData(null, PluginRegistry.PluginRegistry.Instance));
}
[Test]
public void LoadProjectData_EmptyProjectFile_ThrowsArgumentException ()
{
// Act & Assert - File.ReadAllText throws ArgumentException for empty string
_ = Assert.Throws<ArgumentException>(() =>
ProjectPersister.LoadProjectData(string.Empty, PluginRegistry.PluginRegistry.Instance));
}
[Test]
public void LoadProjectData_NullPluginRegistry_ThrowsArgumentNullException ()
{
// Arrange
CreateTestProjectFile("test.log");
// Act & Assert
_ = Assert.Throws<ArgumentNullException>(() =>
ProjectPersister.LoadProjectData(_projectFile, null));
}
#endregion
#region Backward Compatibility
[Test]
public void LoadProjectData_LegacyProjectFormat_ThrowsJsonReaderException ()
{
// Arrange
CreateTestLogFiles("legacy.log");
// Create a legacy format project file (XML)
var legacyXml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<LogExpertProject>
<Files>
<member fileName=""{0}"" />
</Files>
</LogExpertProject>";
var legacyContent = string.Format(CultureInfo.InvariantCulture, legacyXml, Path.Join(_testDirectory, "legacy.log"));
File.WriteAllText(_projectFile, legacyContent);
// Act & Assert - JsonReaderException is not caught, so XML fallback doesn't trigger
_ = Assert.Throws<Newtonsoft.Json.JsonReaderException>(() =>
ProjectPersister.LoadProjectData(_projectFile, PluginRegistry.PluginRegistry.Instance));
}
#endregion
}