Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,476 @@
//
// 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";
#elif NET_4_5
string expected = "MonoTests.System.Configuration.ProviderPoker, System_test_net_4_5, Version=0.0.0.0";
#elif NET_4_0
string expected = "MonoTests.System.Configuration.ProviderPoker, System_test_net_4_0, 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");
}
class Bug8592ConfHolder : ApplicationSettingsBase {
[UserScopedSetting]
public string TestKey1OnHolder {
get { return (string) this ["TestKey1OnHolder"] ?? ""; }
set { this ["TestKey1OnHolder"] = value; }
}
}
[Test]
public void TestBug8592BasicOperations ()
{
var holder = new Bug8592ConfHolder ();
holder.Reset ();
holder.Save ();
Assert.AreEqual ("", holder.TestKey1OnHolder);
holder.TestKey1OnHolder = "candy";
Assert.AreEqual ("candy", holder.TestKey1OnHolder);
holder.Reload ();
Assert.AreEqual ("", holder.TestKey1OnHolder);
holder.TestKey1OnHolder = "candy";
Assert.AreEqual ("candy", holder.TestKey1OnHolder);
holder.Save ();
Assert.AreEqual ("candy", holder.TestKey1OnHolder);
holder.Reload ();
Assert.AreEqual ("candy", holder.TestKey1OnHolder);
holder.Reset ();
Assert.AreEqual ("", holder.TestKey1OnHolder);
}
class Bug8533ConfHolder1 : ApplicationSettingsBase {
[UserScopedSetting]
public string TestKey1OnHolder1 {
get { return (string) this ["TestKey1OnHolder1"] ?? ""; }
set { this ["TestKey1OnHolder1"] = value; }
}
[UserScopedSetting]
public string TestKey1OnHolder2 {
get { return (string) this ["TestKey1OnHolder2"] ?? ""; }
set { this ["TestKey1OnHolder2"] = value; }
}
[UserScopedSetting]
public string TestKey {
get { return (string) this ["TestKey"] ?? ""; }
set { this ["TestKey"] = value; }
}
}
class Bug8533ConfHolder2 : ApplicationSettingsBase {
[UserScopedSetting]
public string TestKey1OnHolder2 {
get { return (string) this ["TestKey1OnHolder2"] ?? ""; }
set { this ["TestKey1OnHolder2"] = value; }
}
[UserScopedSetting]
public string TestKey {
get { return (string) this ["TestKey"] ?? ""; }
set { this ["TestKey"] = value; }
}
}
[Test]
public void TestBug8533ConfHandlerWronglyMixedUp ()
{
var holder1 = new Bug8533ConfHolder1 ();
var holder2 = new Bug8533ConfHolder2 ();
holder1.TestKey1OnHolder1 = "candy";
holder2.TestKey1OnHolder2 = "donut";
holder1.TestKey = "eclair";
holder1.Save ();
holder2.Save ();
holder1.Reload ();
holder2.Reload();
Assert.AreEqual ("", holder1.TestKey1OnHolder2);
Assert.AreEqual ("candy", holder1.TestKey1OnHolder1);
Assert.AreEqual ("donut", holder2.TestKey1OnHolder2);
Assert.AreEqual ("eclair", holder1.TestKey);
Assert.AreEqual ("", holder2.TestKey);
}
}
}
#endif

View File

@@ -0,0 +1,73 @@
2008-09-14 Gert Driesen <drieseng@users.sourceforge.net>
* ConfigXmlDocumentTest.cs: Added test for Load (String).
2008-09-14 Gert Driesen <drieseng@users.sourceforge.net>
* ProviderCollectionTest.cs: Moved to System.Configuration assembly.
2008-06-25 Gert Driesen <drieseng@users.sourceforge.net>
* ConfigurationExceptionTest.cs: Added ctor tests.
* ApplicationSettingsBaseTest.cs: Fixed line endings.
2007-10-30 Arina Itkes <arinai@mainsoft.com>
* SettingsBaseTest.cs: Replacing Win32Exception to
CustomerException for compilation with TARGET_JVM defined.
* ApplicationSettingsBaseTest.cs: Replacing ProviderTypeName
to actual for compilation with TARGET_JVM defined.
2007-06-13 Atsushi Enomoto <atsushi@ximian.com>
* SettingElementTest.cs : test initial Value.
2007-05-31 Atsushi Enomoto <atsushi@ximian.com>
* ApplicationSettingsBaseTest.cs : NotWorking Providers().
2007-05-30 Atsushi Enomoto <atsushi@ximian.com>
* SettingsBaseTest.cs : new test.
* ApplicationSettingsBaseTest.cs : added Providers().
2007-05-28 Atsushi Enomoto <atsushi@ximian.com>
* SettingElementTest.cs : blame catch.
2007-05-28 Atsushi Enomoto <atsushi@ximian.com>
* SettingElementTest.cs : new test.
2007-05-28 Atsushi Enomoto <atsushi@ximian.com>
* ApplicationSettingsBaseTest.cs : enable working test.
2007-01-14 Vladimir Krasnov <vladimirk@mainsoft.com>
* SettingsPropertyValueTest.cs: added tests for IsDirty proprty
2007-01-08 Vladimir Krasnov <vladimirk@mainsoft.com>
* SettingsPropertyValueTest.cs: added tests of default value
deserealization
2006-11-09 Sebastien Pouliot <sebastien@ximian.com>
* ApplicationSettingsBaseTest.cs: Added test case for StringCollection
properties (that encode the string list in XML).
2006-11-05 Vladimir Krasnov <vladimirk@mainsoft.com>
* SettingsPropertyValueTest.cs: added tests of value deserealization
2006-06-29 Atsushi Enomoto <atsushi@ximian.com>
* ApplicationSettingsBaseTest.cs : added test for bug #78654.
2006-05-17 Atsushi Enomoto <atsushi@ximian.com>
* LocalFileSettingsProviderTest.cs : commented some lines in
Initialized() as they don't work under .NET.
* ApplicationSettingsBaseTest.cs : added Synchronized(), test for
bug #78430.

View File

@@ -0,0 +1,81 @@
//
// System.Configuration.ConfigXmlDocumentTest.cs - Unit tests for
// System.Configuration.ConfigXmlDocument.
//
// Author:
// Gert Driesen <drieseng@users.sourceforge.net>
//
// Copyright (C) 2008 Gert Driesen
//
// 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.
//
#if NET_2_0
using System;
using System.IO;
using System.Configuration;
using NUnit.Framework;
namespace MonoTests.System.Configuration {
[TestFixture]
public class ConfigXmlDocumentTest {
private string tempFolder;
[SetUp]
public void SetUp ()
{
tempFolder = Path.Combine (Path.GetTempPath (), this.GetType ().FullName);
if (!Directory.Exists (tempFolder))
Directory.CreateDirectory (tempFolder);
}
[TearDown]
public void TearDown ()
{
if (Directory.Exists (tempFolder))
Directory.Delete (tempFolder, true);
}
[Test]
public void Load ()
{
string config_xml = @"
<configuration>
<appSettings>
<add key='anyKey' value='42' />
</appSettings>
<system.diagnostics />
</configuration>";
string config_file = Path.Combine (tempFolder, "config.xml");
File.WriteAllText (config_file, config_xml);
ConfigXmlDocument doc = new ConfigXmlDocument ();
doc.Load (config_file);
Assert.AreEqual (1, doc.ChildNodes.Count, "ChildNodes");
Assert.AreEqual (config_file, doc.Filename, "Filename");
Assert.AreEqual ("#document", doc.Name, "Name");
File.Delete (config_file);
}
}
}
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,148 @@
//
// System.Configuration.LocalFileSettingsProviderTest.cs - Unit tests
// for System.Configuration.LocalFileSettingsProvider.
//
// Author:
// Chris Toshok <toshok@ximian.com>
//
// Copyright (C) 2005 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.
//
#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;
namespace MonoTests.System.Configuration {
[TestFixture]
public class LocalFileSettingsProviderTest
{
[Test]
public void Properties ()
{
LocalFileSettingsProvider prov = new LocalFileSettingsProvider ();
// defaults, uninitialized
Assert.IsNull (prov.Name, "A1");
Assert.AreEqual ("", prov.ApplicationName, "A2");
prov.ApplicationName = "foo";
Assert.AreEqual ("foo", prov.ApplicationName, "A3");
prov.ApplicationName = null;
Assert.IsNull (prov.ApplicationName, "A4");
}
[Test]
public void Initialized ()
{
LocalFileSettingsProvider prov = new LocalFileSettingsProvider ();
prov.Initialize (null, null);
// defaults, uninitialized
Assert.AreEqual ("LocalFileSettingsProvider", prov.Name, "A1");
Assert.AreEqual ("", prov.ApplicationName, "A2");
prov = new LocalFileSettingsProvider ();
NameValueCollection nv = new NameValueCollection ();
nv.Add ("applicationName", "appName");
prov.Initialize ("hi", nv);
// As these lines below shows, Initialize() behavior is unpredictable. Here I just comment out them and fix run-test-ondotnet tests.
//Assert.AreEqual ("hi", prov.Name, "A3");
//Assert.AreEqual ("hi", prov.Description, "A3.5");
//Assert.AreEqual ("", prov.ApplicationName, "A4");
}
[Test]
public void GetUserScopedPropertyValues ()
{
SettingsAttributeDictionary dict = new SettingsAttributeDictionary ();
UserScopedSettingAttribute attr = new UserScopedSettingAttribute ();
dict.Add (attr.GetType(), attr);
LocalFileSettingsProvider prov = new LocalFileSettingsProvider ();
SettingsContext ctx = new SettingsContext ();
SettingsProperty p = new SettingsProperty ("property",
typeof (int),
prov,
false,
10,
SettingsSerializeAs.Binary,
dict,
false,
false);
SettingsPropertyCollection col = new SettingsPropertyCollection ();
SettingsPropertyValueCollection vals;
col.Add (p);
prov.Initialize (null, null);
vals = prov.GetPropertyValues (ctx, col);
Assert.IsNotNull (vals, "A1");
Assert.AreEqual (1, vals.Count, "A2");
}
[Test]
public void GetApplicationScopedPropertyValues ()
{
SettingsAttributeDictionary dict = new SettingsAttributeDictionary ();
ApplicationScopedSettingAttribute attr = new ApplicationScopedSettingAttribute ();
dict.Add (attr.GetType(), attr);
LocalFileSettingsProvider prov = new LocalFileSettingsProvider ();
SettingsContext ctx = new SettingsContext ();
SettingsProperty p = new SettingsProperty ("property",
typeof (int),
prov,
false,
10,
SettingsSerializeAs.Binary,
dict,
false,
false);
SettingsPropertyCollection col = new SettingsPropertyCollection ();
SettingsPropertyValueCollection vals;
col.Add (p);
prov.Initialize (null, null);
vals = prov.GetPropertyValues (ctx, col);
Assert.IsNotNull (vals, "A1");
Assert.AreEqual (1, vals.Count, "A2");
}
}
}
#endif

View File

@@ -0,0 +1,83 @@
//
// SettingElementTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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.
//
#if NET_2_0
using System;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;
using NUnit.Framework;
namespace MonoTests.System.Configuration
{
[TestFixture]
public class SettingElementTest
{
[Test]
public void Initial ()
{
SettingElement el = new SettingElement ();
Assert.IsNotNull (el.Value, "#1");
Assert.IsNull (el.Value.ValueXml, "#2");
}
[Test]
public void CollectionAddNull ()
{
try {
SettingElementCollection c = new SettingElementCollection ();
c.Add (null);
Assert.Fail ();
} catch (NullReferenceException) {
// .net s cks here
} catch (ArgumentNullException) {
}
}
[Test]
public void CollectionAddNameless ()
{
SettingElement el = new SettingElement ();
Assert.AreEqual (String.Empty, el.Name, "premise #1");
SettingElementCollection c = new SettingElementCollection ();
Assert.AreEqual (ConfigurationElementCollectionType.BasicMap, c.CollectionType, "premise #2");
c.Add (el);
Assert.AreEqual (el, c.Get (""), "#1");
}
[Test]
public void CollectionGetNonExistent ()
{
SettingElementCollection c = new SettingElementCollection ();
Assert.IsNull (c.Get ("nonexistent"));
}
}
}
#endif

View File

@@ -0,0 +1,343 @@
//
// SettingsBaseTest.cs
//
// Author:
// Atsushi Enomoto <atsushi@ximian.com>
//
// Copyright (C) 2007 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
{
[TestFixture]
public class SettingsBaseTest
{
#if TARGET_JVM
class CustomerException : Exception { }
#endif
class MySettings : SettingsBase
{
[UserScopedSetting] // ignored in non-ApplicationSettingsBase
public int Foo {
get { return (int) this ["Foo"]; }
set { this ["Foo"] = value; }
}
[UserScopedSetting] // ignored in non-ApplicationSettingsBase
[DefaultSettingValue ("20")]
public int Bar {
get { return (int) this ["Bar"]; }
set { this ["Bar"] = value; }
}
}
class MySettings2 : SettingsBase
{
int foo;
[UserScopedSetting] // ignored in non-ApplicationSettingsBase
public int Foo {
get { return (int) this ["Foo"]; }
set { this ["Foo"] = value; }
}
public override SettingsPropertyCollection Properties {
get { return null; }
}
public SettingsPropertyCollection BaseProperties {
get { return base.Properties; }
}
}
[Test]
public void PropertyDefaults ()
{
MySettings s = new MySettings ();
Assert.IsNull (s.Properties, "#1");
Assert.IsNull (s.Providers, "#2");
Assert.IsNull (s.Context, "#3");
Assert.AreEqual (0, s.PropertyValues.Count, "#4");
Assert.IsNull (s.Properties, "#5");
Assert.IsNull (s.Providers, "#6");
Assert.IsNull (s.Context, "#7");
s.Initialize (s.Context, s.Properties, s.Providers);
}
[Test]
public void PropertiesOverriden ()
{
MySettings2 s = new MySettings2 ();
s.Initialize (s.Context, new SettingsPropertyCollection (), s.Providers);
Assert.IsNull (s.Properties, "#1");
Assert.IsNotNull (s.BaseProperties, "#2");
Assert.AreEqual (0, s.PropertyValues.Count, "#3");
}
[Test]
public void PropertyValuesInstance ()
{
SettingsPropertyCollection props = new SettingsPropertyCollection ();
SettingsProviderCollection provs = new SettingsProviderCollection ();
MyProvider p = new MyProvider ();
MySettings s = new MySettings ();
props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
provs.Add (p);
s.Initialize (new SettingsContext (), props, provs);
Assert.AreEqual (s.PropertyValues, s.PropertyValues);
}
[Test]
public void PropertyValuesUninitialized ()
{
MySettings s = new MySettings ();
s.Initialize (new SettingsContext (), new SettingsPropertyCollection (), new SettingsProviderCollection ());
s.Properties.Add (new SettingsProperty ("Foo"));
// values are filled only at initialization phase.
Assert.AreEqual (0, s.PropertyValues.Count, "#1");
}
[Test]
public void PropertyValuesInitialized ()
{
SettingsPropertyCollection props = new SettingsPropertyCollection ();
SettingsProviderCollection provs = new SettingsProviderCollection ();
MyProvider p = new MyProvider ();
MySettings s = new MySettings ();
int i;
try {
i = s.Foo;
Assert.Fail ("#1-2");
} catch (SettingsPropertyNotFoundException) {
}
s.Initialize (new SettingsContext (), props, provs);
Assert.AreEqual (0, s.PropertyValues.Count, "#2-1");
Assert.AreEqual (0, s.Context.Count, "#2-2");
props.Add (new SettingsProperty ("Foo", typeof (int), p, false, 10, SettingsSerializeAs.String, null, true, true));
// initialize w/o the provider
s.Initialize (new SettingsContext (), props, provs);
Assert.AreEqual (0, s.PropertyValues.Count, "#3-0");
Assert.AreEqual (100, s.Foo, "#3-1");
// ... !!!
Assert.AreEqual (1, s.PropertyValues.Count, "#3-2");
SettingsPropertyValue v = s.PropertyValues ["Foo"];
Assert.AreEqual (100, v.PropertyValue, "#3-3");
Assert.AreEqual (0, s.Context.Count, "#3-4");
// initialize w/ the provider
provs.Add (p);
provs.Add (new MyProvider2 ("Bar", 25));
props.Add (new SettingsProperty ("Bar", typeof (int), provs ["MyProvider2"], false, 10, SettingsSerializeAs.String, null, true, true));
s.Initialize (new SettingsContext (), props, provs);
Assert.AreEqual (1, s.PropertyValues.Count, "#4-1");
Assert.AreEqual (100, s.Foo, "#4-2");
Assert.AreEqual (25, s.Bar, "#4-3");
// ... !!!
Assert.AreEqual (2, s.PropertyValues.Count, "#4-3-2");
Assert.AreEqual (0, s.Context.Count, "#4-4");
// wrong provider
props.Remove ("Bar");
props.Add (new SettingsProperty ("Bar", typeof (int), provs ["MyProvider"], false, 10, SettingsSerializeAs.String, null, true, true));
s = new MySettings ();
s.Initialize (new SettingsContext (), props, provs);
Assert.AreEqual (0, s.PropertyValues.Count, "#5-1");
Assert.AreEqual (100, s.Foo, "#5-2");
Assert.AreEqual (10, s.Bar, "#5-3");
}
[Test]
public void AddPropertyTypeMismatch ()
{
SettingsPropertyCollection props = new SettingsPropertyCollection ();
SettingsProviderCollection provs = new SettingsProviderCollection ();
MyProvider p = new MyProvider ();
MySettings s = new MySettings ();
props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
provs.Add (p);
s.Initialize (new SettingsContext (), props, provs);
int i = s.Foo; // it still works as int, regardless of the settings property type...
}
[Test]
[Ignore (".NET throws NRE, which means that it is not well designed.")]
public void AddPropertyNoProviderButInProviders ()
{
SettingsPropertyCollection props = new SettingsPropertyCollection ();
SettingsProviderCollection provs = new SettingsProviderCollection ();
MyProvider p = new MyProvider ();
MySettings s = new MySettings ();
props.Add (new SettingsProperty ("Foo", typeof (string), null, false, 10, SettingsSerializeAs.String, null, true, true));
provs.Add (p);
s.Initialize (new SettingsContext (), props, provs);
Assert.AreEqual (100, s.Foo);
}
[Test]
public void ExceptionalGetPropertyValues ()
{
SettingsPropertyCollection props = new SettingsPropertyCollection ();
SettingsProviderCollection provs = new SettingsProviderCollection ();
MyProvider3 p = new MyProvider3 ();
MySettings s = new MySettings ();
props.Add (new SettingsProperty ("Foo", typeof (string), p, false, 10, SettingsSerializeAs.String, null, true, true));
provs.Add (p);
s.Initialize (new SettingsContext (), props, provs);
Assert.AreEqual (0, s.Context.Count, "#0");
try {
Assert.AreEqual (100, s.Foo, "#1");
Assert.Fail ("#2");
#if !TARGET_JVM
} catch (Win32Exception) {
#else
} catch (CustomerException) {
#endif
}
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ProviderCollectionAddNameless ()
{
new SettingsProviderCollection ().Add (
new MyProvider (true));
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ProviderCollectionAddDuplicate ()
{
SettingsProviderCollection c = new SettingsProviderCollection ();
c.Add (new MyProvider ());
c.Add (new MyProvider ());
}
class MyProvider3 : MyProvider
{
public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context, SettingsPropertyCollection props)
{
#if !TARGET_JVM
throw new Win32Exception (); // unlikely thrown otherwise.
#else
throw new CustomerException (); // unlikely thrown otherwise.
#endif
}
}
class MyProvider2 : MyProvider
{
public MyProvider2 (string item, object value)
: base (item, value)
{
}
public override string Name {
get { return "MyProvider2"; }
}
}
class MyProvider : SettingsProvider
{
bool bogus;
string item;
object default_value;
public MyProvider ()
: this (false)
{
}
public MyProvider (bool bogus)
{
this.item = "Foo";
default_value = 100;
this.bogus = bogus;
}
public MyProvider (string item, object value)
{
this.item = item;
this.default_value = value;
}
public override string Name {
get { return bogus ? null : "MyProvider"; }
}
string app;
public override string ApplicationName {
get { return app; }
set { app = value; }
}
public override SettingsPropertyValueCollection GetPropertyValues (SettingsContext context, SettingsPropertyCollection props)
{
SettingsPropertyValueCollection vals =
new SettingsPropertyValueCollection ();
foreach (SettingsProperty p in props)
if (p.Provider == this) {
SettingsPropertyValue pv = new SettingsPropertyValue (p);
if (pv.Name == item)
pv.PropertyValue = default_value;
vals.Add (pv);
}
return vals;
}
public override void SetPropertyValues (SettingsContext context, SettingsPropertyValueCollection collection)
{
throw new Exception ();
}
}
}
}
#endif

View File

@@ -0,0 +1,162 @@
//
// System.Configuration.SettingsPropertyCollectionTest.cs - Unit tests
// for System.Configuration.SettingsPropertyCollection.
//
// Author:
// Chris Toshok <toshok@ximian.com>
//
// Copyright (C) 2005 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.
//
#if NET_2_0
using System;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;
using NUnit.Framework;
namespace MonoTests.System.Configuration {
[TestFixture]
public class SettingsPropertyCollectionTest {
[Test]
public void Add ()
{
SettingsPropertyCollection col = new SettingsPropertyCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
Assert.AreEqual (0, col.Count, "A1");
col.Add (test_prop);
Assert.AreEqual (1, col.Count, "A2");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void AddDuplicate ()
{
SettingsPropertyCollection col = new SettingsPropertyCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
col.Add (test_prop);
Assert.AreEqual (1, col.Count, "A1");
test_prop = new SettingsProperty ("test_prop");
col.Add (test_prop);
Assert.AreEqual (1, col.Count, "A2");
}
[Test]
public void Remove ()
{
SettingsPropertyCollection col = new SettingsPropertyCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
col.Add (test_prop);
Assert.AreEqual (1, col.Count, "A1");
col.Remove ("test_prop");
Assert.AreEqual (0, col.Count, "A2");
}
[Test]
public void Remove_NonExistant ()
{
SettingsPropertyCollection col = new SettingsPropertyCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
col.Add (test_prop);
Assert.AreEqual (1, col.Count, "A1");
col.Remove ("test_prop2");
Assert.AreEqual (1, col.Count, "A2");
}
[Test]
public void Clear ()
{
SettingsPropertyCollection col = new SettingsPropertyCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
col.Add (test_prop);
Assert.AreEqual (1, col.Count, "A1");
col.Clear ();
Assert.AreEqual (0, col.Count, "A2");
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void ReadOnly_Add ()
{
SettingsPropertyCollection col = new SettingsPropertyCollection ();
col.SetReadOnly ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
col.Add (test_prop);
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void ReadOnly_Remove ()
{
SettingsPropertyCollection col = new SettingsPropertyCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
col.Add (test_prop);
col.SetReadOnly ();
col.Remove ("test_prop");
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void ReadOnly_Clear ()
{
SettingsPropertyCollection col = new SettingsPropertyCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
col.Add (test_prop);
col.SetReadOnly ();
col.Clear ();
}
}
}
#endif

View File

@@ -0,0 +1,146 @@
//
// System.Configuration.SettingsPropertyTest.cs - Unit tests for
// System.Configuration.SettingsProperty.
//
// Author:
// Chris Toshok <toshok@ximian.com>
//
// Copyright (C) 2005 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.
//
#if NET_2_0
using System;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;
using NUnit.Framework;
namespace MonoTests.System.Configuration {
[TestFixture]
public class SettingsPropertyTest {
[Test]
public void Ctor_1 ()
{
SettingsProperty p = new SettingsProperty ("property",
typeof (int),
null,
true,
10,
SettingsSerializeAs.Binary,
null,
true,
false);
Assert.AreEqual ("property", p.Name, "A1");
Assert.AreEqual (typeof (int), p.PropertyType, "A2");
Assert.AreEqual (null, p.Provider, "A3");
Assert.AreEqual (10, (int)p.DefaultValue, "A4");
Assert.AreEqual (SettingsSerializeAs.Binary, p.SerializeAs, "A5");
Assert.IsNull (p.Attributes, "A6");
Assert.IsTrue (p.ThrowOnErrorDeserializing, "A7");
Assert.IsFalse (p.ThrowOnErrorSerializing, "A8");
Assert.IsTrue (p.IsReadOnly, "A9");
}
[Test]
public void Ctor_2 ()
{
SettingsProperty q = new SettingsProperty ("property",
typeof (int),
null,
true,
10,
SettingsSerializeAs.Binary,
new SettingsAttributeDictionary(),
true,
false);
SettingsProperty p = new SettingsProperty (q);
Assert.AreEqual ("property", p.Name, "A1");
Assert.AreEqual (typeof (int), p.PropertyType, "A2");
Assert.AreEqual (null, p.Provider, "A3");
Assert.AreEqual (10, (int)p.DefaultValue, "A4");
Assert.AreEqual (SettingsSerializeAs.Binary, p.SerializeAs, "A5");
Assert.IsNotNull (p.Attributes, "A6");
Assert.IsTrue (p.ThrowOnErrorDeserializing, "A7");
Assert.IsFalse (p.ThrowOnErrorSerializing, "A8");
Assert.IsTrue (p.IsReadOnly, "A9");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Ctor_2_AttributesNull ()
{
/* same as above, but a null
* SettingsAttributeDictionary, which causes a
* ANE in the ctor. */
SettingsProperty q = new SettingsProperty ("property",
typeof (int),
null,
true,
10,
SettingsSerializeAs.Binary,
null,
true,
false);
SettingsProperty p = new SettingsProperty (q);
}
[Test]
public void Ctor_2_NameNull ()
{
SettingsProperty q = new SettingsProperty (null,
typeof (int),
null,
true,
10,
SettingsSerializeAs.Binary,
new SettingsAttributeDictionary(),
true,
false);
}
[Test]
public void Ctor_3 ()
{
SettingsProperty p = new SettingsProperty ("property");
Assert.AreEqual ("property", p.Name, "A1");
Assert.AreEqual (null, p.PropertyType, "A2");
Assert.AreEqual (null, p.Provider, "A3");
Assert.AreEqual (null, p.DefaultValue, "A4");
Assert.AreEqual (SettingsSerializeAs.String, p.SerializeAs, "A5");
Assert.IsNotNull (p.Attributes, "A6");
Assert.IsFalse (p.ThrowOnErrorDeserializing, "A7");
Assert.IsFalse (p.ThrowOnErrorSerializing, "A8");
Assert.IsFalse (p.IsReadOnly, "A9");
}
}
}
#endif

View File

@@ -0,0 +1,168 @@
//
// System.Configuration.SettingsPropertyCollectionTest.cs - Unit tests
// for System.Configuration.SettingsPropertyCollection.
//
// Author:
// Chris Toshok <toshok@ximian.com>
//
// Copyright (C) 2005 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.
//
#if NET_2_0
using System;
using System.Text;
using System.Configuration;
using System.Collections.Specialized;
using NUnit.Framework;
namespace MonoTests.System.Configuration {
[TestFixture]
public class SettingsPropertyValueCollectionTest {
[Test]
public void Add ()
{
SettingsPropertyValueCollection col = new SettingsPropertyValueCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
SettingsPropertyValue val = new SettingsPropertyValue (test_prop);
Assert.AreEqual (0, col.Count, "A1");
col.Add (val);
Assert.AreEqual (1, col.Count, "A2");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void AddDuplicate ()
{
SettingsPropertyValueCollection col = new SettingsPropertyValueCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
SettingsPropertyValue val = new SettingsPropertyValue (test_prop);
col.Add (val);
Assert.AreEqual (1, col.Count, "A1");
col.Add (val);
Assert.AreEqual (1, col.Count, "A2");
}
[Test]
public void Remove ()
{
SettingsPropertyValueCollection col = new SettingsPropertyValueCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
SettingsPropertyValue val = new SettingsPropertyValue (test_prop);
col.Add (val);
Assert.AreEqual (1, col.Count, "A1");
col.Remove ("test_prop");
Assert.AreEqual (0, col.Count, "A2");
}
[Test]
public void Remove_NonExistant ()
{
SettingsPropertyValueCollection col = new SettingsPropertyValueCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
SettingsPropertyValue val = new SettingsPropertyValue (test_prop);
col.Add (val);
Assert.AreEqual (1, col.Count, "A1");
col.Remove ("test_prop2");
Assert.AreEqual (1, col.Count, "A2");
}
[Test]
public void Clear ()
{
SettingsPropertyValueCollection col = new SettingsPropertyValueCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
SettingsPropertyValue val = new SettingsPropertyValue (test_prop);
col.Add (val);
Assert.AreEqual (1, col.Count, "A1");
col.Clear ();
Assert.AreEqual (0, col.Count, "A2");
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void ReadOnly_Add ()
{
SettingsPropertyValueCollection col = new SettingsPropertyValueCollection ();
col.SetReadOnly ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
SettingsPropertyValue val = new SettingsPropertyValue (test_prop);
col.Add (val);
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void ReadOnly_Remove ()
{
SettingsPropertyValueCollection col = new SettingsPropertyValueCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
SettingsPropertyValue val = new SettingsPropertyValue (test_prop);
col.Add (val);
col.SetReadOnly ();
col.Remove ("test_prop");
}
[Test]
[ExpectedException (typeof (NotSupportedException))]
public void ReadOnly_Clear ()
{
SettingsPropertyValueCollection col = new SettingsPropertyValueCollection ();
SettingsProperty test_prop = new SettingsProperty ("test_prop");
SettingsPropertyValue val = new SettingsPropertyValue (test_prop);
col.Add (val);
col.SetReadOnly ();
col.Clear ();
}
}
}
#endif

View File

@@ -0,0 +1,441 @@
//
// System.Configuration.SettingsPropertyValueTest.cs - Unit tests for
// System.Configuration.SettingsPropertyValue.
//
// Author:
// Chris Toshok <toshok@ximian.com>
//
// Copyright (C) 2005 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.
//
#if NET_2_0
using System;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.Serialization.Formatters.Binary;
using NUnit.Framework;
namespace MonoTests.System.Configuration {
[TestFixture]
public class SettingsPropertyValueTest {
[Test]
public void Properties ()
{
SettingsProperty p = new SettingsProperty ("property",
typeof (int),
null,
true,
10,
SettingsSerializeAs.String,
null,
true,
false);
SettingsPropertyValue v = new SettingsPropertyValue (p);
Assert.IsFalse (v.Deserialized, "A1");
Assert.IsFalse (v.IsDirty, "A2");
Assert.AreEqual ("property", v.Name, "A3");
Assert.AreEqual (p, v.Property, "A4");
Assert.AreEqual ((object)10, v.PropertyValue, "A5");
Assert.AreEqual (null, v.SerializedValue, "A6");
Assert.IsTrue (v.UsingDefaultValue, "A7");
/* test that setting v.PropertyValue to
* something else causes SerializedValue to
* become not-null */
v.PropertyValue = (object)5;
Assert.AreEqual ("5", v.SerializedValue, "A9");
/* test to see whether or not changing
* SerializeAs causes SerializedValue to
* change */
p.SerializeAs = SettingsSerializeAs.Xml;
Assert.AreEqual ("5", v.SerializedValue, "A11"); /* nope.. */
/* only changing PropertyValue does */
v.PropertyValue = (object)7;
Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>7</int>", ((string)v.SerializedValue).Replace ("\r\n", "\n"), "A13");
}
[Test]
public void Dirty ()
{
SettingsProperty p = new SettingsProperty ("property",
typeof (int),
null,
true,
10,
SettingsSerializeAs.String,
null,
true,
false);
SettingsPropertyValue v = new SettingsPropertyValue (p);
Assert.AreEqual (10, v.PropertyValue, "A0");
Assert.IsFalse (v.IsDirty, "A1");
/* set PropertyValue to something else */
v.PropertyValue = 5;
Assert.IsTrue (v.IsDirty, "A2");
v.IsDirty = false;
/* set PropertyValue to the same thing */
v.PropertyValue = 5;
Assert.IsTrue (v.IsDirty, "A3");
/* try out a non-value type */
p = new SettingsProperty ("property",
typeof (StringWriter),
null,
true,
"",
SettingsSerializeAs.String,
null,
true,
false);
v = new SettingsPropertyValue (p);
Assert.IsNotNull (v.PropertyValue, "A5");
Console.WriteLine (v.PropertyValue);
Assert.IsTrue (v.IsDirty, "A6");
}
[Test]
public void UsingDefaultValue ()
{
SettingsProperty p = new SettingsProperty ("property",
typeof (int),
null,
true,
10,
SettingsSerializeAs.String,
null,
true,
false);
SettingsPropertyValue v = new SettingsPropertyValue (p);
Assert.AreEqual (10, v.PropertyValue, "A1");
Assert.IsTrue (v.UsingDefaultValue, "A2");
/* set PropertyValue to something else */
v.PropertyValue = 5;
Assert.IsFalse (v.UsingDefaultValue, "A3");
/* set PropertyValue back to the default */
v.PropertyValue = 10;
Assert.IsFalse (v.UsingDefaultValue, "A4");
}
[Test]
public void String_Deserialize ()
{
SettingsProperty p = new SettingsProperty ("property",
typeof (int),
null,
true,
10,
SettingsSerializeAs.String,
null,
true,
false);
SettingsPropertyValue v = new SettingsPropertyValue (p);
v.SerializedValue = "123";
Assert.AreEqual (123, v.PropertyValue, "A1");
Assert.AreEqual (typeof(int), v.PropertyValue.GetType (), "A2");
Assert.AreEqual (false, v.UsingDefaultValue, "A3");
}
[Test]
public void Xml_Deserialize ()
{
SettingsProperty p = new SettingsProperty ("property",
typeof (int),
null,
true,
10,
SettingsSerializeAs.Xml,
null,
true,
false);
SettingsPropertyValue v = new SettingsPropertyValue (p);
v.SerializedValue = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>123</int>";
Assert.AreEqual (123, v.PropertyValue, "A1");
Assert.AreEqual (typeof(int), v.PropertyValue.GetType (), "A2");
Assert.AreEqual (false, v.UsingDefaultValue, "A3");
}
[Test]
public void String_Xml_Serialize ()
{
SettingsProperty p = new SettingsProperty ("property",
typeof (int),
null,
true,
10,
SettingsSerializeAs.String,
null,
true,
false);
SettingsPropertyValue v = new SettingsPropertyValue (p);
v.PropertyValue = 10;
Assert.AreEqual (10, v.PropertyValue, "A1");
Assert.AreEqual ("10", v.SerializedValue, "A2");
v.PropertyValue = 10;
p.SerializeAs = SettingsSerializeAs.Xml;
Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>10</int>", ((string)v.SerializedValue).Replace ("\r\n", "\n"), "A3");
}
[Test]
public void Binary_Serialize ()
{
SettingsProperty p = new SettingsProperty ("property",
typeof (int),
null,
true,
10,
SettingsSerializeAs.Binary,
null,
true,
false);
SettingsPropertyValue v = new SettingsPropertyValue (p);
byte[] foo;
v.PropertyValue = 10;
Assert.AreEqual (typeof (byte[]), v.SerializedValue.GetType(), "A1");
foo = (byte[])v.SerializedValue;
v.PropertyValue = 5;
Assert.AreEqual (5, v.PropertyValue, "A2");
BinaryFormatter bf = new BinaryFormatter ();
MemoryStream ms = new MemoryStream (foo);
Assert.AreEqual (10, bf.Deserialize (ms), "A3");
v.Deserialized = false;
v.SerializedValue = foo;
Assert.AreEqual (10, v.PropertyValue, "A4");
}
[Test]
public void DefaultValueType ()
{
SettingsProperty p1 = new SettingsProperty ("property",
typeof (int),
null,
true,
(int) 10,
SettingsSerializeAs.String,
null,
true,
false);
SettingsPropertyValue v1 = new SettingsPropertyValue (p1);
Assert.AreEqual (typeof (int), v1.PropertyValue.GetType (), "A1");
Assert.AreEqual (10, v1.PropertyValue, "A2");
SettingsProperty p2 = new SettingsProperty ("property",
typeof (int),
null,
true,
"10",
SettingsSerializeAs.String,
null,
true,
false);
SettingsPropertyValue v2 = new SettingsPropertyValue (p2);
Assert.AreEqual (typeof (int), v2.PropertyValue.GetType (), "A3");
Assert.AreEqual (10, v2.PropertyValue, "A4");
}
[Serializable]
public class MyData
{
private int intProp = 777;
public int IntProp
{
get { return intProp; }
set { intProp = value; }
}
}
[Test]
public void DefaultValueCompexTypeEmpty ()
{
SettingsProperty p1 = new SettingsProperty ("property",
typeof (MyData),
null,
true,
"",
SettingsSerializeAs.String,
null,
true,
false);
SettingsPropertyValue v1 = new SettingsPropertyValue (p1);
Assert.IsNotNull (v1.PropertyValue, "A1");
Assert.AreEqual (typeof (MyData), v1.PropertyValue.GetType (), "A2");
MyData h = (MyData) v1.PropertyValue;
Assert.AreEqual (777, h.IntProp, "A3");
}
[Test]
public void DefaultValueCompexType ()
{
SettingsProperty p2 = new SettingsProperty ("property",
typeof (MyData),
null,
true,
@"<?xml version=""1.0"" encoding=""utf-16""?><MyData xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""><IntProp>5</IntProp></MyData>",
SettingsSerializeAs.Xml,
null,
true,
false);
SettingsPropertyValue v2 = new SettingsPropertyValue (p2);
Assert.IsNotNull (v2.PropertyValue, "A1");
Assert.AreEqual (typeof (MyData), v2.PropertyValue.GetType (), "A2");
MyData h = (MyData) v2.PropertyValue;
Assert.AreEqual (5, h.IntProp, "A3");
}
[Test]
public void IsDirtyAndValueDateTime ()
{
SettingsProperty sp = new SettingsProperty ("heh");
sp.PropertyType = typeof (DateTime);
SettingsPropertyValue spv = new SettingsPropertyValue (sp);
Assert.IsFalse (spv.IsDirty, "A1");
Assert.IsNotNull (spv.PropertyValue, "A2");
Assert.AreEqual (typeof (DateTime), spv.PropertyValue.GetType (), "A3");
Assert.IsFalse (spv.IsDirty, "A4");
}
[Test]
public void IsDirtyAndValuePrimitive ()
{
SettingsProperty sp = new SettingsProperty ("heh");
sp.PropertyType = typeof (int);
SettingsPropertyValue spv = new SettingsPropertyValue (sp);
Assert.IsFalse (spv.IsDirty, "A1");
Assert.AreEqual (0, spv.PropertyValue, "A2");
Assert.AreEqual (typeof (int), spv.PropertyValue.GetType (), "A3");
Assert.IsFalse (spv.IsDirty, "A4");
}
[Test]
public void IsDirtyAndValueDecimal ()
{
SettingsProperty sp = new SettingsProperty ("heh");
sp.PropertyType = typeof (decimal);
SettingsPropertyValue spv = new SettingsPropertyValue (sp);
Assert.IsFalse (spv.IsDirty, "A1");
Assert.AreEqual (0, spv.PropertyValue, "A2");
Assert.AreEqual (typeof (decimal), spv.PropertyValue.GetType (), "A3");
Assert.IsTrue (spv.IsDirty, "A4");
}
[Test]
public void IsDirtyAndValueString ()
{
SettingsProperty sp = new SettingsProperty ("heh");
sp.PropertyType = typeof (string);
SettingsPropertyValue spv = new SettingsPropertyValue (sp);
Assert.IsFalse (spv.IsDirty, "A1");
Assert.IsNull (spv.PropertyValue, "A2");
Assert.IsFalse (spv.IsDirty, "A3");
SettingsProperty sp2 = new SettingsProperty ("heh");
sp2.PropertyType = typeof (string);
sp2.DefaultValue = "";
SettingsPropertyValue spv2 = new SettingsPropertyValue (sp2);
Assert.IsFalse (spv2.IsDirty, "A4");
Assert.IsNotNull (spv2.PropertyValue, "A5");
Assert.IsFalse (spv2.IsDirty, "A6");
}
[Serializable]
public struct MyData2
{
public int intProp;
}
[Test]
public void IsDirtyAndValueMyData2 ()
{
SettingsProperty sp = new SettingsProperty ("heh");
sp.PropertyType = typeof (MyData2);
SettingsPropertyValue spv = new SettingsPropertyValue (sp);
Assert.IsFalse (spv.IsDirty, "A1");
Assert.IsNotNull (spv.PropertyValue, "A2");
Assert.IsTrue (spv.IsDirty, "A3");
}
[Test]
public void IsDirtyAndValueArrayList ()
{
SettingsProperty sp = new SettingsProperty ("heh");
sp.PropertyType = typeof (ArrayList);
SettingsPropertyValue spv = new SettingsPropertyValue (sp);
Assert.IsFalse (spv.IsDirty, "A1");
Assert.IsNull (spv.PropertyValue, "A2");
Assert.IsFalse (spv.IsDirty, "A3");
SettingsProperty sp2 = new SettingsProperty ("heh");
sp2.PropertyType = typeof (ArrayList);
sp2.DefaultValue = "";
SettingsPropertyValue spv2 = new SettingsPropertyValue (sp2);
Assert.IsFalse (spv2.IsDirty, "A5");
Assert.IsNotNull (spv2.PropertyValue, "A6");
Assert.AreEqual (typeof (ArrayList), spv2.PropertyValue.GetType (), "A7");
Assert.IsTrue (spv2.IsDirty, "A8");
}
}
}
#endif