DoubleColumnTranslator extends FormattableColumnTranslator { @Override public String translate(JdbcResultRow rs, int column) throws SQLException, IOException { Object doubleObject = rs.getFieldValue(column); if(doubleObject instanceof Double) return toString((Double) rs.getFieldValue(column)); else return toString(new Double(rs.getFieldValue(column).toString())); } DoubleColumnTranslator();  DoubleColumnTranslator(String format); @Override String translate(JdbcResultRow rs, int column); @Override String translate(JdbcResultRow rs, String column);  }
@Test public void testFormattedFloat() throws Exception { translator.setFormat("%f"); Float floatVal = new Float("123"); JdbcResultRow row = new JdbcResultRow(); row.setFieldValue("testField", floatVal, Types.DOUBLE); { String translated = translator.translate(row, 0); assertEquals(expected, translated); } { String translated = translator.translate(row, "testField"); assertEquals(expected, translated); } }
@Test public void testFormattedDouble() throws Exception { translator.setFormat("%f"); Double floatVal = new Double("123"); JdbcResultRow row = new JdbcResultRow(); row.setFieldValue("testField", floatVal, Types.DOUBLE); { String translated = translator.translate(row, 0); assertEquals(expected, translated); } { String translated = translator.translate(row, "testField"); assertEquals(expected, translated); } }
@Test public void testFormattedInteger() throws Exception { translator.setFormat("%f"); Integer floatVal = new Integer("123"); JdbcResultRow row = new JdbcResultRow(); row.setFieldValue("testField", floatVal, Types.DOUBLE); { 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("%f"); String floatVal = new String("123"); JdbcResultRow row = new JdbcResultRow(); row.setFieldValue("testField", floatVal, Types.DOUBLE); { 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"); String floatVal = new String("123"); JdbcResultRow row = new JdbcResultRow(); row.setFieldValue("testField", floatVal, Types.DOUBLE); try { translator.translate(row, 0); fail(); } catch (Exception ex) { } }