# HG changeset patch # User Omair Majid # Date 1361840771 18000 # Node ID 121a6919baf55e685da045feb90c20c18940a5c0 # Parent de6b9f5967bd6486c85cac0f80115dc0561955d3 Fail gracefully when no StorageProviders are available Reviewed-by: jerboaa, vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-February/005823.html diff -r de6b9f5967bd -r 121a6919baf5 storage/core/src/main/java/com/redhat/thermostat/storage/internal/DbServiceImpl.java --- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/DbServiceImpl.java Mon Feb 25 18:02:24 2013 -0500 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/DbServiceImpl.java Mon Feb 25 20:06:11 2013 -0500 @@ -64,16 +64,17 @@ DbServiceImpl(String username, String password, String dbUrl) throws StorageException { BundleContext context = FrameworkUtil.getBundle(DbService.class).getBundleContext(); - Storage storage = createStorage(context, username, password, dbUrl); - init(context, storage, dbUrl); + init(context, username, password, dbUrl); } // for testing - DbServiceImpl(BundleContext context, Storage storage, String dbUrl) { - init(context, storage, dbUrl); + DbServiceImpl(BundleContext context, String username, String password, String dbUrl) { + init(context, username, password, dbUrl); } - private void init(BundleContext context, Storage storage, String dbUrl) { + private void init(BundleContext context, String username, String password, String dbUrl) { + Storage storage = createStorage(context, username, password, dbUrl); + this.storage = storage; this.context = context; this.dbUrl = dbUrl; @@ -162,6 +163,9 @@ private static StorageProvider getStorageProvider(BundleContext context, StartupConfiguration config) { try { ServiceReference[] refs = context.getServiceReferences(StorageProvider.class.getName(), null); + if (refs == null) { + throw new StorageException("No storage provider available"); + } for (int i = 0; i < refs.length; i++) { StorageProvider prov = (StorageProvider) context.getService(refs[i]); prov.setConfig(config); diff -r de6b9f5967bd -r 121a6919baf5 storage/core/src/test/java/com/redhat/thermostat/storage/internal/DbServiceImplTest.java --- a/storage/core/src/test/java/com/redhat/thermostat/storage/internal/DbServiceImplTest.java Mon Feb 25 18:02:24 2013 -0500 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/DbServiceImplTest.java Mon Feb 25 20:06:11 2013 -0500 @@ -45,7 +45,6 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import org.junit.After; import org.junit.Before; import org.junit.Test; import org.osgi.framework.ServiceReference; @@ -55,13 +54,15 @@ import com.redhat.thermostat.storage.core.Connection.ConnectionListener; import com.redhat.thermostat.storage.core.DbService; import com.redhat.thermostat.storage.core.Storage; +import com.redhat.thermostat.storage.core.StorageException; +import com.redhat.thermostat.storage.core.StorageProvider; import com.redhat.thermostat.testutils.StubBundleContext; public class DbServiceImplTest { private Connection connection; + private StorageProvider storageProvider; private Storage storage; - private DbService dbService; private StubBundleContext context; @Before @@ -72,17 +73,40 @@ storage = mock(Storage.class); when(storage.getConnection()).thenReturn(connection); - dbService = new DbServiceImpl(context, storage, "http://someUrl.ignored.com"); + storageProvider = mock(StorageProvider.class); + when(storageProvider.canHandleProtocol()).thenReturn(true); + when(storageProvider.createStorage()).thenReturn(storage); + context.registerService(StorageProvider.class, storageProvider, null); } - - @After - public void teardown() { - dbService = null; - context = null; + + @Test + public void testNoStorageProvider() { + context = new StubBundleContext(); + + try { + new DbServiceImpl(context, "ignore", "ignore", "http://ignored.example.com"); + fail("exception expected"); + } catch (StorageException se) { + assertEquals("No storage provider available", se.getMessage()); + } + } + + @Test + public void testNoStorageProviderCanHandleStorageUrl() { + when(storageProvider.canHandleProtocol()).thenReturn(false); + + try { + new DbServiceImpl(context, "ignore", "ignore", "http://ignored.example.com"); + fail("exception expected"); + } catch (StorageException se) { + assertEquals("No storage found for URL http://ignored.example.com", se.getMessage()); + } } @Test public void testConnect() { + DbService dbService = new DbServiceImpl(context, "ignore", "ignore", "http://ignored.example.com"); + dbService.connect(); verify(connection).connect(); @@ -90,6 +114,8 @@ @Test public void testConnectRegistersDbService() { + DbService dbService = new DbServiceImpl(context, "ignore", "ignore", "http://ignored.example.com"); + dbService.connect(); verify(connection).connect(); @@ -103,6 +129,8 @@ @Test public void testConnectRegistersStorage() { + DbService dbService = new DbServiceImpl(context, "ignore", "ignore", "http://ignored.example.com"); + dbService.connect(); verify(connection).connect(); @@ -117,6 +145,8 @@ @SuppressWarnings("rawtypes") @Test public void testConnectEnforcesPreCond() { + DbService dbService = new DbServiceImpl(context, "ignore", "ignore", "http://ignored.example.com"); + ServiceRegistration reg = context.registerService(DbService.class, dbService, null); try { dbService.connect(); @@ -138,6 +168,8 @@ @SuppressWarnings("rawtypes") @Test public void testDisConnectEnforcesPreCond() { + DbService dbService = new DbServiceImpl(context, "ignore", "ignore", "http://ignored.example.com"); + ServiceRegistration reg = context.registerService(DbService.class, dbService, null); try { // Storage == null @@ -160,6 +192,8 @@ @Test public void testDisconnect() { + DbService dbService = new DbServiceImpl(context, "ignore", "ignore", "http://ignored.example.com"); + dbService.connect(); assertNotNull(context.getServiceReference(DbService.class)); @@ -170,6 +204,8 @@ @Test public void testDisconnectUnregistersDbService() { + DbService dbService = new DbServiceImpl(context, "ignore", "ignore", "http://ignored.example.com"); + dbService.connect(); assertNotNull(context.getServiceReference(DbService.class)); @@ -182,6 +218,8 @@ @Test public void testDisconnectUnregistersStorage() { + DbService dbService = new DbServiceImpl(context, "ignore", "ignore", "http://ignored.example.com"); + dbService.connect(); assertNotNull(context.getServiceReference(Storage.class)); @@ -196,13 +234,15 @@ public void canGetStorageUrl() { String connectionURL = "http://test.example.com:8082"; - dbService = new DbServiceImpl(context, null, connectionURL); + DbService dbService = new DbServiceImpl(context, "ignore", "ignore", connectionURL); assertEquals(connectionURL, dbService.getConnectionUrl()); } @Test public void testAddListener() { ConnectionListener listener = mock(ConnectionListener.class); + DbService dbService = new DbServiceImpl(context, "ignore", "ignore", "http://ignored.example.com"); + dbService.addConnectionListener(listener); verify(connection).addListener(listener); } @@ -211,6 +251,7 @@ public void testRemoveListener() { // Remove called regardless of listener actually being added ConnectionListener listener = mock(ConnectionListener.class); + DbService dbService = new DbServiceImpl(context, "ignore", "ignore", "http://ignored.example.com"); dbService.removeConnectionListener(listener); verify(connection).removeListener(listener); }