KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > datasource > test > DataSourceTestCase


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.datasource.test;
9
10 import java.sql.Connection JavaDoc;
11 import java.sql.SQLException JavaDoc;
12 import java.util.Random JavaDoc;
13 import java.util.LinkedList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import org.apache.avalon.framework.component.Component;
16 import org.apache.avalon.framework.component.ComponentException;
17 import org.apache.avalon.framework.configuration.Configuration;
18 import org.apache.avalon.framework.configuration.ConfigurationException;
19 import org.apache.avalon.framework.configuration.DefaultConfiguration;
20 import org.apache.avalon.excalibur.testcase.ExcaliburTestCase;
21 import org.apache.avalon.excalibur.testcase.CascadingAssertionFailedError;
22 import org.apache.avalon.excalibur.datasource.DataSourceComponent;
23 import org.apache.avalon.excalibur.datasource.JdbcDataSource;
24 import org.apache.avalon.excalibur.concurrent.ThreadBarrier;
25
26 /**
27  * Test the DataSource Component. I don't know how to make this generic,
28  * so I'll throw some bones out there, and hope someone can set this up
29  * better.
30  *
31  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
32  */

33 public class DataSourceTestCase
34     extends ExcaliburTestCase
35 {
36     protected boolean isSuccessful;
37     protected ThreadBarrier barrier;
38     protected int connectionCount;
39
40     public DataSourceTestCase(String JavaDoc name)
41     {
42         super(name);
43     }
44
45     public void testOverAllocation()
46     {
47         DataSourceComponent ds = null;
48         this.isSuccessful = false;
49         LinkedList JavaDoc connectionList = new LinkedList JavaDoc();
50
51         try
52         {
53             ds = (DataSourceComponent) manager.lookup( DataSourceComponent.ROLE );
54
55             for( int i = 0; i < 11; i++ )
56             {
57                 connectionList.add( ds.getConnection() );
58             }
59         }
60         catch ( SQLException JavaDoc se )
61         {
62             this.isSuccessful = true;
63             getLogger().info( "The test was successful" );
64         }
65         catch ( ComponentException ce )
66         {
67             if ( getLogger().isDebugEnabled() )
68             {
69                 getLogger().debug( "There was an error in the OverAllocation test", ce );
70             }
71
72             throw new CascadingAssertionFailedError( "There was an error in the OverAllocation test", ce );
73         }
74         finally
75         {
76             assertTrue( "The DataSourceComponent could not be retrieved.", null != ds );
77
78             Iterator JavaDoc connections = connectionList.iterator();
79
80             while ( connections.hasNext() )
81             {
82                 try
83                 {
84                     ( (Connection JavaDoc) connections.next() ).close();
85                 }
86                 catch ( SQLException JavaDoc se )
87                 {
88                     // ignore
89
}
90             }
91
92             connectionList.clear();
93
94             manager.release( (Component) ds );
95         }
96
97         assertTrue( "Exception was not thrown when too many datasource components were retrieved.", this.isSuccessful );
98     }
99
100     public void testNormalUse()
101     {
102         DataSourceComponent ds = null;
103         this.isSuccessful = true;
104
105         try
106         {
107             ds = (DataSourceComponent) manager.lookup( DataSourceComponent.ROLE );
108
109             this.connectionCount = 0;
110
111             for (int i = 0; i < 10; i++)
112             {
113                 (new Thread JavaDoc( new ConnectionThread( this, ds ) ) ).start();
114             }
115
116             this.barrier = new ThreadBarrier(11);
117             try
118             {
119                 this.barrier.barrierSynchronize();
120             }
121             catch(InterruptedException JavaDoc ie)
122             {
123                 // Ignore
124
}
125
126             getLogger().info( "The normal use test passed with " + this.connectionCount + " requests and 10 concurrent threads running" );
127         }
128         catch ( ComponentException ce )
129         {
130             if ( getLogger().isDebugEnabled() )
131             {
132                 getLogger().debug( "There was an error in the normal use test", ce );
133             }
134
135             throw new CascadingAssertionFailedError( "There was an error in the normal use test", ce );
136         }
137         finally
138         {
139             assertTrue( "The DataSourceComponent could not be retrieved.", null != ds );
140
141             manager.release( (Component) ds );
142         }
143
144         assertTrue( "Normal use test failed", this.isSuccessful );
145     }
146
147     class ConnectionThread
148         implements Runnable JavaDoc
149     {
150         protected DataSourceComponent datasource;
151         protected DataSourceTestCase testcase;
152
153         ConnectionThread( DataSourceTestCase testcase,
154                           final DataSourceComponent datasource )
155         {
156             this.datasource = datasource;
157             this.testcase = testcase;
158         }
159
160         public void run()
161         {
162             long end = System.currentTimeMillis() + 5000; // run for 5 seconds
163
Random JavaDoc rnd = new Random JavaDoc();
164
165             while( System.currentTimeMillis() < end && this.testcase.isSuccessful )
166             {
167                 try
168                 {
169                     Connection JavaDoc con = this.datasource.getConnection();
170                     Thread.sleep((long) rnd.nextInt(100)); // sleep for up to 100ms
171
con.close();
172                     this.testcase.connectionCount++;
173                 }
174                 catch( final SQLException JavaDoc se )
175                 {
176                     this.testcase.isSuccessful = false;
177                     this.testcase.getLogger().info( "Failed to get Connection, test failed", se );
178                 }
179                 catch( final InterruptedException JavaDoc ie )
180                 {
181                     // Ignore
182
}
183             }
184
185             try
186             {
187                 this.testcase.barrier.barrierSynchronize();
188             }
189             catch( final InterruptedException JavaDoc ie )
190             {
191                 // Ignore
192
}
193         }
194     }
195 }
196
197
Popular Tags