コード例 #1
0
        public void EnumeratorTest()
        {
            var manager = new HybridResourceManager("KGySoft.CoreLibraries.Resources.TestCompiledResource", GetType().Assembly, resXBaseName);
            var resName = "TestString";

            manager.Source = ResourceManagerSources.CompiledOnly;
            string resCompiled  = manager.GetString(resName, inv);
            var    enumCompiled = manager.GetResourceSet(inv, true, false).GetEnumerator();

            manager.Source = ResourceManagerSources.ResXOnly;
            string resResx  = manager.GetString(resName, inv);
            var    enumResx = manager.GetResourceSet(inv, true, false).GetEnumerator();

            manager.Source = ResourceManagerSources.CompiledAndResX;
            string resHybrid  = manager.GetString(resName, inv);
            var    enumHybrid = manager.GetResourceSet(inv, true, false).GetEnumerator();

            // the hybrid enumerator filters the duplicates
            string[] keysCompiled = enumCompiled.GetKeysEnumerator().ToArray();
            string[] keysResx     = enumResx.GetKeysEnumerator().ToArray();
            string[] keysHybrid   = enumHybrid.GetKeysEnumerator().ToArray();
            Assert.IsTrue(keysCompiled.Length + keysResx.Length > keysHybrid.Length);
            Assert.AreEqual(keysHybrid.Length, keysCompiled.Union(keysResx).Count());
            Assert.AreEqual(keysHybrid.Length, keysHybrid.Distinct().Count());

            // the duplicated values are returned from the resx
            Assert.AreEqual(resResx, resHybrid);
            Assert.AreNotEqual(resCompiled, resHybrid);

            // reset works properly
            enumHybrid.Reset();
            Assert.IsTrue(keysHybrid.SequenceEqual(enumHybrid.GetKeysEnumerator()));

            // during the enumeration an exception occurs in any state of the enumeration
            // 1. during the resx enumeration
            enumHybrid.Reset();
            enumHybrid.MoveNext();
            Assert.IsTrue(keysResx.Contains(enumHybrid.Key.ToString()));
            manager.SetObject("new", 42, inv);
            Throws <InvalidOperationException>(() => enumHybrid.MoveNext());

            // 2. during the compiled enumeration
            enumHybrid = manager.GetResourceSet(inv, true, false).GetEnumerator();
            string compiledOnlyKey = keysCompiled.Except(keysResx).First();

            do
            {
                enumHybrid.MoveNext();
            } while (enumHybrid.Key.ToString() != compiledOnlyKey);
            manager.SetObject("new", -42, inv);
            Throws <InvalidOperationException>(() => enumHybrid.MoveNext());
        }
コード例 #2
0
        public void SaveTest()
        {
            var manager = new HybridResourceManager("KGySoft.CoreLibraries.Resources.TestCompiledResource", GetType().Assembly, resXBaseName);

            // empty manager: save all is false even if forcing
            Assert.IsFalse(manager.IsModified);
            Assert.IsFalse(manager.SaveAllResources(true));

            // non-empty but unmodified manager: saving on forcing
            manager.GetResourceSet(inv, true, false);
            Assert.IsFalse(manager.IsModified);
            Assert.IsFalse(manager.SaveAllResources(false));
            //Assert.IsTrue(manager.SaveAllResources(true)); // - was OK in MSTest as it supports deployment
            manager.ReleaseAllResources();

            // adding a new value to a non-existing resource
            // it will be dirty and can be saved without forcing, then it is not dirty any more
            manager.SetObject("new value en-GB", 42, enGB);
            Assert.IsTrue(manager.IsModified);
            Assert.IsTrue(manager.SaveAllResources(false));
            Assert.IsFalse(manager.IsModified);

            // adding a new value: it will be dirty and saves without forcing, then it is not dirty any more
            manager.SetObject("new value", 42, enUS);
            Assert.IsTrue(manager.IsModified);
            Assert.IsNotNull(manager.GetResourceSet(enUS, false, false));
            Assert.IsFalse(manager.SaveResourceSet(inv));
            //Assert.IsTrue(manager.SaveResourceSet(enUS)); // - was OK in MSTest as it supports deployment
            manager.GetExpandoResourceSet(enUS, ResourceSetRetrieval.GetIfAlreadyLoaded).Save(new MemoryStream()); // in NUnit saving into memory so output folder will not change
            Assert.IsFalse(manager.IsModified);

            // in compiled only mode save returns always false
            manager.SetObject("new value inv", -42, inv);
            Assert.IsTrue(manager.IsModified);
            manager.Source = ResourceManagerSources.CompiledOnly;
            Assert.IsFalse(manager.IsModified);
            Assert.IsFalse(manager.SaveResourceSet(inv));
            Assert.IsFalse(manager.SaveAllResources(true));
            manager.Source = ResourceManagerSources.ResXOnly;
            Assert.IsTrue(manager.IsModified);
            //Assert.IsTrue(manager.SaveResourceSet(inv)); // - was OK in MSTest as it supports deployment
            manager.GetExpandoResourceSet(inv, ResourceSetRetrieval.GetIfAlreadyLoaded).Save(new MemoryStream()); // in NUnit saving into memory so output folder will not change
            Assert.IsFalse(manager.IsModified);

            // removing added new files
            Clean(manager, enGB);
        }
コード例 #3
0
        public void SetNullAndRemoveTest()
        {
            var manager = new HybridResourceManager("KGySoft.CoreLibraries.Resources.TestCompiledResource", GetType().Assembly, resXBaseName);
            var resName = "TestString";
            var resEnUs = manager.GetObject(resName, enUS);

            // enUS has been loaded only so the result came from this rs
            var rsEnUs = manager.GetResourceSet(enUS, false, false);

            Assert.IsNotNull(rsEnUs);
            Assert.IsNull(manager.GetResourceSet(en, false, false));
            Assert.IsNull(manager.GetResourceSet(inv, false, false));

            // enUS is hybrid
            Assert.IsInstanceOf <HybridResourceSet>(rsEnUs);

            // if we nullify the resource, it will hide the compiled one and getting the enUS returns the base value from en
            manager.SetObject(resName, null, enUS);
            var resEn = manager.GetObject(resName, en);

            Assert.AreEqual(resEn, manager.GetObject(resName, enUS));
            Assert.AreNotEqual(resEn, resEnUs);
            Assert.IsNull(rsEnUs.GetObject(resName));

            // but if we remove the resource, the compiled one will be visible
            manager.ReleaseAllResources();
            manager.RemoveObject(resName, enUS);
            var resEnUsCompiled = manager.GetObject(resName, enUS);

            Assert.AreNotEqual(resEnUs, resEnUsCompiled);

            // it came from the enUS, too: after releasing all, only this has been loaded
            rsEnUs = manager.GetResourceSet(enUS, false, false);
            Assert.IsNotNull(rsEnUs);
            Assert.IsNull(manager.GetResourceSet(en, false, false));
            Assert.IsNull(manager.GetResourceSet(inv, false, false));
            Assert.IsNotNull(rsEnUs.GetObject(resName));
        }
コード例 #4
0
        public void GetResourceSetTest()
        {
            var manager = new HybridResourceManager("KGySoft.CoreLibraries.Resources.TestCompiledResource", GetType().Assembly, resXBaseName);

            // checking that invariant exists in all strategies and it has the correct type
            manager.Source = ResourceManagerSources.ResXOnly;
            Assert.AreEqual("ResXResourceSet", manager.GetResourceSet(inv, loadIfExists: true, tryParents: false).GetType().Name);
            manager.Source = ResourceManagerSources.CompiledOnly;
            Assert.AreEqual("RuntimeResourceSet", manager.GetResourceSet(inv, loadIfExists: true, tryParents: false).GetType().Name);
            manager.Source = ResourceManagerSources.CompiledAndResX;
            var rsInv = manager.GetResourceSet(inv, loadIfExists: true, tryParents: false);

            Assert.AreEqual("HybridResourceSet", rsInv.GetType().Name);

            // enUS should not return invariant when [assembly: NeutralResourcesLanguage("en-US")] is not set
            Assert.AreNotSame(rsInv, manager.GetResourceSet(enUS, true, false));

            // and en != inv
            Assert.AreNotSame(rsInv, manager.GetResourceSet(en, true, false));

            // hu does not exist
            Assert.IsNull(manager.GetResourceSet(hu, loadIfExists: true, tryParents: false));

            // but returns inv when parents are required
            Assert.AreSame(rsInv, manager.GetResourceSet(hu, loadIfExists: true, tryParents: true));

            // when already obtained, the already obtained sets are returned for createIfNotExists = false
            Assert.IsNotNull(manager.GetResourceSet(inv, loadIfExists: false, tryParents: false));

            // when not obtained, the tryParents=false will simply return null
            manager.ReleaseAllResources();
            Assert.IsNull(manager.GetResourceSet(inv, loadIfExists: false, tryParents: false));

            // when not obtained but exists, the tryParents=true will also return null if createIfNotExists=false
            Assert.IsNull(manager.GetResourceSet(inv, loadIfExists: false, tryParents: true));

            // but for for non-existing name even this will throw an exception
            manager = new HybridResourceManager("NonExisting", typeof(object).Assembly); // typeof(object): mscorlib has en-US invariant resources language
            Throws <MissingManifestResourceException>(() => manager.GetResourceSet(inv, loadIfExists: false, tryParents: true));

            // createIfNotExists = true will throw an exception as well
            Throws <MissingManifestResourceException>(() => manager.GetResourceSet(inv, loadIfExists: true, tryParents: true));

            // except if tryParents=false, because in this case null will be returned
            Assert.IsNull(manager.GetResourceSet(inv, loadIfExists: true, tryParents: false));

            // loading a resource set where there are more compiled ones and just invariant resx
            manager = new HybridResourceManager("KGySoft.CoreLibraries.Resources.TestCompiledResource", GetType().Assembly, "TestRes");
            Assert.AreEqual("HybridResourceSet", manager.GetResourceSet(inv, true, false).GetType().Name);

            // enUS exists only in compiled
            Assert.AreEqual("RuntimeResourceSet", manager.GetResourceSet(enUS, true, false).GetType().Name);

            // but an expando RS can be forced for it
            Assert.AreEqual("HybridResourceSet", manager.GetExpandoResourceSet(enUS, ResourceSetRetrieval.LoadIfExists, false).GetType().Name);

            // except if source is compiled only
            manager.Source = ResourceManagerSources.CompiledOnly;
            Assert.IsNull(manager.GetExpandoResourceSet(enUS, ResourceSetRetrieval.LoadIfExists, false));

            // in system ResourceManager if a derived culture is required but only a parent is available, then this parent will be
            // cached for derived cultures, too, so groveling is needed only once. Hybrid caches cultures, too, but uses
            // proxies to remark when a cached resource must be replaced.

            // System: requiring hu loads inv and caches this for hu, too
            manager.ReleaseAllResources();
            manager.Source         = ResourceManagerSources.CompiledOnly;
            Assert.IsNotNull(rsInv = manager.GetResourceSet(hu, loadIfExists: true, tryParents: true));
            Assert.AreSame(rsInv, manager.GetResourceSet(inv, loadIfExists: false, tryParents: false));
            Assert.AreSame(rsInv, manager.GetResourceSet(hu, loadIfExists: false, tryParents: false));

            // ResX: requiring hu loads inv and caches a proxy for hu, too, and outside this is transparent so proxy returns inv, too
            manager.ReleaseAllResources();
            manager.Source         = ResourceManagerSources.ResXOnly;
            Assert.IsNotNull(rsInv = manager.GetResourceSet(hu, loadIfExists: true, tryParents: true));
            Assert.AreSame(rsInv, manager.GetResourceSet(inv, loadIfExists: false, tryParents: false));
            Assert.AreSame(rsInv, manager.GetResourceSet(hu, loadIfExists: false, tryParents: false));

            // Hybrid: requiring hu loads inv and caches a proxy for hu, too, and outside this is transparent so proxy returns inv, too
            manager.ReleaseAllResources();
            manager.Source         = ResourceManagerSources.CompiledAndResX;
            Assert.IsNotNull(rsInv = manager.GetResourceSet(hu, loadIfExists: true, tryParents: true));
            Assert.AreSame(rsInv, manager.GetResourceSet(inv, loadIfExists: false, tryParents: false));
            Assert.AreSame(rsInv, manager.GetResourceSet(hu, loadIfExists: false, tryParents: false));

            // now if we change something in hu, the cached inv proxy will be replaced
            manager.SetObject("test", 42, hu);
            ResourceSet rsHU;

            Assert.IsNotNull(rsHU = manager.GetResourceSet(hu, loadIfExists: false, tryParents: false));
            Assert.AreNotSame(rsInv, rsHU);

            // though en exist, we haven't load it yet, so if we don't load it, it will return a proxy for inv, too
            Assert.IsNotNull(rsInv = manager.GetResourceSet(inv, loadIfExists: false, tryParents: false));
            Assert.IsNull(manager.GetResourceSet(enUS, loadIfExists: false, tryParents: false));
            Assert.AreSame(rsInv, manager.GetResourceSet(enUS, loadIfExists: false, tryParents: true));
            Assert.AreSame(rsInv, manager.GetResourceSet(enUS, loadIfExists: false, tryParents: false));

            // but this proxy is replaced when loading the existing file is really requested (this is a difference to system version)
            Assert.AreNotSame(rsInv, manager.GetResourceSet(enUS, loadIfExists: true, tryParents: false));

            // creating inv, inv(en), inv(enGB) (these have unloaded parent); inv(hu), inv(huHU) (these have no unloaded parents)
            manager.ReleaseAllResources();
            Assert.IsNotNull(rsInv = manager.GetResourceSet(inv, loadIfExists: true, tryParents: false));
            Assert.AreSame(rsInv, manager.GetResourceSet(enGB, loadIfExists: false, tryParents: true));
            Assert.AreSame(rsInv, manager.GetResourceSet(huHU, loadIfExists: false, tryParents: true));

            // now if we re-access enGB with load, it returns en, but huHU still returns inv
            Assert.AreNotSame(rsInv, manager.GetResourceSet(enGB, loadIfExists: true, tryParents: true));
            Assert.AreSame(rsInv, manager.GetResourceSet(huHU, loadIfExists: true, tryParents: true));

            // creating inv, inv(en), inv(enGB) (these have unloaded parent); inv(hu), inv(huHU) (these have no unloaded parents)
            manager.ReleaseAllResources();
            rsInv = manager.GetResourceSet(inv, loadIfExists: true, tryParents: false);
            Assert.AreSame(rsInv, manager.GetResourceSet(enGB, loadIfExists: false, tryParents: true));
            Assert.AreSame(rsInv, manager.GetResourceSet(huHU, loadIfExists: false, tryParents: true));

            // now the hu branch is up-to-date but en-GB has unloaded parents because en actually exists but not loaded
            var resourceSets = (Dictionary <string, ResourceSet>)Reflector.GetField(manager, "resourceSets");
            int sets         = resourceSets.Count;

            // "loading" hu does not change anything, since it is up-to date
            Assert.AreSame(rsInv, manager.GetResourceSet(hu, loadIfExists: true, tryParents: false));
            Assert.AreEqual(sets, resourceSets.Count);

            // but loading en clears en-GB, since it depends on that. Re-accessing enGB returns now en
            ResourceSet rsEN;

            Assert.AreNotSame(rsInv, rsEN = manager.GetResourceSet(en, loadIfExists: true, tryParents: false));
            Assert.AreEqual(sets - 1, resourceSets.Count);
            Assert.AreSame(rsEN, manager.GetResourceSet(enGB, loadIfExists: false, tryParents: true));
            Assert.AreEqual(sets, resourceSets.Count);

            // similarly, creating hu clears hu-HU, and re-accessing hu-HU returns hu
            Assert.AreNotSame(rsInv, rsHU = (ResourceSet)manager.GetExpandoResourceSet(hu, ResourceSetRetrieval.CreateIfNotExists, tryParents: false));
            Assert.AreEqual(sets - 1, resourceSets.Count);
            Assert.AreSame(rsHU, manager.GetResourceSet(huHU, loadIfExists: true, tryParents: true));
            Assert.AreEqual(sets, resourceSets.Count);

            // creating inv, inv(en) (unloaded resource); inv(hu), (no unloaded resource)
            manager.ReleaseAllResources();
            rsInv = manager.GetResourceSet(inv, loadIfExists: true, tryParents: false);
            Assert.AreSame(rsInv, manager.GetResourceSet(en, loadIfExists: false, tryParents: true));
            Assert.AreSame(rsInv, manager.GetResourceSet(hu, loadIfExists: true, tryParents: true));
            resourceSets = (Dictionary <string, ResourceSet>)Reflector.GetField(manager, "resourceSets");
            sets         = resourceSets.Count;

            // accessing en-GB will replace en proxy and returns that for en-GB
            var rsENGB = manager.GetResourceSet(enGB, loadIfExists: true, tryParents: true);

            Assert.AreEqual(sets + 1, resourceSets.Count);
            rsEN = manager.GetResourceSet(en, loadIfExists: false, tryParents: false);
            Assert.AreSame(rsEN, rsENGB);
            Assert.AreNotSame(inv, rsENGB);
            sets = resourceSets.Count;

            // but accessing hu-HU just returns the proxy of hu (=inv) and creates a new proxy for hu-HU
            var rsHUHU = manager.GetResourceSet(huHU, loadIfExists: true, tryParents: true);

            Assert.AreEqual(sets + 1, resourceSets.Count);
            rsHU = manager.GetResourceSet(hu, loadIfExists: false, tryParents: false);
            Assert.AreSame(rsHU, rsHUHU);
            Assert.AreSame(rsInv, rsHU);
        }