JDBCStateRepository implements StateRepository { @Override public FeatureState getFeatureState(Feature feature) { try { Connection connection = dataSource.getConnection(); try { String sql = "SELECT FEATURE_ENABLED, STRATEGY_ID, STRATEGY_PARAMS FROM %TABLE% WHERE FEATURE_NAME = ?"; PreparedStatement statement = connection.prepareStatement(insertTableName(sql)); try { statement.setString(1, feature.name()); ResultSet resultSet = statement.executeQuery(); try { if (resultSet.next()) { boolean enabled = resultSet.getInt(Columns.FEATURE_ENABLED) > 0; FeatureState state = new FeatureState(feature, enabled); String strategyId = resultSet.getString(Columns.STRATEGY_ID); if (Strings.isNotBlank(strategyId)) { state.setStrategyId(strategyId.trim()); } String paramData = resultSet.getString(Columns.STRATEGY_PARAMS); if (Strings.isNotBlank(paramData)) { Map<String, String> params = serializer.deserialize(paramData); for (Entry<String, String> param : params.entrySet()) { state.setParameter(param.getKey(), param.getValue()); } } return state; } } finally { DbUtils.closeQuietly(resultSet); } } finally { DbUtils.closeQuietly(statement); } } finally { DbUtils.closeQuietly(connection); } } catch (SQLException e) { throw new IllegalStateException("Failed to fetch the feature's state from the database", e); } return null; } JDBCStateRepository(DataSource dataSource);  JDBCStateRepository(DataSource dataSource, String tableName); @Deprecated  JDBCStateRepository(DataSource dataSource, String tableName, boolean createTable); @Deprecated  JDBCStateRepository(DataSource dataSource, String tableName, boolean createTable, MapSerializer serializer);  JDBCStateRepository(DataSource dataSource, String tableName, boolean createTable, MapSerializer serializer,
        boolean noCommit); private  JDBCStateRepository(Builder builder); @Override FeatureState getFeatureState(Feature feature); @Override void setFeatureState(FeatureState featureState); static Builder newBuilder(DataSource dataSource);  }
@Test void testShouldReadStateWithoutStrategyAndParameters() { update(dataSource, "INSERT INTO TOGGLZ VALUES ('F1', 0, NULL, NULL)"); FeatureState state = repository.getFeatureState(TestFeature.F1); assertNotNull(state); assertEquals(TestFeature.F1, state.getFeature()); assertFalse(state.isEnabled()); assertNull(state.getStrategyId()); assertEquals(0, state.getParameterNames().size()); }
@Test void testShouldReadStateWithStrategyAndParameters() { update(dataSource, "INSERT INTO TOGGLZ VALUES ('F1', 1, 'myStrategy', 'param23=foobar')"); FeatureState state = repository.getFeatureState(TestFeature.F1); assertNotNull(state); assertEquals(TestFeature.F1, state.getFeature()); assertTrue(state.isEnabled()); assertEquals("myStrategy", state.getStrategyId()); assertEquals(1, state.getParameterNames().size()); assertEquals("foobar", state.getParameter("param23")); }