You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by bo...@apache.org on 2016/11/05 18:42:57 UTC

svn commit: r1768270 - /logging/log4net/trunk/tests/src/Util/PatternStringTest.cs

Author: bodewig
Date: Sat Nov  5 18:42:57 2016
New Revision: 1768270

URL: http://svn.apache.org/viewvc?rev=1768270&view=rev
Log:
LOG4NET-526 tests for AppSettingPatternConverter by JocularJoe, closes #39

Modified:
    logging/log4net/trunk/tests/src/Util/PatternStringTest.cs

Modified: logging/log4net/trunk/tests/src/Util/PatternStringTest.cs
URL: http://svn.apache.org/viewvc/logging/log4net/trunk/tests/src/Util/PatternStringTest.cs?rev=1768270&r1=1768269&r2=1768270&view=diff
==============================================================================
--- logging/log4net/trunk/tests/src/Util/PatternStringTest.cs (original)
+++ logging/log4net/trunk/tests/src/Util/PatternStringTest.cs Sat Nov  5 18:42:57 2016
@@ -22,11 +22,14 @@
 using System;
 using log4net.Util;
 using NUnit.Framework;
+using System.IO;
+using System.Reflection;
+using System.Configuration;
 
 namespace log4net.Tests.Util
 {
     [TestFixture]
-    public class PatternStringTest
+    public class PatternStringTest : MarshalByRefObject
     {
         [Test]
         public void TestEnvironmentFolderPathPatternConverter()
@@ -47,5 +50,63 @@ namespace log4net.Tests.Util
                 Assert.AreEqual(Environment.GetFolderPath(specialFolder), evaluatedPattern);
             }
         }
+
+        [Test]
+        public void TestAppSettingPathConverter()
+        {
+            string configurationFileContent = @"
+<configuration>
+  <appSettings>
+    <add key=""TestKey"" value = ""TestValue"" />
+  </appSettings>
+</configuration>
+";
+            string configurationFileName = null;
+            AppDomain appDomain = null;
+            try
+            {
+                configurationFileName = CreateTempConfigFile(configurationFileContent);
+                appDomain = CreateConfiguredDomain("AppSettingsTestDomain", configurationFileName);
+
+                PatternStringTest pst = (PatternStringTest)appDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, this.GetType().FullName);
+                pst.TestAppSettingPathConverterInConfiguredDomain();
+            }
+            finally
+            {
+                if (appDomain != null) AppDomain.Unload(appDomain);
+                if (configurationFileName != null) File.Delete(configurationFileName);
+            }
+        }
+
+        public void TestAppSettingPathConverterInConfiguredDomain()
+        {
+            string pattern = "%appSetting{TestKey}";
+            PatternString patternString = new PatternString(pattern);
+            string evaluatedPattern = patternString.Format();
+            string appSettingValue = ConfigurationManager.AppSettings["TestKey"];
+            Assert.AreEqual("TestValue", appSettingValue, "Expected configuration file to contain a key TestKey with the value TestValue");
+            Assert.AreEqual(appSettingValue, evaluatedPattern, "Evaluated pattern expected to be identical to appSetting value");
+
+            string badPattern = "%appSetting{UnknownKey}";
+            patternString = new PatternString(badPattern);
+            evaluatedPattern = patternString.Format();
+            Assert.AreEqual("(null)", evaluatedPattern, "Evaluated pattern expected to be \"(null)\" for non-existent appSettings key");
+        }
+
+        private static string CreateTempConfigFile(string configurationFileContent)
+        {
+            string fileName = Path.GetTempFileName();
+            File.WriteAllText(fileName, configurationFileContent);
+            return fileName;
+        }
+
+        private static AppDomain CreateConfiguredDomain(string domainName, string configurationFileName)
+        {
+            AppDomainSetup ads = new AppDomainSetup();
+            ads.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory;
+            ads.ConfigurationFile = configurationFileName;
+            AppDomain ad = AppDomain.CreateDomain(domainName, null, ads);
+            return ad;
+        }
     }
 }