1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.performance.pool;
18  
19  import java.io.InputStream;
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.logging.Logger;
24  import org.apache.commons.dbcp.AbandonedConfig;
25  import org.apache.commons.dbcp.AbandonedObjectPool;
26  import org.apache.commons.math.stat.descriptive.SummaryStatistics;
27  import org.apache.commons.pool.impl.GenericObjectPool;
28  import junit.framework.Test;
29  import junit.framework.TestCase;
30  import junit.framework.TestSuite;
31  
32  public class PoolSoakTest extends TestCase {
33      
34  
35    public PoolSoakTest(String name) {
36      super(name);
37    }
38  
39  
40    public static Test suite() {
41      return new TestSuite(PoolSoakTest.class);
42    }
43    
44    protected PoolSoak poolSoak = null;
45    
46    public void setUp() throws Exception {
47       poolSoak = new PoolSoak();
48       poolSoak.configure();
49    }
50    
51    public void testGenericObjectPoolConfig() throws Exception {
52      /** Contents of config file
53      <max-active>15</max-active>
54      <max-idle>15</max-idle>
55      <min-idle>0</min-idle>
56      <max-wait>-1</max-wait>
57      <!-- block, fail, or grow -->
58      <exhausted-action>block</exhausted-action>
59      <test-on-borrow>false</test-on-borrow>
60      <test-on-return>false</test-on-return>
61      <time-between-evictions>-1</time-between-evictions>
62      <tests-per-eviction>3</tests-per-eviction>
63      <idle-timeout>-1</idle-timeout>
64      <test-while-idle>false</test-while-idle>
65      **/
66       poolSoak.getDigester().parse(getInputStream("config-pool.xml"));
67       poolSoak.init();
68       GenericObjectPool pool = poolSoak.getGenericObjectPool();
69       assertEquals(15, pool.getMaxActive());
70       assertEquals(15, pool.getMaxIdle());
71       assertEquals(10, pool.getMinIdle());
72       assertEquals(-1, pool.getMaxWait());
73       assertEquals(GenericObjectPool.WHEN_EXHAUSTED_BLOCK, 
74               pool.getWhenExhaustedAction());
75       assertEquals(false, pool.getTestOnBorrow());
76       assertEquals(false, pool.getTestOnReturn());
77       assertEquals(-1, pool.getTimeBetweenEvictionRunsMillis());
78       assertEquals(3, pool.getNumTestsPerEvictionRun());
79       assertEquals(-1, pool.getMinEvictableIdleTimeMillis());
80       assertEquals(false, pool.getTestWhileIdle());  
81    } 
82    
83    public void testAbandonedObjectPoolConfig() throws Exception {
84     /* 
85     <pool>
86      <!-- GenericObjectPool or AbandonedObjectPool -->
87      <type>AbandonedObjectPool</type>
88      <max-active>15</max-active>
89      <max-idle>-1</max-idle>
90      <min-idle>0</min-idle>
91      <max-wait>-1</max-wait>
92      <!-- block, fail, or grow -->
93      <exhausted-action>grow</exhausted-action>
94      <test-on-borrow>true</test-on-borrow>
95      <test-on-return>false</test-on-return>
96      <time-between-evictions>-1</time-between-evictions>
97      <tests-per-eviction>3</tests-per-eviction>
98      <idle-timeout>-1</idle-timeout>
99      <test-while-idle>true</test-while-idle>
100   </pool>
101   
102   <!-- Ignored unless pool type is AbandonedObjectPool -->
103   <abandoned-config>
104     <log-abandoned>true</log-abandoned>
105     <remove-abandoned>false</remove-abandoned>
106     <abandoned-timeout>50000</abandoned-timeout>
107   </abandoned-config> 
108   */
109       poolSoak.getDigester().parse(getInputStream("config-abandoned.xml"));
110       poolSoak.init();
111       AbandonedObjectPool pool = (AbandonedObjectPool) poolSoak.getGenericObjectPool();
112       assertEquals(15, pool.getMaxActive());
113       assertEquals(-1, pool.getMaxIdle());
114       assertEquals(0, pool.getMinIdle());
115       assertEquals(-1, pool.getMaxWait());
116       assertEquals(GenericObjectPool.WHEN_EXHAUSTED_GROW, 
117               pool.getWhenExhaustedAction());
118       assertEquals(true, pool.getTestOnBorrow());
119       assertEquals(false, pool.getTestOnReturn());
120       assertEquals(-1, pool.getTimeBetweenEvictionRunsMillis());
121       assertEquals(3, pool.getNumTestsPerEvictionRun());
122       assertEquals(-1, pool.getMinEvictableIdleTimeMillis());
123       assertEquals(true, pool.getTestWhileIdle());      
124   }
125   
126   /**
127    * Return an appropriate InputStream for the specified test file (which
128    * must be inside our current package).
129    * 
130    * Borrowed from Commons Digester RuleTestCase.
131    *
132    * @param name Name of the test file we want
133    * @exception IOException if an input/output error occurs
134    */
135   protected InputStream getInputStream(String name) throws IOException {
136 
137       return (this.getClass().getResourceAsStream
138               ("/org/apache/commons/performance/pool/" + name));
139   }
140   
141 }