Skip to content

Commit

Permalink
Add TCK showing using external bundle (h2) and a pax warapped (sqlite)
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Läubrich committed Jan 4, 2023
1 parent 89cf4e3 commit d658720
Show file tree
Hide file tree
Showing 10 changed files with 423 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ private static Map<String, Method> findSettersForBean(Object bean) {
* properties to set.
*/
public static void configure(Object bean, Properties props) {
configure(bean, props, true);
}
public static void configure(Object bean, Properties props, boolean failOnMissing) {
final Map<String, String> map = new HashMap<>();
for (String key : props.stringPropertyNames()) {
map.put(key, props.getProperty(key));
}
BeanConfig.configure(bean, map);
BeanConfig.configure(bean, map, failOnMissing);
}

/**
Expand All @@ -72,18 +75,24 @@ public static void configure(Object bean, Properties props) {
* properties to set. The keys in the Map have to match the bean property names.
*/
public static void configure(Object bean, Map<String, String> props) {

}
public static void configure(Object bean, Map<String, String> props, boolean failOnMissing) {
BeanConfig beanConfig = new BeanConfig(bean);
for (String key : props.keySet()) {
beanConfig.trySetProperty(key, props.get(key));
beanConfig.trySetProperty(key, props.get(key), failOnMissing);
}
}

private void trySetProperty(String key, String value) {
private void trySetProperty(String key, String value, boolean failOnMissing) {
try {
Method method = setters.get(key);
if (method == null) {
if (failOnMissing) {
throw new IllegalArgumentException("No setter in " + bean.getClass()
+ " for property " + key);
}
return;
}
Class<?> paramClass = method.getParameterTypes()[0];
if (paramClass == int.class || paramClass == Integer.class) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,26 @@ public class Activator implements BundleActivator {
@Override
public void start(BundleContext context) throws Exception {
MariaDbDataSourceFactory dsf = new MariaDbDataSourceFactory();
Dictionary<String, String> props = new Hashtable<String, String>();
Dictionary<String, Object> props = new Hashtable<>();
props.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, Driver.class.getName());
props.put(DataSourceFactory.OSGI_JDBC_DRIVER_NAME, "mariadb");
props.put(DataSourceFactory.OSGI_JDBC_CAPABILITY, new String[] {
DataSourceFactory.OSGI_JDBC_CAPABILITY_DRIVER,
DataSourceFactory.OSGI_JDBC_CAPABILITY_DATASOURCE,
DataSourceFactory.OSGI_JDBC_CAPABILITY_CONNECTIONPOOLDATASOURCE,
DataSourceFactory.OSGI_JDBC_CAPABILITY_XADATASOURCE
});
context.registerService(DataSourceFactory.class.getName(), dsf, props);
Dictionary<String, String> props2 = new Hashtable<String, String>();
Dictionary<String, Object> props2 = new Hashtable<>();
//TODO this seems wrong if we want to emulate mysql we should use a different driver class!
props2.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, Driver.class.getName());
props2.put(DataSourceFactory.OSGI_JDBC_DRIVER_NAME, "mysql");
props2.put(DataSourceFactory.OSGI_JDBC_CAPABILITY, new String[] {
DataSourceFactory.OSGI_JDBC_CAPABILITY_DRIVER,
DataSourceFactory.OSGI_JDBC_CAPABILITY_DATASOURCE,
DataSourceFactory.OSGI_JDBC_CAPABILITY_CONNECTIONPOOLDATASOURCE,
DataSourceFactory.OSGI_JDBC_CAPABILITY_XADATASOURCE
});
context.registerService(DataSourceFactory.class.getName(), dsf, props2);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public DataSource createDataSource(Properties props) throws SQLException {
}

private void setProperties(MariaDbDataSource ds, Properties properties) throws SQLException {
if (properties==null) {
return;
}
Properties props = (Properties) properties.clone();
String url = (String) props.remove(DataSourceFactory.JDBC_URL);
if (url != null) {
Expand Down Expand Up @@ -66,7 +69,7 @@ private void setProperties(MariaDbDataSource ds, Properties properties) throws S
ds.setUser(user);

if (!props.isEmpty()) {
BeanConfig.configure(ds, props);
BeanConfig.configure(ds, props, false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public Driver createDriver(Properties props) throws SQLException {

private void setProperties(oracle.jdbc.datasource.OracleDataSource ds, Properties properties)
throws Exception {
if (properties==null) {
return;
}
Properties props = (Properties) properties.clone();

String url = (String) props.remove(DataSourceFactory.JDBC_URL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public class Activator implements BundleActivator {
@Override
public void start(BundleContext context) throws Exception {
SqliteDataSourceFactory dsf = new SqliteDataSourceFactory();
Dictionary<String, String> props = new Hashtable<String, String>();
Dictionary<String, Object> props = new Hashtable<>();
props.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, JDBC.class.getName());
props.put(DataSourceFactory.OSGI_JDBC_DRIVER_NAME, "sqlite");
props.put(DataSourceFactory.OSGI_JDBC_CAPABILITY, new String[] {DataSourceFactory.OSGI_JDBC_CAPABILITY_DATASOURCE, DataSourceFactory.OSGI_JDBC_CAPABILITY_DRIVER});
context.registerService(DataSourceFactory.class.getName(), dsf, props);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.ops4j.pax.jdbc.sqlite.impl;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.util.Properties;
Expand All @@ -30,33 +31,60 @@

public class SqliteDataSourceFactory implements DataSourceFactory {

@Override
public DataSource createDataSource(Properties props) throws SQLException {
SQLiteDataSource dataSource = new SQLiteDataSource();
String url = props.getProperty(JDBC_URL);
if (url == null) {
dataSource.setUrl("jdbc:sqlite:" + props.getProperty(JDBC_DATABASE_NAME));
props.remove(JDBC_DATABASE_NAME);
} else {
dataSource.setUrl(url);
props.remove(JDBC_URL);
}
private final class SQLiteDataSourceExtension extends SQLiteDataSource {
private String username;
private String password;

if (!props.isEmpty()) {
BeanConfig.configure(dataSource, props);
}
return dataSource;
}
public SQLiteDataSourceExtension(String username, String password) {
this.username = username;
this.password = password;
}

@Override
@Override
public Connection getConnection() throws SQLException {
return super.getConnection(username, password);
}
}

@Override
public DataSource createDataSource(Properties props) throws SQLException {
String username = removeProperty(props, JDBC_USER);
String password = removeProperty(props, JDBC_PASSWORD);
SQLiteDataSource dataSource = new SQLiteDataSourceExtension(username, password);
if (props != null) {
String url = props.getProperty(JDBC_URL);
if (url == null) {
dataSource.setUrl("jdbc:sqlite:" + props.getProperty(JDBC_DATABASE_NAME));
props.remove(JDBC_DATABASE_NAME);
} else {
dataSource.setUrl(url);
props.remove(JDBC_URL);
}
if (!props.isEmpty()) {
BeanConfig.configure(dataSource, props, false);
}
}
return dataSource;
}

private String removeProperty(Properties props, String property) {
if (props != null) {
String value = props.getProperty(property);
props.remove(property);
return value;
}
return null;
}

@Override
public ConnectionPoolDataSource createConnectionPoolDataSource(Properties props)
throws SQLException {
throw new UnsupportedOperationException();
throw new SQLException();
}

@Override
public XADataSource createXADataSource(Properties props) throws SQLException {
throw new UnsupportedOperationException();
throw new SQLException();
}

@Override
Expand Down
51 changes: 51 additions & 0 deletions pax-jdbc-tck/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.ops4j.pax</groupId>
<artifactId>jdbc</artifactId>
<version>1.5.6-SNAPSHOT</version>
</parent>
<artifactId>pax-jdbc-tck</artifactId>
<name>OSGi-TCK Tests</name>
<dependencies>
<!-- units under test with embedded factories -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<!-- units under test wrapped by pax-jdbc-->
<dependency>
<groupId>org.ops4j.pax.jdbc</groupId>
<artifactId>pax-jdbc-sqlite</artifactId>
</dependency>
<dependency>
<groupId>org.ops4j.pax.jdbc</groupId>
<artifactId>pax-jdbc-db2</artifactId>
</dependency>
<dependency>
<groupId>org.ops4j.pax.jdbc</groupId>
<artifactId>pax-jdbc-mariadb</artifactId>
</dependency>
<!-- Dependencies for performing the test -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.test.cases.jdbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.jdbc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.laeubisoft</groupId>
<artifactId>osgi-test-framework</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.osgi</artifactId>
</dependency>
</dependencies>
</project>
41 changes: 41 additions & 0 deletions pax-jdbc-tck/src/test/java/org/ops4j/pax/jdbc/tck/JdbcTckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2022 OPS4J.
*
* Licensed 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.ops4j.pax.jdbc.tck;

import org.junit.jupiter.api.extension.ExtendWith;
import org.osgi.test.cases.jdbc.junit.JDBCTestCase;

import de.laeubisoft.osgi.junit5.framework.annotations.WithBundle;
import de.laeubisoft.osgi.junit5.framework.extension.FrameworkExtension;

//#### JDBC services to test ####
//some drivers that already have a custom implementation and we just want to include them to make sure they comply
@WithBundle(value = "com.h2database", start = true, isolated = true)
//pax-jdbc-sqlite
@WithBundle(value = "org.ops4j.pax.jdbc.sqlite", start = true, isolated = true)
@WithBundle(value = "org.xerial.sqlite-jdbc", start = true, isolated = true)
//pax-jdbc-mariadb --> actually obsolete see: https://github.com/ops4j/org.ops4j.pax.jdbc/issues/277
@WithBundle(value = "org.ops4j.pax.jdbc.mariadb", start = true, isolated = true)
@WithBundle(value = "org.mariadb.jdbc", start = true, isolated = true)
//pax-db2 TODO how to test? seems not OpenSource?
// @WithBundle(value = "org.ops4j.pax.jdbc.db2", start = true, isolated = true)

//### basic setup ###
@ExtendWith(FrameworkExtension.class)
@WithBundle("org.osgi.service.jdbc")
public class JdbcTckTest extends JDBCTestCase {

}
Loading

0 comments on commit d658720

Please sign in to comment.