コード例 #1
0
        public void MergeSpecificOnLoadTest()
        {
            var manager = new DynamicResourceManager("KGySoft.CoreLibraries.Resources.TestCompiledResource", GetType().Assembly, resXBaseName)
            {
                AutoSave   = AutoSaveOptions.None,
                AutoAppend = AutoAppendOptions.AppendLastSpecificCulture | AutoAppendOptions.AppendOnLoad
            };

            string key = "TestString";
            IExpandoResourceSet rsinv;

            if (huRunic != null)
            {
                // retrieving spec with merge
                rsinv = manager.GetExpandoResourceSet(inv);
                IExpandoResourceSet rshuRunicHu = manager.GetExpandoResourceSet(huRunicHU, ResourceSetRetrieval.LoadIfExists, true); // this will not create a new rs
                Assert.AreSame(rsinv, rshuRunicHu);
                Assert.IsFalse(rshuRunicHu.GetString(key).StartsWith(LanguageSettings.UntranslatedResourcePrefix, StringComparison.Ordinal));
                rshuRunicHu = manager.GetExpandoResourceSet(huRunicHU, ResourceSetRetrieval.CreateIfNotExists, true); // but this will, and performs merge, too
                Assert.AreNotSame(rsinv, rshuRunicHu);
                Assert.IsTrue(rshuRunicHu.GetString(key).StartsWith(LanguageSettings.UntranslatedResourcePrefix, StringComparison.Ordinal));

                // huRunicHU is proxied into huRunicHULowland, too.
                Assert.IsTrue(manager.GetString(key, huRunicHULowland).StartsWith(LanguageSettings.UntranslatedResourcePrefix, StringComparison.Ordinal));
                Assert.AreSame(rshuRunicHu, manager.GetExpandoResourceSet(huRunicHULowland, ResourceSetRetrieval.GetIfAlreadyLoaded, false));

                // now proxy of huRunicHULowland is replaced by a normal resource set
                manager.AutoAppend = AutoAppendOptions.AppendFirstSpecificCulture | AutoAppendOptions.AppendOnLoad;
                Assert.IsTrue(manager.GetString(key, huRunicHULowland).StartsWith(LanguageSettings.UntranslatedResourcePrefix, StringComparison.Ordinal));
                Assert.AreNotSame(huRunicHU, manager.GetExpandoResourceSet(huRunicHULowland, ResourceSetRetrieval.GetIfAlreadyLoaded, false));

                // now only huRunicHULowland is created (from inv), no proxies because it has no descendants
                manager.ReleaseAllResources();
                Assert.IsTrue(manager.GetString(key, huRunicHULowland).StartsWith(LanguageSettings.UntranslatedResourcePrefix, StringComparison.Ordinal));
                Assert.IsNull(manager.GetExpandoResourceSet(huRunicHU, ResourceSetRetrieval.GetIfAlreadyLoaded, false));

                // now every specific is merged
                manager.ReleaseAllResources();
                manager.AutoAppend = AutoAppendOptions.AppendSpecificCultures | AutoAppendOptions.AppendOnLoad;
                Assert.IsTrue(manager.GetString(key, huRunicHULowland).StartsWith(LanguageSettings.UntranslatedResourcePrefix, StringComparison.Ordinal));
                rsinv       = manager.GetExpandoResourceSet(inv);
                rshuRunicHu = manager.GetExpandoResourceSet(huRunicHU);
                var rshuRunicHULowland = manager.GetExpandoResourceSet(huRunicHULowland);
                Assert.AreNotSame(rsinv, rshuRunicHu);
                Assert.AreNotSame(rshuRunicHu, rshuRunicHULowland);
                return;
            }

            // ---basic tests: no admin rights or not .NET Framework---

            // retrieving spec with merge
            rsinv = manager.GetExpandoResourceSet(inv);
            var rshuHu = manager.GetExpandoResourceSet(huHU, ResourceSetRetrieval.LoadIfExists, true); // this will not create a new rs

            Assert.AreSame(rsinv, rshuHu);
            Assert.IsFalse(rshuHu.GetString(key).StartsWith(LanguageSettings.UntranslatedResourcePrefix, StringComparison.Ordinal));
            rshuHu = manager.GetExpandoResourceSet(huHU, ResourceSetRetrieval.CreateIfNotExists, true); // but this will, and performs merge, too
            Assert.AreNotSame(rsinv, huHU);
            Assert.IsTrue(rshuHu.GetString(key).StartsWith(LanguageSettings.UntranslatedResourcePrefix, StringComparison.Ordinal));

#if NETFRAMEWORK
            Assert.Inconclusive("To run the tests in this class with full functionality, administrator rights are required");
#endif
        }
コード例 #2
0
        public void GetUnknownTest()
        {
            // This is a non-existing resource so MissingManifestResourceException should be thrown by default
            var manager = new DynamicResourceManager(GetType())
            {
                AutoSave       = AutoSaveOptions.None,
                AutoAppend     = AutoAppendOptions.AppendNeutralCultures,
                ThrowException = true
            };

            string key = "unknown";

            // Exception is thrown through base HRM
            Throws <MissingManifestResourceException>(() => manager.GetString(key, inv));

            // Due to possible append options, exception is thrown through derived DRM
            // For the neutral en culture a resource set is created during the traversal
            Throws <MissingManifestResourceException>(() => manager.GetString(key, enUS));
            Assert.IsNull(manager.GetResourceSet(enUS, false, false));
            Assert.IsNotNull(manager.GetResourceSet(en, false, false));

            // Exception is not thrown any more. Instead, a new resource is automatically added
            manager.AutoAppend = AutoAppendOptions.AddUnknownToInvariantCulture;
            manager.ReleaseAllResources();

            Assert.IsTrue(manager.GetString(key, inv).StartsWith(LanguageSettings.UnknownResourcePrefix, StringComparison.Ordinal));
            Assert.IsTrue(manager.GetString(key, en).StartsWith(LanguageSettings.UnknownResourcePrefix, StringComparison.Ordinal));
            Assert.IsTrue(manager.GetString(key, enUS).StartsWith(LanguageSettings.UnknownResourcePrefix, StringComparison.Ordinal));
            manager.ReleaseAllResources();
            Assert.IsTrue(manager.GetString(key, enUS).StartsWith(LanguageSettings.UnknownResourcePrefix, StringComparison.Ordinal));

            // If requested as derived with append, the new resource is merged into derived resources
            manager.AutoAppend = AutoAppendOptions.AddUnknownToInvariantCulture | AutoAppendOptions.AppendNeutralCultures;
            manager.ReleaseAllResources();
            Assert.IsTrue(manager.GetString(key, enUS).StartsWith(LanguageSettings.UntranslatedResourcePrefix + LanguageSettings.UnknownResourcePrefix, StringComparison.Ordinal));
            IExpandoResourceSet rsEn = manager.GetExpandoResourceSet(en, ResourceSetRetrieval.GetIfAlreadyLoaded, false);

            Assert.IsTrue(rsEn.ContainsResource(key));
            Assert.AreSame(rsEn, manager.GetResourceSet(enUS, false, false), "en should be proxied for en-US");
            manager.AutoAppend |= AutoAppendOptions.AppendSpecificCultures;
            Assert.IsTrue(manager.GetString(key, enUS).StartsWith(LanguageSettings.UntranslatedResourcePrefix + LanguageSettings.UnknownResourcePrefix, StringComparison.Ordinal));
            IExpandoResourceSet rsEnUs = manager.GetResourceSet(enUS, false, false) as IExpandoResourceSet;

            Assert.IsNotNull(rsEnUs);
            Assert.AreNotSame(rsEn, rsEnUs, "Due to merge a new resource set should have been created for en-US");
            Assert.IsTrue(rsEnUs.ContainsResource(key));

            // As object: null is added to invariant
            manager.ReleaseAllResources();
            Assert.IsNull(manager.GetObject(key, inv));
            manager.GetExpandoResourceSet(inv, ResourceSetRetrieval.GetIfAlreadyLoaded, false).ContainsResource(key);

            // but null is not merged even if the child resource sets are created
            Assert.IsNull(manager.GetObject(key, enUS));
            Assert.IsNotNull(manager.GetResourceSet(enUS, false, false));
            Assert.IsFalse(manager.GetExpandoResourceSet(enUS, ResourceSetRetrieval.GetIfAlreadyLoaded, false).ContainsResource(key));
            Assert.IsNotNull(manager.GetResourceSet(en, false, false));
            Assert.IsFalse(manager.GetExpandoResourceSet(en, ResourceSetRetrieval.GetIfAlreadyLoaded, false).ContainsResource(key));
            Assert.IsNotNull(manager.GetResourceSet(inv, false, false));
            Assert.IsTrue(manager.GetExpandoResourceSet(inv, ResourceSetRetrieval.GetIfAlreadyLoaded, false).ContainsResource(key));

            // a null object does not turn to string
            Assert.IsNull(manager.GetString(key, enUS));

            // changing back to compiled only sources disables appending invariant so exception will be thrown again
            manager.Source = ResourceManagerSources.CompiledOnly;
            Throws <MissingManifestResourceException>(() => manager.GetString(key, enUS));
        }