001/**
002 * Copyright 2010 The Kuali Foundation Licensed under the
003 * Educational Community License, Version 2.0 (the "License"); you may
004 * not use this file except in compliance with the License. You may
005 * obtain a copy of the License at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS"
011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012 * or implied. See the License for the specific language governing
013 * permissions and limitations under the License.
014 */
015
016package org.kuali.student.common_test_tester;
017
018import static org.junit.Assert.assertEquals;
019import static org.junit.Assert.assertNotNull;
020import static org.junit.Assert.assertTrue;
021
022import java.lang.reflect.Method;
023
024import org.aopalliance.aop.Advice;
025import org.apache.log4j.Logger;
026import org.aspectj.lang.ProceedingJoinPoint;
027import org.junit.Test;
028import org.kuali.student.common.test.spring.AbstractServiceTest;
029import org.kuali.student.common.test.spring.Client;
030import org.kuali.student.common.test.spring.Dao;
031import org.kuali.student.common.test.spring.Daos;
032import org.kuali.student.common.test.spring.IdToObjectEhcacheAdvice;
033import org.kuali.student.common.test.spring.PersistenceFileLocation;
034import org.kuali.student.common_test_tester.support.MyService;
035import org.springframework.aop.aspectj.AspectInstanceFactory;
036import org.springframework.aop.aspectj.AspectJAroundAdvice;
037import org.springframework.aop.aspectj.AspectJExpressionPointcut;
038import org.springframework.aop.aspectj.AspectJPointcutAdvisor;
039import org.springframework.aop.aspectj.SingletonAspectInstanceFactory;
040import org.springframework.aop.framework.ProxyFactory;
041
042@Deprecated
043@Daos( {
044                @Dao(value = "org.kuali.student.common_test_tester.support.MyDaoImpl", testDataFile = "classpath:META-INF/load-my-beans.xml",testSqlFile="classpath:test.sql"),
045                @Dao("org.kuali.student.common_test_tester.support.OtherDaoImpl") })
046@PersistenceFileLocation("classpath:META-INF/test-persistence.xml")
047public class ServiceCommonTest extends AbstractServiceTest {
048        final Logger LOG = Logger.getLogger(ServiceCommonTest.class);
049        @Client(value="org.kuali.student.common_test_tester.support.MyServiceImpl",additionalContextFile="classpath:test-my-additional-context.xml")
050        private MyService client;
051
052        @Test
053        public void test1() {
054                LOG.info(System.getProperty("ks.test.daoImplClasses"));
055                assertNotNull(client.saveString("la la la"));
056        }
057
058        @Test
059        public void testDaoLoader(){
060            String value = "loaded-value";
061            assertEquals(value, client.findValueFromValue(value));
062        }
063        
064        @Test
065        public void testClientCaching() {
066                // Create a proxy for aop caching
067                MyService cachedClient = client;
068                try {
069                        //Create the proxy
070                        ProxyFactory factory = new ProxyFactory(client);
071                        factory.addInterface(MyService.class);
072
073                        //Create the advice (caching) and a factory for that advice
074                        Advice cacheAdvice = new IdToObjectEhcacheAdvice("cachename");
075                        AspectInstanceFactory aif = new SingletonAspectInstanceFactory(
076                                        cacheAdvice);
077                        
078                        //Look up the method object
079                        Method method = IdToObjectEhcacheAdvice.class.getMethod(
080                                        "getFromCache", ProceedingJoinPoint.class);
081                        //Create a new pointcut
082                        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
083                        pointcut
084                                        .setExpression("execution(* org.kuali.student.common_test_tester.support.MyService.find*(..))");
085                        //Create the around advice using our caching aspect and the point cut
086                        AspectJAroundAdvice advice = new AspectJAroundAdvice(method,
087                                        pointcut, aif);
088                        //Create an advisor to wrap our advice
089                        AspectJPointcutAdvisor advisor = new AspectJPointcutAdvisor(advice);
090                        //Add the advisor to the proxy factory
091                        factory.addAdvisor(advisor);
092
093                        //Do the same with a different Method/Expression
094                        method = IdToObjectEhcacheAdvice.class.getMethod("invalidateCache",
095                                        ProceedingJoinPoint.class);
096                        pointcut = new AspectJExpressionPointcut();
097                        pointcut
098                                        .setExpression("execution(* org.kuali.student.common_test_tester.support.MyService.update*(..))");
099                        advice = new AspectJAroundAdvice(method, pointcut, aif);
100                        advisor = new AspectJPointcutAdvisor(advice);
101                        factory.addAdvisor(advisor);
102                        
103                        cachedClient = (MyService) factory.getProxy();
104                } catch (SecurityException e) {
105                        LOG.error(e);
106                } catch (NoSuchMethodException e) {
107                        LOG.error(e);
108                }
109
110                String id = cachedClient.saveString("Cache me!");
111                cachedClient.findStringId(id);
112                // find again but it should be cached by now
113                assertNotNull(cachedClient.findStringId(id));
114                assertTrue(cachedClient.updateValue(id, "Updated!!!"));
115                // now should not be in caches
116                assertNotNull(cachedClient.findStringId(id));
117        }
118}