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

            // serializing and de-serializing removes the unchanged resources
            string testResRef = refManager.GetString(resName);
            string testRes    = manager.GetString(resName);

            Assert.IsNotNull(testResRef);
            Assert.IsNotNull(testRes);
#if NET35
            // TODO .NET 3.5: get/set pointer fields by FieldAccessor
            Assert.Inconclusive("Serializing pointers is not supported");
#endif
            refManager = refManager.DeepClone();
            manager    = manager.DeepClone();
            Assert.AreEqual(testResRef, refManager.GetString(resName));
            Assert.AreEqual(testRes, manager.GetString(resName));

            // introducing a change: serialization preserves the change
            Assert.IsFalse(manager.IsModified);
            manager.SetObject(resName, "new string");
            Assert.IsTrue(manager.IsModified);
            CheckTestingFramework(); // the modified resource sets are searched in ResourceManager.ResourceSets Hashtable in .NET 3.5 and in ResXResourceManager.resourceSets Dictionary above.
            manager = manager.DeepClone();
            Assert.IsTrue(manager.IsModified);
            Assert.AreNotEqual(testRes, manager.GetString(resName));
        }
コード例 #2
0
        public void GetStreamTest()
        {
            var manager = new HybridResourceManager("KGySoft.CoreLibraries.Resources.TestCompiledResource", GetType().Assembly, resXBaseName);

            // Memory stream can be obtained from both compiled and resx, compiled is an unmanaged memory stream
            var resName = "TestSound";

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

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

            Assert.IsInstanceOf <MemoryStream>(compiled);
            Assert.IsInstanceOf <MemoryStream>(resx);
            Assert.AreNotEqual(compiled.GetType(), resx.GetType());

            // Works also for byte[], now MemoryStream is returned for both
            resName        = "TestBinFile";
            manager.Source = ResourceManagerSources.CompiledOnly;
            compiled       = manager.GetStream(resName, inv);
            manager.Source = ResourceManagerSources.ResXOnly;
            resx           = manager.GetStream(resName, inv);
            Assert.IsInstanceOf <MemoryStream>(compiled);
            Assert.IsInstanceOf <MemoryStream>(resx);
            Assert.AreEqual(compiled.GetType(), resx.GetType());

            // For a string exception is thrown when SafeMode = false
            resName = "TestString";
            Assert.IsFalse(manager.SafeMode);
            manager.Source = ResourceManagerSources.CompiledOnly;
            Assert.Throws <InvalidOperationException>(() => manager.GetStream(resName, inv));
            manager.Source = ResourceManagerSources.ResXOnly;
            Throws <InvalidOperationException>(() => manager.GetStream(resName, inv), Res.ResourcesNonStreamResourceWithType(resName, Reflector.StringType));

            // but when SafeMode is true, a string stream is returned
            manager.SafeMode = true;
            manager.Source   = ResourceManagerSources.CompiledOnly;
            compiled         = manager.GetStream(resName, inv);
            Assert.IsInstanceOf <MemoryStream>(compiled);
            Assert.AreEqual(manager.GetString(resName, inv), new StreamReader(compiled, Encoding.Unicode).ReadToEnd());
            manager.Source = ResourceManagerSources.ResXOnly;
            resx           = manager.GetStream(resName, inv);
            Assert.IsInstanceOf <MemoryStream>(resx);
            Assert.AreEqual(manager.GetString(resName, inv), new StreamReader(resx, Encoding.Unicode).ReadToEnd());

#if !NETCOREAPP2_0 // System.NotSupportedException : Cannot read resources that depend on serialization.
            // even for non-string resources
            resName        = "TestImage";
            manager.Source = ResourceManagerSources.CompiledOnly;
            compiled       = manager.GetStream(resName, inv);
            Assert.IsInstanceOf <MemoryStream>(compiled);
            Assert.AreEqual(manager.GetString(resName, inv), new StreamReader(compiled, Encoding.Unicode).ReadToEnd());
            manager.Source = ResourceManagerSources.ResXOnly;
            resx           = manager.GetStream(resName, inv);
            Assert.IsInstanceOf <MemoryStream>(resx);
            Assert.AreEqual(manager.GetString(resName, inv), new StreamReader(resx, Encoding.Unicode).ReadToEnd());
#endif
        }
コード例 #3
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());
        }
コード例 #4
0
        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"));
        }
コード例 #5
0
        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
        }
コード例 #6
0
        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);
        }