DataSourceRegistration implements Closeable { @Override public void close() { if (serviceReg != null) { serviceReg.unregister(); } safeClose(dataSource); } DataSourceRegistration(BundleContext context, DataSourceFactory dsf, final Dictionary config, final Dictionary decryptedConfig, final PreHook preHook); @Override void close();  }
@Test public void testPublishedAndUnpublished() throws ConfigurationException, InvalidSyntaxException, SQLException { Capture<Properties> capturedDsProps = EasyMock.newCapture(); Capture<Dictionary> capturedServiceProps = EasyMock.newCapture(); IMocksControl c = EasyMock.createControl(); BundleContext context = c.createMock(BundleContext.class); final DataSourceFactory dsf = c.createMock(DataSourceFactory.class); DataSource ds = c.createMock(DataSource.class); expect(dsf.createDataSource(capture(capturedDsProps))).andReturn(ds); ServiceRegistration dsSreg = c.createMock(ServiceRegistration.class); expect( context.registerService(eq(DataSource.class.getName()), eq(ds), capture(capturedServiceProps))).andReturn(dsSreg); c.replay(); Dictionary<String, String> properties = new Hashtable<String, String>(); properties.put(DataSourceFactory.OSGI_JDBC_DRIVER_CLASS, H2_DRIVER_CLASS); properties.put(DataSourceFactory.JDBC_DATABASE_NAME, "mydbname"); properties.put(DataSourceFactory.JDBC_DATASOURCE_NAME, "myDsName"); DataSourceRegistration publisher = new DataSourceRegistration(context, dsf, properties, properties, null); c.verify(); Properties dsProps = capturedDsProps.getValue(); assertEquals("mydbname", dsProps.get(DataSourceFactory.JDBC_DATABASE_NAME)); Dictionary serviceProps = capturedServiceProps.getValue(); assertEquals("myDsName", serviceProps.get(DataSourceFactory.JDBC_DATASOURCE_NAME)); assertEquals("myDsName", serviceProps.get("osgi.jndi.service.name")); c.reset(); dsSreg.unregister(); expectLastCall(); c.replay(); publisher.close(); c.verify(); }
@Test public void testPublishedConnectionPoolDS() throws ConfigurationException, InvalidSyntaxException, SQLException { IMocksControl c = EasyMock.createControl(); BundleContext context = c.createMock(BundleContext.class); final DataSourceFactory dsf = c.createMock(DataSourceFactory.class); ConnectionPoolDataSource cpds = c.createMock(ConnectionPoolDataSource.class); expect(dsf.createConnectionPoolDataSource(anyObject(Properties.class))).andReturn(cpds); ServiceRegistration dsSreg = c.createMock(ServiceRegistration.class); expect( context.registerService(eq(ConnectionPoolDataSource.class.getName()), eq(cpds), anyObject(Dictionary.class))).andReturn(dsSreg); dsSreg.unregister(); expectLastCall(); c.replay(); Dictionary<String, String> properties = new Hashtable<String, String>(); properties.put(DataSourceRegistration.JNDI_SERVICE_NAME, "test"); properties.put(DataSourceRegistration.DATASOURCE_TYPE, ConnectionPoolDataSource.class.getSimpleName()); DataSourceRegistration publisher = new DataSourceRegistration(context, dsf, properties, properties, null); publisher.close(); c.verify(); }