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    
016    package org.kuali.student.common_test_tester;
017    
018    import static org.junit.Assert.assertEquals;
019    import static org.junit.Assert.assertNotNull;
020    import static org.junit.Assert.assertTrue;
021    
022    import java.lang.reflect.Method;
023    
024    import org.aopalliance.aop.Advice;
025    import org.apache.log4j.Logger;
026    import org.aspectj.lang.ProceedingJoinPoint;
027    import org.junit.Test;
028    import org.kuali.student.common.test.spring.AbstractServiceTest;
029    import org.kuali.student.common.test.spring.Client;
030    import org.kuali.student.common.test.spring.Dao;
031    import org.kuali.student.common.test.spring.Daos;
032    import org.kuali.student.common.test.spring.IdToObjectEhcacheAdvice;
033    import org.kuali.student.common.test.spring.PersistenceFileLocation;
034    import org.kuali.student.common_test_tester.support.MyService;
035    import org.springframework.aop.aspectj.AspectInstanceFactory;
036    import org.springframework.aop.aspectj.AspectJAroundAdvice;
037    import org.springframework.aop.aspectj.AspectJExpressionPointcut;
038    import org.springframework.aop.aspectj.AspectJPointcutAdvisor;
039    import org.springframework.aop.aspectj.SingletonAspectInstanceFactory;
040    import org.springframework.aop.framework.ProxyFactory;
041    
042    @Daos( {
043                    @Dao(value = "org.kuali.student.common_test_tester.support.MyDaoImpl", testDataFile = "classpath:META-INF/load-my-beans.xml",testSqlFile="classpath:test.sql"),
044                    @Dao("org.kuali.student.common_test_tester.support.OtherDaoImpl") })
045    @PersistenceFileLocation("classpath:META-INF/test-persistence.xml")
046    public class ServiceCommonTest extends AbstractServiceTest {
047            final Logger LOG = Logger.getLogger(ServiceCommonTest.class);
048            @Client(value="org.kuali.student.common_test_tester.support.MyServiceImpl",additionalContextFile="classpath:test-my-additional-context.xml")
049            private MyService client;
050    
051            @Test
052            public void test1() {
053                    LOG.info(System.getProperty("ks.test.daoImplClasses"));
054                    assertNotNull(client.saveString("la la la"));
055            }
056    
057            @Test
058            public void testDaoLoader(){
059                String value = "loaded-value";
060                assertEquals(value, client.findValueFromValue(value));
061            }
062            
063            @Test
064            public void testClientCaching() {
065                    // Create a proxy for aop caching
066                    MyService cachedClient = client;
067                    try {
068                            //Create the proxy
069                            ProxyFactory factory = new ProxyFactory(client);
070                            factory.addInterface(MyService.class);
071    
072                            //Create the advice (caching) and a factory for that advice
073                            Advice cacheAdvice = new IdToObjectEhcacheAdvice("cachename");
074                            AspectInstanceFactory aif = new SingletonAspectInstanceFactory(
075                                            cacheAdvice);
076                            
077                            //Look up the method object
078                            Method method = IdToObjectEhcacheAdvice.class.getMethod(
079                                            "getFromCache", ProceedingJoinPoint.class);
080                            //Create a new pointcut
081                            AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
082                            pointcut
083                                            .setExpression("execution(* org.kuali.student.common_test_tester.support.MyService.find*(..))");
084                            //Create the around advice using our caching aspect and the point cut
085                            AspectJAroundAdvice advice = new AspectJAroundAdvice(method,
086                                            pointcut, aif);
087                            //Create an advisor to wrap our advice
088                            AspectJPointcutAdvisor advisor = new AspectJPointcutAdvisor(advice);
089                            //Add the advisor to the proxy factory
090                            factory.addAdvisor(advisor);
091    
092                            //Do the same with a different Method/Expression
093                            method = IdToObjectEhcacheAdvice.class.getMethod("invalidateCache",
094                                            ProceedingJoinPoint.class);
095                            pointcut = new AspectJExpressionPointcut();
096                            pointcut
097                                            .setExpression("execution(* org.kuali.student.common_test_tester.support.MyService.update*(..))");
098                            advice = new AspectJAroundAdvice(method, pointcut, aif);
099                            advisor = new AspectJPointcutAdvisor(advice);
100                            factory.addAdvisor(advisor);
101                            
102                            cachedClient = (MyService) factory.getProxy();
103                    } catch (SecurityException e) {
104                            LOG.error(e);
105                    } catch (NoSuchMethodException e) {
106                            LOG.error(e);
107                    }
108    
109                    String id = cachedClient.saveString("Cache me!");
110                    cachedClient.findStringId(id);
111                    // find again but it should be cached by now
112                    assertNotNull(cachedClient.findStringId(id));
113                    assertTrue(cachedClient.updateValue(id, "Updated!!!"));
114                    // now should not be in caches
115                    assertNotNull(cachedClient.findStringId(id));
116            }
117    }