Search in sources :

Example 1 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class CsvMapperBuilderTest method testMapToMapStringToDbObject.

@Test
public void testMapToMapStringToDbObject() throws Exception {
    final CsvMapperBuilder<Map<String, DbObject>> builder = csvMapperFactory.newBuilder(new TypeReference<Map<String, DbObject>>() {
    });
    final CsvMapper<Map<String, DbObject>> mapper = builder.addMapping("key1_id", new Object[0]).addMapping("key1_name").addMapping("key2_2_id").addMapping("key2_2_name", 3, new Object[0]).mapper();
    final Iterator<Map<String, DbObject>> iterator = mapper.iterator(new StringReader("1,name1,2,name2"));
    Map<String, DbObject> map;
    map = iterator.next();
    DbObject o = map.get("key1");
    assertEquals(1, o.getId());
    assertEquals("name1", o.getName());
    o = map.get("key2_2");
    assertEquals(2, o.getId());
    assertEquals("name2", o.getName());
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) StringReader(java.io.StringReader) DbObject(org.simpleflatmapper.test.beans.DbObject) DbPartialFinalObject(org.simpleflatmapper.test.beans.DbPartialFinalObject) DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) Map(java.util.Map) Test(org.junit.Test)

Example 2 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class CsvParserTest method testYamlCommentMapper.

@Test
public void testYamlCommentMapper() throws IOException {
    String data = "# comment who cares\n" + "id,name\n" + "# what's up wih all the comments\n" + "1,\"n\"\n" + "# if you need that many comment ....";
    CsvParser.MapToDSL<DbObject> dbObjectMapToDSL = CsvParser.dsl().withYamlComments().mapTo(DbObject.class);
    checkYamlCommentMapperResult(dbObjectMapToDSL.forEach(data, new ListCollector<DbObject>()).getList());
    List<DbObject> dbObjects = new ArrayList<DbObject>();
    Iterator<DbObject> iterator = dbObjectMapToDSL.iterator(data);
    while (iterator.hasNext()) {
        dbObjects.add(iterator.next());
    }
    checkYamlCommentMapperResult(dbObjects);
    // IFJAVA8_START
    checkYamlCommentMapperResult(dbObjectMapToDSL.stream(data).collect(Collectors.toList()));
    // IFJAVA8_END
    File csv = createTempCsv(data);
    checkYamlCommentMapperResult(dbObjectMapToDSL.forEach(csv, new ListCollector<DbObject>()).getList());
    dbObjects = new ArrayList<DbObject>();
    CloseableIterator<DbObject> citerator = dbObjectMapToDSL.iterator(csv);
    while (citerator.hasNext()) {
        dbObjects.add(citerator.next());
    }
    checkYamlCommentMapperResult(dbObjects);
    // IFJAVA8_START
    checkYamlCommentMapperResult(dbObjectMapToDSL.stream(csv).collect(Collectors.toList()));
    // IFJAVA8_END
    checkYamlCommentMapperResult(dbObjectMapToDSL.forEach(new StringReader(data), new ListCollector<DbObject>()).getList());
    dbObjects = new ArrayList<DbObject>();
    iterator = dbObjectMapToDSL.iterator(new StringReader(data));
    while (iterator.hasNext()) {
        dbObjects.add(iterator.next());
    }
    checkYamlCommentMapperResult(dbObjects);
    // IFJAVA8_START
    checkYamlCommentMapperResult(dbObjectMapToDSL.stream(new StringReader(data)).collect(Collectors.toList()));
    // IFJAVA8_END
    checkYamlCommentMapperResult(dbObjectMapToDSL.forEach(new StringBuilder(data), new ListCollector<DbObject>()).getList());
    dbObjects = new ArrayList<DbObject>();
    iterator = dbObjectMapToDSL.iterator(new StringBuilder(data));
    while (iterator.hasNext()) {
        dbObjects.add(iterator.next());
    }
    checkYamlCommentMapperResult(dbObjects);
    // IFJAVA8_START
    checkYamlCommentMapperResult(dbObjectMapToDSL.stream(new StringBuilder(data)).collect(Collectors.toList()));
// IFJAVA8_END
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) CsvParser(org.simpleflatmapper.csv.CsvParser) File(java.io.File) Test(org.junit.Test)

Example 3 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class CsvWriterTest method testWriterWithCustomMapperConfig.

@Test
public void testWriterWithCustomMapperConfig() throws Exception {
    CsvWriter.CsvWriterDSL<DbObject> from = CsvWriter.from(DbObject.class);
    from = from.mapperConfig(from.mapperConfig().mapperBuilderErrorHandler(new MapperBuilderErrorHandler() {

        @Override
        public void accessorNotFound(String msg) {
        }

        @Override
        public void propertyNotFound(Type target, String property) {
        }

        @Override
        public void customFieldError(FieldKey<?> key, String message) {
        }
    }));
    from.columns("nonexistent");
}
Also used : Type(java.lang.reflect.Type) MapperBuilderErrorHandler(org.simpleflatmapper.map.MapperBuilderErrorHandler) CsvWriter(org.simpleflatmapper.csv.CsvWriter) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Example 4 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class CsvWriterTest method testWriterCustomEscaping.

@Test
public void testWriterCustomEscaping() throws ParseException, IOException {
    StringWriter sw = new StringWriter();
    DbObject value = newDbObject();
    value.setName("Arnaud\"What\"");
    CsvWriter.from(DbObject.class).escape('\\').to(sw).append(value);
    assertEquals("id,name,email,creation_time,type_ordinal,type_name\r\n" + "13,\"Arnaud\\\"What\\\"\",email,2015-06-06 17:46:23,type2,type3\r\n", sw.toString());
}
Also used : StringWriter(java.io.StringWriter) DbObject(org.simpleflatmapper.test.beans.DbObject) Test(org.junit.Test)

Example 5 with DbObject

use of org.simpleflatmapper.test.beans.DbObject in project SimpleFlatMapper by arnaudroger.

the class DynamicCsvMapperImplTest method testMultipleThread.

@Test
public void testMultipleThread() throws InterruptedException, ExecutionException {
    final CsvMapper<DbObject> mapper = CsvMapperFactory.newInstance().newMapper(DbObject.class);
    ExecutorService service = Executors.newFixedThreadPool(4);
    final AtomicLong sumOfAllIds = new AtomicLong();
    final AtomicLong nbRow = new AtomicLong();
    final CheckedConsumer<DbObject> handler = new CheckedConsumer<DbObject>() {

        @Override
        public void accept(DbObject t) throws Exception {
            long id = t.getId();
            assertEquals("name" + Long.toHexString(id), t.getName());
            assertEquals("email" + Long.toHexString(id), t.getEmail());
            assertEquals(Type.values()[(int) (id) % 4], t.getTypeName());
            assertEquals(Type.values()[(int) (id) % 4], t.getTypeOrdinal());
            assertEquals(id, t.getCreationTime().getTime() / 1000);
            sumOfAllIds.addAndGet(id);
            nbRow.incrementAndGet();
        }
    };
    final String str = buildCsvContent();
    List<Future<Object>> futures = new ArrayList<Future<Object>>();
    for (int i = 0; i < NBFUTURE; i++) {
        futures.add(service.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                mapper.forEach(new StringReader(str), handler);
                return null;
            }
        }));
    }
    int i = 0;
    for (Future<Object> future : futures) {
        try {
            future.get();
        } catch (Exception e) {
            System.out.println("Future " + i + " fail " + e);
        }
        i++;
    }
    assertEquals(NBFUTURE, i);
    assertEquals(nbRow.get(), NBFUTURE * NBROW);
    int sum = 0;
    for (i = 0; i < NBROW; i++) {
        sum += i;
    }
    assertEquals(sumOfAllIds.get(), NBFUTURE * sum);
}
Also used : CheckedConsumer(org.simpleflatmapper.util.CheckedConsumer) DbObject(org.simpleflatmapper.test.beans.DbObject) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AtomicLong(java.util.concurrent.atomic.AtomicLong) StringReader(java.io.StringReader) DbListObject(org.simpleflatmapper.test.beans.DbListObject) DbObject(org.simpleflatmapper.test.beans.DbObject) DbPartialFinalObject(org.simpleflatmapper.test.beans.DbPartialFinalObject) DbFinalObject(org.simpleflatmapper.test.beans.DbFinalObject) Test(org.junit.Test)

Aggregations

DbObject (org.simpleflatmapper.test.beans.DbObject)83 Test (org.junit.Test)75 Connection (java.sql.Connection)27 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)14 SQLException (java.sql.SQLException)10 CheckedConsumer (org.simpleflatmapper.util.CheckedConsumer)10 ArrayList (java.util.ArrayList)9 MappingException (org.simpleflatmapper.map.MappingException)6 DbFinalObject (org.simpleflatmapper.test.beans.DbFinalObject)6 Type (java.lang.reflect.Type)5 ParseException (java.text.ParseException)5 JdbcMapperFactory (org.simpleflatmapper.jdbc.JdbcMapperFactory)5 StringReader (java.io.StringReader)4 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)4 DbPartialFinalObject (org.simpleflatmapper.test.beans.DbPartialFinalObject)4 Statement (java.sql.Statement)3 MapperBuilderErrorHandler (org.simpleflatmapper.map.MapperBuilderErrorHandler)3 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)3 ListCollector (org.simpleflatmapper.util.ListCollector)3