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));
        }
        public void SetObjectTest()
        {
            LanguageSettings.DisplayLanguage = enUS;
            var manager = new HybridResourceManager(GetType());

            // not existing base: an exception is thrown when an object is about to obtain
            Throws <MissingManifestResourceException>(() => manager.GetObject("unknown"));

            // setting something in display language creates a resource set but the invariant is still missing
            manager.SetObject("StringValue", "String " + LanguageSettings.DisplayLanguage.Name);
            Assert.IsNotNull(manager.GetObject("StringValue"));
            Throws <MissingManifestResourceException>(() => manager.GetObject("unknown"));

            // this creates the invariant resource set, no exception anymore for unknown values
            manager.SetObject("InvariantOnly", 42, inv);
            Assert.IsNull(manager.GetObject("unknown"));

            // accessing something via a derived culture we can obtain the invariant value after all
            Assert.IsNotNull(manager.GetObject("InvariantOnly"));

            // setting something both in derived and invariant: they both can be obtained and they can be different
            manager.SetObject("StringValue", "String invariant", inv);
            Assert.IsNotNull(manager.GetObject("StringValue", inv));
            Assert.AreNotEqual(manager.GetObject("StringValue", inv), manager.GetObject("StringValue"));
            Assert.IsTrue(manager.IsModified);

            // in compiled mode any set operation throws InvalidOperationException and the changes disappear
            manager.Source = ResourceManagerSources.CompiledOnly;
            Throws <InvalidOperationException>(() => manager.SetObject("SetTest", "does not work"));
            Assert.IsFalse(manager.IsModified);
            Throws <MissingManifestResourceException>(() => manager.GetString("StringValue"));

            // is we change to non-compiled mode, changes re-appear
            manager.Source = ResourceManagerSources.ResXOnly;
            Assert.IsTrue(manager.IsModified);
            Assert.IsNotNull(manager.GetString("StringValue"));
        }
        public void GetStringTest()
        {
            var manager = new HybridResourceManager("KGySoft.CoreLibraries.Resources.TestCompiledResource", GetType().Assembly, resXBaseName);

            // When a resource exists in both compiled and resx: resx is taken first
            var resName = "TestString";

            manager.Source = ResourceManagerSources.CompiledAndResX;
            var hybrid = manager.GetString(resName, inv);

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.CompiledOnly;
            var compiled = manager.GetString(resName, inv);

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.ResXOnly;
            var resx = manager.GetString(resName, inv);

            Assert.AreEqual(resx, hybrid);
            Assert.AreNotEqual(resx, compiled);
            Assert.AreNotEqual(compiled, hybrid);

            // When a resource exists only in compiled: .resx is null, others hybrid and compiled are the same
            resName = "TestStringCompiled";

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.CompiledAndResX;
            hybrid         = manager.GetString(resName, inv);

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.CompiledOnly;
            compiled       = manager.GetString(resName, inv);

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.ResXOnly;
            resx           = manager.GetString(resName, inv);

            Assert.AreEqual(compiled, hybrid);
            Assert.IsNull(resx);

            // When a resource exists only in resx: compiled is null, others hybrid and resx are the same
            resName = "TestStringResX";

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.CompiledAndResX;
            hybrid         = manager.GetString(resName, inv);

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.CompiledOnly;
            compiled       = manager.GetString(resName, inv);

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.ResXOnly;
            resx           = manager.GetString(resName, inv);

            Assert.AreEqual(resx, hybrid);
            Assert.IsNull(compiled);

            // Non string throws an exception if not is in safe mode
            resName = "TestBinFile";
            Assert.IsFalse(manager.SafeMode);
            manager.Source = ResourceManagerSources.CompiledOnly;
            Throws <InvalidOperationException>(() => manager.GetString(resName, inv));
            manager.Source = ResourceManagerSources.ResXOnly;
            Throws <InvalidOperationException>(() => manager.GetString(resName, inv));

            // but in safe mode they succeed - the content is different though: ToString vs. raw XML content
            manager.SafeMode = true;
            manager.Source   = ResourceManagerSources.CompiledOnly;
            compiled         = manager.GetString(resName, inv);
            Assert.AreEqual(manager.GetObject(resName, inv).ToString(), compiled);
            manager.Source = ResourceManagerSources.ResXOnly;
            resx           = manager.GetString(resName, inv);
            Assert.AreEqual(manager.GetObject(resName, inv).ToString(), resx);
            Assert.AreNotEqual(compiled, resx);
        }
        public void GetObjectTest()
        {
            var manager = new HybridResourceManager("KGySoft.CoreLibraries.Resources.TestCompiledResource", GetType().Assembly, resXBaseName);

            // When a resource exists in both compiled and resx: resx is taken first
            var resName = "TestString";

            manager.Source = ResourceManagerSources.CompiledAndResX;
            var hybrid = manager.GetObject(resName, inv);

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.CompiledOnly;
            var compiled = manager.GetObject(resName, inv);

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.ResXOnly;
            var resx = manager.GetObject(resName, inv);

            Assert.AreEqual(resx, hybrid);
            Assert.AreNotEqual(resx, compiled);
            Assert.AreNotEqual(compiled, hybrid);

            // When a resource exists only in compiled: resx is null, others hybrid and compiled are the same
            resName = "TestStringCompiled";

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.CompiledAndResX;
            hybrid         = manager.GetObject(resName, inv);

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.CompiledOnly;
            compiled       = manager.GetObject(resName, inv);

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.ResXOnly;
            resx           = manager.GetObject(resName, inv);

            Assert.AreEqual(compiled, hybrid);
            Assert.IsNull(resx);

            // When a resource exists only in resx: compiled is null, others hybrid and resx are the same
            resName = "TestStringResX";

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.CompiledAndResX;
            hybrid         = manager.GetObject(resName, inv);

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.CompiledOnly;
            compiled       = manager.GetObject(resName, inv);

            manager.ReleaseAllResources();
            manager.Source = ResourceManagerSources.ResXOnly;
            resx           = manager.GetObject(resName, inv);

            Assert.AreEqual(resx, hybrid);
            Assert.IsNull(compiled);

            // if a resource exists only in a base, it will be returned
            resName        = "TestBytes";
            manager.Source = ResourceManagerSources.CompiledAndResX;
            Assert.IsNotNull(manager.GetObject(resName, enUS));

            // switching source will return the correct resource even without releasing the resources
            resName        = "TestString";
            hybrid         = manager.GetObject(resName, inv);
            manager.Source = ResourceManagerSources.CompiledOnly;
            compiled       = manager.GetObject(resName, inv);
            Assert.AreNotEqual(hybrid, compiled);

            // proxy test
            manager.Source = ResourceManagerSources.CompiledAndResX;
            manager.ReleaseAllResources();
            hybrid = manager.GetObject(resName, huHU);                // hu and hu-HU are now proxies, last=inv
            Assert.IsNotNull(hybrid);
            Assert.AreSame(hybrid, manager.GetObject(resName, huHU)); // returned from cached lastUsedResourceSet
            Assert.AreSame(hybrid, manager.GetObject(resName, hu));   // returned from cached proxy in resourceSets
            Assert.AreSame(hybrid, manager.GetObject(resName, inv));  // returned from cached non-proxy in resourceSets
        }