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());
}
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
}
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");
}
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());
}
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);
}
Aggregations