Author: dain
Date: Mon Jul 16 16:52:19 2007
New Revision: 556773
URL: http://svn.apache.org/viewvc?view=rev&rev=556773
Log:
Added DataSourcePlugin to handle special configuration needed for java databases
Added:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DataSourcePlugin.java
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DerbyDataSourcePlugin.java
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/HsqldbDataSourcePlugin.java
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/InstantdbDataSourcePlugin.java
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/derby
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/hsqldb
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/idb
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtilTest.java
Modified:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSource.java
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedDataSource.java
Modified:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSource.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSource.java?view=diff&rev=556773&r1=556772&r2=556773
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSource.java
(original)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSource.java
Mon Jul 16 16:52:19 2007
@@ -17,6 +17,13 @@
*/
package org.apache.openejb.resource.jdbc;
+import org.apache.openejb.loader.SystemInstance;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+import java.util.Properties;
+import java.io.File;
+
public class BasicDataSource extends org.apache.commons.dbcp.BasicDataSource {
public synchronized String getJdbcDriver() {
return super.getDriverClassName();
@@ -32,5 +39,37 @@
public synchronized void setJdbcUrl(String string) {
super.setUrl(string);
+ }
+
+ protected synchronized DataSource createDataSource() throws SQLException {
+ if (dataSource != null) {
+ return dataSource;
+ }
+
+ // get the plugin
+ DataSourcePlugin helper =
BasicDataSourceUtil.getDataSourcePlugin(getUrl());
+
+ // configure this
+ if (helper != null) {
+ helper.configure(this);
+ }
+
+ // creat the data source
+ if (helper == null || !helper.enableUserDirHack()) {
+ return super.createDataSource();
+ } else {
+ // wrap super call with code that sets user.dir to openejb.base
and then resets it
+ Properties systemProperties = System.getProperties();
+ synchronized (systemProperties) {
+ String userDir = systemProperties.getProperty("user.dir");
+ try {
+ File base = SystemInstance.get().getBase().getDirectory();
+ systemProperties.setProperty("user.dir",
base.getAbsolutePath());
+ return super.createDataSource();
+ } finally {
+ systemProperties.setProperty("user.dir", userDir);
+ }
+ }
+ }
}
}
Added:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java?view=auto&rev=556773
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java
(added)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtil.java
Mon Jul 16 16:52:19 2007
@@ -0,0 +1,84 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.resource.jdbc;
+
+import org.apache.xbean.finder.ResourceFinder;
+
+import java.sql.SQLException;
+import java.util.Map;
+import java.io.IOException;
+
+public final class BasicDataSourceUtil {
+ private BasicDataSourceUtil() {
+ }
+
+ public static DataSourcePlugin getDataSourcePlugin(String jdbcUrl) throws
SQLException {
+ // determine the vendor based on the jdbcUrl stirng
"jdbc:${Vendor}:properties"
+ String vendor = getJdbcName(jdbcUrl);
+
+ // no vendor so no plugin
+ if (vendor == null) return null;
+
+ // find the plugin class
+ String pluginClassName = null;
+ try {
+ ResourceFinder finder = new ResourceFinder("META-INF");
+ Map<String,String> plugins =
finder.mapAvailableStrings(DataSourcePlugin.class.getName());
+ pluginClassName = plugins.get(vendor);
+ } catch (IOException ignored) {
+ // couldn't determine the plugins, which isn't fatal
+ }
+
+ // no plugin found
+ if (pluginClassName == null || pluginClassName.length() <= 0) {
+ return null;
+ }
+
+ // create the plugin
+ try {
+ Class pluginClass = Class.forName(pluginClassName);
+ DataSourcePlugin plugin = (DataSourcePlugin)
pluginClass.newInstance();
+ return plugin;
+ } catch (ClassNotFoundException e) {
+ throw new SQLException("Unable to load data source helper class '"
+ pluginClassName + "' for database '" + vendor + "'");
+ } catch (Exception e) {
+ throw (SQLException) new SQLException("Unable to create data
source helper class '" + pluginClassName + "' for database '" + vendor +
"'").initCause(e);
+ }
+ }
+
+ public static String getJdbcName(String jdbcUrl) {
+ // nothing gets you nothing
+ if (jdbcUrl == null) return null;
+
+ // strip off "jdbc:"
+ if (!jdbcUrl.startsWith("jdbc:")) {
+ return null;
+ }
+ jdbcUrl = jdbcUrl.substring("jdbc:".length());
+
+ // return text up to first ":" if present
+ int index = jdbcUrl.indexOf(':');
+
+ // It is ok to have no trailing ':'. This may be a url like
jdbc:specialDB.
+ if (index >= 0) {
+ jdbcUrl = jdbcUrl.substring(0, index);
+ }
+
+ return jdbcUrl;
+ }
+}
Modified:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedDataSource.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedDataSource.java?view=diff&rev=556773&r1=556772&r2=556773
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedDataSource.java
(original)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/BasicManagedDataSource.java
Mon Jul 16 16:52:19 2007
@@ -17,6 +17,13 @@
*/
package org.apache.openejb.resource.jdbc;
+import org.apache.openejb.loader.SystemInstance;
+
+import javax.sql.DataSource;
+import java.io.File;
+import java.sql.SQLException;
+import java.util.Properties;
+
public class BasicManagedDataSource extends
org.apache.commons.dbcp.managed.BasicManagedDataSource {
public synchronized String getJdbcDriver() {
return super.getDriverClassName();
@@ -32,5 +39,37 @@
public synchronized void setJdbcUrl(String string) {
super.setUrl(string);
+ }
+
+ protected synchronized DataSource createDataSource() throws SQLException {
+ if (dataSource != null) {
+ return dataSource;
+ }
+
+ // get the plugin
+ DataSourcePlugin helper =
BasicDataSourceUtil.getDataSourcePlugin(getUrl());
+
+ // configure this
+ if (helper != null) {
+ helper.configure(this);
+ }
+
+ // creat the data source
+ if (helper == null || !helper.enableUserDirHack()) {
+ return super.createDataSource();
+ } else {
+ // wrap super call with code that sets user.dir to openejb.base
and then resets it
+ Properties systemProperties = System.getProperties();
+ synchronized (systemProperties) {
+ String userDir = systemProperties.getProperty("user.dir");
+ try {
+ File base = SystemInstance.get().getBase().getDirectory();
+ systemProperties.setProperty("user.dir",
base.getAbsolutePath());
+ return super.createDataSource();
+ } finally {
+ systemProperties.setProperty("user.dir", userDir);
+ }
+ }
+ }
}
}
Added:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DataSourcePlugin.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DataSourcePlugin.java?view=auto&rev=556773
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DataSourcePlugin.java
(added)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DataSourcePlugin.java
Mon Jul 16 16:52:19 2007
@@ -0,0 +1,24 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.resource.jdbc;
+
+public interface DataSourcePlugin {
+ void configure(org.apache.commons.dbcp.BasicDataSource dataSource);
+
+ boolean enableUserDirHack();
+}
Added:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DerbyDataSourcePlugin.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DerbyDataSourcePlugin.java?view=auto&rev=556773
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DerbyDataSourcePlugin.java
(added)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/DerbyDataSourcePlugin.java
Mon Jul 16 16:52:19 2007
@@ -0,0 +1,34 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.resource.jdbc;
+
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.commons.dbcp.*;
+import org.apache.commons.dbcp.BasicDataSource;
+
+import javax.resource.spi.ManagedConnectionFactory;
+
+public class DerbyDataSourcePlugin implements DataSourcePlugin {
+ public void configure(BasicDataSource dataSource) {
+ System.setProperty("derby.system.home",
SystemInstance.get().getBase().getDirectory().getAbsolutePath());
+ }
+
+ public boolean enableUserDirHack() {
+ return true;
+ }
+}
Added:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/HsqldbDataSourcePlugin.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/HsqldbDataSourcePlugin.java?view=auto&rev=556773
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/HsqldbDataSourcePlugin.java
(added)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/HsqldbDataSourcePlugin.java
Mon Jul 16 16:52:19 2007
@@ -0,0 +1,61 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.resource.jdbc;
+
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.commons.dbcp.*;
+import org.apache.commons.dbcp.BasicDataSource;
+
+import java.io.File;
+
+public class HsqldbDataSourcePlugin implements DataSourcePlugin {
+ private static final String HSQL_FILE_URL = "jdbc:hsqldb:file:";
+
+
+ public static String toAbsolutePath(String url) {
+ // is this a hsql file url?
+ if (url == null || !url.startsWith(HSQL_FILE_URL)) {
+ return url;
+ }
+
+ // get the relative path
+ String path = url.substring(HSQL_FILE_URL.length());
+
+ // make an absolute file
+ File file = new File(path);
+ if (!file.isAbsolute()) {
+ File base = SystemInstance.get().getBase().getDirectory();
+ file = new File(base, path);
+ }
+
+ // make an absolute url
+ path = file.getAbsolutePath();
+ return HSQL_FILE_URL + path;
+ }
+
+ public void configure(BasicDataSource dataSource) {
+ String url = dataSource.getUrl();
+ url = toAbsolutePath(url);
+ dataSource.setUrl(url);
+ }
+
+ public boolean enableUserDirHack() {
+ return false;
+ }
+
+}
Added:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/InstantdbDataSourcePlugin.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/InstantdbDataSourcePlugin.java?view=auto&rev=556773
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/InstantdbDataSourcePlugin.java
(added)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/resource/jdbc/InstantdbDataSourcePlugin.java
Mon Jul 16 16:52:19 2007
@@ -0,0 +1,79 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.resource.jdbc;
+
+import org.apache.commons.dbcp.BasicDataSource;
+import org.apache.openejb.loader.SystemInstance;
+import org.apache.xbean.finder.ResourceFinder;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+public class InstantdbDataSourcePlugin implements DataSourcePlugin {
+
+ public void configure(BasicDataSource dataSource) {
+ String jdbcUrl = dataSource.getUrl();
+
+ // jdbc:idb:conf/instantdb.properties
+ String prefix = "jdbc:idb:";
+ int index = jdbcUrl.indexOf(prefix);
+ if (index == -1){
+ return;
+ }
+
+ String confFile = jdbcUrl.substring(index + prefix.length());
+
+ File base = SystemInstance.get().getBase().getDirectory();
+ File file = new File(base, confFile);
+
+
+ if (file.exists()) {
+ // The instantdb properties file is there, we're good
+ return;
+ }
+
+ if (!file.getParentFile().exists()){
+ // The directory the instantdb properties file should live in
+ // doesn't exist, don't bother
+ return;
+ }
+
+ FileOutputStream out = null;
+ try {
+ ResourceFinder finder = new ResourceFinder("");
+ String defaultProperties =
finder.findString("default.instantdb.properties");
+ out = new FileOutputStream(file);
+ out.write(defaultProperties.getBytes());
+ out.flush();
+ } catch (IOException e) {
+ // TODO; Handle this
+ e.printStackTrace();
+ } finally {
+ try {
+ out.close();
+ } catch (IOException e) {
+ }
+ }
+ }
+
+ public boolean enableUserDirHack() {
+ return true;
+ }
+
+}
Added:
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/derby
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/derby?view=auto&rev=556773
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/derby
(added)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/derby
Mon Jul 16 16:52:19 2007
@@ -0,0 +1 @@
+org.apache.openejb.resource.jdbc.DerbyDataSourcePlugin
\ No newline at end of file
Added:
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/hsqldb
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/hsqldb?view=auto&rev=556773
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/hsqldb
(added)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/hsqldb
Mon Jul 16 16:52:19 2007
@@ -0,0 +1 @@
+org.apache.openejb.resource.jdbc.HsqldbDataSourcePlugin
\ No newline at end of file
Added:
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/idb
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/idb?view=auto&rev=556773
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/idb
(added)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/resources/META-INF/org.apache.openejb.resource.jdbc.DataSourcePlugin/idb
Mon Jul 16 16:52:19 2007
@@ -0,0 +1 @@
+org.apache.openejb.resource.jdbc.InstantdbDataSourcePlugin
\ No newline at end of file
Added:
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtilTest.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtilTest.java?view=auto&rev=556773
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtilTest.java
(added)
+++
openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/resource/jdbc/BasicDataSourceUtilTest.java
Mon Jul 16 16:52:19 2007
@@ -0,0 +1,67 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.resource.jdbc;
+
+import junit.framework.TestCase;
+
+import java.sql.SQLException;
+
+public class BasicDataSourceUtilTest extends TestCase {
+ public void testGetJdbcName() {
+ assertEquals("hsqldb",
BasicDataSourceUtil.getJdbcName("jdbc:hsqldb:file:foo"));
+ assertEquals("idb", BasicDataSourceUtil.getJdbcName("jdbc:idb:foo"));
+
+ // not a proper jdbc url
+ assertNull(BasicDataSourceUtil.getJdbcName("notjdbc:hsqldb:file:foo"));
+
+ // no trailing :
+ assertEquals("specialDB",
BasicDataSourceUtil.getJdbcName("jdbc:specialDB"));
+
+ // null
+ assertNull(BasicDataSourceUtil.getJdbcName(null));
+
+ // empty string
+ assertNull(BasicDataSourceUtil.getJdbcName(""));
+ }
+
+ public void testGetDataSourcePlugin() throws Exception {
+ // all current known plugins
+ assertPluginClass("jdbc:hsqldb:file:foo",
HsqldbDataSourcePlugin.class);
+ assertPluginClass("jdbc:idb:foo", InstantdbDataSourcePlugin.class);
+ assertPluginClass("jdbc:derby:foo", DerbyDataSourcePlugin.class);
+
+ // not a proper jdbc url
+
assertNull(BasicDataSourceUtil.getDataSourcePlugin("notjdbc:hsqldb:file:foo"));
+
+ // no trailing :
+ assertPluginClass("jdbc:hsqldb", HsqldbDataSourcePlugin.class);
+
+ // null
+ assertNull(BasicDataSourceUtil.getDataSourcePlugin(null));
+
+ // empty string
+ assertNull(BasicDataSourceUtil.getDataSourcePlugin(""));
+ }
+
+ private void assertPluginClass(String jdbcUrl, Class<? extends
DataSourcePlugin> pluginClass) throws SQLException {
+ DataSourcePlugin plugin =
BasicDataSourceUtil.getDataSourcePlugin(jdbcUrl);
+ assertNotNull(plugin);
+ assertSame(pluginClass, plugin.getClass());
+ }
+
+}