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