1 #region Copyright 2 // 3 // Nini Configuration Project. 4 // Copyright (C) 2006 Brent R. Matzelle. All rights reserved. 5 // 6 // This software is published under the terms of the MIT X11 license, a copy of 7 // which has been included with this distribution in the LICENSE.txt file. 8 // 9 #endregion 10 11 using System; 12 using System.IO; 13 using Nini.Config; 14 15 namespace Nini.Test.Config 16 { 17 public class DotNetConsoleTests 18 { 19 static int assertCount = 0; 20 Main()21 public static int Main () 22 { 23 SetMessage ("Running tests..."); 24 try 25 { 26 WebTest (); 27 FileTest (); 28 FileAndSaveTest (); 29 } 30 catch (Exception e) 31 { 32 SetMessage ("Exception occurred: " + e.Message + "\r\n" + 33 "Stack trace: " + e.StackTrace); 34 Failure (); 35 } 36 37 DisplayResults (); 38 SetMessage ("All tests passed successfully"); 39 40 return 0; 41 } 42 WebTest()43 private static void WebTest () 44 { 45 string[] sections = new string[] { "appSettings", "Pets" }; 46 DotNetConfigSource source = new DotNetConfigSource (sections); 47 IConfig config = source.Configs["appSettings"]; 48 49 Assert (config != null, "IConfig is null"); 50 AssertEquals ("My App", config.Get ("App Name")); 51 52 config = source.Configs["Pets"]; 53 AssertEquals ("rover", config.Get ("dog")); 54 AssertEquals ("muffy", config.Get ("cat")); 55 Assert (config.Get("not here") == null, "Should not be present"); 56 } 57 FileTest()58 private static void FileTest () 59 { 60 DotNetConfigSource source = 61 new DotNetConfigSource (DotNetConfigSource.GetFullConfigPath ()); 62 IConfig config = source.Configs["appSettings"]; 63 64 Assert (config != null, "IConfig is null"); 65 AssertEquals ("My App", config.Get ("App Name")); 66 67 config = source.Configs["Pets"]; 68 AssertEquals ("rover", config.Get ("dog")); 69 AssertEquals ("muffy", config.Get ("cat")); 70 Assert (config.Get("not here") == null, "Should not be present"); 71 } 72 FileAndSaveTest()73 private static void FileAndSaveTest () 74 { 75 DotNetConfigSource source = 76 new DotNetConfigSource (DotNetConfigSource.GetFullConfigPath ()); 77 IConfig config = source.Configs["appSettings"]; 78 79 config = source.Configs["Pets"]; 80 AssertEquals ("rover", config.Get ("dog")); 81 AssertEquals ("muffy", config.Get ("cat")); 82 Assert (config.Get("not here") == null, "Should not be present"); 83 84 config.Set ("dog", "Spots"); 85 config.Set ("cat", "Misha"); 86 87 AssertEquals ("Spots", config.Get ("dog")); 88 AssertEquals ("Misha", config.Get ("cat")); 89 90 // Cannot perform save yet until technical issues resolved 91 /* 92 string fileName = "DotNetConfigSourceTests.exe.config"; 93 source.Save (); 94 95 source = new DotNetConfigSource (); 96 config = source.Configs["Pets"]; 97 AssertEquals ("Spots", config.Get ("dog")); 98 AssertEquals ("Misha", config.Get ("cat")); 99 100 File.Delete (fileName); 101 */ 102 } 103 104 #region Test methods DisplayResults()105 private static void DisplayResults () 106 { 107 SetMessage (""); 108 SetMessage ("Total asserts: " + assertCount); 109 } 110 Assert(bool value, string message)111 private static void Assert (bool value, string message) 112 { 113 assertCount++; 114 115 if (!value) { 116 SetMessage ("Assert failed: " + message); 117 Failure (); 118 } 119 } 120 AssertEquals(string searchValue, string actualValue)121 private static void AssertEquals (string searchValue, 122 string actualValue) 123 { 124 assertCount++; 125 126 if (searchValue != actualValue) { 127 SetMessage (String.Format ("Expected: [{0}], Actual: [{1}]. ", 128 searchValue, actualValue)); 129 Failure (); 130 } 131 } 132 Failure()133 private static void Failure () 134 { 135 DisplayResults (); 136 SetMessage ("Failure stopped operation"); 137 Environment.Exit (1); 138 } 139 SetMessage(string message)140 private static void SetMessage (string message) 141 { 142 Console.WriteLine (message); 143 } 144 #endregion 145 } 146 } 147