KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > core > persist > db > connectionpool > C3P0DatabaseConnectionFactoryTest


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: C3P0DatabaseConnectionFactoryTest.java,v 1.5 2007/01/07 06:14:20 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.core.persist.db.connectionpool;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.SQLException JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import junit.extensions.TestSetup;
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32
33 import org.opensubsystems.core.error.OSSInternalErrorException;
34 import org.opensubsystems.core.persist.db.DatabaseTestSetup;
35 import org.opensubsystems.core.persist.db.DatabaseTestSuite;
36
37 import com.mchange.v2.c3p0.C3P0ProxyConnection;
38 import com.mchange.v2.c3p0.util.TestUtils;
39
40 /**
41  * Test of C3P0 database connection factory.
42  *
43  * @version $Id: C3P0DatabaseConnectionFactoryTest.java,v 1.5 2007/01/07 06:14:20 bastafidli Exp $
44  * @author Julian Legeny
45  * @code.reviewer Miro Halas
46  * @code.reviewed 1.8 2004/11/22 09:47:07 jlegeny
47  */

48 public final class C3P0DatabaseConnectionFactoryTest
49 {
50    // Constructors /////////////////////////////////////////////////////////////
51

52    /**
53     * Private constructor since this class cannot be instantiated
54     */

55    private C3P0DatabaseConnectionFactoryTest(
56    )
57    {
58       // Do nothing
59
}
60    
61    // Public methods ///////////////////////////////////////////////////////////
62

63    /**
64     * Create the suite for this test since this is the only way how to create
65     * test setup which can initialize and shutdown the database for us
66     *
67     * @return Test - suite of tests to run for this database
68     */

69    public static Test suite(
70    )
71    {
72       TestSuite suite = new DatabaseTestSuite("C3P0DatabaseConnectionFactoryTest");
73       suite.addTestSuite(C3P0DatabaseConnectionFactoryTestInternal.class);
74       TestSetup wrapper = new DatabaseTestSetup(suite);
75
76       return wrapper;
77    }
78
79    /**
80     * Internal class which can be included in other test suites directly without
81     * including the above suite. This allows us to group multiple tests
82     * together and the execute the DatabaseTestSetup only once
83     */

84    public static class C3P0DatabaseConnectionFactoryTestInternal
85                           extends PooledDatabaseConnectionFactoryImplBaseTest
86    {
87       /**
88        * Create new C3P0DatabaseConnectionFactoryTest.
89        *
90        * @param strTestName - name of the test
91        */

92       public C3P0DatabaseConnectionFactoryTestInternal(
93          String JavaDoc strTestName
94       )
95       {
96          super(strTestName);
97       }
98    
99       /**
100        * {@inheritDoc}
101        */

102       protected void setUp(
103       ) throws Exception JavaDoc
104       {
105          // initialize connection factory
106
m_connectionFactory = new C3P0DatabaseConnectionFactoryImpl();
107          
108          super.setUp();
109       }
110    
111       /**
112        * {@inheritDoc}
113        */

114       protected Object JavaDoc addItem(
115          Connection JavaDoc newConnection
116       ) throws OSSInternalErrorException
117       {
118          try
119          {
120             return new Integer JavaDoc(TestUtils.physicalConnectionIdentityHashCode(
121                           (C3P0ProxyConnection) newConnection));
122          }
123          catch (SQLException JavaDoc sqlExc)
124          {
125             throw new OSSInternalErrorException("Error occured during getting of physical " +
126                                                 "connection identity hash code.", sqlExc);
127          }
128       }
129    
130       /**
131        * {@inheritDoc}
132        */

133       protected boolean containsConnection(
134          List JavaDoc lstConnections,
135          Connection JavaDoc newConnection
136       ) throws OSSInternalErrorException
137       {
138          Integer JavaDoc iHashCodeFromList = null;
139          Iterator JavaDoc itHelp;
140          boolean bReturn = false;
141    
142          // for each connection from the list test if new requested connection is
143
// contained in the list
144
itHelp = lstConnections.iterator();
145          while (itHelp.hasNext())
146          {
147             
148             iHashCodeFromList = (Integer JavaDoc) itHelp.next();
149             try
150             {
151                bReturn = bReturn || iHashCodeFromList.intValue()
152                          == TestUtils.physicalConnectionIdentityHashCode(
153                                  (C3P0ProxyConnection) newConnection);
154             }
155             catch (SQLException JavaDoc sqlExc)
156             {
157                throw new OSSInternalErrorException("Error occured during getting of physical " +
158                                                    "connection identity hash code.", sqlExc);
159             }
160          }
161    
162          return bReturn;
163       }
164    }
165 }
166
Popular Tags