TestHelpers.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Diagnostics;
  29. using System.IO;
  30. using System.Text;
  31. using NUnit.Framework;
  32. using OpenMetaverse;
  33. namespace OpenSim.Tests.Common
  34. {
  35. public class TestHelpers
  36. {
  37. private static Stream EnableLoggingConfigStream
  38. = new MemoryStream(
  39. Encoding.UTF8.GetBytes(
  40. @"<log4net>
  41. <!-- A1 is set to be a ConsoleAppender -->
  42. <appender name=""A1"" type=""log4net.Appender.ConsoleAppender"">
  43. <!-- A1 uses PatternLayout -->
  44. <layout type=""log4net.Layout.PatternLayout"">
  45. <!-- Print the date in ISO 8601 format -->
  46. <!-- <conversionPattern value=""%date [%thread] %-5level %logger %ndc - %message%newline"" /> -->
  47. <conversionPattern value=""%date %message%newline"" />
  48. </layout>
  49. </appender>
  50. <!-- Set root logger level to DEBUG and its only appender to A1 -->
  51. <root>
  52. <level value=""DEBUG"" />
  53. <appender-ref ref=""A1"" />
  54. </root>
  55. </log4net>"));
  56. private static MemoryStream DisableLoggingConfigStream
  57. = new MemoryStream(
  58. Encoding.UTF8.GetBytes(
  59. // "<?xml version=\"1.0\" encoding=\"utf-8\" ?><configuration><log4net><root><level value=\"OFF\"/><appender-ref ref=\"A1\"/></root></log4net></configuration>"));
  60. //"<?xml version=\"1.0\" encoding=\"utf-8\" ?><configuration><log4net><root><level value=\"OFF\"/></root></log4net></configuration>")));
  61. // "<configuration><log4net><root><level value=\"OFF\"/></root></log4net></configuration>"));
  62. // "<configuration><log4net><root></root></log4net></configuration>")));
  63. // "<configuration><log4net><root/></log4net></configuration>"));
  64. "<log4net><root/></log4net>"));
  65. public static bool AssertThisDelegateCausesArgumentException(TestDelegate d)
  66. {
  67. try
  68. {
  69. d();
  70. }
  71. catch(ArgumentException)
  72. {
  73. return true;
  74. }
  75. return false;
  76. }
  77. /// <summary>
  78. /// A debugging method that can be used to print out which test method you are in
  79. /// </summary>
  80. public static void InMethod()
  81. {
  82. StackTrace stackTrace = new StackTrace();
  83. Console.WriteLine();
  84. Console.WriteLine("===> In Test Method : {0} <===", stackTrace.GetFrame(1).GetMethod().Name);
  85. }
  86. public static void EnableLogging()
  87. {
  88. log4net.Config.XmlConfigurator.Configure(EnableLoggingConfigStream);
  89. EnableLoggingConfigStream.Position = 0;
  90. }
  91. /// <summary>
  92. /// Disable logging whilst running the tests.
  93. /// </summary>
  94. /// <remarks>
  95. /// Remember, if a regression test throws an exception before completing this will not be invoked if it's at
  96. /// the end of the test.
  97. /// TODO: Always invoke this after every test - probably need to make all test cases inherit from a common
  98. /// TestCase class where this can be done.
  99. /// </remarks>
  100. public static void DisableLogging()
  101. {
  102. log4net.Config.XmlConfigurator.Configure(DisableLoggingConfigStream);
  103. DisableLoggingConfigStream.Position = 0;
  104. }
  105. /// <summary>
  106. /// Parse a UUID stem into a full UUID.
  107. /// </summary>
  108. /// <remarks>
  109. /// The fragment will come at the start of the UUID. The rest will be 0s
  110. /// </remarks>
  111. /// <returns></returns>
  112. /// <param name='frag'>
  113. /// A UUID fragment that will be parsed into a full UUID. Therefore, it can only contain
  114. /// cahracters which are valid in a UUID, except for "-" which is currently only allowed if a full UUID is
  115. /// given as the 'fragment'.
  116. /// </param>
  117. public static UUID ParseStem(string stem)
  118. {
  119. string rawUuid = stem.PadRight(32, '0');
  120. return UUID.Parse(rawUuid);
  121. }
  122. /// <summary>
  123. /// Parse tail section into full UUID.
  124. /// </summary>
  125. /// <param name="tail"></param>
  126. /// <returns></returns>
  127. public static UUID ParseTail(int tail)
  128. {
  129. return new UUID(string.Format("00000000-0000-0000-0000-{0:X12}", tail));
  130. }
  131. /// <summary>
  132. /// Parse a UUID tail section into a full UUID.
  133. /// </summary>
  134. /// <remarks>
  135. /// The fragment will come at the end of the UUID. The rest will be 0s
  136. /// </remarks>
  137. /// <returns></returns>
  138. /// <param name='frag'>
  139. /// A UUID fragment that will be parsed into a full UUID. Therefore, it can only contain
  140. /// cahracters which are valid in a UUID, except for "-" which is currently only allowed if a full UUID is
  141. /// given as the 'fragment'.
  142. /// </param>
  143. public static UUID ParseTail(string stem)
  144. {
  145. string rawUuid = stem.PadLeft(32, '0');
  146. return UUID.Parse(rawUuid);
  147. }
  148. }
  149. }