JdbcParameterEntryMapper implements ObjectMapper<JdbcParameterEntry> { @Override public JdbcParameterEntry createObject(ResultSet resultSet) throws SQLException { List<String> levels = new ArrayList<String>(); String currentLevelValue; boolean doneBeforeLimit = false; for (int levelIndex = 0; levelIndex < configuration.levelColumnCount() - 1; ++levelIndex) { currentLevelValue = resultSet.getString("level" + levelIndex); if (currentLevelValue == null) { doneBeforeLimit = true; break; } levels.add(currentLevelValue); } if (!doneBeforeLimit) { String lastLevelValue = resultSet.getString("level" + (configuration.levelColumnCount() - 1)); if (lastLevelValue != null) { levels.addAll(Arrays.asList(lastLevelValue.split("\\" + configuration.excessLevelsSeparator()))); } } String[] levelValues = levels.toArray(new String[levels.size()]); JdbcParameterEntry entry = new JdbcParameterEntry(resultSet.getLong("id"), levelValues); return entry; } JdbcParameterEntryMapper(DefaultJdbcConfig configuration); @Override JdbcParameterEntry createObject(ResultSet resultSet);  }
@Test public void shouldReturnEntryWithSameAmountOfLevelsWhenLevelsBelowLimit() throws SQLException { DefaultJdbcConfig configuration = jdbcConfig().withLevelColumnCount(10).build(); JdbcParameterEntryMapper mapper = new JdbcParameterEntryMapper(configuration); ResultSet resultSet = resultSet().withLong("id", 1).withLong("fk_parameter", 1) .withString("level0", "1").withString("level1", "2").build(); JdbcParameterEntry entry = mapper.createObject(resultSet); ParamEngineAssertions.assertThat(entry).hasLevels(2); }
@Test public void shouldReturnEntryWithMaximumLevelsWhenLevelsEqualsLimit() throws SQLException { DefaultJdbcConfig configuration = jdbcConfig().withLevelColumnCount(2).build(); JdbcParameterEntryMapper mapper = new JdbcParameterEntryMapper(configuration); ResultSet resultSet = resultSet().withLong("id", 1).withLong("fk_parameter", 1) .withString("level0", "1").withString("level1", "2") .build(); JdbcParameterEntry entry = mapper.createObject(resultSet); ParamEngineAssertions.assertThat(entry).hasLevels(2); }
@Test public void shouldReturnEntryWithLevelsExtractedFromSplitLastLevelWhenLevelCountGreaterThanLimit() throws SQLException { DefaultJdbcConfig configuration = jdbcConfig() .withLevelColumnCount(3).withExcessLevelSeparator('|').build(); JdbcParameterEntryMapper mapper = new JdbcParameterEntryMapper(configuration); ResultSet resultSet = resultSet().withLong("id", 1).withLong("fk_parameter", 1) .withString("level0", "1").withString("level1", "2") .withString("level2", "3|4|5") .build(); JdbcParameterEntry entry = mapper.createObject(resultSet); ParamEngineAssertions.assertThat(entry).hasLevels(5); }