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