KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > connection > SimpleConnectionManagerTest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Mathieu Peltier.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.standalone.connection;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.DriverManager JavaDoc;
29 import java.sql.SQLException JavaDoc;
30
31 import junit.framework.Test;
32 import junit.framework.TestSuite;
33 import junit.textui.TestRunner;
34
35 import org.objectweb.cjdbc.common.exceptions.UnreachableBackendException;
36 import org.objectweb.cjdbc.controller.connection.SimpleConnectionManager;
37 import org.objectweb.cjdbc.scenario.templates.NoTemplate;
38 import org.objectweb.cjdbc.scenario.tools.mock.MockDriver;
39
40 import com.mockobjects.sql.MockConnection2;
41
42 /**
43  * <code>SimpleConnectionManager</code> test class.
44  *
45  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
46  * @see org.objectweb.cjdbc.controller.connection.SimpleConnectionManager
47  */

48 public class SimpleConnectionManagerTest extends NoTemplate
49 {
50   /** Fake driver. */
51   private MockDriver mockDriver;
52
53   /** Fake connection. */
54   private MockConnection2 mockConnection;
55
56   /** Simple connection manager pool to test. */
57   private SimpleConnectionManager manager;
58
59   /**
60    * Builds a new TestSuite
61    *
62    * @return the TestSuite
63    */

64   public static Test suite()
65   {
66     return new TestSuite(SimpleConnectionManagerTest.class);
67   }
68
69   /**
70    * Starts a new test
71    *
72    * @param args test parameters
73    */

74   public static void main(String JavaDoc[] args)
75   {
76     TestRunner.run(suite());
77   }
78
79   /**
80    * @see junit.framework.TestCase#setUp()
81    */

82   protected void setUp()
83   {
84     // Create and register the mock driver
85
try
86     {
87       mockConnection = new MockConnection2();
88       mockConnection.setExpectedCloseCalls(1);
89       mockConnection.setupIsClosed(true);
90       mockDriver = new MockDriver();
91       mockDriver.setExpectedConnectCalls(1);
92       mockDriver.setupConnect(mockConnection);
93
94       DriverManager.registerDriver(mockDriver);
95     }
96     catch (SQLException JavaDoc e)
97     {
98       fail("Failed to register driver: " + e);
99     }
100
101     // Create a simple connection manager
102
try
103     {
104       manager = new SimpleConnectionManager("", "", "", "", null, null);
105     }
106     catch (Exception JavaDoc e)
107     {
108       fail("Failed to create simple pool connection manager: " + e);
109     }
110
111     // initialize the connection manager
112
try
113     {
114       manager.initializeConnections();
115     }
116     catch (SQLException JavaDoc e)
117     {
118       fail("Failed to initialize pool connection manager: " + e);
119     }
120   }
121
122   /**
123    * @see junit.framework.TestCase#tearDown()
124    */

125   protected void tearDown()
126   {
127     // Deregister the driver
128
try
129     {
130       DriverManager.deregisterDriver(mockDriver);
131     }
132     catch (SQLException JavaDoc e)
133     {
134       fail("Failed to deregister driver: " + e);
135     }
136   }
137
138   /**
139    * Simple connection manager functionnal test.
140    *
141    * @throws UnreachableBackendException if no connections available test should
142    * fail !
143    */

144   public void testSimpleConnectionManager() throws UnreachableBackendException
145   {
146     // Get a connection from simple connection manager
147
Connection JavaDoc c = manager.getConnection();
148     if (c == null)
149       fail("Failed to get a connection from simple connection manager");
150
151     // Release the connection
152
manager.releaseConnection(c);
153     try
154     {
155       assertTrue(c.isClosed());
156     }
157     catch (SQLException JavaDoc e)
158     {
159       fail("Exception thrown: " + e);
160     }
161     mockConnection.verify();
162     mockDriver.verify();
163   }
164 }
Popular Tags