@Override public void set(PreparedStatement ps, UUID value, int columnIndex, Context context) throws SQLException { if (value == null) { ps.setNull(columnIndex, Types.BINARY); } else { ps.setBytes(columnIndex, UUIDHelper.toBytes(value)); } } }
@Override public UUID get(final R target) throws Exception { final byte[] o = bytesGetter.get(target); if (o == null) return null; return UUIDHelper.fromBytes(o); }
@Override public void set(PreparedStatement ps, UUID value, int columnIndex, Context context) throws SQLException { if (value == null) { ps.setNull(columnIndex, Types.BINARY); } else { ps.setBytes(columnIndex, UUIDHelper.toBytes(value)); } } }
@Override public UUID get(final R target) throws Exception { final byte[] o = bytesGetter.get(target); if (o == null) return null; return UUIDHelper.fromBytes(o); }
@Test public void testUUIDBinary() throws Exception { UUID uuid = UUID.randomUUID(); newFieldMapperAndMapToPS(new ConstantGetter<Object, UUID>(uuid), UUID.class, Types.BINARY, new Object[0]); newFieldMapperAndMapToPS(NullGetter.<Object, UUID>getter(), UUID.class, Types.BINARY, new Object[0]); verify(ps).setBytes(1, UUIDHelper.toBytes(uuid)); verify(ps).setNull(2, Types.BINARY); }
@Override public UUID get(final R target) throws Exception { final Object o = getter.get(target); if (o == null) return null; if (o instanceof String) { return UUID.fromString((String)o); } else if (o instanceof byte[]) { return UUIDHelper.fromBytes((byte[])o); } else if (o instanceof InputStream) { return UUIDHelper.fromBytes(toBytes((InputStream)o)); } throw new IllegalArgumentException("Cannot convert " + o + " to UUID"); }
@Test public void testUUIDUndefinedType() throws Exception { UUID uuid = UUID.randomUUID(); when(resultSet.getObject(1)).thenReturn( uuid.toString(), UUIDHelper.toBytes(uuid), new ByteArrayInputStream(UUIDHelper.toBytes(uuid))); final Getter<ResultSet, UUID> getter = factory.<UUID>newGetter(UUID.class, key(JdbcColumnKey.UNDEFINED_TYPE), IDENTITY.properties()); assertEquals(uuid, getter.get(resultSet)); assertEquals(uuid, getter.get(resultSet)); assertEquals(uuid, getter.get(resultSet)); }
@Override public UUID get(final R target) throws Exception { final Object o = getter.get(target); if (o == null) return null; if (o instanceof String) { return UUID.fromString((String)o); } else if (o instanceof byte[]) { return UUIDHelper.fromBytes((byte[])o); } else if (o instanceof InputStream) { return UUIDHelper.fromBytes(toBytes((InputStream)o)); } throw new IllegalArgumentException("Cannot convert " + o + " to UUID"); }
@Test public void testUUIDBytes() throws Exception { UUID uuid = UUID.randomUUID(); when(resultSet.getBytes(1)).thenReturn(UUIDHelper.toBytes(uuid)); final Getter<ResultSet, UUID> getter = factory.<UUID>newGetter(UUID.class, key(Types.BINARY), IDENTITY.properties()); assertEquals(uuid, getter.get(resultSet)); } }
@Test public void test() throws Exception { UUID uuid = UUID.randomUUID(); @SuppressWarnings("unchecked") Getter<Object, Object> subGetter = mock(Getter.class); UUIDUnspecifiedTypeGetter<Object> getter = new UUIDUnspecifiedTypeGetter<Object>(subGetter); when(subGetter.get(any())).thenReturn(uuid.toString(), UUIDHelper.toBytes(uuid), new ByteArrayInputStream(UUIDHelper.toBytes(uuid)), new Date()); assertEquals(uuid, getter.get(null)); assertEquals(uuid, getter.get(null)); assertEquals(uuid, getter.get(null)); try { getter.get(null); fail(); } catch (IllegalArgumentException e) { } getter.toString(); } }
@Test public void get() throws Exception { UUID uuid = UUID.randomUUID(); BytesUUIDGetter<Object> getter = new BytesUUIDGetter<Object>(new ConstantGetter<Object, byte[]>(UUIDHelper.toBytes(uuid))); assertEquals(uuid, getter.get(null)); getter.toString(); }