public void ClassInitialize()
 {
     if (this.assembly == null)
     {
         this.assembly = WeaverHelper.WeaveAssembly(GetType().Name, WeaverConfig);
     }
 }
Esempio n. 2
0
        public void TestClassWithParameterizedCacheAttributeComplexCacheAttributePassesParametersMin()
        {
            // Arrange
            dynamic cache      = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic testClass1 = WeaverHelper.CreateInstance <TestClassWithParameterizedCacheAttribute>(Assembly, cache);

            // Act
            testClass1.ComplexCacheParameterMethodMin();

            // Assert
            Assert.IsTrue(cache.NumStoreParameterCalls == 1);
            Assert.IsTrue(cache.ParametersPassedToLastStoreCall.Count == 12);
            Assert.IsTrue((bool)cache.ParametersPassedToLastStoreCall["ParameterBool"] == false);
            Assert.IsTrue((byte)cache.ParametersPassedToLastStoreCall["ParameterByte"] == Byte.MinValue);
            Assert.IsTrue((char)cache.ParametersPassedToLastStoreCall["ParameterCharacter"] == Char.MinValue);
            Assert.IsTrue((double)cache.ParametersPassedToLastStoreCall["ParameterDouble"] == Double.MinValue);
            Assert.IsTrue((float)cache.ParametersPassedToLastStoreCall["ParameterFloat"] == Single.MinValue);
            Assert.IsTrue((int)cache.ParametersPassedToLastStoreCall["ParameterInt"] == Int32.MinValue);
            Assert.IsTrue((long)cache.ParametersPassedToLastStoreCall["ParameterLong"] == Int64.MinValue);
            Assert.IsTrue((sbyte)cache.ParametersPassedToLastStoreCall["ParameterSByte"] == SByte.MinValue);
            Assert.IsTrue((short)cache.ParametersPassedToLastStoreCall["ParameterShort"] == Int16.MinValue);
            Assert.IsTrue((uint)cache.ParametersPassedToLastStoreCall["ParameterUInt"] == UInt32.MinValue);
            Assert.IsTrue((ulong)cache.ParametersPassedToLastStoreCall["ParameterULong"] == UInt64.MinValue);
            Assert.IsTrue((ushort)cache.ParametersPassedToLastStoreCall["ParameterUShort"] == UInt16.MinValue);
        }
Esempio n. 3
0
        public void TestClass1ClassLevelCacheAttributeSuccessfullyRemoved()
        {
            dynamic cache      = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic testClass1 = WeaverHelper.CreateInstance <TestClass1>(Assembly, cache);

            Type classType = testClass1.GetType();

            Assert.IsFalse(classType.GetCustomAttributes(true).Any(x => x.GetType().Name == ModuleWeaver.CacheAttributeName));
        }
Esempio n. 4
0
        public void TestClass7MethodLevelNoCacheAttributeSuccessfullyRemoved()
        {
            dynamic cache      = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic testClass7 = WeaverHelper.CreateInstance <TestClass7>(Assembly, cache);

            Type       classType  = testClass7.GetType();
            MethodInfo methodInfo = classType.GetMethod("MethodTwo");

            Assert.IsFalse(methodInfo.GetCustomAttributes(true).Any(x => x.GetType().Name == ModuleWeaver.NoCacheAttributeName));
        }
Esempio n. 5
0
        public void TestClass5WrongCacheGetterTypeSuccessfullyIgnored()
        {
            // Arrange
            dynamic testClass5 = WeaverHelper.CreateInstance <TestClass5>(Assembly);

            // Act
            testClass5.MethodOne(1337);

            // Assert (test should not throw any exceptions)
        }
Esempio n. 6
0
        public void IndividualCache_ShouldNeverWeaveWriteOnlyProperties()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassIndividualProperties>(Assembly, cache);

            // Act
            instance.SetOnlyProperty = "some text";

            // Assert
            Assert.That(cache.NumRemoveCalls, Is.EqualTo(0));
        }
Esempio n. 7
0
        public void GenericMethod()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassGeneric <Tuple <int, double> > >(Assembly, cache);

            // Act
            dynamic value1 = instance.Method1 <int>(0);

            // Assert
            Assert.IsTrue(cache.Contains(string.Format("MethodCache.Tests.TestAssembly.TestClassGeneric`1.Method1_{0}_{1}", typeof(int), 0)));
        }
Esempio n. 8
0
        public void TestClass7MethodLevelNoCacheAttributeNotCachedMethodTwoReturnsDifferentInstances()
        {
            // Arrange
            dynamic cache      = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic testClass7 = WeaverHelper.CreateInstance <TestClass7>(Assembly, cache);

            // Act
            dynamic instance1 = testClass7.MethodTwo("1337");
            dynamic instance2 = testClass7.MethodTwo("1337");

            // Assert
            Assert.IsTrue(!ReferenceEquals(instance1, instance2));
        }
Esempio n. 9
0
        public void ClassLevelCache_ShouldRemoveClassCacheAttribute()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassIndividualProperties>(Assembly, cache);
            Type    type     = instance.GetType();

            // Act
            object[] customAttributes = type.GetCustomAttributes(true);

            // Assert
            Assert.IsEmpty(customAttributes);
        }
Esempio n. 10
0
        public void TestClass1ClassLevelCacheAttributeCachedMethodTwoReturnsSameInstance()
        {
            // Arrange
            dynamic cache      = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic testClass1 = WeaverHelper.CreateInstance <TestClass1>(Assembly, cache);

            // Act
            dynamic instance1 = testClass1.MethodTwo("1337");
            dynamic instance2 = testClass1.MethodTwo("1337");

            // Assert
            Assert.IsTrue(ReferenceEquals(instance1, instance2));
        }
Esempio n. 11
0
        public void TestClass7MethodLevelNoCacheAttributeDoesNotCachesMethodTwo()
        {
            // Arrange
            dynamic cache      = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic testClass7 = WeaverHelper.CreateInstance <TestClass7>(Assembly, cache);

            // Act
            testClass7.MethodTwo("1337");
            testClass7.MethodTwo("1337");

            // Assert
            Assert.IsTrue(cache.NumStoreCalls == 0);
            Assert.IsTrue(cache.NumRetrieveCalls == 0);
        }
Esempio n. 12
0
        public void TestClass6InheritedCacheGetterCanBeUsed()
        {
            // Arrange
            dynamic cache      = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic testClass6 = WeaverHelper.CreateInstance <TestClass6>(Assembly, cache);

            // Act
            testClass6.MethodThree(1337);
            testClass6.MethodThree(1337);

            // Assert
            Assert.IsTrue(cache.NumStoreCalls == 1);
            Assert.IsTrue(cache.NumRetrieveCalls == 1);
        }
Esempio n. 13
0
        public void ClassLevelCache_SettingPropertyShouldInvalidateCache()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassWithProperties>(Assembly, cache);

            // Act
            dynamic value = instance.ReadWriteProperty;

            instance.ReadWriteProperty = 10;

            // Assert
            Assert.That(cache.Contains("MethodCache.Tests.TestAssembly.TestClassWithProperties.ReadWriteProperty"), Is.False);
        }
Esempio n. 14
0
        public void ClassLevelCache_ShouldRemoveNoCacheAttribute()
        {
            // Arrange
            dynamic      cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic      instance = WeaverHelper.CreateInstance <TestClassWithProperties>(Assembly, cache);
            Type         type     = instance.GetType();
            PropertyInfo property = type.GetProperty("ReadOnlyNoCache");

            // Act
            object[] customAttributes = property.GetCustomAttributes(true);

            // Assert
            Assert.IsEmpty(customAttributes);
        }
Esempio n. 15
0
        public void TestClass3ConcreteCacheGetterCachesMethodTwo()
        {
            // Arrange
            dynamic cache      = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic testClass2 = WeaverHelper.CreateInstance <TestClass3>(Assembly, cache);

            // Act
            testClass2.MethodTwo("1337");
            testClass2.MethodTwo("1337");

            // Assert
            Assert.IsTrue(cache.NumStoreCalls == 1);
            Assert.IsTrue(cache.NumRetrieveCalls == 1);
        }
Esempio n. 16
0
        public void TestClass1ClassLevelCacheAttributeCachesMethodOne()
        {
            // Arrange
            dynamic cache      = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic testClass1 = WeaverHelper.CreateInstance <TestClass1>(Assembly, cache);

            // Act
            testClass1.MethodOne(1337);
            testClass1.MethodOne(1337);

            // Assert
            Assert.IsTrue(cache.NumStoreCalls == 1);
            Assert.IsTrue(cache.NumRetrieveCalls == 1);
        }
Esempio n. 17
0
        public void ClassLevelCacheMethodsWhenPropertiesExcluded()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassPropertiesExcluded>(Assembly, cache);

            // Act
            dynamic value = instance.Method(10);

            value = instance.Method(10);

            // Assert
            Assert.IsTrue(cache.NumStoreCalls == 1);
            Assert.IsTrue(cache.NumRetrieveCalls == 1);
        }
Esempio n. 18
0
        public void TestClassWithParameterizedCacheAttributeComplexCacheAttributePassesParametersType()
        {
            // Arrange
            dynamic cache      = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic testClass1 = WeaverHelper.CreateInstance <TestClassWithParameterizedCacheAttribute>(Assembly, cache);

            // Act
            testClass1.ComplexCacheParameterMethodType();

            // Assert
            Assert.IsTrue(cache.NumStoreParameterCalls == 1);
            Assert.IsTrue(cache.ParametersPassedToLastStoreCall.Count == 1);
            Assert.IsInstanceOfType(typeof(Type), cache.ParametersPassedToLastStoreCall["ParameterType"]);
            Assert.IsTrue((Type)cache.ParametersPassedToLastStoreCall["ParameterType"] == typeof(List <int>));
        }
        public void IndividualCache_ShouldCacheMethods()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClass6>(Assembly, cache);

            // Act
            dynamic value = instance.MethodThree(10);

            value = instance.MethodThree(10);

            // Assert
            Assert.That(cache.NumStoreCalls, Is.EqualTo(1));
            Assert.That(cache.NumRetrieveCalls, Is.EqualTo(1));
        }
        public void ClassLevelCache_ShouldCacheMethodWhenSelectedExplicitlyOnClassAttribute()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassPropertiesExcluded>(Assembly, cache);

            // Act
            dynamic value = instance.Method(10);

            value = instance.Method(10);

            // Assert
            Assert.That(cache.NumStoreCalls, Is.EqualTo(1));
            Assert.That(cache.NumRetrieveCalls, Is.EqualTo(1));
        }
Esempio n. 21
0
        public void ClassLevelCache_ShouldBePossibleToSelectivelyEnablePropertyWeaving()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassPropertiesExcluded>(Assembly, cache);

            // Act
            dynamic value = instance.ExplicitlyCachedProperty;

            value = instance.ExplicitlyCachedProperty;

            // Assert
            Assert.IsTrue(cache.NumStoreCalls == 1);
            Assert.IsTrue(cache.NumRetrieveCalls == 1);
        }
Esempio n. 22
0
        public void ClassLevelCache_ShouldCachePropertyWhenMethodsAreExcluded()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassMethodsExcluded>(Assembly, cache);

            // Act
            dynamic value = instance.Property;

            value = instance.Property;

            // Assert
            Assert.IsTrue(cache.NumStoreCalls == 1);
            Assert.IsTrue(cache.NumRetrieveCalls == 1);
        }
Esempio n. 23
0
        public void IndividualCache_ShouldNotCacheAutoproperties()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassIndividualProperties>(Assembly, cache);

            // Act
            dynamic value = instance.AutoProperty;

            value = instance.AutoProperty;

            // Assert
            Assert.IsTrue(cache.NumStoreCalls == 0);
            Assert.IsTrue(cache.NumRetrieveCalls == 0);
        }
Esempio n. 24
0
        public void ClassLevelCache_ShouldRespectNoCacheAttribute()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassWithProperties>(Assembly, cache);

            // Act
            dynamic value = instance.ReadOnlyNoCache;

            value = instance.ReadOnlyNoCache;

            // Assert
            Assert.IsTrue(cache.NumStoreCalls == 0);
            Assert.IsTrue(cache.NumRetrieveCalls == 0);
        }
Esempio n. 25
0
        public void ClassLevelCacheWithoutRemove_ShouldNotCacheReadWriteProperty()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassWithPropertiesWithoutRemove>(Assembly, cache);

            // Act
            dynamic value = instance.ReadWriteProperty;

            value = instance.ReadWriteProperty;

            // Assert
            Assert.IsTrue(cache.NumStoreCalls == 0);
            Assert.IsTrue(cache.NumRetrieveCalls == 0);
        }
Esempio n. 26
0
        public void TestClassWithParameterizedCacheAttributeComplexCacheAttributePassesParametersStringArray()
        {
            // Arrange
            dynamic cache      = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic testClass1 = WeaverHelper.CreateInstance <TestClassWithParameterizedCacheAttribute>(Assembly, cache);

            // Act
            testClass1.ComplexCacheParameterMethodStringArray();

            // Assert
            Assert.IsTrue(cache.NumStoreParameterCalls == 1);
            Assert.IsTrue(cache.ParametersPassedToLastStoreCall.Count == 1);
            Assert.IsInstanceOfType(typeof(string[]), cache.ParametersPassedToLastStoreCall["ParameterStringArray"]);
            Assert.IsTrue(cache.ParametersPassedToLastStoreCall["ParameterStringArray"].Length == 2);
            Assert.IsTrue(cache.ParametersPassedToLastStoreCall["ParameterStringArray"][0] == "1");
            Assert.IsTrue(cache.ParametersPassedToLastStoreCall["ParameterStringArray"][1] == "2");
        }
Esempio n. 27
0
        public void TestClassWithParameterizedCacheAttributeMethodLevelCacheAttributeOverridesParameters()
        {
            // Arrange
            dynamic cache      = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic testClass1 = WeaverHelper.CreateInstance <TestClassWithParameterizedCacheAttribute>(Assembly, cache);

            // Act
            testClass1.OverridenCacheParameterMethod("1", 2);
            testClass1.OverridenCacheParameterMethod("1", 2);

            // Assert
            Assert.IsTrue(cache.NumStoreParameterCalls == 1);
            Assert.IsTrue(cache.NumRetrieveCalls == 1);
            Assert.IsTrue(cache.ParametersPassedToLastStoreCall.Count == 2);
            Assert.IsTrue(cache.ParametersPassedToLastStoreCall["Parameter2"] == 2);
            Assert.IsTrue(cache.ParametersPassedToLastStoreCall["Parameter3"] == true);
        }
Esempio n. 28
0
        public void GenericClassValueTypePropertyExecutesCorrectly()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassGeneric <int> >(Assembly, cache);

            // Act
            dynamic value = instance.Property;

            value             = instance.Property;
            instance.Property = value;

            // Assert
            Assert.IsTrue(cache.NumStoreCalls == 1);
            Assert.IsTrue(cache.NumRetrieveCalls == 1);
            Assert.IsTrue(cache.NumRemoveCalls == 1);
        }
        public void IndividualCache_ShouldCacheEligibleProperties()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassIndividualProperties>(Assembly, cache);

            // Act
            dynamic value = instance.ReadOnlyProperty;

            value = instance.ReadOnlyProperty;
            value = instance.ReadWriteProperty;
            value = instance.ReadWriteProperty;

            // Assert
            Assert.That(cache.NumStoreCalls, Is.EqualTo(2));
            Assert.That(cache.NumRetrieveCalls, Is.EqualTo(2));
        }
Esempio n. 30
0
        public void GenericClassValueTypeExecutesCorrectly()
        {
            // Arrange
            dynamic cache    = WeaverHelper.CreateInstance <DictionaryCache>(Assembly);
            dynamic instance = WeaverHelper.CreateInstance <TestClassGeneric <int> >(Assembly, cache);

            // Act
            dynamic value1 = instance.Method1 <int>(10);

            value1 = instance.Method1 <int>(10);
            dynamic value2 = instance.Method2(10);

            value2 = instance.Method2(10);

            // Assert
            Assert.IsTrue(cache.NumStoreCalls == 2);
            Assert.IsTrue(cache.NumRetrieveCalls == 2);
        }