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.lang.reflect.Proxy;
024: import java.sql.Connection;
025: import java.sql.DriverManager;
026: import java.sql.DriverPropertyInfo;
027: import java.sql.SQLException;
028: import java.util.Collections;
029: import java.util.Properties;
030: import java.util.prefs.Preferences;
031:
032: import net.sf.hajdbc.local.LocalStateManager;
033:
034: import org.easymock.EasyMock;
035: import org.testng.annotations.AfterClass;
036: import org.testng.annotations.BeforeClass;
037: import org.testng.annotations.DataProvider;
038: import org.testng.annotations.Test;
039:
040: /**
041: * Unit test for {@link Driver}.
042: * @author Paul Ferraro
043: * @since 1.0
044: */
045: @SuppressWarnings("nls")
046: public class TestDriver implements java.sql.Driver {
047: private java.sql.Driver driver = new Driver();
048: private Connection connection = EasyMock
049: .createMock(Connection.class);
050:
051: @BeforeClass
052: protected void setUp() throws Exception {
053: DriverManager.registerDriver(new MockDriver(this .connection));
054: Preferences.userNodeForPackage(LocalStateManager.class).put(
055: "test-database-cluster", "database1,database2");
056: }
057:
058: @AfterClass
059: protected void tearDown() throws Exception {
060: DriverManager.deregisterDriver(new MockDriver(this .connection));
061: Preferences.userNodeForPackage(LocalStateManager.class).remove(
062: "test-database-cluster");
063: }
064:
065: /**
066: * Test method for {@link Driver} static initialization.
067: */
068: @Test
069: public void register() {
070: boolean registered = false;
071:
072: for (java.sql.Driver driver : Collections.list(DriverManager
073: .getDrivers())) {
074: if (Driver.class.isInstance(driver)) {
075: registered = true;
076: }
077: }
078:
079: assert registered;
080: }
081:
082: @DataProvider(name="connect")
083: public Object[][] getConnectProvider() {
084: return new Object[][] {
085: new Object[] { "jdbc:ha-jdbc:test-database-cluster",
086: new Properties() },
087: new Object[] { "jdbc:ha-jdbc:invalid-cluster",
088: new Properties() },
089: new Object[] { "jdbc:mock", new Properties() } };
090: }
091:
092: /**
093: * @see java.sql.Driver#connect(java.lang.String, java.util.Properties)
094: */
095: @Test(dataProvider="connect")
096: public Connection connect(String url, Properties properties) {
097: try {
098: Connection connection = this .driver
099: .connect(url, properties);
100:
101: if (url.equals("jdbc:mock")) {
102: assert connection == null : url;
103: } else {
104: assert connection != null : url;
105:
106: assert Proxy.isProxyClass(connection.getClass());
107: assert Proxy.getInvocationHandler(connection)
108: .getClass().equals(
109: ConnectionInvocationHandler.class);
110: }
111: } catch (SQLException e) {
112: assert !url.equals("jdbc:ha-jdbc:test-database-cluster") : url;
113: }
114:
115: return null;
116: }
117:
118: @DataProvider(name="url")
119: public Object[][] getUrlProvider() {
120: return new Object[][] {
121: new Object[] { "jdbc:ha-jdbc:test-database-cluster" },
122: new Object[] { "jdbc:ha-jdbc:" },
123: new Object[] { "jdbc:ha-jdbc" },
124: new Object[] { "jdbc:mock" } };
125: }
126:
127: /**
128: * @see java.sql.Driver#acceptsURL(java.lang.String)
129: */
130: @Test(dataProvider="url")
131: public boolean acceptsURL(String url) throws SQLException {
132: boolean accepted = url.startsWith("jdbc:ha-jdbc:")
133: && url.length() > 13;
134:
135: boolean result = this .driver.acceptsURL(url);
136:
137: assert result == accepted : url;
138:
139: return result;
140: }
141:
142: /**
143: * @see java.sql.Driver#getPropertyInfo(java.lang.String, java.util.Properties)
144: */
145: @Test(dataProvider="connect")
146: public DriverPropertyInfo[] getPropertyInfo(String url,
147: Properties properties) {
148: try {
149: DriverPropertyInfo[] info = this .driver.getPropertyInfo(
150: url, properties);
151:
152: if (url.equals("jdbc:mock")) {
153: assert info == null : url;
154: } else {
155: assert info != null : url;
156: }
157: } catch (SQLException e) {
158: assert !url.equals("jdbc:ha-jdbc:test-database-cluster") : url;
159: }
160:
161: return null;
162: }
163:
164: /**
165: * @see java.sql.Driver#getMajorVersion()
166: */
167: @Test
168: public int getMajorVersion() {
169: int version = this .driver.getMajorVersion();
170:
171: assert version == 2 : version;
172:
173: return version;
174: }
175:
176: /**
177: * @see java.sql.Driver#getMinorVersion()
178: */
179: @Test
180: public int getMinorVersion() {
181: int version = this .driver.getMinorVersion();
182:
183: assert version == 0 : version;
184:
185: return version;
186: }
187:
188: /**
189: * @see java.sql.Driver#jdbcCompliant()
190: */
191: @Test
192: public boolean jdbcCompliant() {
193: boolean compliant = this.driver.jdbcCompliant();
194:
195: assert compliant;
196:
197: return compliant;
198: }
199: }
|