CacheAspectIntegrationTests.cs :  » Inversion-of-Control-Dependency-Injection » Spring.net » Spring » Aspects » Cache » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Inversion of Control Dependency Injection » Spring.net 
Spring.net » Spring » Aspects » Cache » CacheAspectIntegrationTests.cs
#region License

/*
 * Copyright  2002-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#endregion

#region Imports

using System;
using System.Collections;
using System.Collections.Specialized;
using NUnit.Framework;
using Spring.Aop.Framework;
using Spring.Caching;
using Spring.Context;
using Spring.Context.Support;
using Spring.Objects.Factory;

#endregion

namespace Spring.Aspects.Cache{
    /// <summary>
    /// Unit tests for the CacheParameterAdvice class.
    /// </summary>
    /// <author>Aleksandar Seovic</author>
    [TestFixture]
    public sealed class CacheAspectIntegrationTests
    {
        private GenericApplicationContext context;
        private CacheAspect cacheAspect;

        [SetUp]
        public void SetUp()
        {
            context = new GenericApplicationContext();

            cacheAspect = new CacheAspect();
            cacheAspect.ApplicationContext = context;
        }

        [Test]
        public void TestCaching()
        {
            ICache cache = new NonExpiringCache();
            context.ObjectFactory.RegisterSingleton("inventors", cache);

            ProxyFactory pf = new ProxyFactory(new InventorStore());
            pf.AddAdvisors(cacheAspect);

            IInventorStore store = (IInventorStore) pf.GetProxy();

            Assert.AreEqual(0, cache.Count);
            
            IList inventors = store.GetAll();
            Assert.AreEqual(2, cache.Count);

            store.Delete((Inventor) inventors[0]);
            Assert.AreEqual(1, cache.Count);

            Inventor tesla = store.Load("Nikola Tesla");
            Assert.AreEqual(2, cache.Count);

            store.Save(tesla);
            Assert.AreEqual(2, cache.Count);
            Assert.AreEqual("Serbian", ((Inventor)cache.Get("Nikola Tesla")).Nationality);

            store.DeleteAll();
            Assert.AreEqual(0, cache.Count);
        }

        /// <summary>
        /// http://jira.springframework.org/browse/SPRNET-1226
        /// </summary>
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void NoCacheKeySpecified()
        {
            ICache cache = new NonExpiringCache();
            context.ObjectFactory.RegisterSingleton("inventors", cache);

            ProxyFactory pf = new ProxyFactory(new InventorStore());
            pf.AddAdvisors(cacheAspect);

            IInventorStore store = (IInventorStore)pf.GetProxy();
            IList items = store.GetAllNoCacheKey();
            Assert.IsNotNull(items);
        }

        [Test]
        [ExpectedException(typeof(NoSuchObjectDefinitionException))]
        public void CacheDoesNotExist()
        {
            //ICache cache = new NonExpiringCache();
            //context.ObjectFactory.RegisterSingleton("losers", cache);

            ProxyFactory pf = new ProxyFactory(new InventorStore());
            pf.AddAdvisors(cacheAspect);

            IInventorStore store = (IInventorStore)pf.GetProxy();
            IList items = store.GetAll();
        }

        [Test]
        [ExpectedException(typeof(ArgumentException))]
        public void CacheDoesNotImplementICache()
        {
            context.ObjectFactory.RegisterSingleton("inventors", new Object());

            ProxyFactory pf = new ProxyFactory(new InventorStore());
            pf.AddAdvisors(cacheAspect);

            IInventorStore store = (IInventorStore)pf.GetProxy();
            IList items = store.GetAll();
        }

#if NET_2_0
        [Test(Description = "http://jira.springframework.org/browse/SPRNET-959")]
        public void UseMethodInfoForKeyGeneration()
        {
            ICache cache = new NonExpiringCache();
            context.ObjectFactory.RegisterSingleton("defaultCache", cache);

            ProxyFactory pf = new ProxyFactory(new GenericDao<string, int>());
            pf.AddAdvisors(cacheAspect);

            IGenericDao<string, int> dao = (IGenericDao<string, int>)pf.GetProxy();

            Assert.AreEqual(0, cache.Count);

            dao.Load(1);
            Assert.AreEqual(1, cache.Count);

            // actually, it should be null, because default(string) = null
            // but it returns the NullValue marker created by the CacheResultAttribute
            Assert.IsNotNull(cache.Get("String_1"));
        }
#endif
    }

    #region Inner Class : CacheParameterTarget

    public interface IInventorStore
    {
        IList GetAll();
        IList GetAllNoCacheKey();
        Inventor Load(string name);
        void Save(Inventor inventor);
        void Delete(Inventor inventor);
        void DeleteAll();
    }

    public sealed class InventorStore : IInventorStore
    {
        private IDictionary inventors = new ListDictionary();

        public InventorStore()
        {
            Inventor tesla = new Inventor("Nikola Tesla", new DateTime(1856, 7, 9), null);
            Inventor pupin = new Inventor("Mihajlo Pupin", new DateTime(1854, 10, 9), null);
            inventors.Add("Nikola Tesla", tesla);
            inventors.Add("Mihajlo Pupin", pupin);
        }

        [CacheResultItems("inventors", "Name")]
        public IList GetAll()
        {
            return new ArrayList(inventors.Values);
        }


        [CacheResult(CacheName = "inventors")]
        public IList GetAllNoCacheKey()
        {
            return new ArrayList(inventors.Values);
        }

        [CacheResult("inventors", "#name")]
        public Inventor Load(string name)
        {
            return (Inventor) inventors[name];
        }

        public void Save([CacheParameter("inventors", "Name")] Inventor inventor)
        {
            inventor.Nationality = "Serbian";
        }

        [InvalidateCache("inventors", Keys = "#inventor.Name")]
        public void Delete(Inventor inventor)
        {
        }

        [InvalidateCache("inventors")]
        public void DeleteAll()
        {
        }
    }

#if NET_2_0
    public interface IGenericDao<T, IdT>
    {
        T Load(IdT id);
    }

    public sealed class GenericDao<T, IdT> : IGenericDao<T, IdT>
    {
        [CacheResult("defaultCache", "#Load.ReturnType.Name + '_' + #id")]
        public T Load(IdT id)
        {
            return default(T);
        }
    }
#endif

    #endregion
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.