IntegerColumnTranslator extends FormattableColumnTranslator { @Override public String translate(JdbcResultRow rs, int column) throws SQLException, IOException { Object integerObject = rs.getFieldValue(column); if(integerObject instanceof Integer) return toString((Integer) rs.getFieldValue(column)); else return toString(new Integer(rs.getFieldValue(column).toString())); } IntegerColumnTranslator(); @Override String translate(JdbcResultRow rs, int column); @Override String translate(JdbcResultRow rs, String column);  }
@Test public void testFormattedFloat() throws Exception { translator.setFormat("%05d"); Float intVal = new Float("123"); JdbcResultRow row = new JdbcResultRow(); row.setFieldValue("testField", intVal, Types.INTEGER); try { translator.translate(row, 0); fail(); } catch (Exception ex) { } try { translator.translate(row, "testField"); fail(); } catch (Exception ex) { } }
@Test public void testFormattedDouble() throws Exception { translator.setFormat("%05d"); Double doubleVal = new Double("123"); JdbcResultRow row = new JdbcResultRow(); row.setFieldValue("testField", doubleVal, Types.INTEGER); try { translator.translate(row, 0); fail(); } catch (Exception ex) { } try { translator.translate(row, "testField"); fail(); } catch (Exception ex) { } }
@Test public void testFormattedInteger() throws Exception { translator.setFormat("%05d"); Integer intVal = new Integer("123"); JdbcResultRow row = new JdbcResultRow(); row.setFieldValue("testField", intVal, Types.INTEGER); { String translated = translator.translate(row, 0); assertEquals(expected, translated); } { String translated = translator.translate(row, "testField"); assertEquals(expected, translated); } }
@Test public void testFormattedString() throws Exception { translator.setFormat("%05d"); String stringVal = new String("123"); JdbcResultRow row = new JdbcResultRow(); row.setFieldValue("testField", stringVal, Types.INTEGER); { String translated = translator.translate(row, 0); assertEquals(expected, translated); } { String translated = translator.translate(row, "testField"); assertEquals(expected, translated); } }
@Test public void testIllegalFormat() throws Exception { translator.setFormat("%zZX"); Integer intVal = new Integer("123"); JdbcResultRow row = new JdbcResultRow(); row.setFieldValue("testField", intVal, Types.INTEGER); try { translator.translate(row, 0); fail(); } catch (Exception ex) { } }