lucenenet-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [lucenenet] NightOwl888 commented on a change in pull request #287: Implemenation of "System Properties" in .NET (addresses #254)
Date Mon, 25 May 2020 07:01:58 GMT

NightOwl888 commented on a change in pull request #287:
URL: https://github.com/apache/lucenenet/pull/287#discussion_r429763498



##########
File path: src/Lucene.Net.Tests.TestFramework/Configuration/TestConfigurationSetting.cs
##########
@@ -0,0 +1,115 @@
+´╗┐using Lucene.Net.Util;
+using Microsoft.Extensions.Configuration;
+using NUnit.Framework;
+using System;
+using System.IO;
+using System.Reflection;
+
+namespace Lucene.Net.Configuration
+{
+    /*
+     * Licensed to the Apache Software Foundation (ASF) under one or more
+     * contributor license agreements.  See the NOTICE file distributed with
+     * this work for additional information regarding copyright ownership.
+     * The ASF licenses this file to You under the Apache License, Version 2.0
+     * (the "License"); you may not use this file except in compliance with
+     * the License.  You may obtain a copy of the License at
+     *
+     *     http://www.apache.org/licenses/LICENSE-2.0
+     *
+     * Unless required by applicable law or agreed to in writing, software
+     * distributed under the License is distributed on an "AS IS" BASIS,
+     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     * See the License for the specific language governing permissions and
+     * limitations under the License.
+     */
+
+    [TestFixture]
+    class TestConfigurationSettings : LuceneTestCase
+    {
+
+        internal class UnitTestConfigurationRootFactory : IConfigurationRootFactory
+        {
+            public bool IgnoreSecurityExceptionsOnRead { get; set; }
+            /// <summary>
+            /// PAth to be used for configuration settings
+            /// </summary>
+            public static string JsonTestSettingsFolderName { get; set; } = @"Configuration";
+            /// <summary>
+            /// Filename to be used for configuration settings
+            /// </summary>
+            public static string JsonTestSettingsFileName { get; set; } = @"lucene.testsettings.json";
+
+            static string JsonTestPath =
+#if TESTFRAMEWORK_NUNIT
+            NUnit.Framework.TestContext.CurrentContext.TestDirectory;
+#else
+                            AppDomain.CurrentDomain.BaseDirectory;
+#endif
+            public UnitTestConfigurationRootFactory()
+            {
+            }
+
+            protected IConfigurationRoot configuration = new ConfigurationBuilder().Add(new
LuceneDefaultConfigurationSource() { Prefix = "lucene:", IgnoreSecurityExceptionsOnRead =
false }
+            ).AddJsonFile(Path.Combine(new string[] { JsonTestPath, JsonTestSettingsFolderName,
JsonTestSettingsFileName })).Build();
+
+            public virtual IConfigurationRoot CurrentConfiguration
+            {
+                get
+                {
+                    return configuration;
+                }
+            }
+        }
+
+        public static IConfigurationRootFactory TestConfigurationFactory;
+
+        [OneTimeSetUp]
+        public override void BeforeClass()
+        {
+            base.BeforeClass();
+            // set an Enviroment variable used in the test
+            string testKey = "lucene:tests:setup";
+            string testValue = "setup";
+            Environment.SetEnvironmentVariable(testKey, testValue);
+            ConfigurationSettings.SetConfigurationRootFactory(new UnitTestConfigurationRootFactory());

Review comment:
       Please do not plug in these components to `ConfigurationSettings.SetConfigurationRootFactory()`
when testing them, as there is no way to control which order the tests are run in and therefore
no way to tell whether tests in Lucene.Net.Tests.TestFramework are using the `TestConfigurationRootFactory`
or `UnitTestConfigurationRootFactory` class when they are run.
   
   Instead, you can mock the setup here in the test fixture as instance members so the static
state of `ConfigurationSettings` isn't polluted by these tests, and will always use the settings
read from `TestConfigurationRootFactory`.
   
   ```c#
       public abstract class ConfigurationSettingsTestCase : LuceneTestCase
       {
           internal IProperties SystemProperties { get; private set; }
   
           public IConfigurationSettings ConfigurationSettings { get; private set; }
   
   
           protected abstract IConfigurationRoot LoadConfiguration();
   
           public override void BeforeClass()
           {
               base.BeforeClass();
               var configurationRoot = LoadConfiguration();
               // Set up mocks for ConfigurationSettings and SystemProperties
               ConfigurationSettings = new ConfigurationSettingsImpl(configurationRoot);
               SystemProperties = new Properties(() => configurationRoot);
           }
   
           public interface IConfigurationSettings
           {
               IConfigurationRoot CurrentConfiguration { get; }
           }
   
           public class ConfigurationSettingsImpl : IConfigurationSettings
           {
               private readonly IConfigurationRoot configurationRoot;
   
               public ConfigurationSettingsImpl(IConfigurationRoot configurationRoot)
               {
                   this.configurationRoot = configurationRoot ?? throw new ArgumentNullException(nameof(configurationRoot));
               }
   
               public IConfigurationRoot CurrentConfiguration => configurationRoot;
           }
       }
   
   
       class TestConfigurationSettings : ConfigurationSettingsTestCase
       {
           // This variable must be unique in all of the tests
           private const string EnvironmentVariablePrefix = "lucenetest:";
   
           internal class UnitTestConfigurationRootFactory : IConfigurationRootFactory
           {
               public bool IgnoreSecurityExceptionsOnRead { get; set; }
               /// <summary>
               /// PAth to be used for configuration settings
               /// </summary>
               public static string JsonTestSettingsFolderName { get; set; } = @"Configuration";
               /// <summary>
               /// Filename to be used for configuration settings
               /// </summary>
               public static string JsonTestSettingsFileName { get; set; } = @"lucene.testsettings.json";
   
               static string JsonTestPath =
   #if TESTFRAMEWORK_NUNIT
               NUnit.Framework.TestContext.CurrentContext.TestDirectory;
   #else
                               AppDomain.CurrentDomain.BaseDirectory;
   #endif
               public UnitTestConfigurationRootFactory()
               {
               }
   
               protected IConfigurationRoot configuration = new ConfigurationBuilder().Add(new
LuceneDefaultConfigurationSource() { Prefix = EnvironmentVariablePrefix, IgnoreSecurityExceptionsOnRead
= false }
               ).AddJsonFile(Path.Combine(new string[] { JsonTestPath, JsonTestSettingsFolderName,
JsonTestSettingsFileName })).Build();
   
               public virtual IConfigurationRoot CurrentConfiguration
               {
                   get
                   {
                       return configuration;
                   }
               }
           }
   
           public static IConfigurationRootFactory TestConfigurationFactory;
   
           protected override IConfigurationRoot LoadConfiguration()
           {
               return new UnitTestConfigurationRootFactory().CurrentConfiguration;
           }
   
           [OneTimeSetUp]
           public override void BeforeClass()
           {
               base.BeforeClass();
               // set an Enviroment variable used in the test
               string testKey = EnvironmentVariablePrefix + "tests:setup";
               string testValue = "setup";
               Environment.SetEnvironmentVariable(testKey, testValue);
           }
   
           [Test]
           public virtual void ReadEnvironmentTest()
           {
               string testKey = "tests:setup";
               string testValue = "setup";
               Assert.AreEqual(testValue, ConfigurationSettings.CurrentConfiguration[testKey]);
               Assert.AreEqual(testValue, SystemProperties.GetProperty(testKey));
           }
           [Test]
           public virtual void SetEnvironmentTest()
           {
               string testKey = EnvironmentVariablePrefix + "tests:setting";
               string testValue = "test.success";
               ConfigurationSettings.CurrentConfiguration[testKey] = testValue;
               Assert.AreEqual(testValue, ConfigurationSettings.CurrentConfiguration[testKey]);
               Assert.AreEqual(testValue, SystemProperties.GetProperty(testKey));
           }
   
           [Test]
           public virtual void TestSetandUnset()
           {
               string testKey = "tests:locale";
               string testValue_fr = "fr";
               string testValue_en = "en";
               //ConfigurationSettings.CurrentConfiguration[testKey] = testValue_fr;
               Assert.AreEqual(testValue_fr, ConfigurationSettings.CurrentConfiguration["tests:locale"]);
               Assert.AreEqual(testValue_fr, ConfigurationSettings.CurrentConfiguration[testKey]);
               Assert.AreEqual(testValue_fr, SystemProperties.GetProperty(testKey));
               ConfigurationSettings.CurrentConfiguration[testKey] = testValue_en;
               Assert.AreEqual(testValue_en, ConfigurationSettings.CurrentConfiguration[testKey]);
               Assert.AreEqual(testValue_en, SystemProperties.GetProperty(testKey));
               ConfigurationSettings.CurrentConfiguration.Reload();
               Assert.AreEqual(testValue_fr, ConfigurationSettings.CurrentConfiguration[testKey]);
               Assert.AreEqual(testValue_fr, SystemProperties.GetProperty(testKey));
           } 
       }
   ```
   
   The `Properties` and `IProperties` implementations are in https://github.com/bongohrtech/lucenenet/pull/139.
   




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message