Skip to content

Commit 4e84f34

Browse files
author
Christoph Läubrich
committed
Add TCK showing using external bundle (h2) and a pax warapped (sqlite)
1 parent ffa6df0 commit 4e84f34

File tree

7 files changed

+376
-24
lines changed

7 files changed

+376
-24
lines changed

pax-jdbc-common/src/main/java/org/ops4j/pax/jdbc/common/BeanConfig.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ private static Map<String, Method> findSettersForBean(Object bean) {
5656
* properties to set.
5757
*/
5858
public static void configure(Object bean, Properties props) {
59+
configure(bean, props, true);
60+
}
61+
public static void configure(Object bean, Properties props, boolean failOnMissing) {
5962
final Map<String, String> map = new HashMap<>();
6063
for (String key : props.stringPropertyNames()) {
6164
map.put(key, props.getProperty(key));
6265
}
63-
BeanConfig.configure(bean, map);
66+
BeanConfig.configure(bean, map, failOnMissing);
6467
}
6568

6669
/**
@@ -72,18 +75,24 @@ public static void configure(Object bean, Properties props) {
7275
* properties to set. The keys in the Map have to match the bean property names.
7376
*/
7477
public static void configure(Object bean, Map<String, String> props) {
78+
79+
}
80+
public static void configure(Object bean, Map<String, String> props, boolean failOnMissing) {
7581
BeanConfig beanConfig = new BeanConfig(bean);
7682
for (String key : props.keySet()) {
77-
beanConfig.trySetProperty(key, props.get(key));
83+
beanConfig.trySetProperty(key, props.get(key), failOnMissing);
7884
}
7985
}
8086

81-
private void trySetProperty(String key, String value) {
87+
private void trySetProperty(String key, String value, boolean failOnMissing) {
8288
try {
8389
Method method = setters.get(key);
8490
if (method == null) {
91+
if (failOnMissing) {
8592
throw new IllegalArgumentException("No setter in " + bean.getClass()
8693
+ " for property " + key);
94+
}
95+
return;
8796
}
8897
Class<?> paramClass = method.getParameterTypes()[0];
8998
if (paramClass == int.class || paramClass == Integer.class) {

pax-jdbc-sqlite/src/main/java/org/ops4j/pax/jdbc/sqlite/impl/Activator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ public class Activator implements BundleActivator {
2828
@Override
2929
public void start(BundleContext context) throws Exception {
3030
SqliteDataSourceFactory dsf = new SqliteDataSourceFactory();
31-
Dictionary<String, String> props = new Hashtable<String, String>();
31+
Dictionary<String, Object> props = new Hashtable<>();
3232
props.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, JDBC.class.getName());
3333
props.put(DataSourceFactory.OSGI_JDBC_DRIVER_NAME, "sqlite");
34+
props.put(DataSourceFactory.OSGI_JDBC_CAPABILITY, new String[] {DataSourceFactory.OSGI_JDBC_CAPABILITY_DATASOURCE, DataSourceFactory.OSGI_JDBC_CAPABILITY_DRIVER});
3435
context.registerService(DataSourceFactory.class.getName(), dsf, props);
3536
}
3637

pax-jdbc-sqlite/src/main/java/org/ops4j/pax/jdbc/sqlite/impl/SqliteDataSourceFactory.java

+47-19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.ops4j.pax.jdbc.sqlite.impl;
1717

18+
import java.sql.Connection;
1819
import java.sql.Driver;
1920
import java.sql.SQLException;
2021
import java.util.Properties;
@@ -30,33 +31,60 @@
3031

3132
public class SqliteDataSourceFactory implements DataSourceFactory {
3233

33-
@Override
34-
public DataSource createDataSource(Properties props) throws SQLException {
35-
SQLiteDataSource dataSource = new SQLiteDataSource();
36-
String url = props.getProperty(JDBC_URL);
37-
if (url == null) {
38-
dataSource.setUrl("jdbc:sqlite:" + props.getProperty(JDBC_DATABASE_NAME));
39-
props.remove(JDBC_DATABASE_NAME);
40-
} else {
41-
dataSource.setUrl(url);
42-
props.remove(JDBC_URL);
43-
}
34+
private final class SQLiteDataSourceExtension extends SQLiteDataSource {
35+
private String username;
36+
private String password;
4437

45-
if (!props.isEmpty()) {
46-
BeanConfig.configure(dataSource, props);
47-
}
48-
return dataSource;
49-
}
38+
public SQLiteDataSourceExtension(String username, String password) {
39+
this.username = username;
40+
this.password = password;
41+
}
5042

51-
@Override
43+
@Override
44+
public Connection getConnection() throws SQLException {
45+
return super.getConnection(username, password);
46+
}
47+
}
48+
49+
@Override
50+
public DataSource createDataSource(Properties props) throws SQLException {
51+
String username = removeProperty(props, JDBC_USER);
52+
String password = removeProperty(props, JDBC_PASSWORD);
53+
SQLiteDataSource dataSource = new SQLiteDataSourceExtension(username, password);
54+
if (props != null) {
55+
String url = props.getProperty(JDBC_URL);
56+
if (url == null) {
57+
dataSource.setUrl("jdbc:sqlite:" + props.getProperty(JDBC_DATABASE_NAME));
58+
props.remove(JDBC_DATABASE_NAME);
59+
} else {
60+
dataSource.setUrl(url);
61+
props.remove(JDBC_URL);
62+
}
63+
if (!props.isEmpty()) {
64+
BeanConfig.configure(dataSource, props, false);
65+
}
66+
}
67+
return dataSource;
68+
}
69+
70+
private String removeProperty(Properties props, String property) {
71+
if (props != null) {
72+
String value = props.getProperty(property);
73+
props.remove(property);
74+
return value;
75+
}
76+
return null;
77+
}
78+
79+
@Override
5280
public ConnectionPoolDataSource createConnectionPoolDataSource(Properties props)
5381
throws SQLException {
54-
throw new UnsupportedOperationException();
82+
throw new SQLException();
5583
}
5684

5785
@Override
5886
public XADataSource createXADataSource(Properties props) throws SQLException {
59-
throw new UnsupportedOperationException();
87+
throw new SQLException();
6088
}
6189

6290
@Override

pax-jdbc-tck/pom.xml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<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">
2+
<modelVersion>4.0.0</modelVersion>
3+
<parent>
4+
<groupId>org.ops4j.pax</groupId>
5+
<artifactId>jdbc</artifactId>
6+
<version>1.5.6-SNAPSHOT</version>
7+
</parent>
8+
<artifactId>pax-jdbc-tck</artifactId>
9+
<name>OSGi-TCK Tests</name>
10+
<dependencies>
11+
<!-- units under test -->
12+
<dependency>
13+
<groupId>com.h2database</groupId>
14+
<artifactId>h2</artifactId>
15+
<scope>test</scope>
16+
</dependency>
17+
<dependency>
18+
<groupId>org.ops4j.pax.jdbc</groupId>
19+
<artifactId>pax-jdbc-sqlite</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.xerial</groupId>
23+
<artifactId>sqlite-jdbc</artifactId>
24+
</dependency>
25+
<!-- Dependencies for performing the test -->
26+
<dependency>
27+
<groupId>org.osgi</groupId>
28+
<artifactId>org.osgi.test.cases.jdbc</artifactId>
29+
<version>8.1.0</version>
30+
<scope>test</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.osgi</groupId>
34+
<artifactId>org.osgi.service.jdbc</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>de.laeubisoft</groupId>
39+
<artifactId>osgi-test-framework</artifactId>
40+
<version>0.0.1</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.eclipse.platform</groupId>
45+
<artifactId>org.eclipse.osgi</artifactId>
46+
<version>3.18.200</version>
47+
</dependency>
48+
</dependencies>
49+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2022 OPS4J.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.ops4j.pax.jdbc.tck;
17+
18+
import org.junit.jupiter.api.extension.ExtendWith;
19+
import org.osgi.test.cases.jdbc.junit.JDBCTestCase;
20+
21+
import de.laeubisoft.osgi.junit5.framework.annotations.WithBundle;
22+
import de.laeubisoft.osgi.junit5.framework.extension.FrameworkExtension;
23+
24+
//JDBC services to test
25+
@WithBundle(value = "com.h2database", start = true, isolated = true)
26+
@WithBundle(value = "org.ops4j.pax.jdbc.sqlite", start = true, isolated = true)
27+
@WithBundle(value = "org.xerial.sqlite-jdbc", start = true, isolated = true)
28+
//basic setup
29+
@ExtendWith(FrameworkExtension.class)
30+
@WithBundle("org.osgi.service.jdbc")
31+
public class JdbcTckTest extends JDBCTestCase {
32+
33+
}

0 commit comments

Comments
 (0)