public void GetMetaStringTest() { var manager = new HybridResourceManager("KGySoft.CoreLibraries.Resources.TestCompiledResource", GetType().Assembly, resXBaseName); var resName = "TestString"; manager.Source = ResourceManagerSources.CompiledAndResX; var hybrid = manager.GetMetaString(resName, inv); manager.ReleaseAllResources(); manager.Source = ResourceManagerSources.CompiledOnly; var compiled = manager.GetMetaString(resName, inv); manager.ReleaseAllResources(); manager.Source = ResourceManagerSources.ResXOnly; var resx = manager.GetMetaString(resName, inv); // meta exists only in non-compiled resources Assert.IsNotNull(hybrid); Assert.IsNotNull(resx); Assert.IsNull(compiled); manager.Source = ResourceManagerSources.CompiledAndResX; hybrid = manager.GetMetaString(resName, en); manager.ReleaseAllResources(); manager.Source = ResourceManagerSources.CompiledOnly; compiled = manager.GetMetaString(resName, en); manager.ReleaseAllResources(); manager.Source = ResourceManagerSources.ResXOnly; resx = manager.GetMetaString(resName, en); // there is no fallback for meta Assert.IsNull(hybrid); Assert.IsNull(resx); Assert.IsNull(compiled); }
public void DisposeTest() { var manager = new HybridResourceManager("KGySoft.CoreLibraries.Resources.TestCompiledResource", GetType().Assembly, resXBaseName); manager.Dispose(); Throws <ObjectDisposedException>(() => manager.ReleaseAllResources()); Throws <ObjectDisposedException>(() => manager.GetString("TestString")); manager.Dispose(); // this will not throw anything manager = new HybridResourceManager("KGySoft.CoreLibraries.Resources.TestCompiledResource", GetType().Assembly, resXBaseName); manager.Source = ResourceManagerSources.CompiledOnly; manager.Dispose(); Throws <ObjectDisposedException>(() => manager.ReleaseAllResources()); Throws <ObjectDisposedException>(() => manager.GetString("TestString")); manager.Dispose(); // this will not throw anything }
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); }
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 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 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); }
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 }