View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common_test_tester;
17  
18  import static org.junit.Assert.assertEquals;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertTrue;
21  
22  import java.lang.reflect.Method;
23  
24  import org.aopalliance.aop.Advice;
25  import org.apache.log4j.Logger;
26  import org.aspectj.lang.ProceedingJoinPoint;
27  import org.junit.Test;
28  import org.kuali.student.common.test.spring.AbstractServiceTest;
29  import org.kuali.student.common.test.spring.Client;
30  import org.kuali.student.common.test.spring.Dao;
31  import org.kuali.student.common.test.spring.Daos;
32  import org.kuali.student.common.test.spring.IdToObjectEhcacheAdvice;
33  import org.kuali.student.common.test.spring.PersistenceFileLocation;
34  import org.kuali.student.common_test_tester.support.MyService;
35  import org.springframework.aop.aspectj.AspectInstanceFactory;
36  import org.springframework.aop.aspectj.AspectJAroundAdvice;
37  import org.springframework.aop.aspectj.AspectJExpressionPointcut;
38  import org.springframework.aop.aspectj.AspectJPointcutAdvisor;
39  import org.springframework.aop.aspectj.SingletonAspectInstanceFactory;
40  import org.springframework.aop.framework.ProxyFactory;
41  
42  @Deprecated
43  @Daos( {
44  		@Dao(value = "org.kuali.student.common_test_tester.support.MyDaoImpl", testDataFile = "classpath:META-INF/load-my-beans.xml",testSqlFile="classpath:test.sql"),
45  		@Dao("org.kuali.student.common_test_tester.support.OtherDaoImpl") })
46  @PersistenceFileLocation("classpath:META-INF/test-persistence.xml")
47  public class ServiceCommonTest extends AbstractServiceTest {
48  	final Logger LOG = Logger.getLogger(ServiceCommonTest.class);
49  	@Client(value="org.kuali.student.common_test_tester.support.MyServiceImpl",additionalContextFile="classpath:test-my-additional-context.xml")
50  	private MyService client;
51  
52  	@Test
53  	public void test1() {
54  		LOG.info(System.getProperty("ks.test.daoImplClasses"));
55  		assertNotNull(client.saveString("la la la"));
56  	}
57  
58  	@Test
59  	public void testDaoLoader(){
60  	    String value = "loaded-value";
61  	    assertEquals(value, client.findValueFromValue(value));
62  	}
63  	
64  	@Test
65  	public void testClientCaching() {
66  		// Create a proxy for aop caching
67  		MyService cachedClient = client;
68  		try {
69  			//Create the proxy
70  			ProxyFactory factory = new ProxyFactory(client);
71  			factory.addInterface(MyService.class);
72  
73  			//Create the advice (caching) and a factory for that advice
74  			Advice cacheAdvice = new IdToObjectEhcacheAdvice("cachename");
75  			AspectInstanceFactory aif = new SingletonAspectInstanceFactory(
76  					cacheAdvice);
77  			
78  			//Look up the method object
79  			Method method = IdToObjectEhcacheAdvice.class.getMethod(
80  					"getFromCache", ProceedingJoinPoint.class);
81  			//Create a new pointcut
82  			AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
83  			pointcut
84  					.setExpression("execution(* org.kuali.student.common_test_tester.support.MyService.find*(..))");
85  			//Create the around advice using our caching aspect and the point cut
86  			AspectJAroundAdvice advice = new AspectJAroundAdvice(method,
87  					pointcut, aif);
88  			//Create an advisor to wrap our advice
89  			AspectJPointcutAdvisor advisor = new AspectJPointcutAdvisor(advice);
90  			//Add the advisor to the proxy factory
91  			factory.addAdvisor(advisor);
92  
93  			//Do the same with a different Method/Expression
94  			method = IdToObjectEhcacheAdvice.class.getMethod("invalidateCache",
95  					ProceedingJoinPoint.class);
96  			pointcut = new AspectJExpressionPointcut();
97  			pointcut
98  					.setExpression("execution(* org.kuali.student.common_test_tester.support.MyService.update*(..))");
99  			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 }