ThreadLocalTargetSourceTests.java
Upload User: jiancairen
Upload Date: 2007-08-27
Package Size: 26458k
Code Size: 4k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.aop.target;
  17. import junit.framework.TestCase;
  18. import org.springframework.aop.interceptor.SideEffectBean;
  19. import org.springframework.beans.ITestBean;
  20. import org.springframework.beans.factory.xml.XmlBeanFactory;
  21. import org.springframework.core.io.ClassPathResource;
  22. /**
  23.  * @author Rod Johnson
  24.  */
  25. public class ThreadLocalTargetSourceTests extends TestCase {
  26. /** Initial count value set in bean factory XML */
  27. private static final int INITIAL_COUNT = 10;
  28. private XmlBeanFactory beanFactory;
  29. protected void setUp() throws Exception {
  30. this.beanFactory = new XmlBeanFactory(new ClassPathResource("threadLocalTests.xml", getClass()));
  31. }
  32. /**
  33.  * We must simulate container shutdown, which should clear threads.
  34.  */
  35. protected void tearDown() {
  36. this.beanFactory.destroySingletons();
  37. }
  38. /**
  39.  * Check we can use two different ThreadLocalTargetSources
  40.  * managing objects of different types without them interfering
  41.  * with one another.
  42.  */
  43. public void testUseDifferentManagedInstancesInSameThread() {
  44. SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment");
  45. assertEquals(INITIAL_COUNT, apartment.getCount() );
  46. apartment.doWork();
  47. assertEquals(INITIAL_COUNT + 1, apartment.getCount() );
  48. ITestBean test = (ITestBean) beanFactory.getBean("threadLocal2");
  49. assertEquals("Rod", test.getName());
  50. assertEquals("Kerry", test.getSpouse().getName());
  51. }
  52. public void testReuseInSameThread() {
  53. SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment");
  54. assertEquals(INITIAL_COUNT, apartment.getCount() );
  55. apartment.doWork();
  56. assertEquals(INITIAL_COUNT + 1, apartment.getCount() );
  57. apartment = (SideEffectBean) beanFactory.getBean("apartment");
  58. assertEquals(INITIAL_COUNT + 1, apartment.getCount() );
  59. }
  60. /**
  61.  * Relies on introduction.
  62.  */
  63. public void testCanGetStatsViaMixin() {
  64. ThreadLocalTargetSourceStats stats = (ThreadLocalTargetSourceStats) beanFactory.getBean("apartment");
  65. // +1 because creating target for stats call counts
  66. assertEquals(1, stats.getInvocationCount());
  67. SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment");
  68. apartment.doWork();
  69. // +1 again
  70. assertEquals(3, stats.getInvocationCount());
  71. // + 1 for states call!
  72. assertEquals(3, stats.getHitCount());
  73. apartment.doWork();
  74. assertEquals(6, stats.getInvocationCount());
  75. assertEquals(6, stats.getHitCount());
  76. // Only one thread so only one object can have been bound
  77. assertEquals(1, stats.getObjectCount());
  78. }
  79. public void testNewThreadHasOwnInstance() throws InterruptedException {
  80. SideEffectBean apartment = (SideEffectBean) beanFactory.getBean("apartment");
  81. assertEquals(INITIAL_COUNT, apartment.getCount() );
  82. apartment.doWork();
  83. apartment.doWork();
  84. apartment.doWork();
  85. assertEquals(INITIAL_COUNT + 3, apartment.getCount() );
  86. class Runner implements Runnable {
  87. public SideEffectBean mine;
  88. public void run() {
  89. this.mine = (SideEffectBean) beanFactory.getBean("apartment");
  90. assertEquals(INITIAL_COUNT, mine.getCount() );
  91. mine.doWork();
  92. assertEquals(INITIAL_COUNT + 1, mine.getCount() );
  93. }
  94. }
  95. Runner r = new Runner();
  96. Thread t = new Thread(r);
  97. t.start();
  98. t.join();
  99. assertNotNull(r);
  100. // Check it didn't affect the other thread's copy
  101. assertEquals(INITIAL_COUNT + 3, apartment.getCount() );
  102. // When we use other thread's copy in this thread 
  103. // it should behave like ours
  104. assertEquals(INITIAL_COUNT + 3, r.mine.getCount() );
  105. // Bound to two threads
  106. assertEquals(2, ((ThreadLocalTargetSourceStats) apartment).getObjectCount());
  107. }
  108. }