public void TestComparisonValueExtractor() { // testing on remote cache INamedCache cache = CacheFactory.GetCache(CacheName); cache.Clear(); Score score = new Score(1, 1, 126, 10000L, 1.24f, 1432.55, new decimal(11223344), new RawInt128(new byte[] { 1 }), 1); cache.Insert("score1", score); IValueExtractor exByte = new ReflectionExtractor("getByteValue"); IValueExtractor exShort = new ReflectionExtractor("getShortValue"); IValueExtractor exInt = new ReflectionExtractor("getIntValue"); IValueExtractor exLong = new ReflectionExtractor("getLongValue"); IValueExtractor exFloat = new ReflectionExtractor("getFloatValue"); IValueExtractor exDouble = new ReflectionExtractor("getDoubleValue"); IValueExtractor exDecimal = new ReflectionExtractor("getBigDecimalValue"); IValueExtractor exInt128 = new ReflectionExtractor("getBigIntegerValue"); //different ways to instantiate same extractor ComparisonValueExtractor cve1 = new ComparisonValueExtractor(exByte, exShort); ComparisonValueExtractor cve2 = new ComparisonValueExtractor(exInt, exLong); ComparisonValueExtractor cve3 = new ComparisonValueExtractor(exFloat, exDouble); ComparisonValueExtractor cve4 = new ComparisonValueExtractor(exDecimal, exDecimal); ComparisonValueExtractor cve5 = new ComparisonValueExtractor(exInt128, exInt128); IFilter filter = new EqualsFilter(cve1, score.ByteValue - score.ShortValue); ICollection keys = cache.GetKeys(filter); Assert.IsNotEmpty(keys); Assert.IsTrue(keys.Count == 1); filter = new EqualsFilter(cve2, score.IntValue - score.LongValue); keys = cache.GetKeys(filter); Assert.IsNotEmpty(keys); Assert.IsTrue(keys.Count == 1); filter = new EqualsFilter(cve3, score.FloatValue - score.DoubleValue); keys = cache.GetKeys(filter); Assert.IsNotEmpty(keys); Assert.IsTrue(keys.Count == 1); filter = new EqualsFilter(cve4, (decimal)0); keys = cache.GetKeys(filter); Assert.IsNotEmpty(keys); Assert.IsTrue(keys.Count == 1); filter = new EqualsFilter(cve5, NumberUtils.DecimalToRawInt128(score.RawInt128Value.ToDecimal() - score.RawInt128Value.ToDecimal())); keys = cache.GetKeys(filter); Assert.IsNotEmpty(keys); Assert.IsTrue(keys.Count == 1); CacheFactory.Shutdown(); }
public void TestGetKeys() { INamedCache cache = CacheFactory.GetCache(CacheName); cache.Clear(); IDictionary dict = new Hashtable(); for (int i = 1; i <= 10; i++) { dict.Add(GetKeyObject("key" + i), i); } cache.InsertAll(dict); IFilter filter = new GreaterFilter(IdentityExtractor.Instance, 5); object[] keys = cache.GetKeys(filter); Assert.AreEqual(keys.Length, 5); for (int i = 6; i <= 10; i++) { Assert.Contains(GetKeyObject("key" + i), keys); } CacheFactory.Shutdown(); }
public void TestConfiguredTriggerListener() { IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory; IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-extend-maptrigger-cache-config.xml"); ccf.Config = config; INamedCache cache = CacheFactory.GetCache(CACHE_NAME); cache.Clear(); SimplePerson pIn = new SimplePerson("123-45-6789", "Eddie", "Van Halen", 1955, "987-65-4321", new String[] { "456-78-9123" }); try { cache.Insert(1, pIn); Object[] keys = cache.GetKeys(AlwaysFilter.Instance); Assert.AreEqual(keys.Length, 1); SimplePerson pOut = (SimplePerson)cache[1]; Assert.AreEqual(pIn.LastName.ToUpper(), pOut.LastName); } finally { CacheFactory.Shutdown(); } }
public void TestFilters() { IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory; IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-local-cache-config.xml"); ccf.Config = config; INamedCache cache = CacheFactory.GetCache("local-default"); cache.Clear(); cache.Insert("5", "5"); cache.Insert(1, "10"); cache.Insert("g", "15"); cache.Insert("b", "20"); cache.Insert("1", "105"); IFilter[] filters = new IFilter[] { new EqualsFilter(IdentityExtractor.Instance, "20"), new LikeFilter(IdentityExtractor.Instance, "1%", '\\', true) }; AnyFilter anyFilter = new AnyFilter(filters); ICollection results = cache.GetKeys(anyFilter); Assert.AreEqual(4, results.Count); Assert.IsTrue(CollectionUtils.Contains(results, 1)); Assert.IsTrue(CollectionUtils.Contains(results, "g")); Assert.IsTrue(CollectionUtils.Contains(results, "b")); Assert.IsTrue(CollectionUtils.Contains(results, "1")); filters = new IFilter[] { new EqualsFilter(IdentityExtractor.Instance, "20"), new LikeFilter(IdentityExtractor.Instance, "5%", '\\', true) }; anyFilter = new AnyFilter(filters); ICacheEntry[] entries = cache.GetEntries(anyFilter); Assert.AreEqual(2, entries.Length); Assert.Contains("b", new object[] { entries[0].Key, entries[1].Key }); Assert.Contains("5", new object[] { entries[0].Key, entries[1].Key }); Assert.Contains("20", new object[] { entries[0].Value, entries[1].Value }); Assert.Contains("5", new object[] { entries[0].Value, entries[1].Value }); CacheFactory.Shutdown(); }
public void TestIdentityExtractor() { IValueExtractor extractor = IdentityExtractor.Instance; IValueExtractor extractor1 = IdentityExtractor.Instance; Assert.IsNotNull(extractor); Assert.AreEqual(extractor, extractor1); Assert.AreEqual(extractor.ToString(), extractor1.ToString()); Assert.AreEqual(extractor.GetHashCode(), extractor1.GetHashCode()); object o = new DictionaryEntry("key", "value"); object o1 = extractor.Extract(o); Assert.AreEqual(o, o1); IdentityExtractor ie = extractor as IdentityExtractor; Assert.AreEqual(ie.Compare("ana", "cikic"), -1); TestQueryCacheEntry entry1 = new TestQueryCacheEntry("1", 1); TestQueryCacheEntry entry2 = new TestQueryCacheEntry("2", 2); Assert.AreEqual(ie.CompareEntries(entry2, entry1), 1); //testing on remote cache INamedCache cache = CacheFactory.GetCache(CacheName); cache.Clear(); Hashtable ht = new Hashtable(); ht.Add("identityExtractorKey1", 435); ht.Add("identityExtractorKey2", 253); ht.Add("identityExtractorKey3", 3); ht.Add("identityExtractorKey4", null); ht.Add("identityExtractorKey5", -3); cache.InsertAll(ht); IFilter filter = new EqualsFilter(extractor, 253); ICollection keys = cache.GetKeys(filter); Assert.Contains("identityExtractorKey2", (IList)keys); Assert.AreEqual(keys.Count, 1); CacheFactory.Shutdown(); }
public void TestInvoke() { IConfigurableCacheFactory ccf = CacheFactory.ConfigurableCacheFactory; IXmlDocument config = XmlHelper.LoadXml("assembly://Coherence.Tests/Tangosol.Resources/s4hc-local-cache-config.xml"); ccf.Config = config; INamedCache cache = CacheFactory.GetCache("local-default"); cache.Clear(); Hashtable ht = new Hashtable(); ht.Add("conditionalPutKey1", 435); ht.Add("conditionalPutKey2", 253); ht.Add("conditionalPutKey3", 3); ht.Add("conditionalPutKey4", 200); ht.Add("conditionalPutKey5", 333); cache.InsertAll(ht); IFilter greaterThen600 = new GreaterFilter(IdentityExtractor.Instance, 600); IFilter greaterThen200 = new GreaterFilter(IdentityExtractor.Instance, 200); // no entries with value>600 ICollection keys = cache.GetKeys(greaterThen600); Assert.IsTrue(keys.Count == 0); // invoke processor for one entry with filter that will evaluate to false // again, no entries with value>600 IEntryProcessor processor = new ConditionalPut(greaterThen600, 666); cache.Invoke("conditionalPutKey1", processor); keys = cache.GetKeys(greaterThen600); Assert.IsTrue(keys.Count == 0); // invoke processor for one entry with filter that will evaluate to true // this will change one entry processor = new ConditionalPut(AlwaysFilter.Instance, 666); cache.Invoke("conditionalPutKey1", processor); keys = cache.GetKeys(greaterThen600); Assert.AreEqual(keys.Count, 1); Assert.AreEqual(cache["conditionalPutKey1"], 666); // 3 entries with value>200 keys = cache.GetKeys(greaterThen200); Assert.AreEqual(keys.Count, 3); // invoke processor for these three entries processor = new ConditionalPut(greaterThen200, 666); cache.InvokeAll(cache.Keys, processor); keys = cache.GetKeys(greaterThen600); Assert.AreEqual(keys.Count, 3); cache.Clear(); ht = new Hashtable(); ht.Add("conditionalPutAllKey1", 435); ht.Add("conditionalPutAllKey2", 253); ht.Add("conditionalPutAllKey3", 200); ht.Add("conditionalPutAllKey4", 333); cache.InsertAll(ht); Hashtable htPut = new Hashtable(); htPut.Add("conditionalPutAllKey1", 100); htPut.Add("conditionalPutAllKey6", 80); htPut.Add("conditionalPutAllKey3", 10); // put key1 and compare cache value with the put one processor = new ConditionalPutAll(AlwaysFilter.Instance, htPut); cache.Invoke("conditionalPutAllKey1", processor); Assert.IsNotNull(cache["conditionalPutAllKey1"]); Assert.AreEqual(cache["conditionalPutAllKey1"], htPut["conditionalPutAllKey1"]); // TODO: Decide wheter putall should insert new entries or not // put all keys from htPut and compare cache values with put ones //cache.InvokeAll(htPut.Keys, processor); //Assert.IsTrue(cache.Count == 5); //Assert.AreEqual(cache["conditionalPutAllKey1"], htPut["conditionalPutAllKey1"]); //Assert.AreEqual(cache["conditionalPutAllKey6"], htPut["conditionalPutAllKey6"]); //Assert.AreEqual(cache["conditionalPutAllKey3"], htPut["conditionalPutAllKey3"]); //htPut.Clear(); //htPut.Add("conditionalPutAllKey4", 355); //processor = new ConditionalPutAll(AlwaysFilter.Instance, htPut); //cache.InvokeAll(new GreaterFilter(IdentityExtractor.Instance, 300), processor); //Assert.AreEqual(cache["conditionalPutAllKey4"], htPut["conditionalPutAllKey4"]); CacheFactory.Shutdown(); }
public void TestMultiExtractor() { IValueExtractor extractor = new MultiExtractor("field,Property,GetMethod"); IValueExtractor re1 = new ReflectionExtractor("field"); IValueExtractor re2 = new ReflectionExtractor("Property"); IValueExtractor re3 = new ReflectionExtractor("GetMethod"); IValueExtractor[] res = { re1, re2, re3 }; IValueExtractor extractor1 = new MultiExtractor(res); Assert.IsNotNull(extractor); Assert.IsNotNull(extractor1); Assert.AreEqual(extractor, extractor1); Assert.AreEqual(extractor.ToString(), extractor1.ToString()); Assert.AreEqual(extractor.GetHashCode(), extractor1.GetHashCode()); IValueExtractor[] extractors = (extractor as MultiExtractor).Extractors; Assert.IsNotNull(extractors); Assert.AreEqual(extractors.Length, 3); Assert.AreEqual(extractors[0], re1); Assert.AreEqual(extractors[1], re2); Assert.AreEqual(extractors[2], re3); ReflectionTestType o1 = new ReflectionTestType(); IList list = (IList)extractor.Extract(o1); Assert.AreEqual(list.Count, extractors.Length); for (int i = 0; i < extractors.Length; i++) { Assert.AreEqual(list[i], extractors[i].Extract(o1)); } Assert.IsNull(extractor.Extract(null)); ReflectionTestType o2 = new ReflectionTestType(); TestQueryCacheEntry entry1 = new TestQueryCacheEntry("key1", o1); TestQueryCacheEntry entry2 = new TestQueryCacheEntry("key2", o2); Assert.AreEqual((extractor as MultiExtractor).CompareEntries(entry1, entry2), 0); o2.field = 100; Assert.AreEqual((extractor as MultiExtractor).CompareEntries(entry1, entry2), -1); // testing on remote cache INamedCache cache = CacheFactory.GetCache(CacheName); cache.Clear(); Address a1 = new Address("street1", "city1", "state1", "zip1"); Address a2 = new Address("treet2", "city2", "tate2", "zip2"); Address a3 = new Address("street3", "city3", "state3", "zip3"); Hashtable ht = new Hashtable(); ht.Add("multiExtractorKey1", a1); ht.Add("multiExtractorKey2", a2); ht.Add("multiExtractorKey3", a3); cache.InsertAll(ht); extractor1 = new ReflectionExtractor("getStreet"); IValueExtractor extractor2 = new ReflectionExtractor("getState"); extractor = new MultiExtractor(new IValueExtractor[] { extractor1, extractor2 }); IFilter filter = new ContainsFilter(extractor, "street1"); ICollection keys = cache.GetKeys(filter); Assert.AreEqual(keys.Count, 1); Assert.Contains("multiExtractorKey1", (IList)keys); CacheFactory.Shutdown(); }
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(); }
public void TestReflectionExtractor() { IValueExtractor extractor = new ReflectionExtractor("field"); IValueExtractor extractor1 = new ReflectionExtractor("field"); IValueExtractor extractor2 = IdentityExtractor.Instance; Assert.IsNotNull(extractor); Assert.AreEqual(extractor, extractor1); Assert.AreNotEqual(extractor, extractor2); Assert.AreEqual(extractor.ToString(), extractor1.ToString()); Assert.AreEqual(extractor.GetHashCode(), extractor1.GetHashCode()); ReflectionTestType o = new ReflectionTestType(); Assert.AreEqual(extractor.Extract(o), o.field); extractor = new ReflectionExtractor("Property"); Assert.AreEqual(extractor.Extract(o), o.Property); extractor = new ReflectionExtractor("GetMethod"); Assert.AreEqual(extractor.Extract(o), o.GetMethod()); Assert.IsNull(extractor.Extract(null)); try { extractor = new ReflectionExtractor("InvalidMember"); extractor.Extract(o); } catch (Exception e) { Assert.IsInstanceOf(typeof(ArgumentException), e.InnerException); } // 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("reflectionExtractorKey1", a1); ht.Add("reflectionExtractorKey2", a2); ht.Add("reflectionExtractorKey3", a3); cache.InsertAll(ht); extractor = new ReflectionExtractor("getCity"); IFilter filter = new EqualsFilter(extractor, "city1"); ICollection keys = cache.GetKeys(filter); Assert.Contains("reflectionExtractorKey1", (IList)keys); Assert.Contains("reflectionExtractorKey3", (IList)keys); Assert.AreEqual(keys.Count, 2); extractor = new ReflectionExtractor("Sum", new object[] { 10 }); o = new ReflectionTestType(); o.field = 10; Assert.AreEqual(extractor.Extract(o), 20); o.field = 4; Assert.AreEqual(extractor.Extract(o), 14); CacheFactory.Shutdown(); }
public void TestKeyExtractor() { IValueExtractor extractor = new KeyExtractor((IValueExtractor)null); IValueExtractor extractor1 = new KeyExtractor((IValueExtractor)null); IValueExtractor extractor2 = IdentityExtractor.Instance; Assert.IsNotNull(extractor); Assert.AreEqual(extractor, extractor1); Assert.AreNotEqual(extractor, extractor2); Assert.AreEqual(extractor.ToString(), extractor1.ToString()); Assert.AreEqual(extractor.GetHashCode(), extractor1.GetHashCode()); extractor = new KeyExtractor(IdentityExtractor.Instance); Assert.IsNotNull(extractor); IValueExtractor innerExtractor = (extractor as KeyExtractor).Extractor; Assert.IsNotNull(innerExtractor); Assert.IsInstanceOf(typeof(IdentityExtractor), innerExtractor); object o = "value"; Assert.AreEqual(o, extractor.Extract(o)); extractor = new KeyExtractor("field"); Assert.IsNotNull(extractor); innerExtractor = (extractor as KeyExtractor).Extractor; Assert.IsNotNull(innerExtractor); Assert.IsInstanceOf(typeof(ReflectionExtractor), innerExtractor); ReflectionExtractor re = innerExtractor as ReflectionExtractor; Assert.AreEqual(re.MemberName, "field"); o = new ReflectionTestType(); Assert.AreEqual(extractor.Extract(o), (o as ReflectionTestType).field); extractor = new KeyExtractor("InnerMember.field"); Assert.IsNotNull(extractor); innerExtractor = (extractor as KeyExtractor).Extractor; Assert.IsNotNull(innerExtractor); Assert.IsInstanceOf(typeof(ChainedExtractor), innerExtractor); ChainedExtractor ce = innerExtractor as ChainedExtractor; Assert.IsNotNull(ce.Extractors); Assert.AreEqual(ce.Extractors.Length, 2); Assert.AreEqual(extractor.Extract(o), (o as ReflectionTestType).InnerMember.field); // testing on remote cache INamedCache cache = CacheFactory.GetCache(CacheName); cache.Clear(); Hashtable ht = new Hashtable(); ht.Add("identityExtractorKey1", 435); ht.Add("identityExtractorKey2", 253); ht.Add("identityExtractorKey3", 3); ht.Add("identityExtractorKey4", null); ht.Add("identityExtractorKey5", -3); cache.InsertAll(ht); extractor = new KeyExtractor(IdentityExtractor.Instance); IFilter filter = new EqualsFilter(extractor, "identityExtractorKey3"); ICollection keys = cache.GetKeys(filter); Assert.Contains("identityExtractorKey3", (IList)keys); Assert.AreEqual(keys.Count, 1); CacheFactory.Shutdown(); }
public void TestConditionalPut() { ConditionalPut conditionalPut = new ConditionalPut(AlwaysFilter.Instance, 1500); ConditionalPut conditionalPut1 = new ConditionalPut(AlwaysFilter.Instance, 1500); IInvocableCacheEntry entry = new LocalCache.Entry(new LocalCache(), "key1", 1200); Object result = conditionalPut.Process(entry); Assert.AreEqual(1500, entry.Value); ConditionalPut conditionalPut2 = new ConditionalPut(new GreaterFilter(IdentityExtractor.Instance, 100), 100); entry = new LocalCache.Entry(new LocalCache(), "key1", 1200); result = conditionalPut2.Process(entry); Assert.AreEqual(100, entry.Value); entry = new LocalCache.Entry(new LocalCache(), "key1", 80); result = conditionalPut2.Process(entry); Assert.AreEqual(80, entry.Value); Assert.AreEqual(conditionalPut, conditionalPut1); Assert.AreEqual(conditionalPut.ToString(), conditionalPut1.ToString()); Assert.AreEqual(conditionalPut.GetHashCode(), conditionalPut1.GetHashCode()); // testing on remote cache INamedCache cache = CacheFactory.GetCache(CacheName); cache.Clear(); Hashtable ht = new Hashtable(); ht.Add("conditionalPutKey1", 435); ht.Add("conditionalPutKey2", 253); ht.Add("conditionalPutKey3", 3); ht.Add("conditionalPutKey4", 200); ht.Add("conditionalPutKey5", 333); cache.InsertAll(ht); IFilter greaterThen600 = new GreaterFilter(IdentityExtractor.Instance, 600); IFilter greaterThen200 = new GreaterFilter(IdentityExtractor.Instance, 200); // no entries with value>600 ICollection keys = cache.GetKeys(greaterThen600); Assert.IsTrue(keys.Count == 0); // invoke processor for one entry with filter that will evaluate to false // again, no entries with value>600 IEntryProcessor processor = new ConditionalPut(greaterThen600, 666); cache.Invoke("conditionalPutKey1", processor); keys = cache.GetKeys(greaterThen600); Assert.IsTrue(keys.Count == 0); // invoke processor for one entry with filter that will evaluate to true // this will change one entry processor = new ConditionalPut(AlwaysFilter.Instance, 666); cache.Invoke("conditionalPutKey1", processor); keys = cache.GetKeys(greaterThen600); Assert.AreEqual(keys.Count, 1); Assert.AreEqual(cache["conditionalPutKey1"], 666); // 3 entries with value>200 keys = cache.GetKeys(greaterThen200); Assert.AreEqual(keys.Count, 3); // invoke processor for these three entries processor = new ConditionalPut(greaterThen200, 666); IDictionary results = cache.InvokeAll(cache.Keys, processor); keys = cache.GetKeys(greaterThen600); Assert.AreEqual(keys.Count, 3); CacheFactory.Shutdown(); }