コード例 #1
0
        public void TestChainedExtractor()
        {
            IValueExtractor extractor = new ChainedExtractor("InnerMember.field");

            IValueExtractor re1 = new ReflectionExtractor("InnerMember");
            IValueExtractor re2 = new ReflectionExtractor("field");

            IValueExtractor[] res = { re1, re2 };

            IValueExtractor extractor1 = new ChainedExtractor(re1, re2);
            IValueExtractor extractor2 = new ChainedExtractor(res);

            Assert.IsNotNull(extractor);
            Assert.IsNotNull(extractor1);
            Assert.IsNotNull(extractor2);
            Assert.AreEqual(extractor, extractor1);
            Assert.AreEqual(extractor1, extractor2);
            Assert.AreEqual(extractor.ToString(), extractor1.ToString());
            Assert.AreEqual(extractor.GetHashCode(), extractor1.GetHashCode());

            IValueExtractor[] extractors = (extractor as ChainedExtractor).Extractors;
            Assert.IsNotNull(extractors);
            Assert.AreEqual(extractors.Length, 2);
            Assert.AreEqual(extractors[0], re1);
            Assert.AreEqual(extractors[1], re2);

            ReflectionTestType o = new ReflectionTestType();

            Assert.AreEqual(extractor.Extract(o), o.InnerMember.field);
            Assert.AreEqual(extractor.Extract(o), extractors[1].Extract(extractors[0].Extract(o)));
            Assert.IsNull(extractor.Extract(null));

            // testing on remote cache
            INamedCache cache = CacheFactory.GetCache(CacheName);

            cache.Clear();

            Address a1 = new Address("street1", "city1", "state1", "zip1");
            Address a2 = new Address("street2", "city2", "state2", "zip2");
            Address a3 = new Address("street3", "city1", "state3", "zip3");

            Hashtable ht = new Hashtable();

            ht.Add(a1, "chainedExtractorValue1");
            ht.Add(a2, "chainedExtractorValue2");
            ht.Add(a3, "chainedExtractorValue3");
            cache.InsertAll(ht);

            extractor1 = new KeyExtractor(IdentityExtractor.Instance);
            extractor2 = new KeyExtractor(new ReflectionExtractor("getCity"));
            extractor  = new KeyExtractor(new ChainedExtractor(extractor1, extractor2));

            IFilter     filter = new EqualsFilter(extractor, "city1");
            ICollection keys   = cache.GetKeys(filter);
            ArrayList   list   = new ArrayList(keys);

            Assert.AreEqual(keys.Count, 2);
            Assert.AreEqual(((Address)list[0]).City, "city1");
            Assert.AreEqual(((Address)list[1]).City, "city1");

            CacheFactory.Shutdown();
        }