TestUtil.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // TestUtil.cs
  3. //
  4. // Author:
  5. // Martin Baulig <[email protected]>
  6. //
  7. // Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.IO;
  28. using System.Reflection;
  29. namespace MonoTests.System.Configuration.Util {
  30. public static class TestUtil {
  31. public static void RunWithTempFile (Action<string> action)
  32. {
  33. var filename = Path.GetTempFileName ();
  34. try {
  35. File.Delete (filename);
  36. action (filename);
  37. } finally {
  38. if (File.Exists (filename))
  39. File.Delete (filename);
  40. }
  41. }
  42. // Action<T1,T2> doesn't exist in .NET 2.0
  43. public delegate void MyAction<T1,T2> (T1 t1, T2 t2);
  44. public static void RunWithTempFiles (MyAction<string,string> action)
  45. {
  46. var file1 = Path.GetTempFileName ();
  47. var file2 = Path.GetTempFileName ();
  48. try {
  49. File.Delete (file1);
  50. File.Delete (file2);
  51. action (file1, file2);
  52. } finally {
  53. if (File.Exists (file1))
  54. File.Delete (file1);
  55. if (File.Exists (file2))
  56. File.Delete (file2);
  57. }
  58. }
  59. public static string DotNetVersion {
  60. get {
  61. #if NET_4_5
  62. return "net_4_5";
  63. #elif NET_4_0
  64. return "net_4_0";
  65. #else
  66. return "net_2_0";
  67. #endif
  68. }
  69. }
  70. public static string ThisDllName {
  71. get {
  72. var asm = Assembly.GetCallingAssembly ();
  73. return Path.GetFileName (asm.Location);
  74. }
  75. }
  76. public static string ThisConfigFileName {
  77. get {
  78. #if TARGET_JVM
  79. return "System.Configuration.Test20.jar.config";
  80. #else
  81. var asm = Assembly.GetCallingAssembly ();
  82. var exe = Path.GetFileName (asm.Location);
  83. return exe + ".config";
  84. #endif
  85. }
  86. }
  87. }
  88. }