CommonsPoolTargetSourceTests.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.framework.Advised;
  19. import org.springframework.aop.interceptor.SideEffectBean;
  20. import org.springframework.beans.Person;
  21. import org.springframework.beans.factory.xml.XmlBeanFactory;
  22. import org.springframework.core.io.ClassPathResource;
  23. import org.springframework.util.SerializationTestUtils;
  24. /**
  25.  * Tests for pooling invoker interceptor
  26.  * TODO need to make these tests stronger: it's hard to
  27.  * make too many assumptions about a pool
  28.  * @author Rod Johnson
  29.  */
  30. public class CommonsPoolTargetSourceTests extends TestCase {
  31. /** Initial count value set in bean factory XML */
  32. private static final int INITIAL_COUNT = 10;
  33. private XmlBeanFactory beanFactory;
  34. protected void setUp() throws Exception {
  35. this.beanFactory = new XmlBeanFactory(new ClassPathResource("commonsPoolTests.xml", getClass()));
  36. }
  37. /**
  38.  * We must simulate container shutdown, which should clear threads.
  39.  */
  40. protected void tearDown() {
  41. // Will call pool.close()
  42. this.beanFactory.destroySingletons();
  43. }
  44. private void testFunctionality(String name) {
  45. SideEffectBean pooled = (SideEffectBean) beanFactory.getBean(name);
  46. assertEquals(INITIAL_COUNT, pooled.getCount() );
  47. pooled.doWork();
  48. assertEquals(INITIAL_COUNT + 1, pooled.getCount() );
  49. pooled = (SideEffectBean) beanFactory.getBean(name);
  50. // Just check that it works--we can't make assumptions
  51. // about the count
  52. pooled.doWork();
  53. //assertEquals(INITIAL_COUNT + 1, apartment.getCount() );
  54. }
  55. public void testFunctionality() {
  56. testFunctionality("pooled");
  57. }
  58. public void testFunctionalityWithNoInterceptors() {
  59. testFunctionality("pooledNoInterceptors");
  60. }
  61. public void testConfigMixin() {
  62. SideEffectBean pooled = (SideEffectBean) beanFactory.getBean("pooledWithMixin");
  63. assertEquals(INITIAL_COUNT, pooled.getCount() );
  64. PoolingConfig conf = (PoolingConfig) beanFactory.getBean("pooledWithMixin");
  65. // TODO one invocation from setup
  66. //assertEquals(1, conf.getInvocations());
  67. pooled.doWork();
  68. // assertEquals("No objects active", 0, conf.getActive());
  69. assertEquals("Correct target source", 25, conf.getMaxSize());
  70. // assertTrue("Some free", conf.getFree() > 0);
  71. //assertEquals(2, conf.getInvocations());
  72. assertEquals(25, conf.getMaxSize());
  73. }
  74. public void testTargetSourceSerializableWithoutConfigMixin() throws Exception {
  75. CommonsPoolTargetSource cpts = (CommonsPoolTargetSource) beanFactory.getBean("personPoolTargetSource");
  76. SingletonTargetSource serialized = (SingletonTargetSource) SerializationTestUtils.serializeAndDeserialize(cpts);
  77. assertTrue(serialized.getTarget() instanceof Person);
  78. }
  79. public void testProxySerializableWithoutConfigMixin() throws Exception {
  80. Person pooled = (Person) beanFactory.getBean("pooledPerson");
  81. //System.out.println(((Advised) pooled).toProxyConfigString());
  82. assertTrue(((Advised) pooled).getTargetSource() instanceof CommonsPoolTargetSource);
  83. //((Advised) pooled).setTargetSource(new SingletonTargetSource(new SerializablePerson()));
  84. Person serialized = (Person) SerializationTestUtils.serializeAndDeserialize(pooled);
  85. assertTrue(((Advised) serialized).getTargetSource() instanceof SingletonTargetSource);
  86. serialized.setAge(25);
  87. assertEquals(25, serialized.getAge());
  88. }
  89. }