-
Notifications
You must be signed in to change notification settings - Fork 637
/
Copy pathOSGiTest.java
103 lines (89 loc) · 3.81 KB
/
OSGiTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package org.sqlite;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import de.laeubisoft.osgi.junit5.framework.annotations.EmbeddedFramework;
import de.laeubisoft.osgi.junit5.framework.annotations.WithBundle;
import de.laeubisoft.osgi.junit5.framework.extension.FrameworkExtension;
import de.laeubisoft.osgi.junit5.framework.services.FrameworkEvents;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import javax.sql.ConnectionPoolDataSource;
import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.osgi.framework.launch.Framework;
import org.osgi.service.jdbc.DataSourceFactory;
import org.osgi.test.common.annotation.InjectService;
import org.osgi.test.common.service.ServiceAware;
import org.osgi.test.junit5.service.ServiceExtension;
import org.sqlite.javax.SQLiteConnectionPoolDataSource;
@WithBundle(value = "org.xerial.sqlite-jdbc", start = true, isolated = true)
@WithBundle(value = "org.osgi.service.jdbc")
@ExtendWith(ServiceExtension.class)
@ExtendWith(FrameworkExtension.class)
public class OSGiTest {
@BeforeAll
public static void beforeTest(
@EmbeddedFramework Framework framework,
@InjectService FrameworkEvents frameworkEvents) {
FrameworkExtension.printBundles(framework, System.out::println);
FrameworkExtension.printComponents(framework, System.out::println);
frameworkEvents.assertErrorFree();
}
@InjectService(filter = "(osgi.jdbc.driver.class=org.sqlite.JDBC)")
ServiceAware<DataSourceFactory> datasourceFactory;
@BeforeEach
public void checkService() {
assertEquals(
1,
datasourceFactory.size(),
"There should be exactly one DataSourceFactory for SQLite!");
}
@Test
public void testCreateDriver() throws SQLException {
Driver driver = getFactory().createDriver(null);
assertClass(JDBC.class, driver);
}
@Test
public void testCreateDataSource() throws SQLException {
DataSource dataSource = getFactory().createDataSource(null);
assertClass(SQLiteDataSource.class, dataSource);
}
@Test
public void testCreateConnectionPoolDataSource() throws SQLException {
ConnectionPoolDataSource dataSource = getFactory().createConnectionPoolDataSource(null);
assertClass(SQLiteConnectionPoolDataSource.class, dataSource);
}
@Test
public void testCreateXADataSource() throws SQLException {
DataSourceFactory service = getFactory();
assertThrows(SQLException.class, () -> service.createXADataSource(null));
}
@Test
public void testCreateConnection() throws SQLException {
Properties props = new Properties();
props.setProperty(DataSourceFactory.JDBC_URL, "jdbc:sqlite:");
DataSource dataSource = getFactory().createDataSource(props);
try (Connection connection = dataSource.getConnection()) {
Statement stmt = connection.createStatement();
stmt.executeUpdate("create table sample(id, name)");
stmt.executeUpdate("insert into sample values(1, \"leo\")");
stmt.executeUpdate("insert into sample values(2, \"yui\")");
}
}
private DataSourceFactory getFactory() {
DataSourceFactory service = datasourceFactory.getService();
assertNotNull(service);
return service;
}
private static void assertClass(Class<?> clazz, Object obj) {
assertNotNull(obj);
assertEquals(clazz.getName(), obj.getClass().getName());
}
}