summaryrefslogtreecommitdiff
path: root/unittests/Enchant.Net.Tests/BrokerTests.cs
diff options
context:
space:
mode:
authorEric Albright <eric_albright@sil.org>2008-06-11 04:16:48 +0000
committerEric Albright <eric_albright@sil.org>2008-06-11 04:16:48 +0000
commitf26a55e59a9c6587fac03362d57617b93981ec6e (patch)
treed2d25eac3dbba44c2be1fd97a17797826e9743b1 /unittests/Enchant.Net.Tests/BrokerTests.cs
parent4e466d50fae1d50a132740090c23c7674ab7435a (diff)
downloadenchant-f26a55e59a9c6587fac03362d57617b93981ec6e.tar.gz
Enchant.Net - handle dictionaries being disposed of explicitly:
dictionary adds Disposed event broker removes dictionary from cache when it is disposed broker holds list of weak references in cache so dictionaries can still be garbage collected automatically when they go out of scope git-svn-id: svn+ssh://svn.abisource.com/svnroot/enchant/trunk@24226 bcba8976-2d24-0410-9c9c-aab3bd5fdfd6
Diffstat (limited to 'unittests/Enchant.Net.Tests/BrokerTests.cs')
-rw-r--r--unittests/Enchant.Net.Tests/BrokerTests.cs130
1 files changed, 112 insertions, 18 deletions
diff --git a/unittests/Enchant.Net.Tests/BrokerTests.cs b/unittests/Enchant.Net.Tests/BrokerTests.cs
index 892df34..288fb69 100644
--- a/unittests/Enchant.Net.Tests/BrokerTests.cs
+++ b/unittests/Enchant.Net.Tests/BrokerTests.cs
@@ -81,28 +81,33 @@ namespace Enchant.Tests
Path.Combine(providerDir, "libenchant_ispell.dll"), true);
File.Copy("libenchant_myspell.dll",
Path.Combine(providerDir, "libenchant_myspell.dll"), true);
+ InstallDictionary("myspell", new string[] { "en_US.aff", "en_US.dic" });
+ }
- string dictionarySourceDir =
- Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(
- Directory.GetCurrentDirectory(), ".."), ".."),
- "lib"), "share"),
- "enchant"), "myspell");
+ static private void InstallDictionary(string provider, IEnumerable<string> files)
+ {
+ string dictionarySourceDir =
+ Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(Path.Combine(
+ Directory.GetCurrentDirectory(), ".."), ".."),
+ "lib"), "share"),
+ "enchant"), provider);
- string dictionaryDestDir = Path.Combine(Path.Combine(Path.Combine(
- Directory.GetCurrentDirectory(), "share"), "enchant"),
- "myspell");
+ string dictionaryDestDir = Path.Combine(Path.Combine(Path.Combine(
+ Directory.GetCurrentDirectory(), "share"), "enchant"),
+ provider);
- if (!Directory.Exists(dictionaryDestDir))
- {
- Directory.CreateDirectory(dictionaryDestDir);
- }
+ if (!Directory.Exists(dictionaryDestDir))
+ {
+ Directory.CreateDirectory(dictionaryDestDir);
+ }
- File.Copy(Path.Combine(dictionarySourceDir, "en_US.aff"),
- Path.Combine(dictionaryDestDir, "en_US.aff"), true);
+ foreach (string file in files)
+ {
+ File.Copy(Path.Combine(dictionarySourceDir, file),
+ Path.Combine(dictionaryDestDir, file), true);
- File.Copy(Path.Combine(dictionarySourceDir, "en_US.dic"),
- Path.Combine(dictionaryDestDir, "en_US.dic"), true);
- }
+ }
+ }
[TestFixtureTearDown]
public void FixtureTearDown()
@@ -187,7 +192,75 @@ namespace Enchant.Tests
}
}
- [Test]
+ [Test]
+ public void RequestDictionary_CachingEnabled_DictionaryReRequested_SameReference()
+ {
+ using (Broker broker = new Broker())
+ {
+ broker.CacheDictionaries = true;
+ Dictionary dictionaryFirstRequest = broker.RequestDictionary("en_US");
+ Dictionary dictionarySecondRequest = broker.RequestDictionary("en_US");
+
+ Assert.AreSame(dictionaryFirstRequest, dictionarySecondRequest);
+ }
+ }
+
+ [Test]
+ public void RequestDictionary_CachingEnabled_DictionaryDisposedThenReRequested_DifferentReference()
+ {
+ using (Broker broker = new Broker())
+ {
+ broker.CacheDictionaries = true;
+ Dictionary dictionaryFirstRequest;
+ using (dictionaryFirstRequest = broker.RequestDictionary("en_US")) {}
+ Dictionary dictionarySecondRequest = broker.RequestDictionary("en_US");
+
+ Assert.AreNotSame(dictionaryFirstRequest, dictionarySecondRequest);
+ }
+ }
+
+ [Test]
+ public void RequestDictionary_CachingDisabled_DictionaryReRequested_DifferentReference()
+ {
+ using (Broker broker = new Broker())
+ {
+ broker.CacheDictionaries = false;
+ Dictionary dictionaryFirstRequest = broker.RequestDictionary("en_US");
+ Dictionary dictionarySecondRequest = broker.RequestDictionary("en_US");
+
+ Assert.AreNotSame(dictionaryFirstRequest, dictionarySecondRequest);
+ }
+ }
+
+ [Test]
+ public void RequestDictionary_CachingDisabled_DictionaryDisposedThenReRequested_DifferentReference()
+ {
+ using (Broker broker = new Broker())
+ {
+ broker.CacheDictionaries = false;
+ Dictionary dictionaryFirstRequest;
+ using (dictionaryFirstRequest = broker.RequestDictionary("en_US"))
+ {
+ }
+ Dictionary dictionarySecondRequest = broker.RequestDictionary("en_US");
+
+ Assert.AreNotSame(dictionaryFirstRequest, dictionarySecondRequest);
+ }
+ }
+
+ [Test]
+ [ExpectedException(typeof(ObjectDisposedException))]
+ public void Dispose_UseDictionaryAfterBrokerDisposed_Throws()
+ {
+ Dictionary dictionary;
+ using (Broker broker = new Broker())
+ {
+ dictionary = broker.RequestDictionary("en_US");
+ }
+ DictionaryInfo info = dictionary.Information;
+ }
+
+ [Test]
[ExpectedException(typeof (ApplicationException))]
public void RequestDictionary_DictionaryDoesNotExist_Throws()
{
@@ -219,5 +292,26 @@ namespace Enchant.Tests
broker.SetOrdering("en_US", "aspell, myspell, ispell");
}
}
+
+ [Test]
+ public void Finalize_DictionaryGoesOutOfScope_Finalized()
+ {
+ using (Broker broker = new Broker())
+ {
+ broker.CacheDictionaries = true;
+ WeakReference dictionaryReference = GetDictionaryReference(broker);
+ GC.Collect();
+ GC.WaitForPendingFinalizers();
+ GC.Collect();
+ Assert.IsFalse(dictionaryReference.IsAlive);
+ }
+ }
+
+ //this will allow the dictionary object to go out of scope
+ private static WeakReference GetDictionaryReference(Broker broker)
+ {
+ Dictionary dictionary = broker.RequestDictionary("en_US");
+ return new WeakReference(dictionary);
+ }
}
}