KickJava   Java API By Example, From Geeks To Geeks.

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


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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package org.apache.avalon.excalibur.datasource.test;
21
22 import java.sql.Connection JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.Random JavaDoc;
27
28 import org.apache.avalon.excalibur.datasource.DataSourceComponent;
29 import org.apache.avalon.excalibur.testcase.CascadingAssertionFailedError;
30 import org.apache.avalon.excalibur.testcase.ExcaliburTestCase;
31 import org.apache.avalon.framework.component.Component;
32 import org.apache.avalon.framework.component.ComponentException;
33
34 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
35
36 /**
37  * Test the DataSource Component. I don't know how to make this generic,
38  * so I'll throw some bones out there, and hope someone can set this up
39  * better.
40  *
41  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
42  */

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