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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
|
//
// System.Configuration.ApplicationsSettingsBaseTest.cs - Unit tests
// for System.Configuration.ApplicationSettingsBase.
//
// Author:
// Chris Toshok <toshok@ximian.com>
//
// Copyright (C) 2005, 2006 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//#define SPEW
#if NET_2_0
using System;
using System.Text;
using System.Configuration;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using NUnit.Framework;
using CategoryAttribute = NUnit.Framework.CategoryAttribute;
namespace MonoTests.System.Configuration {
class ProviderPoker : LocalFileSettingsProvider {
public override void Initialize (string name,
NameValueCollection values)
{
#if SPEW
Console.WriteLine ("Initialize '{0}'", name);
Console.WriteLine (Environment.StackTrace);
#endif
if (name == null)
name = "ProviderPoker";
base.Initialize (name, values);
}
public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context,
SettingsPropertyCollection properties)
{
#if SPEW
Console.WriteLine (Environment.StackTrace);
#endif
return base.GetPropertyValues (context, properties);
}
public override void SetPropertyValues (SettingsContext context,
SettingsPropertyValueCollection values)
{
#if SPEW
Console.WriteLine (Environment.StackTrace);
#endif
base.SetPropertyValues (context, values);
}
public override string ApplicationName {
get {
#if SPEW
Console.WriteLine (Environment.StackTrace);
#endif
return base.ApplicationName;
}
set {
#if SPEW
Console.WriteLine ("ApplicationName = {0}", value);
Console.WriteLine (Environment.StackTrace);
#endif
base.ApplicationName = value;
}
}
}
/* a basic settings class. just two settings, one application
* scoped, one user scoped */
class TestSettings1 : ApplicationSettingsBase
{
public TestSettings1() : base ("TestSettings1")
{
}
[ApplicationScopedSetting]
[DefaultSettingValue ("root")]
public string Username {
get { return (string)this["Username"]; }
set { this["Username"] = value; }
}
[UserScopedSetting]
[DefaultSettingValue ("8 Cambridge Center")]
public string Address {
get { return (string)this["Address"]; }
set { this["Address"] = value; }
}
}
/* an error according to msdn2 docs. both ApplicationScoped
* and UserScoped attributes on the same property */
class TestSettings2 : ApplicationSettingsBase
{
public TestSettings2() : base ("TestSettings2")
{
}
[ApplicationScopedSetting]
[UserScopedSetting]
[SettingsProvider (typeof (ProviderPoker))]
public string Username {
get { return (string)this["Username"]; }
set { this["Username"] = value; }
}
}
/* a custom provider for our setting */
class TestSettings3 : ApplicationSettingsBase
{
public TestSettings3() : base ("TestSettings3")
{
}
[ApplicationScopedSetting]
[SettingsProvider (typeof (ProviderPoker))]
public string Username {
get { return (string)this["Username"]; }
set { this["Username"] = value; }
}
}
class TestSettings4 : ApplicationSettingsBase {
public TestSettings4 ()
: base ("TestSettings4")
{
}
[ApplicationScopedSetting]
[DefaultSettingValue ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<ArrayOfString xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">\r\n <string>go</string>\r\n <string>mono</string>\r\n </ArrayOfString>")]
public StringCollection Values {
get { return (StringCollection) this ["Values"]; }
}
}
[TestFixture]
public class ApplicationSettingsBaseTest
{
[Test]
public void TestSettings1_Properties ()
{
TestSettings1 settings = new TestSettings1 ();
IEnumerator props = settings.Properties.GetEnumerator();
Assert.IsNotNull (props, "A1");
Assert.IsTrue (props.MoveNext(), "A2");
Assert.AreEqual ("Address", ((SettingsProperty)props.Current).Name, "A3");
Assert.IsTrue (props.MoveNext(), "A4");
Assert.AreEqual ("Username", ((SettingsProperty)props.Current).Name, "A5");
Assert.AreEqual ("root", settings.Username, "A6");
Assert.AreEqual ("8 Cambridge Center", settings.Address, "A7");
}
[Test]
public void TestSettings1_Provider ()
{
TestSettings1 settings = new TestSettings1 ();
/* since we didn't specify a provider for any
* of them, they should all use the
* LocalFileSettingsProvider */
foreach (SettingsProperty prop in settings.Properties) {
Assert.AreEqual (typeof (LocalFileSettingsProvider), prop.Provider.GetType(), "A1");
Console.WriteLine ("'{0}': '{1}'", prop.Provider.Name, prop.Provider.ApplicationName);
}
}
[Test]
public void TestSettings1_SetProperty ()
{
TestSettings1 settings = new TestSettings1 ();
bool setting_changing = false;
bool setting_changed = false;
settings.SettingChanging += delegate (object sender, SettingChangingEventArgs e) {
setting_changing = true;
Assert.AreEqual ("Username", e.SettingName, "A1");
Assert.AreEqual ("toshok", e.NewValue, "A2");
Assert.AreEqual ("TestSettings1", e.SettingKey, "A3");
Assert.AreEqual (settings.GetType().FullName, e.SettingClass, "A4");
};
settings.PropertyChanged += delegate (object sender, PropertyChangedEventArgs e) {
Assert.IsTrue (setting_changing, "A5");
setting_changed = true;
Assert.AreEqual ("Username", e.PropertyName, "A6");
};
settings.Username = "toshok";
Assert.IsTrue (setting_changing && setting_changed, "A7");
Assert.AreEqual ("toshok", settings.Username, "A8");
}
[Test]
public void TestSettings1_SetPropertyCancel ()
{
TestSettings1 settings = new TestSettings1 ();
bool setting_changing = false;
bool setting_changed = false;
settings.SettingChanging += delegate (object sender, SettingChangingEventArgs e) {
setting_changing = true;
Assert.AreEqual ("Username", e.SettingName, "A1");
Assert.AreEqual ("toshok", e.NewValue, "A2");
Assert.AreEqual ("TestSettings1", e.SettingKey, "A3");
Assert.AreEqual (settings.GetType().FullName, e.SettingClass, "A4");
e.Cancel = true;
};
settings.PropertyChanged += delegate (object sender, PropertyChangedEventArgs e) {
setting_changed = true;
Assert.Fail ("shouldn't reach here.", "A5");
};
settings.Username = "toshok";
Assert.IsTrue (setting_changing, "A6");
Assert.IsFalse (setting_changed, "A7");
Assert.AreEqual ("root", settings.Username, "A8");
}
[Test]
public void TestSettings1_SettingsLoaded ()
{
TestSettings1 settings = new TestSettings1 ();
bool settings_loaded = false;
SettingsProvider loaded_provider = null;
settings.SettingsLoaded += delegate (object sender, SettingsLoadedEventArgs e) {
settings_loaded = true;
loaded_provider = e.Provider;
};
Assert.AreEqual ("root", settings.Username, "A1");
Assert.IsTrue (settings_loaded, "A2");
Assert.AreEqual (loaded_provider, settings.Properties ["Username"].Provider, "A3");
}
[Test]
[Category ("NotWorking")]
public void TestSettings1_SetPropertyReset ()
{
TestSettings1 settings = new TestSettings1 ();
settings.Username = "toshok";
Assert.AreEqual ("toshok", settings.Username, "A1");
settings.Reset ();
Assert.AreEqual ("root", settings.Username, "A2");
}
[Test]
public void TestSettings2_Properties ()
{
// This test will fail when there are newer versions
// of the test assemblies - so conditionalize it in
// such cases.
#if TARGET_JVM
string expected = "MonoTests.System.Configuration.ProviderPoker, System.Test, Version=0.0.0.0";
#else
string expected = "MonoTests.System.Configuration.ProviderPoker, System_test_net_2_0, Version=0.0.0.0";
#endif
Assert.AreEqual (expected, new SettingsProviderAttribute (typeof (ProviderPoker)).ProviderTypeName.Substring (0, expected.Length), "#1");
TestSettings2 settings = new TestSettings2 ();
/* should throw ConfigurationException */
IEnumerator props = settings.Properties.GetEnumerator();
}
[Test]
[Ignore ("On MS.NET it returns null ...")]
public void TestSettings3_Properties ()
{
TestSettings3 settings = new TestSettings3 ();
Assert.AreEqual ("root", settings.Username, "A1");
}
public static void Main (string[] args)
{
ApplicationSettingsBaseTest test = new ApplicationSettingsBaseTest();
test.TestSettings1_Properties();
}
[Test]
public void Synchronized ()
{
Bug78430 s = new Bug78430 ();
s.Initialize (null, new SettingsPropertyCollection (),
new SettingsProviderCollection ());
SettingsBase sb = SettingsBase.Synchronized (s);
Assert.IsTrue (sb.IsSynchronized, "#1");
Assert.IsTrue (sb is Bug78430, "#2");
// these checks are so cosmetic, actually not
// worthy of testing.
Assert.IsTrue (Object.ReferenceEquals (s, sb), "#3");
Assert.IsFalse (sb.Properties.IsSynchronized, "#4");
}
class Bug78430 : SettingsBase
{
}
[Test] // bug #78654
public void DefaultSettingValueAs ()
{
Assert.AreEqual (1, new Bug78654 ().IntSetting);
}
class Bug78654 : ApplicationSettingsBase
{
[UserScopedSettingAttribute ()]
[DefaultSettingValueAttribute ("1")]
public int IntSetting {
get { return ((int)(this ["IntSetting"])); }
}
}
[Test]
public void TestSettings4_StringCollection_DefaultSettingValue ()
{
TestSettings4 settings = new TestSettings4 ();
Assert.AreEqual (2, settings.Values.Count, "Count");
Assert.AreEqual ("go", settings.Values[0], "0");
Assert.AreEqual ("mono", settings.Values[1], "1");
}
[Test]
[Category ("NotWorking")]
public void Providers ()
{
Assert.AreEqual (0, new TestSettings1 ().Providers.Count);
}
class Bug532180 : ApplicationSettingsBase {
[UserScopedSetting]
[DefaultSettingValue("10")]
public int IntSetting {
get { return (int)this["IntSetting"]; }
set { this["IntSetting"] = value; }
}
}
[Test] // bug #532180
public void DefaultSettingValueAsWithReload() {
Bug532180 settings = new Bug532180();
Assert.AreEqual(10, settings.IntSetting, "A1");
settings.IntSetting = 1;
Assert.AreEqual(1, settings.IntSetting, "A2");
settings.Reload();
Assert.AreEqual(10, settings.IntSetting, "A3");
}
}
}
#endif
|