001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.sql;
022:
023: import java.sql.Connection;
024: import java.sql.Driver;
025: import java.sql.DriverManager;
026: import java.sql.SQLException;
027: import java.util.Properties;
028:
029: import javax.management.DynamicMBean;
030:
031: import org.easymock.EasyMock;
032: import org.testng.annotations.DataProvider;
033: import org.testng.annotations.Test;
034:
035: /**
036: * Unit test for {@link DriverDatabase}
037: * @author Paul Ferraro
038: * @since 1.0
039: */
040: @SuppressWarnings("nls")
041: public class TestDriverDatabase extends
042: AbstractTestDatabase<DriverDatabase, Driver> implements
043: InactiveDriverDatabaseMBean {
044: static {
045: try {
046: DriverManager.registerDriver(new MockDriver(null));
047: } catch (SQLException e) {
048: assert false : e;
049: }
050: }
051:
052: private Driver driver = EasyMock.createStrictMock(Driver.class);
053:
054: void replay() {
055: EasyMock.replay(this .driver);
056: }
057:
058: void verify() {
059: EasyMock.verify(this .driver);
060: EasyMock.reset(this .driver);
061: }
062:
063: @Override
064: protected DriverDatabase createDatabase(String id) {
065: DriverDatabase database = new DriverDatabase();
066:
067: database.setId(id);
068:
069: return database;
070: }
071:
072: @DataProvider(name="driver-class")
073: public Object[][] driverClassProvider() {
074: return new Object[][] {
075: new Object[] { MockDriver.class.getName() },
076: new Object[] { Object.class.getName() },
077: new Object[] { "invalid.class" }, new Object[] { "" },
078: new Object[] { null } };
079: }
080:
081: @Test(dataProvider="driver-class")
082: public void setDriver(String driver) {
083: boolean isDriver = false;
084:
085: try {
086: isDriver = (driver == null)
087: || driver.isEmpty()
088: || Driver.class.isAssignableFrom(Class
089: .forName(driver));
090: } catch (ClassNotFoundException e) {
091: // Ignore
092: }
093:
094: DriverDatabase database = this .createDatabase("1");
095:
096: database.clean();
097:
098: try {
099: database.setDriver(driver);
100:
101: assert isDriver : driver;
102:
103: if ((driver == null) || driver.isEmpty()) {
104: assert !database.isDirty();
105: } else {
106: assert database.isDirty();
107:
108: database.clean();
109: }
110:
111: database.setDriver(driver);
112:
113: assert !database.isDirty();
114: } catch (IllegalArgumentException e) {
115: assert !isDriver : driver;
116:
117: assert !database.isDirty();
118: }
119: }
120:
121: @DataProvider(name="driver")
122: Object[][] driverProvider() {
123: return new Object[][] { new Object[] { this .driver } };
124: }
125:
126: /**
127: * @see net.sf.hajdbc.Database#connect(T)
128: */
129: @SuppressWarnings("unchecked")
130: @Test(dataProvider="driver")
131: public Connection connect(Driver driver) throws SQLException {
132: DriverDatabase database = this .createDatabase("1");
133:
134: database.setUrl("jdbc:mock:test");
135:
136: Connection connection = EasyMock.createMock(Connection.class);
137:
138: EasyMock
139: .expect(
140: this .driver.connect("jdbc:mock:test",
141: new Properties()))
142: .andReturn(connection);
143:
144: this .replay();
145:
146: Connection result = database.connect(driver);
147:
148: this .verify();
149:
150: assert connection == result;
151:
152: database.setUser("a");
153: database.setPassword("b");
154:
155: Properties properties = new Properties();
156:
157: properties.setProperty("user", "a");
158: properties.setProperty("password", "b");
159:
160: EasyMock.expect(
161: this .driver.connect("jdbc:mock:test", properties))
162: .andReturn(connection);
163:
164: this .replay();
165:
166: result = database.connect(driver);
167:
168: this .verify();
169:
170: assert result == connection;
171:
172: database.setProperty("ssl", "true");
173:
174: EasyMock.expect(
175: this .driver.connect("jdbc:mock:test", properties))
176: .andReturn(connection);
177:
178: this .replay();
179:
180: result = database.connect(driver);
181:
182: this .verify();
183:
184: assert result == connection;
185:
186: return result;
187: }
188:
189: /**
190: * @see net.sf.hajdbc.Database#createConnectionFactory()
191: */
192: @Test
193: public Driver createConnectionFactory() {
194: DriverDatabase database = this .createDatabase("1");
195: database.setDriver(MockDriver.class.getName());
196: database.setUrl("jdbc:mock:test");
197:
198: Driver driver = database.createConnectionFactory();
199:
200: assert driver.getClass().equals(MockDriver.class) : driver
201: .getClass().getName();
202:
203: return driver;
204: }
205:
206: @DataProvider(name="url")
207: Object[][] urlProvider() {
208: return new Object[][] { new Object[] { "jdbc:mock:test" },
209: new Object[] { "jdbc:invalid" } };
210: }
211:
212: /**
213: * @see net.sf.hajdbc.sql.InactiveDriverDatabaseMBean#setUrl(java.lang.String)
214: */
215: @Test(dataProvider="url")
216: public void setUrl(String url) {
217: DriverDatabase database = this .createDatabase("1");
218:
219: database.clean();
220:
221: boolean accepted = url.startsWith("jdbc:mock");
222:
223: try {
224: database.setUrl(url);
225:
226: assert accepted : url;
227:
228: String value = database.getUrl();
229:
230: assert value.equals(url) : value;
231:
232: assert database.isDirty();
233:
234: database.clean();
235:
236: assert !database.isDirty();
237:
238: database.setUrl(url);
239:
240: assert !database.isDirty();
241: } catch (IllegalArgumentException e) {
242: assert !accepted : url;
243:
244: assert !database.isDirty();
245: }
246: }
247:
248: /**
249: * @see net.sf.hajdbc.sql.ActiveDriverDatabaseMBean#getUrl()
250: */
251: @Test
252: public String getUrl() {
253: DriverDatabase database = this .createDatabase("1");
254:
255: String url = database.getUrl();
256:
257: assert url == null : url;
258:
259: database.setUrl("jdbc:mock:test");
260:
261: url = database.getUrl();
262:
263: assert url.equals("jdbc:mock:test") : url;
264:
265: return url;
266: }
267:
268: /**
269: * @see net.sf.hajdbc.sql.ActiveDriverDatabaseMBean#getDriver()
270: */
271: @Test
272: public String getDriver() {
273: DriverDatabase database = this .createDatabase("1");
274:
275: String driver = database.getDriver();
276:
277: assert driver == null : driver;
278:
279: database.setDriver(MockDriver.class.getName());
280:
281: driver = database.getDriver();
282:
283: assert driver.equals(MockDriver.class.getName()) : driver;
284:
285: return driver;
286: }
287:
288: /**
289: * @see net.sf.hajdbc.Database#getActiveMBean()
290: */
291: @Test
292: public DynamicMBean getActiveMBean() {
293: DriverDatabase database = this .createDatabase("1");
294:
295: DynamicMBean mbean = database.getActiveMBean();
296:
297: String className = mbean.getMBeanInfo().getClassName();
298:
299: try {
300: assert ActiveDriverDatabaseMBean.class
301: .isAssignableFrom(Class.forName(className)) : className;
302: } catch (ClassNotFoundException e) {
303: assert false : e;
304: }
305:
306: return mbean;
307: }
308:
309: /**
310: * @see net.sf.hajdbc.Database#getInactiveMBean()
311: */
312: @Test
313: public DynamicMBean getInactiveMBean() {
314: DriverDatabase database = this .createDatabase("1");
315:
316: DynamicMBean mbean = database.getInactiveMBean();
317:
318: String className = mbean.getMBeanInfo().getClassName();
319:
320: try {
321: assert InactiveDriverDatabaseMBean.class
322: .isAssignableFrom(Class.forName(className)) : className;
323: } catch (ClassNotFoundException e) {
324: assert false : e;
325: }
326:
327: return mbean;
328: }
329: }
|