Moved SystemProperties file from TestFramework.Support to Core.Support and put in permission error suppressing
Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/ddb0f5d9 Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/ddb0f5d9 Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/ddb0f5d9 Branch: refs/heads/api-work Commit: ddb0f5d90aa62ac75b5f70bd8665572cad43c000 Parents: 5460c80 Author: Shad Storhaug <[email protected]> Authored: Fri Mar 3 06:11:22 2017 +0700 Committer: Shad Storhaug <[email protected]> Committed: Sun Mar 5 17:08:27 2017 +0700 ---------------------------------------------------------------------- src/Lucene.Net.Core/Lucene.Net.csproj | 1 + src/Lucene.Net.Core/Support/SystemProperties.cs | 152 +++++++++++++++++++ .../Lucene.Net.TestFramework.csproj | 3 - .../Support/SystemProperties.cs | 63 -------- .../Util/LuceneTestCase.cs | 4 +- 5 files changed, 155 insertions(+), 68 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucenenet/blob/ddb0f5d9/src/Lucene.Net.Core/Lucene.Net.csproj ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Lucene.Net.csproj b/src/Lucene.Net.Core/Lucene.Net.csproj index 5375f9e..40006b3 100644 --- a/src/Lucene.Net.Core/Lucene.Net.csproj +++ b/src/Lucene.Net.Core/Lucene.Net.csproj @@ -672,6 +672,7 @@ <Compile Include="Support\StringCharSequenceWrapper.cs" /> <Compile Include="Support\StringExtensions.cs" /> <Compile Include="Support\StringTokenizer.cs" /> + <Compile Include="Support\SystemProperties.cs" /> <Compile Include="Support\TaskSchedulerCompletionService.cs" /> <Compile Include="Support\ThreadFactory.cs" /> <Compile Include="Support\TimeHelper.cs" /> http://git-wip-us.apache.org/repos/asf/lucenenet/blob/ddb0f5d9/src/Lucene.Net.Core/Support/SystemProperties.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.Core/Support/SystemProperties.cs b/src/Lucene.Net.Core/Support/SystemProperties.cs new file mode 100644 index 0000000..5746e27 --- /dev/null +++ b/src/Lucene.Net.Core/Support/SystemProperties.cs @@ -0,0 +1,152 @@ +using System; +using System.Security; + +namespace Lucene.Net.Support +{ + /// <summary> + /// Helper for environment variables. This class helps to convert the environment + /// variables to int or bool data types and also silently handles read permission + /// errors. + /// <para/> + /// For instructions how to set environment variables for your OS, see + /// <a href="https://www.schrodinger.com/kb/1842">https://www.schrodinger.com/kb/1842</a>. + /// <para/> + /// Note that if you want to load any of these settings for your application from a + /// configuration file, it is recommended your application load them at startup and + /// call <see cref="SystemProperties.SetProperty(string, string)"/> to set them. + /// <para/> + /// Set the environment variable <c>lucene.ignoreSecurityExceptions</c> to <c>false</c> + /// to change the read behavior of these methods to throw the underlying exception + /// instead of returning the default value. + /// </summary> + public static class SystemProperties + { + /// <summary> + /// Retrieves the value of an environment variable from the current process. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <returns>The environment variable value.</returns> + public static string GetProperty(string key) + { + return GetProperty(key, null); + } + + /// <summary> + /// Retrieves the value of an environment variable from the current process, + /// with a default value if it doens't exist or the caller doesn't have + /// permission to read the value. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <param name="defaultValue">The value to use if the environment variable does not exist + /// or the caller doesn't have permission to read the value.</param> + /// <returns>The environment variable value.</returns> + public static string GetProperty(string key, string defaultValue) + { + return GetProperty<string>(key, defaultValue, + (str) => + { + return str; + } + ); + } + + /// <summary> + /// Retrieves the value of an environment variable from the current process + /// as <see cref="bool"/>. If the value cannot be cast to <see cref="bool"/>, returns <c>false</c>. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <returns>The environment variable value.</returns> + public static bool GetPropertyAsBoolean(string key) + { + return GetPropertyAsBoolean(key, false); + } + + /// <summary> + /// Retrieves the value of an environment variable from the current process as <see cref="bool"/>, + /// with a default value if it doens't exist, the caller doesn't have permission to read the value, + /// or the value cannot be cast to a <see cref="bool"/>. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <param name="defaultValue">The value to use if the environment variable does not exist, + /// the caller doesn't have permission to read the value, or the value cannot be cast to <see cref="bool"/>.</param> + /// <returns>The environment variable value.</returns> + public static bool GetPropertyAsBoolean(string key, bool defaultValue) + { + return GetProperty<bool>(key, defaultValue, + (str) => + { + bool value; + return bool.TryParse(str, out value) ? value : defaultValue; + } + ); + } + + /// <summary> + /// Retrieves the value of an environment variable from the current process + /// as <see cref="int"/>. If the value cannot be cast to <see cref="int"/>, returns <c>0</c>. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <returns>The environment variable value.</returns> + public static int GetPropertyAsInt32(string key) + { + return GetPropertyAsInt32(key, 0); + } + + /// <summary> + /// Retrieves the value of an environment variable from the current process as <see cref="int"/>, + /// with a default value if it doens't exist, the caller doesn't have permission to read the value, + /// or the value cannot be cast to a <see cref="int"/>. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <param name="defaultValue">The value to use if the environment variable does not exist, + /// the caller doesn't have permission to read the value, or the value cannot be cast to <see cref="int"/>.</param> + /// <returns>The environment variable value.</returns> + public static int GetPropertyAsInt32(string key, int defaultValue) + { + return GetProperty<int>(key, defaultValue, + (str) => + { + int value; + return int.TryParse(str, out value) ? value : defaultValue; + } + ); + } + + private static T GetProperty<T>(string key, T defaultValue, Func<string, T> conversionFunction) + { + string setting; + if (ignoreSecurityExceptions) + { + try + { + setting = Environment.GetEnvironmentVariable(key); + } + catch (SecurityException) + { + setting = null; + } + } + else + { + setting = Environment.GetEnvironmentVariable(key); + } + + return string.IsNullOrEmpty(setting) + ? defaultValue + : conversionFunction(setting); + } + + private static bool ignoreSecurityExceptions = GetPropertyAsBoolean("lucene.ignoreSecurityExceptions", true); + + /// <summary> + /// Creates, modifies, or deletes an environment variable stored in the current process. + /// </summary> + /// <param name="key">The name of the environment variable.</param> + /// <param name="value">The new environment variable value.</param> + /// <exception cref="SecurityException">The caller does not have the required permission to perform this operation.</exception> + public static void SetProperty(string key, string value) + { + Environment.SetEnvironmentVariable(key, value); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/ddb0f5d9/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj b/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj index a5c20b3..63771dd 100644 --- a/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj +++ b/src/Lucene.Net.TestFramework/Lucene.Net.TestFramework.csproj @@ -430,9 +430,6 @@ <Compile Include="Support\RandomizedTest.cs"> <SubType>Code</SubType> </Compile> - <Compile Include="Support\SystemProperties.cs"> - <SubType>Code</SubType> - </Compile> <Compile Include="Util\ApiScanTestBase.cs" /> <Compile Include="Util\Automaton\AutomatonTestUtil.cs"> <SubType>Code</SubType> http://git-wip-us.apache.org/repos/asf/lucenenet/blob/ddb0f5d9/src/Lucene.Net.TestFramework/Support/SystemProperties.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Support/SystemProperties.cs b/src/Lucene.Net.TestFramework/Support/SystemProperties.cs deleted file mode 100644 index cc0ecdb..0000000 --- a/src/Lucene.Net.TestFramework/Support/SystemProperties.cs +++ /dev/null @@ -1,63 +0,0 @@ -using System; - -namespace Lucene.Net.Support -{ - public static class SystemProperties - { - public static string GetProperty(string key) - { - return GetProperty(key, null); - } - - public static string GetProperty(string key, string defaultValue) - { - return GetProperty<string>(key, defaultValue, - (str) => - { - return str; - }); - } - - public static bool GetPropertyAsBoolean(string key) - { - return GetPropertyAsBoolean(key, false); - } - - public static bool GetPropertyAsBoolean(string key, bool defaultValue) - { - return GetProperty<bool>(key, defaultValue, - (str) => - { - bool value; - return bool.TryParse(str, out value) ? value : defaultValue; - }); - } - - public static int GetPropertyAsInt(string key) - { - return GetPropertyAsInt(key, 0); - } - - public static int GetPropertyAsInt(string key, int defaultValue) - { - return GetProperty<int>(key, defaultValue, - (str) => - { - int value; - return int.TryParse(str, out value) ? value : defaultValue; - } - ); - } - - private static T GetProperty<T>(string key, T defaultValue, System.Func<string, T> conversionFunction) - { - string setting = Environment.GetEnvironmentVariable(key); - return string.IsNullOrEmpty(setting) ? defaultValue : conversionFunction(setting); - } - - public static void SetProperty(string key, string value) - { - Environment.SetEnvironmentVariable(key, value); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/lucenenet/blob/ddb0f5d9/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs ---------------------------------------------------------------------- diff --git a/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs b/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs index 5b93c3e..c7521a6 100644 --- a/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs +++ b/src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs @@ -315,7 +315,7 @@ namespace Lucene.Net.Util /// A random multiplier which you should use when writing random tests: /// multiply it by the number of iterations to scale your tests (for nightly builds). /// </summary> - public static readonly int RANDOM_MULTIPLIER = SystemProperties.GetPropertyAsInt("tests.multiplier", 1); + public static readonly int RANDOM_MULTIPLIER = SystemProperties.GetPropertyAsInt32("tests.multiplier", 1); /// <summary> /// TODO: javadoc? </summary> @@ -379,7 +379,7 @@ namespace Lucene.Net.Util LEAVE_TEMPORARY = defaultValue; CORE_DIRECTORIES = new List<string>(FS_DIRECTORIES); CORE_DIRECTORIES.Add("RAMDirectory"); - int maxFailures = SystemProperties.GetPropertyAsInt(SYSPROP_MAXFAILURES, int.MaxValue); + int maxFailures = SystemProperties.GetPropertyAsInt32(SYSPROP_MAXFAILURES, int.MaxValue); bool failFast = SystemProperties.GetPropertyAsBoolean(SYSPROP_FAILFAST, false); if (failFast)
