1 16 17 package org.springframework.aop.target; 18 19 import junit.framework.TestCase; 20 21 import org.springframework.aop.interceptor.SideEffectBean; 22 import org.springframework.beans.ITestBean; 23 import org.springframework.beans.factory.xml.XmlBeanFactory; 24 import org.springframework.core.io.ClassPathResource; 25 26 29 public class ThreadLocalTargetSourceTests extends TestCase { 30 31 32 private static final int INITIAL_COUNT = 10; 33 34 private XmlBeanFactory beanFactory; 35 36 protected void setUp() throws Exception { 37 this.beanFactory = new XmlBeanFactory(new ClassPathResource("threadLocalTests.xml", getClass())); 38 } 39 40 43 protected void tearDown() { 44 this.beanFactory.destroySingletons(); 45 } 46 47 52 public void testUseDifferentManagedInstancesInSameThread() { 53 SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment"); 54 assertEquals(INITIAL_COUNT, apartment.getCount() ); 55 apartment.doWork(); 56 assertEquals(INITIAL_COUNT + 1, apartment.getCount() ); 57 58 ITestBean test = (ITestBean) beanFactory.getBean("threadLocal2"); 59 assertEquals("Rod", test.getName()); 60 assertEquals("Kerry", test.getSpouse().getName()); 61 } 62 63 public void testReuseInSameThread() { 64 SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment"); 65 assertEquals(INITIAL_COUNT, apartment.getCount() ); 66 apartment.doWork(); 67 assertEquals(INITIAL_COUNT + 1, apartment.getCount() ); 68 69 apartment = (SideEffectBean) beanFactory.getBean("apartment"); 70 assertEquals(INITIAL_COUNT + 1, apartment.getCount() ); 71 } 72 73 76 public void testCanGetStatsViaMixin() { 77 ThreadLocalTargetSourceStats stats = (ThreadLocalTargetSourceStats) beanFactory.getBean("apartment"); 78 assertEquals(1, stats.getInvocationCount()); 80 SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment"); 81 apartment.doWork(); 82 assertEquals(3, stats.getInvocationCount()); 84 assertEquals(3, stats.getHitCount()); 86 apartment.doWork(); 87 assertEquals(6, stats.getInvocationCount()); 88 assertEquals(6, stats.getHitCount()); 89 assertEquals(1, stats.getObjectCount()); 91 } 92 93 public void testNewThreadHasOwnInstance() throws InterruptedException { 94 SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment"); 95 assertEquals(INITIAL_COUNT, apartment.getCount() ); 96 apartment.doWork(); 97 apartment.doWork(); 98 apartment.doWork(); 99 assertEquals(INITIAL_COUNT + 3, apartment.getCount() ); 100 101 class Runner implements Runnable { 102 public SideEffectBean mine; 103 public void run() { 104 this.mine = (SideEffectBean) beanFactory.getBean("apartment"); 105 assertEquals(INITIAL_COUNT, mine.getCount() ); 106 mine.doWork(); 107 assertEquals(INITIAL_COUNT + 1, mine.getCount() ); 108 } 109 } 110 Runner r = new Runner(); 111 Thread t = new Thread (r); 112 t.start(); 113 t.join(); 114 115 assertNotNull(r); 116 117 assertEquals(INITIAL_COUNT + 3, apartment.getCount() ); 119 120 assertEquals(INITIAL_COUNT + 3, r.mine.getCount() ); 123 124 assertEquals(2, ((ThreadLocalTargetSourceStats) apartment).getObjectCount()); 126 } 127 128 } 129 | Popular Tags |