Search in sources :

Example 1 with TypeReference

use of org.simpleflatmapper.util.TypeReference in project SimpleFlatMapper by arnaudroger.

the class CsvMapperBuilderTest method testMapToMapStringString.

@Test
public void testMapToMapStringString() throws Exception {
    final CsvMapper<Map<String, String>> mapper = csvMapperFactory.newBuilder(new TypeReference<Map<String, String>>() {
    }).addMapping("key1").addMapping("key2").mapper();
    final Iterator<Map<String, String>> iterator = mapper.iterator(new StringReader("v1,v2\na1"));
    Map<String, String> map;
    map = iterator.next();
    assertEquals("v1", map.get("key1"));
    assertEquals("v2", map.get("key2"));
    map = iterator.next();
    assertEquals("a1", map.get("key1"));
    assertEquals(null, map.get("key2"));
}
Also used : StringReader(java.io.StringReader) TypeReference(org.simpleflatmapper.util.TypeReference) Map(java.util.Map) Test(org.junit.Test)

Example 2 with TypeReference

use of org.simpleflatmapper.util.TypeReference in project SimpleFlatMapper by arnaudroger.

the class JoolTupleTest method testCsvParser.

@Test
public void testCsvParser() throws IOException {
    final CsvParser.StaticMapToDSL<Tuple3<Long, Integer, Short>> mapToDSL = CsvParser.mapTo(new TypeReference<Tuple3<Long, Integer, Short>>() {
    }).defaultHeaders();
    final Iterator<Tuple3<Long, Integer, Short>> iterator = mapToDSL.iterator(new StringReader("6,7,3\n7,8,9"));
    final Tuple3<Long, Integer, Short> tuple1 = iterator.next();
    assertEquals(6l, tuple1.v1().longValue());
    assertEquals(7, tuple1.v2().intValue());
    assertEquals((short) 3, tuple1.v3().shortValue());
    final Tuple3<Long, Integer, Short> tuple2 = iterator.next();
    assertEquals(7l, tuple2.v1().longValue());
    assertEquals(8, tuple2.v2().intValue());
    assertEquals((short) 9, tuple2.v3().shortValue());
}
Also used : Tuple3(org.jooq.lambda.tuple.Tuple3) StringReader(java.io.StringReader) CsvParser(org.simpleflatmapper.csv.CsvParser) TypeReference(org.simpleflatmapper.util.TypeReference) Test(org.junit.Test)

Example 3 with TypeReference

use of org.simpleflatmapper.util.TypeReference in project SimpleFlatMapper by arnaudroger.

the class ConstantSourceMapperBuilderTest method testIssue495Simple.

@Test
public void testIssue495Simple() throws Exception {
    TypeReference<List<Tuple3<Foo, Foo, Foo>>> typeReference = new TypeReference<List<Tuple3<Foo, Foo, Foo>>>() {
    };
    ClassMeta<List<Tuple3<Foo, Foo, Foo>>> classMeta = this.classMeta.getReflectionService().getClassMeta(typeReference.getType());
    Predicate<PropertyMeta<?, ?>> propertyFilter = new Predicate<PropertyMeta<?, ?>>() {

        @Override
        public boolean test(PropertyMeta<?, ?> propertyMeta) {
            return true;
        }
    };
    PropertyFinder<?> propertyFinder = classMeta.newPropertyFinder(propertyFilter);
    PropertyMeta<?, ?> p1 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("bar", 0, false, false), new Object[0]);
    PropertyMeta<?, ?> p2 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("bar", 0, false, false), new Object[0]);
    assertEquals("[0].element0.bar", p1.getPath());
    assertEquals("[0].element1.bar", p2.getPath());
    propertyFinder = classMeta.newPropertyFinder(propertyFilter);
    p1 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("elt0_elt0_bar", 0, false, false), new Object[0]);
    p2 = propertyFinder.findProperty(new DefaultPropertyNameMatcher("elt1_elt0_bar", 0, false, false), new Object[0]);
    assertEquals("[0].element0.bar", p1.getPath());
    assertEquals("[1].element0.bar", p2.getPath());
}
Also used : DefaultPropertyNameMatcher(org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher) Predicate(org.simpleflatmapper.util.Predicate) Tuple3(org.simpleflatmapper.tuple.Tuple3) List(java.util.List) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) TypeReference(org.simpleflatmapper.util.TypeReference) Test(org.junit.Test)

Example 4 with TypeReference

use of org.simpleflatmapper.util.TypeReference in project SimpleFlatMapper by arnaudroger.

the class AbstractMapperBuilderTest method testSetDbObject.

@Test
public void testSetDbObject() {
    ClassMeta<Set<DbObject>> classMeta = ReflectionService.disableAsm().<Set<DbObject>>getClassMeta(new TypeReference<Set<DbObject>>() {
    }.getType());
    Mapper<Object[], Set<DbObject>> mapper = new SampleMapperBuilder<Set<DbObject>>(classMeta).addMapping("1_id").addMapping("1_name").addMapping("2_id").addMapping("2_name").mapper();
    Set<DbObject> map = mapper.map(new Object[] { 1l, "name1", 2l, "name2" });
    assertEquals(2, map.size());
    Iterator<DbObject> it = map.iterator();
    DbObject o = it.next();
    if (o.getId() == 1l) {
        assertEquals("name1", o.getName());
        o = it.next();
        assertEquals(2l, o.getId());
        assertEquals("name2", o.getName());
    } else {
        assertEquals("name2", o.getName());
        o = it.next();
        assertEquals(1l, o.getId());
        assertEquals("name1", o.getName());
    }
}
Also used : Set(java.util.Set) DbObject(org.simpleflatmapper.test.beans.DbObject) TypeReference(org.simpleflatmapper.util.TypeReference) Test(org.junit.Test)

Example 5 with TypeReference

use of org.simpleflatmapper.util.TypeReference in project SimpleFlatMapper by arnaudroger.

the class AbstractMapperFactoryTest method testMeta.

@Test
public void testMeta() {
    MapperFactory mapperFactory = new MapperFactory();
    ClassMeta<String> stringClassMeta = mapperFactory.getClassMeta(String.class);
    Assert.assertEquals(stringClassMeta, mapperFactory.getClassMeta((Type) String.class));
    Assert.assertEquals(stringClassMeta, mapperFactory.getClassMeta(new TypeReference<String>() {
    }));
    assertNotNull(mapperFactory.getClassMetaWithExtraInstantiator(String.class, null));
    assertNotNull(mapperFactory.getClassMetaWithExtraInstantiator((Type) String.class, null));
    assertNotNull(mapperFactory.getClassMetaWithExtraInstantiator(new TypeReference<String>() {
    }, null));
}
Also used : Type(java.lang.reflect.Type) AbstractMapperFactory(org.simpleflatmapper.map.mapper.AbstractMapperFactory) TypeReference(org.simpleflatmapper.util.TypeReference) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)15 TypeReference (org.simpleflatmapper.util.TypeReference)15 List (java.util.List)5 StringReader (java.io.StringReader)3 DefaultPropertyNameMatcher (org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher)3 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)3 StringWriter (java.io.StringWriter)2 Type (java.lang.reflect.Type)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 Tuple3 (org.jooq.lambda.tuple.Tuple3)2 DbObject (org.simpleflatmapper.test.beans.DbObject)2 Tuple3 (org.simpleflatmapper.tuple.Tuple3)2 Predicate (org.simpleflatmapper.util.Predicate)2 GenericArrayType (java.lang.reflect.GenericArrayType)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1