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 Dec 28, 2022
1 parent ffa6df0 commit 4e84f34
Show file tree
Hide file tree
Showing 7 changed files with 376 additions and 24 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,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
49 changes: 49 additions & 0 deletions pax-jdbc-tck/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<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 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ops4j.pax.jdbc</groupId>
<artifactId>pax-jdbc-sqlite</artifactId>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>
<!-- Dependencies for performing the test -->
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.test.cases.jdbc</artifactId>
<version>8.1.0</version>
<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>
<version>0.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.platform</groupId>
<artifactId>org.eclipse.osgi</artifactId>
<version>3.18.200</version>
</dependency>
</dependencies>
</project>
33 changes: 33 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,33 @@
/*
* 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
@WithBundle(value = "com.h2database", start = true, isolated = true)
@WithBundle(value = "org.ops4j.pax.jdbc.sqlite", start = true, isolated = true)
@WithBundle(value = "org.xerial.sqlite-jdbc", start = true, isolated = true)
//basic setup
@ExtendWith(FrameworkExtension.class)
@WithBundle("org.osgi.service.jdbc")
public class JdbcTckTest extends JDBCTestCase {

}
Loading

0 comments on commit 4e84f34

Please sign in to comment.