private Map<Parameter, Getter<? super Object, ?>> parameters(InstantiatorDefinition instantiatorDefinition, boolean allowNull, boolean builderIgnoresNullValues) throws Exception { Map<Parameter, Getter<? super Object, ?>> parameterGetterMap = new HashMap<Parameter, Getter<? super Object, ?>>(); for(Parameter parameter : instantiatorDefinition.getParameters()) { Object value = neutralValue(parameter.getGenericType(), allowNull, builderIgnoresNullValues); parameterGetterMap.put(parameter, new ConstantGetter<Object, Object>(value)); } return parameterGetterMap; }
@Test public void testObject() { Object value = new Object(); assertEquals(value, new ConstantGetter<Object, Object>(value).get(null)); }
private Map<Parameter, Getter<? super Object, ?>> parameters(InstantiatorDefinition instantiatorDefinition, boolean allowNull, boolean builderIgnoresNullValues) throws Exception { Map<Parameter, Getter<? super Object, ?>> parameterGetterMap = new HashMap<Parameter, Getter<? super Object, ?>>(); for(Parameter parameter : instantiatorDefinition.getParameters()) { Object value = neutralValue(parameter.getGenericType(), allowNull, builderIgnoresNullValues); parameterGetterMap.put(parameter, new ConstantGetter<Object, Object>(value)); } return parameterGetterMap; }
private Map<Parameter, Getter<? super Object, ?>> parametersWithExpectedValue(InstantiatorDefinition instantiatorDefinition, Parameter param, boolean allowNull, boolean builderIgnoresNullValues) throws Exception { Map<Parameter, Getter<? super Object, ?>> parameterGetterMap = parameters(instantiatorDefinition, allowNull, builderIgnoresNullValues); parameterGetterMap.put(param, new ConstantGetter<Object, Object>(markValue(param.getGenericType(), builderIgnoresNullValues))); return parameterGetterMap; }
private Map<Parameter, Getter<? super Object, ?>> parametersWithExpectedValue(InstantiatorDefinition instantiatorDefinition, Parameter param, boolean allowNull, boolean builderIgnoresNullValues) throws Exception { Map<Parameter, Getter<? super Object, ?>> parameterGetterMap = parameters(instantiatorDefinition, allowNull, builderIgnoresNullValues); parameterGetterMap.put(param, new ConstantGetter<Object, Object>(markValue(param.getGenericType(), builderIgnoresNullValues))); return parameterGetterMap; }
@SuppressWarnings("unchecked") @Override public <P> Getter<Object, P> newGetter(Type target, SampleFieldKey key, Object... properties) { if (key.getIndex() == 16) { return new Getter<Object, P>() { @Override public P get(Object target) throws Exception { throw new RuntimeException("Error !"); } }; } if (target.equals(Date.class)) { return (Getter<Object, P>) new ConstantGetter<Object, Date>(DATE); } else if (target.equals(String.class)) { return (Getter<Object, P>) new ConstantGetter<Object, String>(STRING); } return null; } };
@Test public void testMapSqlDate() throws Exception { final java.sql.Date date = new java.sql.Date(new Date().getTime()); newFieldMapperAndMapToPS(new ConstantGetter<Object, java.sql.Date>(date), java.sql.Date.class); newFieldMapperAndMapToPS(NullGetter.<Object, java.sql.Date>getter(), java.sql.Date.class); verify(ps).setDate(1, date); verify(ps).setNull(2, Types.DATE); }
@Test public void testJavaZonedDateTime() throws Exception { java.time.ZonedDateTime value = java.time.ZonedDateTime.now(); newFieldMapperAndMapToPS(new ConstantGetter<Object, java.time.ZonedDateTime>(value), java.time.ZonedDateTime.class); newFieldMapperAndMapToPS(NullGetter.<Object, java.time.ZonedDateTime>getter(), java.time.ZonedDateTime.class); verify(ps).setTimestamp(1, new Timestamp(value.toInstant().toEpochMilli())); verify(ps).setNull(2, Types.TIMESTAMP); }
@Test public void testMapDateNoSqlType() throws Exception { final Date date = new Date(); newFieldMapperAndMapToPS(new ConstantGetter<Object, Date>(date), Date.class); newFieldMapperAndMapToPS(NullGetter.<Object, Date>getter(), Date.class); verify(ps).setTimestamp(1, new Timestamp(date.getTime())); verify(ps).setNull(2, Types.TIMESTAMP); }
@Test public void testJodaDateTime() throws Exception { org.joda.time.DateTime value = new org.joda.time.DateTime(); newFieldMapperAndMapToPS(new ConstantGetter<Object, org.joda.time.DateTime>(value), org.joda.time.DateTime.class); newFieldMapperAndMapToPS(NullGetter.<Object, org.joda.time.DateTime>getter(), org.joda.time.DateTime.class); verify(ps).setTimestamp(1, new Timestamp(value.getMillis())); verify(ps).setNull(2, Types.TIMESTAMP); }
@Test public void testMapTimestamp() throws Exception { final Timestamp date = new Timestamp(new Date().getTime()); newFieldMapperAndMapToPS(new ConstantGetter<Object, Timestamp>(date), Timestamp.class); newFieldMapperAndMapToPS(NullGetter.<Object, Timestamp>getter(), Timestamp.class); verify(ps).setTimestamp(1, date); verify(ps).setNull(2, Types.TIMESTAMP); }
@Test public void testJavaInstant() throws Exception { java.time.Instant value = java.time.Instant.now(); newFieldMapperAndMapToPS(new ConstantGetter<Object, java.time.Instant>(value), java.time.Instant.class); newFieldMapperAndMapToPS(NullGetter.<Object, java.time.Instant>getter(), java.time.Instant.class); verify(ps).setTimestamp(1, new Timestamp(value.toEpochMilli())); verify(ps).setNull(2, Types.TIMESTAMP); }
@Test public void testMapCalendar() throws Exception { final Date date = new Date(); final Calendar cal = Calendar.getInstance(); cal.setTime(date); newFieldMapperAndMapToPS(new ConstantGetter<Object, Calendar>(cal), Calendar.class); newFieldMapperAndMapToPS(NullGetter.<Object, Calendar>getter(), Calendar.class); verify(ps).setTimestamp(1, new Timestamp(date.getTime())); verify(ps).setNull(2, Types.TIMESTAMP); }
@Test public void testJavaLocalDate() throws Exception { java.time.LocalDate value = java.time.LocalDate.now(); java.time.ZoneId zoneId = ZoneId.of("America/Los_Angeles"); newFieldMapperAndMapToPS(new ConstantGetter<Object, java.time.LocalDate>(value), java.time.LocalDate.class, new JavaZoneIdProperty(zoneId)); newFieldMapperAndMapToPS(NullGetter.<Object, java.time.LocalDate>getter(), java.time.LocalDate.class); verify(ps).setDate(1, new java.sql.Date(value.atStartOfDay(zoneId).toInstant().toEpochMilli())); verify(ps).setNull(2, Types.DATE); }
@Test public void testMapTime() throws Exception { final Time date = new Time(new Date().getTime()); newFieldMapperAndMapToPS(new ConstantGetter<Object, Time>(date), Time.class); newFieldMapperAndMapToPS(NullGetter.<Object, Time>getter(), Time.class); verify(ps).setTime(1, date); verify(ps).setNull(2, Types.TIME); }
@Test public void testJavaLocalDateTime() throws Exception { java.time.LocalDateTime value = java.time.LocalDateTime.now(); ZoneId zoneId = ZoneId.of("America/Los_Angeles"); newFieldMapperAndMapToPS(new ConstantGetter<Object, java.time.LocalDateTime>(value), java.time.LocalDateTime.class, new JavaZoneIdProperty(zoneId)); newFieldMapperAndMapToPS(NullGetter.<Object, java.time.LocalDateTime>getter(), java.time.LocalDateTime.class); verify(ps).setTimestamp(1, new Timestamp(value.atZone(zoneId).toInstant().toEpochMilli())); verify(ps).setNull(2, Types.TIMESTAMP); }
@Test public void testReader() throws Exception { Reader value = mock(Reader.class); newFieldMapperAndMapToPS(new ConstantGetter<Object, Reader>(value), Reader.class); newFieldMapperAndMapToPS(NullGetter.<Object, Reader>getter(), Reader.class); verify(ps).setCharacterStream(1, value); verify(ps).setNull(2, Types.VARCHAR); }
@Test public void testClob() throws Exception { Clob value = mock(Clob.class); newFieldMapperAndMapToPS(new ConstantGetter<Object, Clob>(value), Clob.class); newFieldMapperAndMapToPS(NullGetter.<Object, Clob>getter(), Clob.class); verify(ps).setClob(1, value); verify(ps).setNull(2, Types.CLOB); }
@Test public void testBlob() throws Exception { Blob value = mock(Blob.class); newFieldMapperAndMapToPS(new ConstantGetter<Object, Blob>(value), Blob.class); newFieldMapperAndMapToPS(NullGetter.<Object, Blob>getter(), Blob.class); verify(ps).setBlob(1, value); verify(ps).setNull(2, Types.BINARY); }
@Test public void testUUIDString() throws Exception { UUID uuid = UUID.randomUUID(); newFieldMapperAndMapToPS(new ConstantGetter<Object, UUID>(uuid), UUID.class); newFieldMapperAndMapToPS(NullGetter.<Object, UUID>getter(), UUID.class); verify(ps).setString(1, uuid.toString()); verify(ps).setNull(2, Types.VARCHAR); }