コード例 #1
0
        public void TestNullCache()
        {
            ICache nullCache = NullImplementation.GetCache();

            Assert.IsNotNull(nullCache);
            Assert.IsInstanceOf(typeof(NullImplementation.NullCache), nullCache);
            Assert.AreEqual(nullCache.Count, 0);
            IList list = new ArrayList();

            list.Add(1);
            IDictionary ht = new Hashtable();

            ht.Add(1, 1);
            ht.Add(2, 2);
            nullCache.InsertAll(ht);
            Assert.AreEqual(nullCache.Count, 0);
            IDictionary res = nullCache.GetAll(list);

            Assert.IsNotNull(res);
            Assert.IsInstanceOf(typeof(NullImplementation.NullDictionary), res);
            object o = nullCache.Insert(1, 1);

            Assert.IsFalse(nullCache.Contains(1));
            Assert.IsNull(o);
            o = nullCache.Insert(1, 1, 100);
            Assert.IsFalse(nullCache.Contains(1));
            Assert.IsNull(o);

            ICollection entries = nullCache.Entries;

            Assert.IsNotNull(entries);
            Assert.IsInstanceOf(typeof(NullImplementation.NullCollection), entries);
            Assert.AreEqual(entries.Count, 0);

            ICacheEnumerator nullEnumerator = nullCache.GetEnumerator();

            Assert.IsNotNull(nullEnumerator);
            Assert.IsInstanceOf(typeof(NullImplementation.NullEnumerator), nullEnumerator);
            Assert.IsFalse(nullEnumerator.MoveNext());

            int count = 0;

            foreach (DictionaryEntry entry in nullCache)
            {
                count++;
            }
            Assert.AreEqual(count, 0);
        }
        public void ConverterCacheEnumeratorTests()
        {
            ICache     cache = InstantiateCache();
            IConverter conv  = new ConvertDown();

            for (int i = 0; i < 3; i++)
            {
                cache.Add(i, i + 1);
            }
            ICacheEnumerator cacheEnumerator = cache.GetEnumerator();

            ICacheEnumerator convEnum = ConverterCollections.GetCacheEnumerator(cacheEnumerator, conv, conv, conv);

            Assert.IsNotNull(convEnum);
            Assert.IsTrue(convEnum.MoveNext());
            convEnum.MoveNext();
            convEnum.MoveNext();
            Assert.IsFalse(convEnum.MoveNext());
            convEnum.Reset();
            Assert.IsTrue(convEnum.MoveNext());

            object      o     = convEnum.Current;
            ICacheEntry entry = convEnum.Entry;

            Assert.AreEqual(o, entry);

            Assert.AreEqual(entry.Key, convEnum.Key);
            Assert.AreEqual(entry.Value, convEnum.Value);
            Assert.AreEqual(entry.Key, conv.Convert(cacheEnumerator.Key));
            Assert.AreEqual(entry.Value, conv.Convert(cacheEnumerator.Value));

            Assert.IsInstanceOf(typeof(ConverterCollections.ConverterCacheEnumerator), convEnum);
            ConverterCollections.ConverterCacheEnumerator cce =
                convEnum as ConverterCollections.ConverterCacheEnumerator;
            Assert.IsNotNull(cce);
            Assert.AreEqual(cce.ConverterKeyUp, conv);
            Assert.AreEqual(cce.ConverterValueUp, conv);
        }
        public void ConverterCacheTests()
        {
            ICache     cache = InstantiateCache();
            IConverter cDown = new ConvertDown();
            IConverter cUp   = new ConvertUp();

            for (int i = 1; i <= 3; i++)
            {
                cache.Add(cDown.Convert(i), cDown.Convert(i + 1));
            }

            ICache convCache = ConverterCollections.GetCache(cache, cUp, cDown, cUp, cDown);

            Assert.IsNotNull(convCache);
            Assert.AreEqual(convCache.Count, 3);
            convCache.Insert(4, 5);
            Assert.AreEqual(convCache.Count, 4);
            Assert.AreEqual(convCache["4"], "5");
            Assert.AreEqual(cache[cDown.Convert(4)], cDown.Convert(5));

            ICollection entries = convCache.Entries;

            Assert.IsNotNull(entries);
            Assert.AreEqual(entries.Count, 4);
            ArrayList        list            = new ArrayList(entries);
            ICacheEnumerator cacheEnumerator = convCache.GetEnumerator();

            Assert.IsNotNull(cacheEnumerator);
            Assert.IsTrue(cacheEnumerator.MoveNext());
            cacheEnumerator.Reset();
            for (int i = 0; i < list.Count && cacheEnumerator.MoveNext(); i++)
            {
                object o1 = list[i];
                object o2 = cacheEnumerator.Current;
                Assert.IsNotNull(o1);
                Assert.IsNotNull(o2);
                Assert.IsInstanceOf(typeof(ICacheEntry), o1);
                Assert.IsInstanceOf(typeof(ICacheEntry), o2);
                ICacheEntry e1 = o1 as ICacheEntry;
                ICacheEntry e2 = o2 as ICacheEntry;
                Assert.IsNotNull(e1);
                Assert.IsNotNull(e2);
                Assert.AreEqual(e1.Key, e2.Key);
                Assert.AreEqual(e1.Value, e2.Value);
            }

            ArrayList keys = new ArrayList();

            keys.Add(1);
            keys.Add(3);
            IDictionary result = convCache.GetAll(keys);

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Count, 2);
            Assert.AreEqual(result["1"], "2");
            Assert.AreEqual(result["3"], "4");

            IDictionary d = new Hashtable();

            for (int i = 5; i < 7; i++)
            {
                d.Add(i, i + 1);
            }
            convCache.InsertAll(d);
            Assert.AreEqual(convCache.Count, 6);
            Assert.AreEqual(convCache["6"], "7");

            Assert.IsInstanceOf(typeof(ConverterCollections.ConverterCache), convCache);
            ConverterCollections.ConverterCache cc = convCache as ConverterCollections.ConverterCache;
            Assert.IsNotNull(cc);
            Assert.AreEqual(cc.Cache, cache);
        }