public void Log_WhenLogFileDoesNotExists_CreatesFileAndLogsText() { // Arrange string path = "error.log"; Assert.IsFalse(File.Exists(path), "Test setup failed."); var provider = new XmlFileLoggingProvider(LoggingEventType.Debug, path, null); File.Delete(path); Assert.IsFalse(File.Exists(path), "Test setup failed."); try { // Act provider.Log("Some message"); // Assert Assert.IsTrue(File.Exists(path), "The provider did not create the file and did not log."); } finally { File.Delete(path); } }
public void Path_ConstructorSuppliedWithRelativePath_ReturnsRootedPath() { // Arrange string relativePath = "log.txt"; // Act XmlFileLoggingProvider provider = CreateXmlFileLogger(relativePath); // Assert Assert.IsTrue(Path.IsPathRooted(provider.Path), "XmlFileLoggingProvider.Path did not return a rooted path. Path: " + provider.Path); }
public void Path_ConstructorSuppliedWithUncanonicalName_ReturnsCanonicalName() { // Arrange string uncanonicalPath = "c:\\windows\\..\\log.xml"; string expectedCanonicalPath = "c:\\log.xml"; // Act XmlFileLoggingProvider provider = CreateXmlFileLogger(uncanonicalPath); // Assert Assert.AreEqual(expectedCanonicalPath, provider.Path, "XmlFileLoggingProvider.Path did not return a canonical path. Path: " + provider.Path); }
public void Initialize_WithMissingPath_ThrowsExpectedException() { // Arrange var provider = new XmlFileLoggingProvider(); var invalidConfiguration = new NameValueCollection(); try { // Act provider.Initialize("Valid name", invalidConfiguration); // Assert Assert.Fail("Exception expected"); } catch (Exception ex) { Assert.IsTrue(ex.Message.Contains("missing 'path' attribute"), "Exception not expressive enough. Actual message: " + ex.Message); } }
public void Log_UninitializedProvider_ThrowsDescriptiveException() { // Arrange var provider = new XmlFileLoggingProvider(); try { // Act provider.Log("Some message"); // Assert Assert.Fail("Exception expected."); } catch (InvalidOperationException ex) { Assert.IsTrue(ex.Message.Contains("The provider has not been initialized"), "A provider that hasn't been initialized correctly, should throw a descriptive " + "exception. Actual: " + ex.Message + Environment.NewLine + ex.StackTrace); Assert.IsTrue(ex.Message.Contains("XmlFileLoggingProvider"), "The message should contain the type name of the unitialized provider. Actual: " + ex.Message); } }