@Test public void testConstantValueProperty() { ConstantValueProperty<String> cvp = new ConstantValueProperty<String>("aa", String.class); assertEquals("aa", cvp.getValue()); assertEquals(String.class, cvp.getType()); assertEquals("ConstantValue{aa}", cvp.toString()); }
@SuppressWarnings("unchecked") public B addColumn(K key, FieldMapperColumnDefinition<K> columnDefinition) { final FieldMapperColumnDefinition<K> composedDefinition = columnDefinition.compose(mapperConfig.columnDefinitions().getColumnDefinition(key)); final K mappedColumnKey = composedDefinition.rename(key); if (composedDefinition.has(ConstantValueProperty.class)) { ConstantValueProperty staticValueProperty = composedDefinition.lookFor(ConstantValueProperty.class); PropertyMeta<T, Object> meta = new ObjectPropertyMeta<T, Object>(key.getName(), classMeta.getType(), reflectionService, staticValueProperty.getType(), ScoredGetter.of(new ConstantGetter<T, Object>(staticValueProperty.getValue()), 1), null, null); propertyMappingsBuilder.addProperty(key, columnDefinition, meta); } else { propertyMappingsBuilder.addProperty(mappedColumnKey, composedDefinition); } return (B) this; }
@Test public void testCustomGetterOnNonExistantProp() throws Exception { PreparedStatementMapperBuilder<Object> mapperBuilder = JdbcMapperFactory.newInstance() .addColumnProperty("text", new ConstantValueProperty<String>("value2", String.class)) .buildFrom(Object.class); mapperBuilder.addColumn("text"); FieldMapper<Object, PreparedStatement> mapper = mapperBuilder.mapper(); PreparedStatement ps = mock(PreparedStatement.class); mapper.mapTo(new Object(), ps, null); verify(ps).setString(1, "value2"); } }
public SqlParameterSourceBuilder<T> add(JdbcColumnKey key, FieldMapperColumnDefinition<JdbcColumnKey> columnDefinition) { final FieldMapperColumnDefinition<JdbcColumnKey> composedDefinition = columnDefinition.compose(mapperConfig.columnDefinitions().getColumnDefinition(key)); final JdbcColumnKey mappedColumnKey = composedDefinition.rename(key); PropertyMapping<T, Object, JdbcColumnKey> propertyMapping; if (composedDefinition.has(ConstantValueProperty.class)) { ConstantValueProperty staticValueProperty = composedDefinition.lookFor(ConstantValueProperty.class); PropertyMeta<T, Object> meta = new ObjectPropertyMeta<T, Object>(key.getName(), builder.getClassMeta().getType(), reflectionService, staticValueProperty.getType(), ScoredGetter.of(new ConstantGetter<T, Object>(staticValueProperty.getValue()), 1), null, null); propertyMapping = builder.addProperty(key, columnDefinition, meta); } else { propertyMapping = builder.addProperty(mappedColumnKey, composedDefinition); } parameters.add(build(key, propertyMapping)); return this; }
@Test public void testConstantValue() throws Exception { ClassMeta<DbObject> classMeta = ReflectionService.newInstance().<DbObject>getClassMeta(DbObject.class); Writerbuilder<DbObject> builder = new Writerbuilder<DbObject>(classMeta); builder.addColumn("id"); builder.addColumn("blop", new ConstantValueProperty<String>("blop", String.class)); DbObject dbObject = DbObject.newInstance(); List<Object> list = new ArrayList<Object>(); builder.mapper().mapTo(dbObject, list, MappingContext.EMPTY_CONTEXT); assertEquals(list.get(0), dbObject.getId()); assertEquals("blop", list.get(1)); }
@SuppressWarnings("unchecked") public B addColumn(K key, FieldMapperColumnDefinition<K> columnDefinition) { final FieldMapperColumnDefinition<K> composedDefinition = columnDefinition.compose(mapperConfig.columnDefinitions().getColumnDefinition(key)); final K mappedColumnKey = composedDefinition.rename(key); if (composedDefinition.has(ConstantValueProperty.class)) { ConstantValueProperty staticValueProperty = composedDefinition.lookFor(ConstantValueProperty.class); PropertyMeta<T, Object> meta = new ObjectPropertyMeta<T, Object>(key.getName(), classMeta.getType(), reflectionService, staticValueProperty.getType(), ScoredGetter.of(new ConstantGetter<T, Object>(staticValueProperty.getValue()), 1), null, null); propertyMappingsBuilder.addProperty(key, columnDefinition, meta); } else { propertyMappingsBuilder.addProperty(mappedColumnKey, composedDefinition); } return (B) this; }