PlatformProvider implements Provider<Platform> { @Override public Platform get() { try { String productName = jdbcTxManager.getConnection().getMetaData().getDatabaseProductName(); for (Platform platform : platforms) if (platform.detect(productName)) return platform; throw new OxalisLoadingException(String.format("Unable to load platform for '%s'.", productName)); } catch (SQLException e) { throw new OxalisLoadingException("Unable to detect database platform.", e); } } @Inject  PlatformProvider(JdbcTxManager jdbcTxManager, Set<Platform> platforms); @Override Platform get();  }
@Test public void simple() throws Exception { Mockito.doReturn(connection).when(jdbcTxManager).getConnection(); Mockito.doReturn(databaseMetaData).when(connection).getMetaData(); Mockito.doReturn("H2").when(databaseMetaData).getDatabaseProductName(); Platform platform = new H2Platform(); PlatformProvider platformProvider = new PlatformProvider(jdbcTxManager, Collections.singleton(platform)); Assert.assertEquals(platformProvider.get(), platform); }
@Test(expectedExceptions = OxalisLoadingException.class) public void notFound() throws Exception { Mockito.doReturn(connection).when(jdbcTxManager).getConnection(); Mockito.doReturn(databaseMetaData).when(connection).getMetaData(); Mockito.doReturn("MySQL").when(databaseMetaData).getDatabaseProductName(); Platform platform = new H2Platform(); PlatformProvider platformProvider = new PlatformProvider(jdbcTxManager, Collections.singleton(platform)); platformProvider.get(); }
@Test(expectedExceptions = OxalisLoadingException.class) public void exception() throws Exception { Mockito.doReturn(connection).when(jdbcTxManager).getConnection(); Mockito.doThrow(new SQLException()).when(connection).getMetaData(); Platform platform = new H2Platform(); PlatformProvider platformProvider = new PlatformProvider(jdbcTxManager, Collections.singleton(platform)); platformProvider.get(); }