Search in sources :

Example 11 with Context

use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.

the class AbstractMapperBuilderTest method testPrimitive.

@Test
public void testPrimitive() {
    Object[] data = { true, (byte) 1, (char) 2, (short) 3, 4, (long) 5, (float) 6, (double) 7 };
    SetRowMapper<Object[], Object[][], DbPrimitiveObject, Exception> mapper;
    DbPrimitiveObject o;
    mapper = addPrimitiveFields(new SampleMapperBuilder<DbPrimitiveObject>(ReflectionService.newInstance().getClassMeta(DbPrimitiveObject.class)));
    o = mapper.map(data);
    validatePrimitiveData(o);
    final List<SampleFieldKey> errors = new ArrayList<SampleFieldKey>();
    MapperConfig<SampleFieldKey, Object[]> mapperConfig = MapperConfig.<SampleFieldKey, Object[]>fieldMapperConfig().fieldMapperErrorHandler(new FieldMapperErrorHandler<SampleFieldKey>() {

        @Override
        public void errorMappingField(SampleFieldKey key, Object source, Object target, Exception error, Context mappingContext) throws MappingException {
            errors.add(key);
        }
    });
    ReflectionService reflectionService = ReflectionService.newInstance(false);
    mapper = addPrimitiveFields(new SampleMapperBuilder<DbPrimitiveObject>(reflectionService.getClassMeta(DbPrimitiveObject.class), mapperConfig));
    o = mapper.map(data);
    validatePrimitiveData(o);
    assertTrue(errors.isEmpty());
    o = mapper.map(new Object[0]);
    assertEquals(8, errors.size());
    assertNotNull(o);
}
Also used : MappingContext(org.simpleflatmapper.map.MappingContext) Context(org.simpleflatmapper.converter.Context) ArrayList(java.util.ArrayList) ReflectionService(org.simpleflatmapper.reflect.ReflectionService) MappingException(org.simpleflatmapper.map.MappingException) MapperBuildingException(org.simpleflatmapper.map.MapperBuildingException) MappingException(org.simpleflatmapper.map.MappingException) DbListObject(org.simpleflatmapper.test.beans.DbListObject) DbObject(org.simpleflatmapper.test.beans.DbObject) DbFinal1DeepObject(org.simpleflatmapper.test.beans.DbFinal1DeepObject) DbPrimitiveObject(org.simpleflatmapper.test.beans.DbPrimitiveObject) DbPartialFinalObject(org.simpleflatmapper.test.beans.DbPartialFinalObject) DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) SampleFieldKey(org.simpleflatmapper.test.map.SampleFieldKey) DbPrimitiveObject(org.simpleflatmapper.test.beans.DbPrimitiveObject) Test(org.junit.Test)

Example 12 with Context

use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.

the class Issue675Test method testSourceFactory.

@Test
public void testSourceFactory() {
    String query = "SELECT * FROM table WHERE type = :myEnum and id = :id";
    SqlParameterSourceFactory<Issue675> sourceFactory = JdbcTemplateMapperFactory.newInstance().addColumnProperty("myEnum", new ConverterProperty<MyEnum, Long>(new ContextualConverter<MyEnum, Long>() {

        @Override
        public Long convert(MyEnum in, Context context) throws Exception {
            switch(in) {
                case ONE:
                    return 1l;
                case TWO:
                    return 2l;
                default:
                    throw new IllegalArgumentException();
            }
        }
    }, MyEnum.class)).newSqlParameterSourceFactory(Issue675.class, query);
    SqlParameterSource sqlParameterSource = sourceFactory.newSqlParameterSource(new Issue675(1, MyEnum.TWO));
    assertEquals(1l, sqlParameterSource.getValue("id"));
    assertEquals(2l, sqlParameterSource.getValue("myEnum"));
}
Also used : Context(org.simpleflatmapper.converter.Context) SqlParameterSource(org.springframework.jdbc.core.namedparam.SqlParameterSource) ConverterProperty(org.simpleflatmapper.map.property.ConverterProperty) Test(org.junit.Test)

Example 13 with Context

use of org.simpleflatmapper.converter.Context in project SimpleFlatMapper by arnaudroger.

the class JdbcTemplateMapperFactoryTest method testIssue618OverrideEnum.

@Test
public void testIssue618OverrideEnum() throws SQLException {
    JdbcTemplateMapperFactory config = JdbcTemplateMapperFactory.newInstance().addGetterForType(new Predicate<Type>() {

        @Override
        public boolean test(Type type) {
            return TypeHelper.isAssignable(LookupValue.class, type);
        }
    }, new ContextualGetterFactory<ResultSet, JdbcColumnKey>() {

        @Override
        public <P> ContextualGetter<ResultSet, P> newGetter(Type target, JdbcColumnKey key, MappingContextFactoryBuilder<?, ? extends FieldKey<?>> mappingContextFactoryBuilder, Object... properties) {
            Class enumClass = TypeHelper.toClass(target);
            final Enum[] values = EnumHelper.getValues(enumClass);
            final int index = key.getIndex();
            return new ContextualGetter<ResultSet, P>() {

                @Override
                public P get(ResultSet resultSet, Context context) throws Exception {
                    int i = resultSet.getInt(index);
                    for (Enum e : values) {
                        if (((LookupValue) e).lookupValue() == i) {
                            return (P) e;
                        }
                    }
                    return null;
                }
            };
        }
    });
    ResultSetExtractorImpl<Issue618> rse = JdbcTemplateMapperFactory.newInstance(config).newResultSetExtractor(Issue618.class);
    Connection conn = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);
    Statement st = null;
    ResultSet rs = null;
    if (conn == null)
        return;
    try {
        st = conn.createStatement();
        rs = st.executeQuery("SELECT * FROM (VALUES (1, 1), (2, 2)) AS t (id, enum618) ");
        List<Issue618> issue618s = rse.extractData(rs);
        assertEquals(Arrays.asList(new Issue618(1, Enum618.A), new Issue618(2, Enum618.B)), issue618s);
    } finally {
        if (rs != null)
            rs.close();
        if (st != null)
            st.close();
        conn.close();
    }
}
Also used : Context(org.simpleflatmapper.converter.Context) Statement(java.sql.Statement) ContextualGetter(org.simpleflatmapper.map.getter.ContextualGetter) Connection(java.sql.Connection) SQLException(java.sql.SQLException) ParseException(java.text.ParseException) JdbcTemplateMapperFactory(org.simpleflatmapper.jdbc.spring.JdbcTemplateMapperFactory) JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) Type(java.lang.reflect.Type) ResultSet(java.sql.ResultSet) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Aggregations

Context (org.simpleflatmapper.converter.Context)13 Test (org.junit.Test)9 DbObject (org.simpleflatmapper.test.beans.DbObject)6 MappingException (org.simpleflatmapper.map.MappingException)4 ContextualConverter (org.simpleflatmapper.converter.ContextualConverter)3 Type (java.lang.reflect.Type)2 BigInteger (java.math.BigInteger)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 ParseException (java.text.ParseException)2 FieldMapperErrorHandler (org.simpleflatmapper.map.FieldMapperErrorHandler)2 SampleFieldKey (org.simpleflatmapper.test.map.SampleFieldKey)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 UUID (java.util.UUID)1