|
| 1 | +using FluentAssertions; |
| 2 | +using TE.FileWatcher.Configuration; |
| 3 | +using Xunit; |
| 4 | +using IOFile = System.IO.File; |
| 5 | +using IODirectory = System.IO.Directory; |
| 6 | + |
| 7 | +namespace FileWatcher.Tests.Configuration |
| 8 | +{ |
| 9 | + public class XmlFileTests : IDisposable |
| 10 | + { |
| 11 | + private readonly string _testDirectory; |
| 12 | + private readonly List<string> _testFiles = new(); |
| 13 | + |
| 14 | + public XmlFileTests() |
| 15 | + { |
| 16 | + _testDirectory = Path.Combine(Path.GetTempPath(), $"XmlFileTests_{Guid.NewGuid()}"); |
| 17 | + IODirectory.CreateDirectory(_testDirectory); |
| 18 | + } |
| 19 | + |
| 20 | + public void Dispose() |
| 21 | + { |
| 22 | + foreach (var file in _testFiles.Where(IOFile.Exists)) |
| 23 | + { |
| 24 | + try { IOFile.Delete(file); } |
| 25 | + catch { } |
| 26 | + } |
| 27 | + if (IODirectory.Exists(_testDirectory)) |
| 28 | + { |
| 29 | + try { IODirectory.Delete(_testDirectory, true); } |
| 30 | + catch { } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void XmlFile_WithNullPathAndName_ShouldUseDefaults() |
| 36 | + { |
| 37 | + // Act |
| 38 | + var xmlFile = new XmlFile(null, null); |
| 39 | + |
| 40 | + // Assert - Should not throw |
| 41 | + xmlFile.Should().NotBeNull(); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public void XmlFile_WithPathAndNullName_ShouldUseDefaultName() |
| 46 | + { |
| 47 | + // Act |
| 48 | + var xmlFile = new XmlFile(_testDirectory, null); |
| 49 | + |
| 50 | + // Assert |
| 51 | + xmlFile.Should().NotBeNull(); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public void XmlFile_WithNullPathAndName_ShouldUseDefaultName() |
| 56 | + { |
| 57 | + // Act |
| 58 | + var xmlFile = new XmlFile(null, "custom.xml"); |
| 59 | + |
| 60 | + // Assert |
| 61 | + xmlFile.Should().NotBeNull(); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void XmlFile_WithValidPathAndName_ShouldNotThrow() |
| 66 | + { |
| 67 | + // Act |
| 68 | + System.Action act = () => new XmlFile(_testDirectory, "config.xml"); |
| 69 | + |
| 70 | + // Assert |
| 71 | + act.Should().NotThrow(); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void XmlFile_WithEmptyPath_ShouldHandleGracefully() |
| 76 | + { |
| 77 | + // Act |
| 78 | + System.Action act = () => new XmlFile("", "config.xml"); |
| 79 | + |
| 80 | + // Assert |
| 81 | + act.Should().NotThrow(); |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public void XmlFile_WithEmptyName_ShouldHandleGracefully() |
| 86 | + { |
| 87 | + // Act |
| 88 | + System.Action act = () => new XmlFile(_testDirectory, ""); |
| 89 | + |
| 90 | + // Assert |
| 91 | + act.Should().NotThrow(); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void XmlFile_WithNonExistentPath_ShouldHandleGracefully() |
| 96 | + { |
| 97 | + // Act |
| 98 | + System.Action act = () => new XmlFile("C:\\NonExistent\\Path", "config.xml"); |
| 99 | + |
| 100 | + // Assert |
| 101 | + act.Should().NotThrow(); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public void XmlFile_WithSpecialCharactersInName_ShouldAccept() |
| 106 | + { |
| 107 | + // Act |
| 108 | + System.Action act = () => new XmlFile(_testDirectory, "my-config_v2.xml"); |
| 109 | + |
| 110 | + // Assert |
| 111 | + act.Should().NotThrow(); |
| 112 | + } |
| 113 | + |
| 114 | + [Fact] |
| 115 | + public void XmlFile_DefaultConfigFile_ShouldBeConfigXml() |
| 116 | + { |
| 117 | + // Assert |
| 118 | + XmlFile.DEFAULTCONFIGFILE.Should().Be("config.xml"); |
| 119 | + } |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public void XmlFile_WithRelativePath_ShouldHandleGracefully() |
| 123 | + { |
| 124 | + // Act |
| 125 | + System.Action act = () => new XmlFile("./config", "test.xml"); |
| 126 | + |
| 127 | + // Assert |
| 128 | + act.Should().NotThrow(); |
| 129 | + } |
| 130 | + |
| 131 | + [Fact] |
| 132 | + public void XmlFile_WithLongPath_ShouldHandleGracefully() |
| 133 | + { |
| 134 | + // Arrange |
| 135 | + var longPath = Path.Combine(_testDirectory, new string('a', 200)); |
| 136 | + |
| 137 | + // Act |
| 138 | + System.Action act = () => new XmlFile(longPath, "config.xml"); |
| 139 | + |
| 140 | + // Assert |
| 141 | + act.Should().NotThrow(); |
| 142 | + } |
| 143 | + |
| 144 | + [Fact] |
| 145 | + public void XmlFile_WithUNCPath_ShouldHandleGracefully() |
| 146 | + { |
| 147 | + // Act |
| 148 | + System.Action act = () => new XmlFile("\\\\server\\share", "config.xml"); |
| 149 | + |
| 150 | + // Assert |
| 151 | + act.Should().NotThrow(); |
| 152 | + } |
| 153 | + |
| 154 | + [Fact] |
| 155 | + public void XmlFile_CalledMultipleTimes_ShouldCreateDifferentInstances() |
| 156 | + { |
| 157 | + // Act |
| 158 | + var xmlFile1 = new XmlFile(_testDirectory, "config.xml"); |
| 159 | + var xmlFile2 = new XmlFile(_testDirectory, "config.xml"); |
| 160 | + |
| 161 | + // Assert |
| 162 | + xmlFile1.Should().NotBeSameAs(xmlFile2); |
| 163 | + } |
| 164 | + |
| 165 | + [Fact] |
| 166 | + public void XmlFile_WithWhitespacePath_ShouldHandleGracefully() |
| 167 | + { |
| 168 | + // Act |
| 169 | + System.Action act = () => new XmlFile(" ", "config.xml"); |
| 170 | + |
| 171 | + // Assert |
| 172 | + act.Should().NotThrow(); |
| 173 | + } |
| 174 | + |
| 175 | + [Fact] |
| 176 | + public void XmlFile_WithWhitespaceName_ShouldHandleGracefully() |
| 177 | + { |
| 178 | + // Act |
| 179 | + System.Action act = () => new XmlFile(_testDirectory, " "); |
| 180 | + |
| 181 | + // Assert |
| 182 | + act.Should().NotThrow(); |
| 183 | + } |
| 184 | + |
| 185 | + [Fact] |
| 186 | + public void XmlFile_WithPathContainingSpaces_ShouldAccept() |
| 187 | + { |
| 188 | + // Arrange |
| 189 | + var pathWithSpaces = Path.Combine(_testDirectory, "path with spaces"); |
| 190 | + if (!IODirectory.Exists(pathWithSpaces)) |
| 191 | + { |
| 192 | + IODirectory.CreateDirectory(pathWithSpaces); |
| 193 | + } |
| 194 | + |
| 195 | + // Act |
| 196 | + System.Action act = () => new XmlFile(pathWithSpaces, "config.xml"); |
| 197 | + |
| 198 | + // Assert |
| 199 | + act.Should().NotThrow(); |
| 200 | + } |
| 201 | + |
| 202 | + [Fact] |
| 203 | + public void XmlFile_WithNameContainingSpaces_ShouldAccept() |
| 204 | + { |
| 205 | + // Act |
| 206 | + System.Action act = () => new XmlFile(_testDirectory, "my config file.xml"); |
| 207 | + |
| 208 | + // Assert |
| 209 | + act.Should().NotThrow(); |
| 210 | + } |
| 211 | + |
| 212 | + [Fact] |
| 213 | + public void XmlFile_WithDotInPath_ShouldHandleGracefully() |
| 214 | + { |
| 215 | + // Act |
| 216 | + System.Action act = () => new XmlFile(".", "config.xml"); |
| 217 | + |
| 218 | + // Assert |
| 219 | + act.Should().NotThrow(); |
| 220 | + } |
| 221 | + |
| 222 | + [Fact] |
| 223 | + public void XmlFile_WithDoubleDotInPath_ShouldHandleGracefully() |
| 224 | + { |
| 225 | + // Act |
| 226 | + System.Action act = () => new XmlFile("..", "config.xml"); |
| 227 | + |
| 228 | + // Assert |
| 229 | + act.Should().NotThrow(); |
| 230 | + } |
| 231 | + |
| 232 | + [Fact] |
| 233 | + public void XmlFile_WithMixedSlashesInPath_ShouldHandleGracefully() |
| 234 | + { |
| 235 | + // Act |
| 236 | + System.Action act = () => new XmlFile("C:/test\\path/mixed", "config.xml"); |
| 237 | + |
| 238 | + // Assert |
| 239 | + act.Should().NotThrow(); |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments